Skip to content

Commit 8753d4a

Browse files
committed
fix(test): exit code of lime test
1 parent 8b68ee8 commit 8753d4a

File tree

8 files changed

+234
-2
lines changed

8 files changed

+234
-2
lines changed

lib/vendor/lime/lime.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,7 @@ function lime_shutdown()
952952
);
953953

954954
ob_start();
955-
// see http://trac.symfony-project.org/ticket/5437 for the explanation on the weird "cd" thing
956-
passthru(sprintf('cd & %s %s 2>&1', escapeshellarg($this->php_cli), escapeshellarg($test_file)), $return);
955+
$return = $this->executePhpFile($test_file);
957956
ob_end_clean();
958957
unlink($test_file);
959958

@@ -1123,6 +1122,20 @@ public function get_failed_files()
11231122
{
11241123
return isset($this->stats['failed_files']) ? $this->stats['failed_files'] : array();
11251124
}
1125+
1126+
/**
1127+
* The command fails if the path to php interpreter contains spaces.
1128+
* The only workaround is adding a "nop" command call before the quoted command.
1129+
* The weird "cd &".
1130+
*
1131+
* see http://trac.symfony-project.org/ticket/5437
1132+
*/
1133+
public function executePhpFile(string $phpFile): int
1134+
{
1135+
passthru(sprintf('cd & %s %s 2>&1', escapeshellarg($this->php_cli), escapeshellarg($phpFile)), $return);
1136+
1137+
return $return;
1138+
}
11261139
}
11271140

11281141
class lime_coverage extends lime_registration
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
require_once __DIR__.'/../../../../bootstrap/unit.php';
4+
5+
$test = new lime_test();
6+
7+
$test->is(false, true);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
require_once __DIR__.'/../../../../bootstrap/unit.php';
4+
5+
$test = new lime_test(1);
6+
7+
$test->is(false, true);
8+
$test->is(true, true);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
require_once __DIR__.'/../../../../bootstrap/unit.php';
4+
5+
$test = new lime_test(2);
6+
7+
$test->is(false, true);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
require_once __DIR__.'/../../../../bootstrap/unit.php';
4+
5+
$test = new lime_test();
6+
7+
$test->is(true, true);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
require_once __DIR__.'/../../../../bootstrap/unit.php';
4+
5+
$test = new lime_test(1);
6+
7+
$test->is(true, true);
8+
$test->is(true, true);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
require_once __DIR__.'/../../../../bootstrap/unit.php';
4+
5+
$test = new lime_test(2);
6+
7+
$test->is(true, true);

