Skip to content

Commit 030d2e7

Browse files
committed
fixup! fix(test): exit code of lime test
1 parent 824471f commit 030d2e7

File tree

5 files changed

+246
-138
lines changed

5 files changed

+246
-138
lines changed

lib/vendor/lime/lime.php

Lines changed: 87 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,8 +1004,6 @@ public function run()
10041004
$this->stats['files'][$file] = array();
10051005
$stats = &$this->stats['files'][$file];
10061006

1007-
$relative_file = $this->get_relative_file($file);
1008-
10091007
$test_file = tempnam($this->options['test_path'], 'lime_test').'.php';
10101008
$result_file = tempnam($this->options['test_path'], 'lime_result');
10111009
file_put_contents($test_file, <<<EOF
@@ -1033,81 +1031,49 @@ function lime_shutdown()
10331031
'file' => $file,
10341032
'tests' => array(),
10351033
'stats' => array(
1036-
'plan' => 1,
1037-
'total' => 1,
1038-
'failed' => array(0),
1034+
'plan' => null,
1035+
'total' => 0,
1036+
'failed' => array(),
10391037
'passed' => array(),
10401038
'skipped' => array(),
1041-
'errors' => array(),
1039+
'errors' => array(
1040+
array(
1041+
'message' => 'Missing test report. It is probably due to a Parse error.',
1042+
)
1043+
),
10421044
),
10431045
),
10441046
);
10451047
}
10461048
unlink($result_file);
10471049

10481050
$file_stats = &$stats['output'][0]['stats'];
1049-
$this->stats['total'] += $file_stats['total'];
10501051

10511052
$delta = $this->computePlanDeltaFromFileStats($file_stats);
10521053

1053-
if (0 === $stats['status_code']) {
1054-
$stats['status'] = 'ok';
1055-
} else {
1056-
$stats['status'] = $file_stats['failed'] ? 'not ok' : ($file_stats['errors'] ? 'errors' : 'dubious');
1057-
}
1058-
1059-
if (true === $this->full_output)
1060-
{
1061-
$this->output->echoln(sprintf('%s%s%s', $relative_file, '.....', $stats['status']));
1062-
}
1063-
else
1064-
{
1065-
$this->output->echoln(sprintf('%s%s%s', substr($relative_file, -min(67, strlen($relative_file))), str_repeat('.', 70 - min(67, strlen($relative_file))), $stats['status']));
1066-
}
1067-
1068-
if ('dubious' == $stats['status'])
1069-
{
1070-
$this->output->echoln(sprintf(' Test returned status %s', $stats['status_code']));
1071-
}
1054+
$stats['status'] = $this->computeStatusWithCodeAndFileStats(
1055+
$stats['status_code'],
1056+
$file_stats
1057+
);
10721058

1073-
if ('ok' != $stats['status'])
1074-
{
1059+
if ('ok' !== $stats['status']) {
10751060
$this->stats['failed_files'][] = $file;
10761061
}
10771062

1078-
if ($delta > 0)
1079-
{
1080-
$this->output->echoln(sprintf(' Looks like you planned %d tests but only ran %d.', $file_stats['plan'], $file_stats['total']));
1063+
$this->stats['total'] += $file_stats['total'];
10811064

1065+
if ($delta > 0) {
10821066
$this->stats['failed_tests'] += $delta;
10831067
$this->stats['total'] += $delta;
10841068
}
1085-
else if ($delta < 0)
1086-
{
1087-
$this->output->echoln(sprintf(' Looks like you planned %s test but ran %s extra.', $file_stats['plan'], $file_stats['total'] - $file_stats['plan']));
1088-
}
10891069

1090-
if (false !== $file_stats && $file_stats['failed'])
1091-
{
1070+
if ($file_stats['failed']) {
10921071
$this->stats['failed_tests'] += count($file_stats['failed']);
1093-
1094-
$this->output->echoln(sprintf(" Failed tests: %s", implode(', ', $file_stats['failed'])));
10951072
}
10961073

1097-
if (false !== $file_stats && $file_stats['errors'])
1098-
{
1099-
$this->output->echoln(' Errors:');
1074+
$this->writeFileSummary($file, $stats['status']);
11001075

1101-
$error_count = count($file_stats['errors']);
1102-
for ($i = 0; $i < 3 && $i < $error_count; ++$i)
1103-
{
1104-
$this->output->echoln(' - ' . $file_stats['errors'][$i]['message'], null, false);
1105-
}
1106-
if ($error_count > 3)
1107-
{
1108-
$this->output->echoln(sprintf(' ... and %s more', $error_count-3));
1109-
}
1110-
}
1076+
$this->writeFileDetails($stats, $file_stats, $delta);
11111077
}
11121078

11131079
if (count($this->stats['failed_files']))
@@ -1197,6 +1163,75 @@ private function computePlanDeltaFromFileStats(array $fileStats): int
11971163
}
11981164
}
11991165

