From 04d0339ca0f9db4071bd263c46201a8df4c43a1c Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Wed, 10 Apr 2024 00:52:20 +0200 Subject: [PATCH] fixup! fix(test): exit code of lime test -- add proof of current behaviour --- test/unit/vendor/lime/fixtures/failed.php | 7 + .../failed_with_plan_less_than_total.php | 8 + .../failed_with_plan_more_than_total.php | 7 + test/unit/vendor/lime/fixtures/pass.php | 7 + .../lime/fixtures/pass_with_one_error.php | 13 + .../fixtures/pass_with_one_parse_error.php | 7 + .../pass_with_one_throw_exception.php | 9 + .../pass_with_plan_less_than_total.php | 8 + .../pass_with_plan_more_than_total.php | 7 + test/unit/vendor/lime/fixtures/skip.php | 7 + .../vendor/lime/fixtures/skip_with_plan.php | 7 + test/unit/vendor/lime/lime_harnessTest.php | 261 ++++++++++++++++++ test/unit/vendor/lime/lime_testTest.php | 178 ++++++++++++ test/unit/vendor/lime/tools/TestCase.php | 39 +++ .../vendor/lime/tools/lime_no_colorizer.php | 8 + 15 files changed, 573 insertions(+) create mode 100644 test/unit/vendor/lime/fixtures/failed.php create mode 100644 test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total.php create mode 100644 test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total.php create mode 100644 test/unit/vendor/lime/fixtures/pass.php create mode 100644 test/unit/vendor/lime/fixtures/pass_with_one_error.php create mode 100644 test/unit/vendor/lime/fixtures/pass_with_one_parse_error.php create mode 100644 test/unit/vendor/lime/fixtures/pass_with_one_throw_exception.php create mode 100644 test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total.php create mode 100644 test/unit/vendor/lime/fixtures/pass_with_plan_more_than_total.php create mode 100644 test/unit/vendor/lime/fixtures/skip.php create mode 100644 test/unit/vendor/lime/fixtures/skip_with_plan.php create mode 100644 test/unit/vendor/lime/lime_harnessTest.php create mode 100644 test/unit/vendor/lime/lime_testTest.php create mode 100644 test/unit/vendor/lime/tools/TestCase.php create mode 100644 test/unit/vendor/lime/tools/lime_no_colorizer.php diff --git a/test/unit/vendor/lime/fixtures/failed.php b/test/unit/vendor/lime/fixtures/failed.php new file mode 100644 index 000000000..bd0186881 --- /dev/null +++ b/test/unit/vendor/lime/fixtures/failed.php @@ -0,0 +1,7 @@ +is(false, true); diff --git a/test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total.php b/test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total.php new file mode 100644 index 000000000..47e0c69f8 --- /dev/null +++ b/test/unit/vendor/lime/fixtures/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/fixtures/failed_with_plan_more_than_total.php b/test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total.php new file mode 100644 index 000000000..7bf7da135 --- /dev/null +++ b/test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total.php @@ -0,0 +1,7 @@ +is(false, true); diff --git a/test/unit/vendor/lime/fixtures/pass.php b/test/unit/vendor/lime/fixtures/pass.php new file mode 100644 index 000000000..93afc9a6e --- /dev/null +++ b/test/unit/vendor/lime/fixtures/pass.php @@ -0,0 +1,7 @@ +is(true, true); diff --git a/test/unit/vendor/lime/fixtures/pass_with_one_error.php b/test/unit/vendor/lime/fixtures/pass_with_one_error.php new file mode 100644 index 000000000..4491a9a80 --- /dev/null +++ b/test/unit/vendor/lime/fixtures/pass_with_one_error.php @@ -0,0 +1,13 @@ + true, +]); + +trigger_error('some user error message', E_USER_ERROR); + +$test->is(true, true); diff --git a/test/unit/vendor/lime/fixtures/pass_with_one_parse_error.php b/test/unit/vendor/lime/fixtures/pass_with_one_parse_error.php new file mode 100644 index 000000000..39535fef4 --- /dev/null +++ b/test/unit/vendor/lime/fixtures/pass_with_one_parse_error.php @@ -0,0 +1,7 @@ +is(true, true); diff --git a/test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total.php b/test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total.php new file mode 100644 index 000000000..13d7600b8 --- /dev/null +++ b/test/unit/vendor/lime/fixtures/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/fixtures/pass_with_plan_more_than_total.php b/test/unit/vendor/lime/fixtures/pass_with_plan_more_than_total.php new file mode 100644 index 000000000..6c0d577cc --- /dev/null +++ b/test/unit/vendor/lime/fixtures/pass_with_plan_more_than_total.php @@ -0,0 +1,7 @@ +is(true, true); diff --git a/test/unit/vendor/lime/fixtures/skip.php b/test/unit/vendor/lime/fixtures/skip.php new file mode 100644 index 000000000..215bfe70e --- /dev/null +++ b/test/unit/vendor/lime/fixtures/skip.php @@ -0,0 +1,7 @@ +skip('some skip message'); diff --git a/test/unit/vendor/lime/fixtures/skip_with_plan.php b/test/unit/vendor/lime/fixtures/skip_with_plan.php new file mode 100644 index 000000000..6447ce880 --- /dev/null +++ b/test/unit/vendor/lime/fixtures/skip_with_plan.php @@ -0,0 +1,7 @@ +skip('some skip message', $plan); diff --git a/test/unit/vendor/lime/lime_harnessTest.php b/test/unit/vendor/lime/lime_harnessTest.php new file mode 100644 index 000000000..1ba5d3890 --- /dev/null +++ b/test/unit/vendor/lime/lime_harnessTest.php @@ -0,0 +1,261 @@ +output->colorizer = new lime_no_colorizer(); + + return $harness; + } + + private function whenExecuteHarnessWithOneFileWillHaveStatusCodeAndOutput($name, $expectedOverallSucceed, $expectedOutput) + { + $this->info($name); + + $harness = $this->makeHarness(); + + $harness->register([ + __DIR__."/fixtures/$name.php", + ]); + + $this->assertHarnessWithExitStatusAndOutput($harness, $expectedOverallSucceed, $expectedOutput); + } + + private function assertHarnessWithExitStatusAndOutput(lime_harness $harness, $expectedOverallSucceed, string $expectedOutput) + { + ob_start(); + $allTestsSucceed = $harness->run(); + $output = ob_get_clean(); + + $this->is($expectedOverallSucceed, $allTestsSucceed, 'expect overall test '.($expectedOverallSucceed ? 'succeed' : 'failed')); + + $this->is($this->removeTrailingSpaces($output), $expectedOutput, 'test harness result output'); + } + + private function removeTrailingSpaces(string $output): string + { + return preg_replace("/ *\n/", "\n", $output); + } + + public function testOnlyOneTestFile(): void + { + foreach ($this->provideOnlyOneTestFile() as $parameters) { + $this->whenExecuteHarnessWithOneFileWillHaveStatusCodeAndOutput(...$parameters); + } + } + + private function provideOnlyOneTestFile(): iterable + { + yield [ + /* name */ 'pass', + /* expectedOverallSucceed */ true, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/pass..................................ok + All tests successful. + Files=1, Tests=1 + +EOF + ]; + + yield [ + /* name */ 'pass_with_plan_less_than_total', + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/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 + ]; + + yield [ + /* name */ 'pass_with_plan_more_than_total', + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/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 +-------------------------------------------------------------------------- +pass_with_plan_more_than_total 255 1 0 0 +Failed 1/1 test scripts, 0.00% okay. 1/2 subtests failed, 50.00% okay. + +EOF + ]; + + yield [ + /* name */ 'pass_with_one_error', + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/pass_with_one_error...................errors + Errors: + - Notice: some user error message +Failed Test Stat Total Fail Errors List of Failed +-------------------------------------------------------------------------- +e/fixtures/pass_with_one_error 0 1 0 1 +Failed 1/1 test scripts, 0.00% okay. 0/1 subtests failed, 100.00% okay. + +EOF + ]; + + yield [ + /* name */ 'pass_with_one_throw_exception', + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/pass_with_one_throw_exception.........errors + Errors: + - LogicException: some exception message +Failed Test Stat Total Fail Errors List of Failed +-------------------------------------------------------------------------- +/pass_with_one_throw_exception 0 0 0 1 +Failed 1/1 test scripts, 0.00% okay. 0/0 subtests failed, 0.00% okay. + +EOF + ]; + + yield [ + /* name */ 'pass_with_one_parse_error', + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/pass_with_one_parse_error.............dubious + Test returned status 255 + Failed tests: 0 +Failed Test Stat Total Fail Errors List of Failed +-------------------------------------------------------------------------- +ures/pass_with_one_parse_error 255 1 1 0 0 +Failed 1/1 test scripts, 0.00% okay. 1/0 subtests failed, 0.00% okay. + +EOF + ]; + + yield [ + /* name */ 'skip', + /* expectedOverallSucceed */ true, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/skip..................................ok + All tests successful. + Files=1, Tests=1 + +EOF + ]; + + yield [ + /* name */ 'skip_with_plan', + /* expectedOverallSucceed */ true, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/skip_with_plan........................ok + All tests successful. + Files=1, Tests=2 + +EOF + ]; + + yield [ + /* name */ 'failed', + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/failed................................not ok + Failed tests: 1 +Failed Test Stat Total Fail Errors List of Failed +-------------------------------------------------------------------------- +it/vendor/lime/fixtures/failed 0 1 1 0 1 +Failed 1/1 test scripts, 0.00% okay. 1/1 subtests failed, 0.00% okay. + +EOF + ]; + + yield [ + /* name */ 'failed_with_plan_less_than_total', + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total......dubious + Test returned status 255 + Looks like you planned 1 test but ran 1 extra. + Failed tests: 1 +Failed Test Stat Total Fail Errors List of Failed +-------------------------------------------------------------------------- +iled_with_plan_less_than_total 255 2 1 0 1 +Failed 1/1 test scripts, 0.00% okay. 1/2 subtests failed, 50.00% okay. + +EOF + ]; + + yield [ + /* name */ 'failed_with_plan_more_than_total', + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total......dubious + Test returned status 255 + Looks like you planned 2 tests but only ran 1. + Failed tests: 1 +Failed Test Stat Total Fail Errors List of Failed +-------------------------------------------------------------------------- +iled_with_plan_more_than_total 255 1 1 0 1 +Failed 1/1 test scripts, 0.00% okay. 2/2 subtests failed, 0.00% okay. + +EOF + ]; + } + + public function test_registerFilesWithGlob_thenRunThemAll(): void + { + $harness = $this->makeHarness(); + + $harness->register_glob(__DIR__."/fixtures/*.php"); + + $this->assertHarnessWithExitStatusAndOutput($harness, false, <<<'EOF' +test/unit/vendor/lime/fixtures/failed................................not ok + Failed tests: 1 +test/unit/vendor/lime/fixtures/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/fixtures/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/fixtures/pass..................................ok +test/unit/vendor/lime/fixtures/pass_with_one_error...................errors + Errors: + - Notice: some user error message +test/unit/vendor/lime/fixtures/pass_with_one_parse_error.............dubious + Test returned status 255 + Failed tests: 0 +test/unit/vendor/lime/fixtures/pass_with_one_throw_exception.........errors + Errors: + - LogicException: some exception message +test/unit/vendor/lime/fixtures/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/fixtures/pass_with_plan_more_than_total........dubious + Test returned status 255 + Looks like you planned 2 tests but only ran 1. +test/unit/vendor/lime/fixtures/skip..................................ok +test/unit/vendor/lime/fixtures/skip_with_plan........................ok +Failed Test Stat Total Fail Errors List of Failed +-------------------------------------------------------------------------- +it/vendor/lime/fixtures/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 +e/fixtures/pass_with_one_error 0 1 0 1 +ures/pass_with_one_parse_error 255 1 1 0 0 +/pass_with_one_throw_exception 0 0 0 1 +pass_with_plan_less_than_total 255 2 0 0 +pass_with_plan_more_than_total 255 1 0 0 +Failed 8/11 test scripts, 27.27% okay. 6/14 subtests failed, 57.14% okay. + +EOF); + } +} + +(new lime_harnessTest())->run(); diff --git a/test/unit/vendor/lime/lime_testTest.php b/test/unit/vendor/lime/lime_testTest.php new file mode 100644 index 000000000..24dec6536 --- /dev/null +++ b/test/unit/vendor/lime/lime_testTest.php @@ -0,0 +1,178 @@ +info($name); + + ob_start(); + $exitCode = (new lime_harness())->executePhpFile(__DIR__.'/fixtures/'.$name.'.php'); + $output = ob_get_clean(); + + $this->is($exitCode, $expectedStatusCode, 'with test '.$name.' will exit with status code '.$expectedStatusCode); + + $this->is($this->removeTrailingSpaces($output), $expectedOutput, 'test '.$name.' output'); + } + + private function removeTrailingSpaces(string $output): string + { + return preg_replace("/ *\n/", "\n", $output); + } + + public function testFiles() + { + foreach ($this->provideTestCases() as $parameters) { + $this->whenExecutePhpFileWillHaveStatusCodeAndOutput(...$parameters); + } + } + + private function provideTestCases() + { + yield [ + /* name */ 'pass', + /* expectedStatusCode */ 0, + /* expectedOutput */ <<<'EOF' +ok 1 +1..1 +# Looks like everything went fine. + +EOF + ]; + + yield [ + /* name */ 'failed', + /* expectedStatusCode */ 0, + /* expectedOutput */ <<<'EOF' +not ok 1 +# Failed test (./test/unit/vendor/lime/fixtures/failed.php at line 7) +# got: false +# expected: true +1..1 +# Looks like you failed 1 tests of 1. + +EOF + ]; + + yield [ + /* name */ 'failed_with_plan_less_than_total', + /* expectedStatusCode */ 0, + /* expectedOutput */ <<<'EOF' +1..1 +not ok 1 +# Failed test (./test/unit/vendor/lime/fixtures/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 + ]; + + yield [ + /* name */ 'failed_with_plan_more_than_total', + /* expectedStatusCode */ 0, + /* expectedOutput */ <<<'EOF' +1..2 +not ok 1 +# Failed test (./test/unit/vendor/lime/fixtures/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 + ]; + + yield [ + /* 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 + ]; + + yield [ + /* 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 + ]; + + yield [ + /* name */ 'pass_with_one_error', + /* expectedStatusCode */ 0, + /* expectedOutput */ <<<'EOF' + + + Notice: some user error message + (in test/unit/vendor/lime/fixtures/pass_with_one_error.php on line + 11) + + +Exception trace: + at test/unit/vendor/lime/fixtures/pass_with_one_error.php:11 + trigger_error() at test/unit/vendor/lime/fixtures/pass_with_one_error.php:11 + +ok 1 +1..1 +# Looks like everything went fine. + +EOF + ]; + + yield [ + /* name */ 'pass_with_one_throw_exception', + /* expectedStatusCode */ 0, + /* expectedOutput */ <<<'EOF' + + + LogicException: some exception message + (in + test/unit/vendor/lime/fixtures/pass_with_one_throw_exception.php + on line 7) + + +# Looks like everything went fine. + +EOF + ]; + + yield [ + /* name */ 'skip', + /* expectedStatusCode */ 0, + /* expectedOutput */ <<<'EOF' +ok 1 # SKIP some skip message +1..1 +# Looks like everything went fine. + +EOF + ]; + + yield [ + /* name */ 'skip_with_plan', + /* expectedStatusCode */ 0, + /* expectedOutput */ <<<'EOF' +1..2 +ok 1 # SKIP some skip message +ok 2 # SKIP some skip message +# Looks like everything went fine. + +EOF + ]; + } +} + +(new lime_testTest())->run(); diff --git a/test/unit/vendor/lime/tools/TestCase.php b/test/unit/vendor/lime/tools/TestCase.php new file mode 100644 index 000000000..933222ae3 --- /dev/null +++ b/test/unit/vendor/lime/tools/TestCase.php @@ -0,0 +1,39 @@ +test = new lime_test(); + } + + public function run(): void + { + foreach ($this->findAllTests() as $methodName) { + $this->test->diag($methodName); + + $this->$methodName(); + } + } + + private function findAllTests(): iterable + { + foreach (get_class_methods($this) as $methodName) { + if (0 === strpos($methodName, 'test')) { + yield $methodName; + } + } + } + + protected function info($message): void + { + $this->test->info($message); + } + + protected function is($expected, $actual, $message = ''): void + { + $this->test->is($expected, $actual, $message); + } +} diff --git a/test/unit/vendor/lime/tools/lime_no_colorizer.php b/test/unit/vendor/lime/tools/lime_no_colorizer.php new file mode 100644 index 000000000..9bc5f45df --- /dev/null +++ b/test/unit/vendor/lime/tools/lime_no_colorizer.php @@ -0,0 +1,8 @@ +