test/unit/vendor/lime/limeTest.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
3+
require_once __DIR__.'/../../../bootstrap/unit.php';
4+
5+
function removeTrailingSpaces(string $output): string
6+
{
7+
return preg_replace("/ *\n/", "\n", $output);
8+
}
9+
10+
function whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput)
11+
{
12+
$test->diag($name);
13+
14+
ob_start();
15+
$exitCode = (new lime_harness())->executePhpFile(__DIR__.'/fixtures/'.$name.'.php');
16+
$output = ob_get_clean();
17+
18+
$test->is($exitCode, $expectedStatusCode, 'with test '.$name.' will exit with status code '.$expectedStatusCode);
19+
20+
$test->is(removeTrailingSpaces($output), $expectedOutput, 'test '.$name.' output');
21+
}
22+
23+
function whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message)
24+
{
25+
$harness = new lime_harness();
26+
$harness->output->colorizer = new lime_no_colorizer();
27+
28+
$harness->register($files);
29+
30+
ob_start();
31+
$allTestsSucceed = $harness->run();
32+
$output = ob_get_clean();
33+
34+
$test->is($expectedOverallSucceed, $allTestsSucceed, $message);
35+
36+
$test->is(removeTrailingSpaces($output), $expectedOutput, 'test harness result');
37+
}
38+
39+
class lime_no_colorizer extends lime_colorizer
40+
{
41+
public function __construct()
42+
{
43+
}
44+
}
45+
46+
$test = new lime_test(16);
47+
48+
$files = [
49+
__DIR__.'/fixtures/failed.php',
50+
__DIR__.'/fixtures/failed_with_plan_less_than_total.php',
51+
__DIR__.'/fixtures/failed_with_plan_more_than_total.php',
52+
__DIR__.'/fixtures/pass.php',
53+
__DIR__.'/fixtures/pass_with_plan_less_than_total.php',
54+
__DIR__.'/fixtures/pass_with_plan_more_than_total.php',
55+
];
56+
$expectedOverallSucceed = false;
57+
$expectedOutput = <<<'EOF'
58+
test/unit/vendor/lime/fixtures/failed................................not ok
59+
Failed tests: 1
60+
test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total......dubious
61+
Test returned status 255
62+
Looks like you planned 1 test but ran 1 extra.
63+
Failed tests: 1
64+
test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total......dubious
65+
Test returned status 255
66+
Looks like you planned 2 tests but only ran 1.
67+
Failed tests: 1
68+
test/unit/vendor/lime/fixtures/pass..................................ok
69+
test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total........dubious
70+
Test returned status 255
71+
Looks like you planned 1 test but ran 1 extra.
72+
test/unit/vendor/lime/fixtures/pass_with_plan_more_than_total........dubious
73+
Test returned status 255
74+
Looks like you planned 2 tests but only ran 1.
75+
Failed Test Stat Total Fail Errors List of Failed
76+
--------------------------------------------------------------------------
77+
it/vendor/lime/fixtures/failed 0 1 1 0 1
78+
iled_with_plan_less_than_total 255 2 1 0 1
79+
iled_with_plan_more_than_total 255 1 1 0 1
80+
pass_with_plan_less_than_total 255 2 0 0
81+
pass_with_plan_more_than_total 255 1 0 0
82+
Failed 5/6 test scripts, 16.67% okay. 5/10 subtests failed, 50.00% okay.
83+
84+
EOF;
85+
$message = 'with at least one failed test file will fail the overall test suite';
86+
whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message);
87+
88+
$files = [__DIR__.'/fixtures/pass_with_plan_less_than_total.php'];
89+
$expectedOverallSucceed = false;
90+
$expectedOutput = <<<'EOF'
91+
test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total........dubious
92+
Test returned status 255
93+
Looks like you planned 1 test but ran 1 extra.
94+
Failed Test Stat Total Fail Errors List of Failed
95+
--------------------------------------------------------------------------
96+
pass_with_plan_less_than_total 255 2 0 0
97+
Failed 1/1 test scripts, 0.00% okay. 0/2 subtests failed, 100.00% okay.
98+
99+
EOF;
100+
$message = 'with at least one test file that not follow the plan will fail the overall test suite';
101+
whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message);
102+
103+
104+
$name = 'pass';
105+
$expectedStatusCode = 0;
106+
$expectedOutput = <<<'EOF'
107+
ok 1
108+
1..1
109+
# Looks like everything went fine.
110+
111+
EOF;
112+
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);
113+
114+
$name = 'failed';
115+
$expectedStatusCode = 0;
116+
$expectedOutput = <<<'EOF'
117+
not ok 1
118+
# Failed test (./test/unit/vendor/lime/fixtures/failed.php at line 7)
119+
# got: false
120+
# expected: true
121+
1..1
122+
# Looks like you failed 1 tests of 1.
123+
124+
EOF;
125+
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);
126+
127+
$name = 'failed_with_plan_less_than_total';
128+
$expectedStatusCode = 0;
129+
$expectedOutput = <<<'EOF'
130+
1..1
131+
not ok 1
132+
# Failed test (./test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total.php at line 7)
133+
# got: false
134+
# expected: true
135+
ok 2
136+
# Looks like you planned 1 tests but ran 1 extra.
137+
# Looks like you failed 1 tests of 2.
138+
139+
EOF;
140+
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);
141+
142+
$name = 'failed_with_plan_more_than_total';
143+
$expectedStatusCode = 0;
144+
$expectedOutput = <<<'EOF'
145+
1..2
146+
not ok 1
147+
# Failed test (./test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total.php at line 7)
148+
# got: false
149+
# expected: true
150+
# Looks like you planned 2 tests but only ran 1.
151+
# Looks like you failed 1 tests of 1.
152+
153+
EOF;
154+
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);
155+
156+
$name = 'pass_with_plan_less_than_total';
157+
$expectedStatusCode = 0;
158+
$expectedOutput = <<<'EOF'
159+
1..1
160+
ok 1
161+
ok 2
162+
# Looks like you planned 1 tests but ran 1 extra.
163+
164+
EOF;
165+
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);
166+
167+
$name = 'pass_with_plan_more_than_total';
168+
$expectedStatusCode = 0;
169+
$expectedOutput = <<<'EOF'
170+
1..2
171+
ok 1
172+
# Looks like you planned 2 tests but only ran 1.
173+
174+
EOF;
175+
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);

0 commit comments

Comments
 (0)