Browse Source

[test] sys test upgrade
* Test PHP
* Decoupled sys test from RunCi, particularly RunCi doesn't have to pass the test arguments.
* Added test cases for Sys.command, sys.io.Process, and Sys.exitCode

Andy Li 10 years ago
parent
commit
5e4b7f9793

+ 1 - 1
.travis.yml

@@ -44,7 +44,7 @@ script:
   - mkdir ~/haxelib && haxelib setup ~/haxelib
   - haxelib git hx-yaml https://github.com/mikestead/hx-yaml master src
   - haxe -version
-  - haxe -neko RunCi.n -main RunCi -lib hx-yaml
+  - haxe RunCi.hxml
   - neko RunCi.n
 
 branches:

+ 1 - 1
appveyor.yml

@@ -60,5 +60,5 @@ build_script:
 test_script:
     - cd %APPVEYOR_BUILD_FOLDER%/tests/
     - haxe -version
-    - haxe -neko RunCi.n -main RunCi -lib hx-yaml
+    - haxe RunCi.hxml
     - neko RunCi.n

+ 11 - 2
tests/README.md

@@ -6,8 +6,8 @@ We have a number of test suites, which are placed in their own folders in this d
 
  1. Change to this directory.
  2. Install lib used by the script: `haxelib git hx-yaml https://github.com/mikestead/hx-yaml master src`.
- 3. Compile the script: `haxe -neko RunCi.n -main RunCi -lib hx-yaml`.
- 4. Define the test target by `export TEST=$TARGET`, where `$TARGET` should be one of `macro`, `neko`, `js`, `php`, `cpp`, `flash9`, `as3`, `java`, `cs`, `python`, or `third-party`. However, `flash9`, `as3`, and `third-party` are not likely to work on local machines (TODO).
+ 3. Compile the script: `haxe RunCi.hxml`.
+ 4. Define the test target by `export TEST=$TARGET` (or `set "TEST=$TARGET"` on Windows), where `$TARGET` should be one of `macro`, `neko`, `js`, `php`, `cpp`, `flash9`, `as3`, `java`, `cs`, `python`, or `third-party`. However, `flash9`, `as3`, and `third-party` are not likely to work on local machines (TODO).
  5. Run it: `neko RunCi.n`.
 
 Note that the script will try to look for test dependencies and install them if they are not found. Look at the `getXXXDependencies` functions for the details.
@@ -22,3 +22,12 @@ Assuming all test dependencies has been installed, we compile and run the unit t
  2. Compile: `haxe compile.hxml`.
  3. Start a dev server: `nekotools server`.
  4. Open `http://localhost:2000/unit.html` in your browser.
+
+## Sys tests
+
+The "sys" folder contains tests for the system targets. It can also be run separately instead of using "RunCi.hx".
+
+Assuming all test dependencies has been installed, we compile and run the sys tests for all targets at once as follows:
+
+ 1. Change to the "sys" directory.
+ 2. If you're on Windows, comment out the relevant lines in "run.hxml". `haxe run.hxml`.

+ 10 - 16
tests/RunCi.hx

