Skip to content

Commit

Permalink
fixup! 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 3290406 commit f25c0cd
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 20 deletions.
5 changes: 4 additions & 1 deletion lib/plugins/sfDoctrinePlugin/test/bin/coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@

$finder = sfFinder::type('file')->name('*.php')->prune('vendor')->prune('test')->prune('data');
$c->register($finder->in($c->base_dir));
$c->run();

$allTestsSucceed = $c->run();

exit($allTestsSucceed ? 0 : 1);
99 changes: 81 additions & 18 deletions lib/vendor/lime/lime.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@
*/
class lime_test
{
const EPSILON = 0.0000000001;
public const EPSILON = 0.0000000001;

protected $test_nb = 0;
protected $output = null;
protected $results = array();
protected $options = array();

static protected $all_results = array();
protected static $all_results = array();

private const STATE_PASS = 0;
private const STATE_FAIL = 1;

private static $instanceCount = 0;
private static $finalState = self::STATE_PASS;

public function __construct($plan = null, $options = array())
{
++self::$instanceCount;

// for BC
if (!is_array($options))
{
Expand Down Expand Up @@ -130,31 +138,86 @@ static public function to_xml($results = null)

public function __destruct()
{
$plan = $this->results['stats']['plan'];
$passed = count($this->results['stats']['passed']);
$testSuiteState = $this->determineAndPrintStateOfTestSuite();

flush();

$this->keepTheWorstState($testSuiteState);

$this->finalizeLastInstanceDestructorWithProcessExit();
}

private function determineAndPrintStateOfTestSuite(): int
{
$planState = $this->determineAndPrintStateOfPlan();
$failed = count($this->results['stats']['failed']);

if ($failed) {
$passed = count($this->results['stats']['passed']);

$this->output->red_bar(sprintf("# Looks like you failed %d tests of %d.", $failed, $passed + $failed));

return self::STATE_FAIL;
}

if (self::STATE_FAIL === $planState) {
return self::STATE_FAIL;
}

$this->output->green_bar("# Looks like everything went fine.");

return self::STATE_PASS;
}

private function determineAndPrintStateOfPlan(): int
{
$plan = $this->results['stats']['plan'];
$total = $this->results['stats']['total'];
is_null($plan) and $plan = $total and $this->output->echoln(sprintf("1..%d", $plan));

if ($total > $plan)
{
$this->output->red_bar(sprintf("# Looks like you planned %d tests but ran %d extra.", $plan, $total - $plan));
if (null === $plan) {
$plan = $total;

$this->output->echoln(sprintf("1..%d", $plan));
}
elseif ($total < $plan)
{

if ($total > $plan) {
$this->output->red_bar(sprintf("# Looks like you planned %d tests but ran %d extra.", $plan, $total - $plan));
} elseif ($total < $plan) {
$this->output->red_bar(sprintf("# Looks like you planned %d tests but only ran %d.", $plan, $total));
}

if ($failed)
{
$this->output->red_bar(sprintf("# Looks like you failed %d tests of %d.", $failed, $passed + $failed));
return $total === $plan ? self::STATE_PASS : self::STATE_FAIL;
}

private function keepTheWorstState(int $state): void
{
if ($this->stateIsTheWorst($state)) {
self::$finalState = $state;
}
else if ($total == $plan)
{
$this->output->green_bar("# Looks like everything went fine.");
}

private function stateIsTheWorst(int $state): bool
{
return self::$finalState < $state;
}

private function finalizeLastInstanceDestructorWithProcessExit(): void
{
--self::$instanceCount;

if (0 === self::$instanceCount) {
exit($this->determineExitCodeFromState(self::$finalState));
}
}

flush();
private function determineExitCodeFromState(int $state): int
{
switch ($state) {
case self::STATE_PASS:
return 0;
default:
return 1;
}
}

/**
Expand Down Expand Up @@ -969,7 +1032,7 @@ function lime_shutdown()
$delta = 0;
if ($return > 0)
{
$stats['status'] = $file_stats['errors'] ? 'errors' : 'dubious';
$stats['status'] = $file_stats['failed'] ? 'not ok' : ($file_stats['errors'] ? 'errors' : 'dubious');
$stats['status_code'] = $return;
}
else
Expand Down
5 changes: 4 additions & 1 deletion test/bin/coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@
$finder = sfFinder::type('file')->name($name.'.class.php')->prune('vendor')->prune('test')->prune('data');

$c->register($finder->in($c->base_dir));
$c->run();

$allTestsSucceed = $c->run();

exit($allTestsSucceed ? 0 : 1);

0 comments on commit f25c0cd

Please sign in to comment.