From 3290406d3c91bca96a746475cc21a392259042c0 Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Wed, 27 Mar 2024 00:19:37 +0100 Subject: [PATCH] fix(test): exit code of lime test --- lib/vendor/lime/lime.php | 17 +- test/unit/vendor/lime/lime_harness.php | 175 ++++++++++++++++++ test/unit/vendor/lime/unit/failed.php | 7 + .../unit/failed_with_plan_less_than_total.php | 8 + .../unit/failed_with_plan_more_than_total.php | 7 + test/unit/vendor/lime/unit/pass.php | 7 + .../unit/pass_with_plan_less_than_total.php | 8 + .../unit/pass_with_plan_more_than_total.php | 7 + 8 files changed, 234 insertions(+), 2 deletions(-) create mode 100644 test/unit/vendor/lime/lime_harness.php create mode 100644 test/unit/vendor/lime/unit/failed.php create mode 100644 test/unit/vendor/lime/unit/failed_with_plan_less_than_total.php create mode 100644 test/unit/vendor/lime/unit/failed_with_plan_more_than_total.php create mode 100644 test/unit/vendor/lime/unit/pass.php create mode 100644 test/unit/vendor/lime/unit/pass_with_plan_less_than_total.php create mode 100644 test/unit/vendor/lime/unit/pass_with_plan_more_than_total.php diff --git a/lib/vendor/lime/lime.php b/lib/vendor/lime/lime.php index c09016fb2..59a99de34 100644 --- a/lib/vendor/lime/lime.php +++ b/lib/vendor/lime/lime.php @@ -952,8 +952,7 @@ function lime_shutdown() ); ob_start(); - // see http://trac.symfony-project.org/ticket/5437 for the explanation on the weird "cd" thing - passthru(sprintf('cd & %s %s 2>&1', escapeshellarg($this->php_cli), escapeshellarg($test_file)), $return); + $return = $this->executePhpFile($test_file); ob_end_clean(); unlink($test_file); @@ -1123,6 +1122,20 @@ public function get_failed_files() { return isset($this->stats['failed_files']) ? $this->stats['failed_files'] : array(); } + + /** + * The command fails if the path to php interpreter contains spaces. + * The only workaround is adding a "nop" command call before the quoted command. + * The weird "cd &". + * + * see http://trac.symfony-project.org/ticket/5437 + */ + public function executePhpFile(string $phpFile): int + { + passthru(sprintf('cd & %s %s 2>&1', escapeshellarg($this->php_cli), escapeshellarg($phpFile)), $return); + + return $return; + } } class lime_coverage extends lime_registration diff --git a/test/unit/vendor/lime/lime_harness.php b/test/unit/vendor/lime/lime_harness.php new file mode 100644 index 000000000..2b5a77062 --- /dev/null +++ b/test/unit/vendor/lime/lime_harness.php @@ -0,0 +1,175 @@ +diag($name); + + ob_start(); + $exitCode = (new lime_harness())->executePhpFile(__DIR__.'/unit/'.$name.'.php'); + $output = ob_get_clean(); + + $test->is($exitCode, $expectedStatusCode, 'with test '.$name.' will exit with status code '.$expectedStatusCode); + + $test->is(removeTrailingSpaces($output), $expectedOutput, 'test '.$name.' output'); +} + +function whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message) +{ + $harness = new lime_harness(); + $harness->output->colorizer = new lime_no_colorizer(); + + $harness->register($files); + + ob_start(); + $allTestsSucceed = $harness->run(); + $output = ob_get_clean(); + + $test->is($expectedOverallSucceed, $allTestsSucceed, $message); + + $test->is(removeTrailingSpaces($output), $expectedOutput, 'test harness result'); +} + +class lime_no_colorizer extends lime_colorizer +{ + public function __construct() + { + } +} + +$test = new lime_test(16); + +$files = [ + __DIR__.'/unit/failed.php', + __DIR__.'/unit/failed_with_plan_less_than_total.php', + __DIR__.'/unit/failed_with_plan_more_than_total.php', + __DIR__.'/unit/pass.php', + __DIR__.'/unit/pass_with_plan_less_than_total.php', + __DIR__.'/unit/pass_with_plan_more_than_total.php', +]; +$expectedOverallSucceed = false; +$expectedOutput = <<<'EOF' +test/unit/vendor/lime/unit/failed....................................not ok + Failed tests: 1 +test/unit/vendor/lime/unit/failed_with_plan_less_than_total..........dubious + Test returned status 255 + Looks like you planned 1 test but ran 1 extra. + Failed tests: 1 +test/unit/vendor/lime/unit/failed_with_plan_more_than_total..........dubious + Test returned status 255 + Looks like you planned 2 tests but only ran 1. + Failed tests: 1 +test/unit/vendor/lime/unit/pass......................................ok +test/unit/vendor/lime/unit/pass_with_plan_less_than_total............dubious + Test returned status 255 + Looks like you planned 1 test but ran 1 extra. +test/unit/vendor/lime/unit/pass_with_plan_more_than_total............dubious + Test returned status 255 + Looks like you planned 2 tests but only ran 1. +Failed Test Stat Total Fail Errors List of Failed +-------------------------------------------------------------------------- +t/unit/vendor/lime/unit/failed 0 1 1 0 1 +iled_with_plan_less_than_total 255 2 1 0 1 +iled_with_plan_more_than_total 255 1 1 0 1 +pass_with_plan_less_than_total 255 2 0 0 +pass_with_plan_more_than_total 255 1 0 0 +Failed 5/6 test scripts, 16.67% okay. 5/10 subtests failed, 50.00% okay. + +EOF; +$message = 'with at least one failed test will fail the overall test suite'; +whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message); + +$files = [__DIR__.'/unit/pass_with_plan_less_than_total.php']; +$expectedOverallSucceed = false; +$expectedOutput = <<<'EOF' +test/unit/vendor/lime/unit/pass_with_plan_less_than_total............dubious + Test returned status 255 + Looks like you planned 1 test but ran 1 extra. +Failed Test Stat Total Fail Errors List of Failed +-------------------------------------------------------------------------- +pass_with_plan_less_than_total 255 2 0 0 +Failed 1/1 test scripts, 0.00% okay. 0/2 subtests failed, 100.00% okay. + +EOF; +$message = 'with at least one test that not follow the plan will fail the overall test suite'; +whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message); + + +$name = 'pass'; +$expectedStatusCode = 0; +$expectedOutput = <<<'EOF' +ok 1 +1..1 +# Looks like everything went fine. + +EOF; +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); + +$name = 'failed'; +$expectedStatusCode = 0; +$expectedOutput = <<<'EOF' +not ok 1 +# Failed test (./test/unit/vendor/lime/unit/failed.php at line 7) +# got: false +# expected: true +1..1 +# Looks like you failed 1 tests of 1. + +EOF; +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); + +$name = 'failed_with_plan_less_than_total'; +$expectedStatusCode = 0; +$expectedOutput = <<<'EOF' +1..1 +not ok 1 +# Failed test (./test/unit/vendor/lime/unit/failed_with_plan_less_than_total.php at line 7) +# got: false +# expected: true +ok 2 +# Looks like you planned 1 tests but ran 1 extra. +# Looks like you failed 1 tests of 2. + +EOF; +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); + +$name = 'failed_with_plan_more_than_total'; +$expectedStatusCode = 0; +$expectedOutput = <<<'EOF' +1..2 +not ok 1 +# Failed test (./test/unit/vendor/lime/unit/failed_with_plan_more_than_total.php at line 7) +# got: false +# expected: true +# Looks like you planned 2 tests but only ran 1. +# Looks like you failed 1 tests of 1. + +EOF; +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); + +$name = 'pass_with_plan_less_than_total'; +$expectedStatusCode = 0; +$expectedOutput = <<<'EOF' +1..1 +ok 1 +ok 2 +# Looks like you planned 1 tests but ran 1 extra. + +EOF; +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); + +$name = 'pass_with_plan_more_than_total'; +$expectedStatusCode = 0; +$expectedOutput = <<<'EOF' +1..2 +ok 1 +# Looks like you planned 2 tests but only ran 1. + +EOF; +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); diff --git a/test/unit/vendor/lime/unit/failed.php b/test/unit/vendor/lime/unit/failed.php new file mode 100644 index 000000000..bd0186881 --- /dev/null +++ b/test/unit/vendor/lime/unit/failed.php @@ -0,0 +1,7 @@ +is(false, true); diff --git a/test/unit/vendor/lime/unit/failed_with_plan_less_than_total.php b/test/unit/vendor/lime/unit/failed_with_plan_less_than_total.php new file mode 100644 index 000000000..47e0c69f8 --- /dev/null +++ b/test/unit/vendor/lime/unit/failed_with_plan_less_than_total.php @@ -0,0 +1,8 @@ +is(false, true); +$test->is(true, true); diff --git a/test/unit/vendor/lime/unit/failed_with_plan_more_than_total.php b/test/unit/vendor/lime/unit/failed_with_plan_more_than_total.php new file mode 100644 index 000000000..7bf7da135 --- /dev/null +++ b/test/unit/vendor/lime/unit/failed_with_plan_more_than_total.php @@ -0,0 +1,7 @@ +is(false, true); diff --git a/test/unit/vendor/lime/unit/pass.php b/test/unit/vendor/lime/unit/pass.php new file mode 100644 index 000000000..93afc9a6e --- /dev/null +++ b/test/unit/vendor/lime/unit/pass.php @@ -0,0 +1,7 @@ +is(true, true); diff --git a/test/unit/vendor/lime/unit/pass_with_plan_less_than_total.php b/test/unit/vendor/lime/unit/pass_with_plan_less_than_total.php new file mode 100644 index 000000000..13d7600b8 --- /dev/null +++ b/test/unit/vendor/lime/unit/pass_with_plan_less_than_total.php @@ -0,0 +1,8 @@ +is(true, true); +$test->is(true, true); diff --git a/test/unit/vendor/lime/unit/pass_with_plan_more_than_total.php b/test/unit/vendor/lime/unit/pass_with_plan_more_than_total.php new file mode 100644 index 000000000..6c0d577cc --- /dev/null +++ b/test/unit/vendor/lime/unit/pass_with_plan_more_than_total.php @@ -0,0 +1,7 @@ +is(true, true);