@@ -477,11 +477,6 @@ class RunCi {
 	static function main():Void {
 		Sys.putEnv("OCAMLRUNPARAM", "b");
 
-		var args = [
-			for (arg in new haxe.xml.Fast(Xml.parse(File.getContent('$sysDir/args.xml'))).node.args.nodes.arg)
-			arg.innerData
-		];
-
 		var tests:Array<TEST> = switch (ci) {
 			case null:
 				[Sys.getEnv("TEST") == null ? Macro : Sys.getEnv("TEST")];
@@ -525,7 +520,7 @@ class RunCi {
 
 					changeDirectory(sysDir);
 					runCommand("haxe", ["compile-macro.hxml"]);
-					runCommand("haxe", ["compile-each.hxml", "--run", "Main"].concat(args));
+					runCommand("haxe", ["compile-each.hxml", "--run", "Main"]);
 
 					//BYTECODE
 					switch (ci) {
@@ -549,12 +544,15 @@ class RunCi {
 
 					changeDirectory(sysDir);
 					runCommand("haxe", ["compile-neko.hxml"]);
-					changeDirectory("bin/neko");
-					runCommand("neko", ["sys.n"].concat(args));
+					runCommand("neko", ["bin/neko/sys.n"]);
 				case Php:
 					getPhpDependencies();
 					runCommand("haxe", ["compile-php.hxml","-D","travis"]);
 					runCommand("php", ["bin/php/index.php"]);
+
+					changeDirectory(sysDir);
+					runCommand("haxe", ["compile-php.hxml"]);
+					runCommand("php", ["bin/php/Main/index.php"]);
 				case Python:
 					var pys = getPythonDependencies();
 
@@ -565,9 +563,8 @@ class RunCi {
 
 					changeDirectory(sysDir);
 					runCommand("haxe", ["compile-python.hxml"]);
-					changeDirectory("bin/python");
 					for (py in pys) {
-						runCommand(py, ["sys.py"].concat(args));
+						runCommand(py, ["bin/python/sys.py"]);
 					}
 
 					changeDirectory(miscDir + "pythonImport");
@@ -591,8 +588,7 @@ class RunCi {
 
 					changeDirectory(sysDir);
 					runCommand("haxe", ["compile-cpp.hxml"]);
-					changeDirectory("bin/cpp");
-					runCpp("Main-debug", args);
+					runCpp("bin/cpp/Main-debug", []);
 				case Js:
 					getJSDependencies();
 
@@ -636,8 +632,7 @@ class RunCi {
 
 					changeDirectory(sysDir);
 					runCommand("haxe", ["compile-java.hxml"]);
-					changeDirectory("bin/java");
-					runCommand("java", ["-jar", "Main-Debug.jar"].concat(args));
+					runCommand("java", ["-jar", "bin/java/Main-Debug.jar"]);
 
 					infoMsg("Testing java-lib extras");
 					changeDirectory('$unitDir/bin');
@@ -684,8 +679,7 @@ class RunCi {
 
 					changeDirectory(sysDir);
 					runCommand("haxe", ["compile-cs.hxml"]);
-					changeDirectory("bin/cs");
-					runCs("bin/Main-Debug.exe", args);
+					runCs("bin/cs/bin/Main-Debug.exe", []);
 
 					changeDirectory(miscDir + "csTwoLibs");
 					for (i in 1...5)

+ 3 - 0
tests/RunCi.hxml

@@ -0,0 +1,3 @@
+-neko RunCi.n
+-main RunCi
+-lib hx-yaml

+ 2 - 0
tests/sys/.gitignore

@@ -0,0 +1,2 @@
+TestArguments.txt
+testcase-test-file.txt

+ 11 - 0
tests/sys/compile-cpp.hxml

@@ -1,2 +1,13 @@
 compile-each.hxml
+-main Main
+-cpp bin/cpp
+
+--next
+compile-each.hxml
+-main TestArguments
+-cpp bin/cpp
+
+--next
+compile-each.hxml
+-main ExitCode
 -cpp bin/cpp

+ 11 - 0
tests/sys/compile-cs.hxml

@@ -1,2 +1,13 @@
 compile-each.hxml
+-main Main
 -cs bin/cs
+
+--next
+compile-each.hxml
+-main TestArguments
+-cs bin/cs
+
+--next
+compile-each.hxml
+-main ExitCode
+-cs bin/cs

+ 1 - 2
tests/sys/compile-each.hxml

@@ -1,4 +1,3 @@
 -debug
 -cp src
--resource args.xml
--main Main
+-resource args.xml

+ 11 - 0
tests/sys/compile-java.hxml

@@ -1,2 +1,13 @@
 compile-each.hxml
+-main Main
 -java bin/java
+
+--next
+compile-each.hxml
+-main TestArguments
+-java bin/java
+
+--next
+compile-each.hxml
+-main ExitCode
+-java bin/java

+ 1 - 0
tests/sys/compile-macro.hxml

@@ -1,2 +1,3 @@
 compile-each.hxml
+-main Main
 --interp

+ 12 - 1
tests/sys/compile-neko.hxml

@@ -1,2 +1,13 @@
 compile-each.hxml
--neko bin/neko/sys.n
+-main Main
+-neko bin/neko/sys.n
+
+--next
+compile-each.hxml
+-main TestArguments
+-neko bin/neko/TestArguments.n
+
+--next
+compile-each.hxml
+-main ExitCode
+-neko bin/neko/ExitCode.n

+ 13 - 0
tests/sys/compile-php.hxml

@@ -0,0 +1,13 @@
+compile-each.hxml
+-main Main
+-php bin/php/Main
+
+--next
+compile-each.hxml
+-main TestArguments
+-php bin/php/TestArguments
+
+--next
+compile-each.hxml
+-main ExitCode
+-php bin/php/ExitCode

+ 12 - 1
tests/sys/compile-python.hxml

@@ -1,2 +1,13 @@
 compile-each.hxml
--python bin/python/sys.py
+-main Main
+-python bin/python/sys.py
+
+--next
+compile-each.hxml
+-main TestArguments
+-python bin/python/TestArguments.py
+
+--next
+compile-each.hxml
+-main ExitCode
+-python bin/python/ExitCode.py

+ 3 - 0
tests/sys/compile.hxml

@@ -1,3 +1,6 @@
+# This is not run by CIs, just for convenience when testing manually.
+# Note that compile-macro.hxml is not included.
+
 --next compile-neko.hxml
 --next compile-python.hxml
 --next compile-cpp.hxml

+ 28 - 0
tests/sys/run.hxml

@@ -0,0 +1,28 @@
+# This is not run by CIs, just for convenience when testing manually.
+
+# Compile everything first.
+compile.hxml
+
+# Mac/Linux
+--next
+-cmd echo Neko   && neko bin/neko/sys.n
+-cmd echo Python && python3 bin/python/sys.py
+-cmd echo Cpp    && bin/cpp/Main-Debug
+-cmd echo CS     && mono bin/cs/bin/Main-Debug.exe
+-cmd echo Java   && java -jar bin/java/Main-Debug.jar
+-cmd echo Php    && php bin/php/Main/index.php
+
+# Windows
+# --next
+# -cmd echo Neko   && neko bin\neko\sys.n
+# -cmd echo Python && python3 bin\python\sys.py
+# -cmd echo Cpp    && bin\cpp\Main-Debug.exe
+# -cmd echo CS     && bin\cs\bin\Main-Debug.exe
+# -cmd echo Java   && java -jar bin\java\Main-Debug.jar
+# -cmd echo Php    && php bin\php\Main\index.php
+
+# Macro has to be placed at the end since it would exit the compilation process.
+--next
+-cmd echo Macro
+--next
+compile-macro.hxml

+ 37 - 0
tests/sys/src/ExitCode.hx

@@ -0,0 +1,37 @@
+/**
+	This is intented to be used by TestSys and io.TestProcess.
+*/
+class ExitCode {
+	static public var bin:String =
+	#if neko
+		"bin/neko/ExitCode.n";
+	#elseif cpp
+		#if debug
+			"bin/cpp/ExitCode-debug";
+		#else
+			"bin/cpp/ExitCode";
+		#end
+	#elseif cs
+		#if debug
+			"bin/cs/bin/ExitCode-Debug.exe";
+		#else
+			"bin/cs/bin/ExitCode.exe";
+		#end
+	#elseif java
+		#if debug
+			"bin/java/ExitCode-Debug.jar";
+		#else
+			"bin/java/ExitCode.jar";
+		#end
+	#elseif python
+		"bin/python/ExitCode.py";
+	#elseif php
+		"bin/php/ExitCode/index.php";
+	#else
+		null;
+	#end
+	
+	static function main():Void {
+		Sys.exit(Std.parseInt(Sys.args()[0]));
+	}
+}

+ 1 - 0
tests/sys/src/Main.hx

@@ -4,6 +4,7 @@ class Main {
 		runner.add(new TestSys());
 		runner.add(new TestFileSystem());
 		runner.add(new io.TestFileInput());
+		runner.add(new io.TestProcess());
 		var code = runner.run() ? 0 : 1;
 		Sys.exit(code);
 	}

+ 63 - 0
tests/sys/src/TestArguments.hx

@@ -0,0 +1,63 @@
+/**
+	This test is intented to be used by TestSys and io.TestProcess.
+*/
+class TestArguments extends haxe.unit.TestCase {
+	static public var expectedArgs(get, null):Array<String>;
+	static function get_expectedArgs() {
+		return expectedArgs != null ? expectedArgs : expectedArgs = [
+			for (arg in new haxe.xml.Fast(Xml.parse(haxe.Resource.getString("args.xml"))).node.args.nodes.arg)
+			arg.innerData
+		];
+	}
+
+	static public var bin:String =
+	#if neko
+		"bin/neko/TestArguments.n";
+	#elseif cpp
+		#if debug
+			"bin/cpp/TestArguments-debug";
+		#else
+			"bin/cpp/TestArguments";
+		#end
+	#elseif cs
+		#if debug
+			"bin/cs/bin/TestArguments-Debug.exe";
+		#else
+			"bin/cs/bin/TestArguments.exe";
+		#end
+	#elseif java
+		#if debug
+			"bin/java/TestArguments-Debug.jar";
+		#else
+			"bin/java/TestArguments.jar";
+		#end
+	#elseif python
+		"bin/python/TestArguments.py";
+	#elseif php
+		"bin/php/TestArguments/index.php";
+	#else
+		null;
+	#end
+
+	function testArgs() {
+		var args = Sys.args();
+		// trace(args);
+		for (i in 0...expectedArgs.length) {
+			assertEquals(expectedArgs[i], args[i]);
+		}
+		assertEquals(expectedArgs.length, args.length);
+	}
+
+	static function main():Void {
+		var log = sys.io.File.write("TestArguments.txt");
+		haxe.unit.TestRunner.print = function(v){
+			log.writeString(v);
+		};
+		var runner = new haxe.unit.TestRunner();
+		runner.add(new TestArguments());
+		var code = runner.run() ? 0 : 1;
+		log.flush();
+		log.close();
+		Sys.exit(code);
+	}
+}

+ 75 - 15
tests/sys/src/TestSys.hx

@@ -1,28 +1,87 @@
 class TestSys extends haxe.unit.TestCase {
-	// it receives the arguments passed to Haxe command line
-	#if !interp
-	function testArgs() {
-		var args = Sys.args();
-		var expectedArgs = [
-			for (arg in new haxe.xml.Fast(Xml.parse(haxe.Resource.getString("args.xml"))).node.args.nodes.arg)
-			arg.innerData
-		];
-		// trace(args);
-		assertEquals(expectedArgs.length, args.length);
-		for (i in 0...expectedArgs.length) {
-			assertEquals(expectedArgs[i], args[i]);
+	function testCommand() {
+		var bin = TestArguments.bin;
+		var args = TestArguments.expectedArgs;
+
+		var exitCode = Sys.command("haxe", ["compile-each.hxml", "--run", "TestArguments"].concat(args));
+		assertEquals(0, exitCode);
+
+		var exitCode =
+			#if (macro || interp)
+				Sys.command("haxe", ["compile-each.hxml", "--run", "TestArguments"].concat(args));
+			#elseif cpp
+				Sys.command(bin, args);
+			#elseif cs
+				switch (Sys.systemName()) {
+					case "Windows":
+						Sys.command(bin, args);
+					case _:
+						Sys.command("mono", [bin].concat(args));
+				}
+			#elseif java
+				Sys.command("java", ["-jar", bin].concat(args));
+			#elseif python
+				Sys.command("python3", [bin].concat(args));
+			#elseif neko
+				Sys.command("neko", [bin].concat(args));
+			#elseif php
+				Sys.command("php", [bin].concat(args));
+			#else
+				-1;
+			#end
+		assertEquals(0, exitCode);
+	}
+
+	function testExitCode() {
+		var bin = ExitCode.bin;
+
+		// Just test only a few to save time.
+		// They have special meanings: http://tldp.org/LDP/abs/html/exitcodes.html
+		var codes = [0, 1, 2, 126, 127, 128, 130, 255];
+
+		for (code in codes) {
+			var args = [Std.string(code)];
+			var exitCode = Sys.command("haxe", ["compile-each.hxml", "--run", "ExitCode"].concat(args));
+			assertEquals(code, exitCode);
+		}
+
+		for (code in codes) {
+			var args = [Std.string(code)];
+			var exitCode =
+				#if (macro || interp)
+					Sys.command("haxe", ["compile-each.hxml", "--run", "ExitCode"].concat(args));
+				#elseif cpp
+					Sys.command(bin, args);
+				#elseif cs
+					switch (Sys.systemName()) {
+						case "Windows":
+							Sys.command(bin, args);
+						case _:
+							Sys.command("mono", [bin].concat(args));
+					}
+				#elseif java
+					Sys.command("java", ["-jar", bin].concat(args));
+				#elseif python
+					Sys.command("python3", [bin].concat(args));
+				#elseif neko
+					Sys.command("neko", [bin].concat(args));
+				#elseif php
+					Sys.command("php", [bin].concat(args));
+				#else
+					-1;
+				#end
+			assertEquals(code, exitCode);
 		}
 	}
-	#end
 
 	function testEnv() {
-		#if !java
+		#if !(java || php)
 		Sys.putEnv("foo", "value");
 		assertEquals("value", Sys.getEnv("foo"));
 		#end
 		assertEquals(null, Sys.getEnv("doesn't exist"));
 
-		#if !java
+		#if !(java || php)
 		var env = Sys.environment();
 		assertEquals("value", env.get("foo"));
 		#end
@@ -37,6 +96,7 @@ class TestSys extends haxe.unit.TestCase {
 			return haxe.io.Path.addTrailingSlash(haxe.io.Path.normalize(path));
 		}
 		assertEquals(normalize(newCwd), normalize(Sys.getCwd()));
+		Sys.setCwd(cur);
 	}
 	#end
 }

+ 4 - 2
tests/sys/src/io/TestFileInput.hx

@@ -16,7 +16,7 @@ class TestFileInput extends haxe.unit.TestCase {
 
 	public function new() {
 		super();
-		path = 'testcase-test-file';
+		path = 'testcase-test-file.txt';
 	}
 
 	override public function setup() {
@@ -24,7 +24,7 @@ class TestFileInput extends haxe.unit.TestCase {
 	}
 
 	override public function tearDown() {
-		//FileSystem.deleteFile(path);
+		FileSystem.deleteFile(path);
 	}
 
 	public function testRead() {
@@ -129,6 +129,7 @@ class TestFileInput extends haxe.unit.TestCase {
 		file.close();
 	}
 
+	#if !php
 	public function testSeekEofLast() {
 		var file : FileInput = File.read(path);
 		assertEquals(116, file.readByte());
@@ -213,6 +214,7 @@ class TestFileInput extends haxe.unit.TestCase {
 		}
 		file.close();
 	}
+	#end
 
 }
 

+ 84 - 0
tests/sys/src/io/TestProcess.hx

@@ -0,0 +1,84 @@
+package io;
+
+import sys.io.Process;
+
+class TestProcess extends haxe.unit.TestCase {
+	#if !(python || php)
+	function testArguments() {
+		var bin = TestArguments.bin;
+		var args = TestArguments.expectedArgs;
+
+		var process = new Process("haxe", ["compile-each.hxml", "--run", "TestArguments"].concat(args));
+		assertEquals(0, process.exitCode());
+
+		var process =
+			#if (macro || interp)
+				new Process("haxe", ["compile-each.hxml", "--run", "TestArguments"].concat(args));
+			#elseif cpp
+				new Process(bin, args);
+			#elseif cs
+				switch (Sys.systemName()) {
+					case "Windows":
+						new Process(bin, args);
+					case _:
+						new Process("mono", [bin].concat(args));
+				}
+			#elseif java
+				new Process("java", ["-jar", bin].concat(args));
+			#elseif python
+				new Process("python3", [bin].concat(args));
+			#elseif neko
+				new Process("neko", [bin].concat(args));
+			#elseif php
+				new Process("php", [bin].concat(args));
+			#else
+				null;
+			#end
+		assertEquals(0, process.exitCode());
+	}
+	#end
+
+	#if !python
+	function testExitCode() {
+		var bin = ExitCode.bin;
+
+		// Just test only a few to save time.
+		// They have special meanings: http://tldp.org/LDP/abs/html/exitcodes.html
+		var codes = [0, 1, 2, 126, 127, 128, 130, 255];
+
+		for (code in codes) {
+			var args = [Std.string(code)];
+			var process = new Process("haxe", ["compile-each.hxml", "--run", "ExitCode"].concat(args));
+			assertEquals(code, process.exitCode());
+		}
+
+		for (code in codes) {
+			var args = [Std.string(code)];
+			var process =
+				#if (macro || interp)
+					new Process("haxe", ["compile-each.hxml", "--run", "ExitCode"].concat(args));
+				#elseif cpp
+					new Process(bin, args);
+				#elseif cs
+					switch (Sys.systemName()) {
+						case "Windows":
+							new Process(bin, args);
+						case _:
+							new Process("mono", [bin].concat(args));
+					}
+				#elseif java
+					new Process("java", ["-jar", bin].concat(args));
+				#elseif python
+					new Process("python3", [bin].concat(args));
+				#elseif neko
+					new Process("neko", [bin].concat(args));
+				#elseif php
+					new Process("php", [bin].concat(args));
+				#else
+					null;
+				#end
+			assertEquals(code, process.exitCode());
+		}
+	}
+	#end
+}