1166+
private function computeStatusWithCodeAndFileStats(int $statusCode, array $fileStats): string
1167+
{
1168+
if (0 === $statusCode) {
1169+
return 'ok';
1170+
}
1171+
1172+
if ($fileStats['failed']) {
1173+
return 'not ok';
1174+
}
1175+
1176+
if ($fileStats['errors']) {
1177+
return 'errors';
1178+
}
1179+
1180+
return 'dubious';
1181+
}
1182+
1183+
private function writeFileSummary(string $file, string $status): void
1184+
{
1185+
$relativeFile = $this->get_relative_file($file);
1186+
1187+
if (true === $this->full_output) {
1188+
$this->output->echoln(sprintf('%s%s%s', $relativeFile, '.....', $status));
1189+
} else {
1190+
$this->output->echoln(sprintf('%s%s%s',
1191+
substr($relativeFile, -min(67, strlen($relativeFile))),
1192+
str_repeat('.', 70 - min(67, strlen($relativeFile))),
1193+
$status
1194+
));
1195+
}
1196+
}
1197+
1198+
private function writeFileDetails(array $stats, array $fileStats, int $delta): void
1199+
{
1200+
if ('dubious' === $stats['status'])
1201+
{
1202+
$this->output->echoln(sprintf(' Test returned status %s', $stats['status_code']));
1203+
}
1204+
1205+
if ($delta > 0)
1206+
{
1207+
$this->output->echoln(sprintf(' Looks like you planned %d tests but only ran %d.', $fileStats['plan'], $fileStats['total']));
1208+
}
1209+
else if ($delta < 0)
1210+
{
1211+
$this->output->echoln(sprintf(' Looks like you planned %s test but ran %s extra.', $fileStats['plan'], $fileStats['total'] - $fileStats['plan']));
1212+
}
1213+
1214+
if (false !== $fileStats && $fileStats['failed'])
1215+
{
1216+
$this->output->echoln(sprintf(" Failed tests: %s", implode(', ', $fileStats['failed'])));
1217+
}
1218+
1219+
if (false !== $fileStats && $fileStats['errors'])
1220+
{
1221+
$this->output->echoln(' Errors:');
1222+
1223+
$error_count = count($fileStats['errors']);
1224+
for ($i = 0; $i < 3 && $i < $error_count; ++$i)
1225+
{
1226+
$this->output->echoln(' - ' . $fileStats['errors'][$i]['message'], null, false);
1227+
}
1228+
if ($error_count > 3)
1229+
{
1230+
$this->output->echoln(sprintf(' ... and %s more', $error_count-3));
1231+
}
1232+
}
1233+
}
1234+
12001235
public function get_failed_files()
12011236
{
12021237
return isset($this->stats['failed_files']) ? $this->stats['failed_files'] : array();

test/unit/vendor/lime/fixtures/pass_with_one_error.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
'error_reporting' => true,
99
]);
1010

11-
trigger_error('some use error message', E_USER_ERROR);
11+
trigger_error('some user error message', E_USER_ERROR);
1212

1313
$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();
6+
7+
parse_error

test/unit/vendor/lime/limeTest.php renamed to test/unit/vendor/lime/lime_harnessTest.php

Lines changed: 19 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@ function removeTrailingSpaces(string $output): string
77
return preg_replace("/ *\n/", "\n", $output);
88
}
99

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-
2310
function whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message)
2411
{
2512
$harness = new lime_harness();
@@ -33,7 +20,7 @@ function whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expe
3320

3421
$test->is($expectedOverallSucceed, $allTestsSucceed, $message);
3522

36-
$test->is(removeTrailingSpaces($output), $expectedOutput, 'test harness result');
23+
$test->is(removeTrailingSpaces($output), $expectedOutput, 'test harness result output');
3724
}
3825

3926
class lime_no_colorizer extends lime_colorizer
@@ -43,7 +30,7 @@ public function __construct()
4330
}
4431
}
4532

46-
$test = new lime_test(22);
33+
$test = new lime_test(12);
4734

4835
$files = [
4936
__DIR__.'/fixtures/failed.php',
@@ -83,6 +70,7 @@ public function __construct()
8370
$message = 'with at least one failed test file will fail the overall test suite';
8471
whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message);
8572

