Skip to content

Commit f25c0cd

Browse files
committed
fixup! fix(test): exit code of lime test
1 parent 3290406 commit f25c0cd

File tree

3 files changed

+89
-20
lines changed

3 files changed

+89
-20
lines changed

lib/plugins/sfDoctrinePlugin/test/bin/coverage.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@
3232

3333
$finder = sfFinder::type('file')->name('*.php')->prune('vendor')->prune('test')->prune('data');
3434
$c->register($finder->in($c->base_dir));
35-
$c->run();
35+
36+
$allTestsSucceed = $c->run();
37+
38+
exit($allTestsSucceed ? 0 : 1);

lib/vendor/lime/lime.php

+81-18
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,25 @@
1616
*/
1717
class lime_test
1818
{
19-
const EPSILON = 0.0000000001;
19+
public const EPSILON = 0.0000000001;
2020

2121
protected $test_nb = 0;
2222
protected $output = null;
2323
protected $results = array();
2424
protected $options = array();
2525

26-
static protected $all_results = array();
26+
protected static $all_results = array();
27+
28+
private const STATE_PASS = 0;
29+
private const STATE_FAIL = 1;
30+
31+
private static $instanceCount = 0;
32+
private static $finalState = self::STATE_PASS;
2733

2834
public function __construct($plan = null, $options = array())
2935
{
36+
++self::$instanceCount;
37+
3038
// for BC
3139
if (!is_array($options))
3240
{
@@ -130,31 +138,86 @@ static public function to_xml($results = null)
130138

131139
public function __destruct()
132140
{
133-
$plan = $this->results['stats']['plan'];
134-
$passed = count($this->results['stats']['passed']);
141+
$testSuiteState = $this->determineAndPrintStateOfTestSuite();
142+
143+
flush();
144+
145+
$this->keepTheWorstState($testSuiteState);
146+
147+
$this->finalizeLastInstanceDestructorWithProcessExit();
148+
}
149+
150+
private function determineAndPrintStateOfTestSuite(): int
151+
{
152+
$planState = $this->determineAndPrintStateOfPlan();
135153
$failed = count($this->results['stats']['failed']);
154+
155+
if ($failed) {
156+
$passed = count($this->results['stats']['passed']);
157+
158+
$this->output->red_bar(sprintf("# Looks like you failed %d tests of %d.", $failed, $passed + $failed));
159+
160+
return self::STATE_FAIL;
161+
}
162+
163+
if (self::STATE_FAIL === $planState) {
164+
return self::STATE_FAIL;
165+
}
166+
167+
$this->output->green_bar("# Looks like everything went fine.");
168+
169+
return self::STATE_PASS;
170+
}
171+
172+
private function determineAndPrintStateOfPlan(): int
173+
{
174+
$plan = $this->results['stats']['plan'];
136175
$total = $this->results['stats']['total'];
137-
is_null($plan) and $plan = $total and $this->output->echoln(sprintf("1..%d", $plan));
138176

139-
if ($total > $plan)
140-
{
141-
$this->output->red_bar(sprintf("# Looks like you planned %d tests but ran %d extra.", $plan, $total - $plan));
177+
if (null === $plan) {
178+
$plan = $total;
179+
180+
$this->output->echoln(sprintf("1..%d", $plan));
142181
}
143-
elseif ($total < $plan)
144-
{
182+
183+
if ($total > $plan) {
184+
$this->output->red_bar(sprintf("# Looks like you planned %d tests but ran %d extra.", $plan, $total - $plan));
185+
} elseif ($total < $plan) {
145186
$this->output->red_bar(sprintf("# Looks like you planned %d tests but only ran %d.", $plan, $total));
146187
}
147188

148-
if ($failed)
149-
{
150-
$this->output->red_bar(sprintf("# Looks like you failed %d tests of %d.", $failed, $passed + $failed));
189+
return $total === $plan ? self::STATE_PASS : self::STATE_FAIL;
190+
}
191+
192+
private function keepTheWorstState(int $state): void
193+
{
194+
if ($this->stateIsTheWorst($state)) {
195+
self::$finalState = $state;
151196
}
152-
else if ($total == $plan)
153-
{
154-
$this->output->green_bar("# Looks like everything went fine.");
197+
}
198+
199+
private function stateIsTheWorst(int $state): bool
200+
{
201+
return self::$finalState < $state;
202+
}
203+
204+
private function finalizeLastInstanceDestructorWithProcessExit(): void
205+
{
206+
--self::$instanceCount;
207+
208+
if (0 === self::$instanceCount) {
209+
exit($this->determineExitCodeFromState(self::$finalState));
155210
}
211+
}
156212

157-
flush();
213+
private function determineExitCodeFromState(int $state): int
214+
{
215+
switch ($state) {
216+
case self::STATE_PASS:
217+
return 0;
218+
default:
219+
return 1;
220+
}
158221
}
159222

160223
/**
@@ -969,7 +1032,7 @@ function lime_shutdown()
9691032
$delta = 0;
9701033
if ($return > 0)
9711034
{
972-
$stats['status'] = $file_stats['errors'] ? 'errors' : 'dubious';
1035+
$stats['status'] = $file_stats['failed'] ? 'not ok' : ($file_stats['errors'] ? 'errors' : 'dubious');
9731036
$stats['status_code'] = $return;
9741037
}
9751038
else

test/bin/coverage.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@
4242
$finder = sfFinder::type('file')->name($name.'.class.php')->prune('vendor')->prune('test')->prune('data');
4343

4444
$c->register($finder->in($c->base_dir));
45-
$c->run();
45+
46+
$allTestsSucceed = $c->run();
47+
48+
exit($allTestsSucceed ? 0 : 1);

0 commit comments

Comments
 (0)