Skip to content

Commit

Permalink
fix(test): exit code of lime test
Browse files Browse the repository at this point in the history
  • Loading branch information
alquerci committed Mar 27, 2024
1 parent 8b68ee8 commit 3290406
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/vendor/lime/lime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down
175 changes: 175 additions & 0 deletions test/unit/vendor/lime/lime_harness.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

require_once __DIR__.'/../../../bootstrap/unit.php';

function removeTrailingSpaces(string $output): string
{
return preg_replace("/ *\n/", "\n", $output);
}

function whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput)
{
$test->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);
7 changes: 7 additions & 0 deletions test/unit/vendor/lime/unit/failed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

require_once __DIR__.'/../../../../bootstrap/unit.php';

$test = new lime_test();

$test->is(false, true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

require_once __DIR__.'/../../../../bootstrap/unit.php';

$test = new lime_test(1);

$test->is(false, true);
$test->is(true, true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

require_once __DIR__.'/../../../../bootstrap/unit.php';

$test = new lime_test(2);

$test->is(false, true);
7 changes: 7 additions & 0 deletions test/unit/vendor/lime/unit/pass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

require_once __DIR__.'/../../../../bootstrap/unit.php';

$test = new lime_test();

$test->is(true, true);
8 changes: 8 additions & 0 deletions test/unit/vendor/lime/unit/pass_with_plan_less_than_total.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

require_once __DIR__.'/../../../../bootstrap/unit.php';

$test = new lime_test(1);

$test->is(true, true);
$test->is(true, true);
7 changes: 7 additions & 0 deletions test/unit/vendor/lime/unit/pass_with_plan_more_than_total.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

require_once __DIR__.'/../../../../bootstrap/unit.php';

$test = new lime_test(2);

$test->is(true, true);

0 comments on commit 3290406

Please sign in to comment.