Skip to content

Commit

Permalink
[tests] Clean up test files
Browse files Browse the repository at this point in the history
- Remove unused imports
- Avoid wildcard imports
- Change var to final
- Use import.hx
  • Loading branch information
tobil4sk committed Jan 9, 2022
1 parent 5b6eb3a commit aeaf7a2
Show file tree
Hide file tree
Showing 25 changed files with 285 additions and 342 deletions.
28 changes: 13 additions & 15 deletions test/HaxelibTests.hx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import haxe.io.Path;
import haxelib.client.Vcs.VcsID;
import haxe.unit.TestRunner;
import sys.*;
import sys.io.*;
import sys.FileSystem;
import sys.io.Process;

import tests.*;

using StringTools;

class HaxelibTests {
public static function runCommand(cmd:String, args:Array<String>):Void
{
public static function runCommand(cmd:String, args:Array<String>):Void {
Sys.println('Command: $cmd $args');

var exitCode = Sys.command(cmd, args);
final exitCode = Sys.command(cmd, args);

Sys.println('Command exited with $exitCode: $cmd $args');

Expand All @@ -20,38 +19,37 @@ class HaxelibTests {
}

static function cmdSucceed(cmd:String, ?args:Array<String>):Bool {
var p = try {
final p = try {
new Process(cmd, args);
} catch(e:Dynamic) {
return false;
}
var exitCode = p.exitCode();
final exitCode = p.exitCode();
p.close();
return exitCode == 0;
}

static public function deleteDirectory(dir:String) {
if (!FileSystem.exists(dir)) return;
var exitCode = switch (Sys.systemName()) {
final exitCode = switch (Sys.systemName()) {
case "Windows":
Sys.command("rmdir", ["/S", "/Q", StringTools.replace(FileSystem.fullPath(dir), "/", "\\")]);
case _:
Sys.command("rm", ["-rf", dir]);
}
if (exitCode != 0) {
if (exitCode != 0)
throw 'unable to delete $dir';
}
}

static function main():Void {
var r = new TestRunner();
final r = new TestRunner();

r.add(new TestSemVer());
r.add(new TestData());
r.add(new TestRemoveSymlinks());
r.add(new TestRemoveSymlinksBroken());

var isCI = Sys.getEnv("CI") != null;
final isCI = Sys.getEnv("CI") != null;

// The test repo https://bitbucket.org/fzzr/hx.signal is gone.
// if (isCI || cmdSucceed("hg", ["version"])) {
Expand All @@ -72,7 +70,7 @@ class HaxelibTests {

r.add(new TestInstall());

var success = r.run();
final success = r.run();
Sys.exit(success ? 0 : 1);
}
}
73 changes: 39 additions & 34 deletions test/IntegrationTests.hx
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import haxe.unit.*;
import haxe.*;
import haxe.io.*;
import sys.*;
import sys.io.*;
import haxelib.*;

import haxe.Json;
import sys.FileSystem;
import sys.io.Process;
import sys.io.File;

import haxe.unit.TestRunner;

import haxelib.SemVer;

using StringTools;
using haxe.io.Path;
using IntegrationTests;

typedef UserRegistration = {
user:String,
email:String,
fullname:String,
pw:String
final user:String;
final email:String;
final fullname:String;
final pw:String;
}

class IntegrationTests extends TestBase {
static var projectRoot:String = Sys.getCwd();
var haxelibBin:String = Path.join([projectRoot, "run.n"]);
public var server(default, null):String = switch (Sys.getEnv("HAXELIB_SERVER")) {
static final projectRoot:String = Sys.getCwd();
final haxelibBin:String = Path.join([projectRoot, "run.n"]);
public final server = switch (Sys.getEnv("HAXELIB_SERVER")) {
case null:
"localhost";
case url:
url;
};
public var serverPort(default, null) = switch (Sys.getEnv("HAXELIB_SERVER_PORT")) {
public final serverPort = switch (Sys.getEnv("HAXELIB_SERVER_PORT")) {
case null:
2000;
case port:
Expand All @@ -32,35 +37,35 @@ class IntegrationTests extends TestBase {
public var serverUrl(get, null):String;
function get_serverUrl() return serverUrl != null ? serverUrl : serverUrl = 'http://${server}:${serverPort}/';

static var originalRepo(default, never) = {
var p = new Process("haxelib", ["config"]);
var originalRepo = Path.normalize(p.stdout.readLine());
static final originalRepo = {
final p = new Process("haxelib", ["config"]);
final originalRepo = p.stdout.readLine().normalize();
p.close();
if (repo == originalRepo) {
throw "haxelib repo is the same as test repo: " + repo;
}
originalRepo;
};
static public var repo(default, never) = "repo_integration_tests";
static public var bar(default, never):UserRegistration = {
static public final repo = "repo_integration_tests";
static public final bar = {
user: "Bar",
email: "[email protected]",
fullname: "Bar",
pw: "barpassword",
};
static public var foo(default, never):UserRegistration = {
static public final foo = {
user: "Foo",
email: "[email protected]",
fullname: "Foo",
pw: "foopassword",
};
static public var deepAuthor(default, never):UserRegistration = {
static public final deepAuthor = {
user: "DeepAuthor",
email: "[email protected]",
fullname: "Jonny Deep",
pw: "deep thought"
}
static public var anotherGuy(default, never):UserRegistration = {
static public final anotherGuy = {
user: "AnotherGuy",
email: "[email protected]",
fullname: "Another Guy",
Expand All @@ -73,7 +78,7 @@ class IntegrationTests extends TestBase {
clientVer;
else {
clientVer = {
var r = haxelib(["version"]).result();
final r = haxelib(["version"]).result();
if (r.code == 0)
SemVer.ofString(switch(r.out.trim()) {
case _.split(" ") => parts: parts[0];
Expand All @@ -90,7 +95,7 @@ class IntegrationTests extends TestBase {
}

function haxelib(args:Array<String>, ?input:String):Process {
var p = #if system_haxelib
final p = #if system_haxelib
new Process("haxelib", ["-R", serverUrl].concat(args));
#else
new Process("neko", [haxelibBin, "-R", serverUrl].concat(args));
Expand Down Expand Up @@ -120,20 +125,20 @@ class IntegrationTests extends TestBase {
assertTrue(true);
}

var dbConfig:Dynamic = Json.parse(File.getContent("www/dbconfig.json"));
final dbConfig:Dynamic = Json.parse(File.getContent("www/dbconfig.json"));
var dbCnx:sys.db.Connection;
function resetDB():Void {
var db = dbConfig.database;
final db = dbConfig.database;
dbCnx.request('DROP DATABASE IF EXISTS ${db};');
dbCnx.request('CREATE DATABASE ${db};');

var filesPath = "www/files/3.0";
final filesPath = "www/files/3.0";
for (item in FileSystem.readDirectory(filesPath)) {
if (item.endsWith(".zip")) {
FileSystem.deleteFile(Path.join([filesPath, item]));
}
}
var tmpPath = "tmp";
final tmpPath = "tmp";
for (item in FileSystem.readDirectory(filesPath)) {
if (item.endsWith(".tmp")) {
FileSystem.deleteFile(Path.join([tmpPath, item]));
Expand Down Expand Up @@ -173,9 +178,9 @@ class IntegrationTests extends TestBase {
}

static public function result(p:Process):{out:String, err:String, code:Int} {
var out = p.stdout.readAll().toString();
var err = p.stderr.readAll().toString();
var code = p.exitCode();
final out = p.stdout.readAll().toString();
final err = p.stderr.readAll().toString();
final code = p.exitCode();
p.close();
return {out:out, err:err, code:code};
}
Expand All @@ -189,9 +194,9 @@ class IntegrationTests extends TestBase {
}

static function main():Void {
var prevDir = Sys.getCwd();
final prevDir = Sys.getCwd();

var runner = new TestRunner();
final runner = new TestRunner();
runner.add(new tests.integration.TestEmpty());
runner.add(new tests.integration.TestSetup());
runner.add(new tests.integration.TestSimple());
Expand All @@ -205,7 +210,7 @@ class IntegrationTests extends TestBase {
runner.add(new tests.integration.TestDev());
runner.add(new tests.integration.TestRun());
runner.add(new tests.integration.TestPath());
var success = runner.run();
final success = runner.run();

if (!success) {
Sys.exit(1);
Expand Down
18 changes: 10 additions & 8 deletions test/TestBase.hx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import sys.*;
import sys.io.*;
import haxe.io.*;
import haxe.unit.*;
import sys.FileSystem;
import sys.io.Process;
import haxe.io.Eof;
import haxe.unit.TestCase;


class TestBase extends TestCase {
static var haxelibPath = FileSystem.fullPath("run.n");
static final haxelibPath = FileSystem.fullPath("run.n");

public function runHaxelib(args:Array<String>, printProgress = false) {
var p = new Process("neko", [haxelibPath].concat(args));
final p = new Process("neko", [haxelibPath].concat(args));
var stdout = '';
var stderr = '';
var eofCount = 0;
var c;
var c:Int;
while (eofCount < 2) {
eofCount = 0;
try {
Expand All @@ -32,7 +34,7 @@ class TestBase extends TestCase {
Sys.stdout().flush();
Sys.stderr().flush();
}
var exitCode = p.exitCode();
final exitCode = p.exitCode();
p.close();
return {
stdout: stdout,
Expand Down
3 changes: 1 addition & 2 deletions test/tests/TestData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -278,5 +278,4 @@ class TestData extends TestBase {
}
return Json.stringify(infos);
}

}
}
2 changes: 1 addition & 1 deletion test/tests/TestGit.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ class TestGit extends TestVcs {
public function new():Void {
super(VcsID.Git, "Git", FileSystem.fullPath(REPO_PATH), "0.9.2");
}
}
}
2 changes: 1 addition & 1 deletion test/tests/TestHg.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ class TestHg extends TestVcs {
public function new():Void {
super(VcsID.Hg, "Mercurial", FileSystem.fullPath(REPO_PATH), "78edb4b");
}
}
}
Loading

0 comments on commit aeaf7a2

Please sign in to comment.