73+
8674
$files = [__DIR__.'/fixtures/pass_with_plan_less_than_total.php'];
8775
$expectedOverallSucceed = false;
8876
$expectedOutput = <<<'EOF'
@@ -98,12 +86,13 @@ public function __construct()
9886
$message = 'with at least one test file that not follow the plan will fail the overall test suite';
9987
whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message);
10088

89+
10190
$files = [__DIR__.'/fixtures/pass_with_one_error.php'];
10291
$expectedOverallSucceed = false;
10392
$expectedOutput = <<<'EOF'
10493
test/unit/vendor/lime/fixtures/pass_with_one_error...................errors
10594
Errors:
106-
- Notice: some use error message
95+
- Notice: some user error message
10796
Failed Test Stat Total Fail Errors List of Failed
10897
--------------------------------------------------------------------------
10998
e/fixtures/pass_with_one_error 1 1 0 1
@@ -113,6 +102,7 @@ public function __construct()
113102
$message = 'with at least one error will fail the overall test suite';
114103
whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message);
115104

105+
116106
$files = [__DIR__.'/fixtures/pass_with_one_throw_exception.php'];
117107
$expectedOverallSucceed = false;
118108
$expectedOutput = <<<'EOF'
@@ -128,6 +118,7 @@ public function __construct()
128118
$message = 'with at least one thrown Exception will fail the overall test suite';
129119
whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message);
130120

121+
131122
$files = [__DIR__.'/fixtures/pass.php'];
132123
$expectedOverallSucceed = true;
133124
$expectedOutput = <<<'EOF'
@@ -136,78 +127,21 @@ public function __construct()
136127
Files=1, Tests=1
137128

138129
EOF;
139-
$message = 'with all tests passes without error or exception will succeed the overall test suite';
130+
$message = 'with all tests passes without error and exception will succeed the overall test suite';
140131
whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message);
141132

142-
$name = 'pass';
143-
$expectedStatusCode = 0;
144-
$expectedOutput = <<<'EOF'
145-
ok 1
146-
1..1
147-
# Looks like everything went fine.
148-
149-
EOF;
150-
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);
151-
152-
$name = 'failed';
153-
$expectedStatusCode = 1;
154-
$expectedOutput = <<<'EOF'
155-
not ok 1
156-
# Failed test (./test/unit/vendor/lime/fixtures/failed.php at line 7)
157-
# got: false
158-
# expected: true
159-
1..1
160-
# Looks like you failed 1 tests of 1.
161-
162-
EOF;
163-
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);
164133

165-
$name = 'failed_with_plan_less_than_total';
166-
$expectedStatusCode = 1;
167-
$expectedOutput = <<<'EOF'
168-
1..1
169-
not ok 1
170-
# Failed test (./test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total.php at line 7)
171-
# got: false
172-
# expected: true
173-
ok 2
174-
# Looks like you planned 1 tests but ran 1 extra.
175-
# Looks like you failed 1 tests of 2.
176-
177-
EOF;
178-
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);
179-
180-
$name = 'failed_with_plan_more_than_total';
181-
$expectedStatusCode = 1;
182-
$expectedOutput = <<<'EOF'
183-
1..2
184-
not ok 1
185-
# Failed test (./test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total.php at line 7)
186-
# got: false
187-
# expected: true
188-
# Looks like you planned 2 tests but only ran 1.
189-
# Looks like you failed 1 tests of 1.
190-
191-
EOF;
192-
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);
193-
194-
$name = 'pass_with_plan_less_than_total';
195-
$expectedStatusCode = 255;
196-
$expectedOutput = <<<'EOF'
197-
1..1
198-
ok 1
199-
ok 2
200-
# Looks like you planned 1 tests but ran 1 extra.
201-
202-
EOF;
203-
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);
204-
205-
$name = 'pass_with_plan_more_than_total';
206-
$expectedStatusCode = 255;
134+
$files = [__DIR__.'/fixtures/pass_with_one_parse_error.php'];
135+
$expectedOverallSucceed = false;
207136
$expectedOutput = <<<'EOF'
208-
1..2
209-
ok 1
210-
# Looks like you planned 2 tests but only ran 1.
137+
test/unit/vendor/lime/fixtures/pass_with_one_parse_error.............errors
138+
Errors:
139+
- Missing test report. It is probably due to a Parse error.
140+
Failed Test Stat Total Fail Errors List of Failed
141+
--------------------------------------------------------------------------
142+
ures/pass_with_one_parse_error 255 0 0 1
143+
Failed 1/1 test scripts, 0.00% okay. 0/0 subtests failed, 0.00% okay.
211144

212145
EOF;
213-
whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput);
146+
$message = 'with parse error will fail the overall test suite';
147+
whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message);

0 commit comments

Comments
 (0)