Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When tests are focused via DSL, exit with a non-zero code. #204

Merged
merged 1 commit into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Next release

- **[NEW]** Introduced focused specs for powerful test isolation ([#199],
[#197], [#194], [#188], [#185], [#181])
[#204], [#197], [#194], [#188], [#185], [#181])
- **[IMPROVED]** Stack trace output now excludes irrelevant information ([#203],
[#170])
- **[FIXED]** Fixed error handler signature ([#198] - thanks [@YuraLukashik])
Expand All @@ -21,6 +21,7 @@
[#199]: https://github.com/peridot-php/peridot/pull/199
[#201]: https://github.com/peridot-php/peridot/pull/201
[#203]: https://github.com/peridot-php/peridot/pull/203
[#204]: https://github.com/peridot-php/peridot/pull/204

## 1.18.1 (2016-04-23)

Expand Down
14 changes: 14 additions & 0 deletions specs/command.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,19 @@
assert($exit == 1, "exit code should be 1");
});
});

context('when there are DSL focused tests', function() {
it('should return an exit code', function() {
$suite = new Suite('DSL focused suite', function() {});
$test = new Test('focused', function() {}, true);
$suite->addTest($test);
$runner = new Runner($suite, $this->configuration, $this->emitter);
$command = new Command($runner, $this->configuration, $this->factory, $this->emitter);
$command->setApplication($this->application);
$exit = $command->run(new ArrayInput([], $this->definition), $this->output);

assert($exit == 2, 'exit code should be 2');
});
});
});
});
26 changes: 21 additions & 5 deletions specs/runner.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@
$this->eventEmitter->on('test.start', function() use (&$count) {
$count++;
});
$this->runner->run(new TestResult($this->eventEmitter));
$result = new TestResult($this->eventEmitter);
$this->runner->run($result);
assert(1 == $count, 'expected 1 test:start events to fire');
assert(!$result->isFocusedByDsl(), 'should not be focused by DSL');
});

it('should apply focus patterns if a skip pattern has been set', function() {
Expand All @@ -73,8 +75,10 @@
$this->eventEmitter->on('test.start', function() use (&$count) {
$count++;
});
$this->runner->run(new TestResult($this->eventEmitter));
$result = new TestResult($this->eventEmitter);
$this->runner->run($result);
assert(1 == $count, 'expected 1 test:start events to fire');
assert(!$result->isFocusedByDsl(), 'should not be focused by DSL');
});

it('should apply focus patterns if both focus and skip patterns are set', function() {
Expand All @@ -85,8 +89,17 @@
$this->eventEmitter->on('test.start', function() use (&$count) {
$count++;
});
$this->runner->run(new TestResult($this->eventEmitter));
$result = new TestResult($this->eventEmitter);
$this->runner->run($result);
assert(1 == $count, 'expected 1 test:start events to fire');
assert(!$result->isFocusedByDsl(), 'should not be focused by DSL');
});

it('should mark the result as focused by DSL where appropriate', function() {
$this->suite->addTest(new Test('another passing spec', function() {}, true));
$result = new TestResult($this->eventEmitter);
$this->runner->run($result);
assert($result->isFocusedByDsl(), 'should be focused by DSL');
});

it("should emit a start event when the runner starts", function() {
Expand All @@ -98,14 +111,17 @@
assert($emitted, 'start event should have been emitted');
});

it("should emit an end event with run time when the runner ends", function() {
it("should emit an end event with run time and result when the runner ends", function() {
$time = null;
$this->eventEmitter->on('runner.end', function($timeToRun) use (&$time) {
$emittedResult = null;
$this->eventEmitter->on('runner.end', function($timeToRun, $result) use (&$time, &$emittedResult) {
$time = $timeToRun;
$emittedResult = $result;
});
$result = new TestResult(new EventEmitter());
$this->runner->run($result);
assert(is_numeric($time) && $result->getTestCount() > 0, 'end event with a time arg should have been emitted');
assert($emittedResult === $result, 'end event with a result arg should have been emitted');
});

it("should emit a fail event when a spec fails", function() {
Expand Down
16 changes: 15 additions & 1 deletion specs/spec-reporter.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use Evenement\EventEmitter;
use Peridot\Configuration;
use Peridot\Core\Test;
use Peridot\Core\TestResult;
use Peridot\Reporter\SpecReporter;
use Symfony\Component\Console\Output\BufferedOutput;

Expand Down Expand Up @@ -59,7 +60,7 @@
$this->emitter->emit('test.passed', [new Test('passing test', function() {})]);
$this->emitter->emit('test.failed', [new Test('failing test', function() {}), $this->exception]);
$this->emitter->emit('test.pending', [new Test('pending test', function(){})]);
$this->footer = $this->reporter->footer();
$this->reporter->footer();
$this->contents = $this->output->fetch();
});

Expand Down Expand Up @@ -87,6 +88,19 @@
});
});

describe('->warnings()', function() {
it('should output DSL focused test warnings', function() {
$this->configuration->disableColors();
$result = new TestResult($this->emitter);
$result->setIsFocusedByDsl(true);
$this->emitter->emit('runner.end', [1.0, $result]);
$this->reporter->warnings($result);
$this->contents = $this->output->fetch();
$expected = 'WARNING: Tests have been focused programmatically.';
assert(strstr($this->contents, $expected) !== false, 'should contain DSL focused warning');
});
});

});

function throwException(&$pattern)
Expand Down
8 changes: 8 additions & 0 deletions specs/test-result.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,12 @@
assert($result->getTestCount() === 1, 'should have returned test count');
});
});

describe('focused by DSL accessors', function () {
it('should allow access to the DSL focus state', function () {
$result = new TestResult($this->eventEmitter);
$result->setIsFocusedByDsl(true);
assert($result->isFocusedByDsl(), 'should be focused by DSL');
});
});
});
5 changes: 4 additions & 1 deletion src/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->eventEmitter->emit('peridot.load', [$this, $this->configuration]);


return $this->getResult();
}

Expand Down Expand Up @@ -179,6 +178,10 @@ protected function getResult()
return 1;
}

if ($result->isFocusedByDsl()) {
return 2;
}

return 0;
}
}
28 changes: 28 additions & 0 deletions src/Core/TestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class TestResult
*/
protected $pendingCount = 0;

/**
* True if focused specs were configured via DSL functions
*
* @var bool
*/
protected $isFocusedByDsl = false;

/**
* @param EventEmitterInterface $eventEmitter
*/
Expand Down Expand Up @@ -178,4 +185,25 @@ public function setPendingCount($pendingCount)
$this->pendingCount = $pendingCount;
return $this;
}

/**
* Returns true if focused specs were configured via DSL functions
*
* @return bool
*/
public function isFocusedByDsl()
{
return $this->isFocusedByDsl;
}

/**
* Mark this result as having focused specs configured via DSL functions
*
* @param bool $isFocusedByDsl
*/
public function setIsFocusedByDsl($isFocusedByDsl)
{
$this->isFocusedByDsl = $isFocusedByDsl;
return $this;
}
}
14 changes: 14 additions & 0 deletions src/Reporter/AbstractBaseReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Peridot\Core\HasEventEmitterTrait;
use Peridot\Core\Test;
use Peridot\Core\TestInterface;
use Peridot\Core\TestResult;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;

Expand Down Expand Up @@ -58,6 +59,7 @@ abstract class AbstractBaseReporter implements ReporterInterface
'white' => ['left' => "\033[37m", 'right' => "\033[39m"],
'success' => ['left' => "\033[32m", 'right' => "\033[39m"],
'error' => ['left' => "\033[31m", 'right' => "\033[39m"],
'warning' => ['left' => "\033[33m", 'right' => "\033[39m"],
'muted' => ['left' => "\033[90m", 'right' => "\033[0m"],
'pending' => ['left' => "\033[36m", 'right' => "\033[39m"],
);
Expand Down Expand Up @@ -187,6 +189,18 @@ public function footer()
}
}

/**
* Output result warnings
*
* @param TestResult $result
*/
public function warnings(TestResult $result)
{
if ($result->isFocusedByDsl()) {
$this->output->writeln($this->color('warning', 'WARNING: Tests have been focused programmatically.'));
}
}

/**
* Output a test failure.
*
Expand Down
10 changes: 8 additions & 2 deletions src/Reporter/SpecReporter.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
namespace Peridot\Reporter;

use Peridot\Core\Test;
use Peridot\Core\Suite;
use Peridot\Core\Test;
use Peridot\Core\TestResult;
use Peridot\Runner\Context;

/**
Expand Down Expand Up @@ -102,9 +103,14 @@ public function onTestPending(Test $test)
));
}

public function onRunnerEnd()
/**
* @param float $time
* @param TestResult $result
*/
public function onRunnerEnd($time, TestResult $result)
{
$this->footer();
$this->warnings($result);
}

/**
Expand Down
20 changes: 13 additions & 7 deletions src/Runner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ public function __construct(Suite $suite, Configuration $configuration, EventEmi
*/
public function run(TestResult $result)
{
$focusPattern = $this->configuration->getFocusPattern();
$skipPattern = $this->configuration->getSkipPattern();

if ($focusPattern !== null || $skipPattern !== null) {
$this->suite->applyFocusPatterns($focusPattern, $skipPattern);
}
$this->applyFocus($result);

$this->eventEmitter->on('test.failed', function () {
if ($this->configuration->shouldStopOnFailure()) {
Expand All @@ -62,6 +57,17 @@ public function run(TestResult $result)
$this->suite->setEventEmitter($this->eventEmitter);
$start = microtime(true);
$this->suite->run($result);
$this->eventEmitter->emit('runner.end', [microtime(true) - $start]);
$this->eventEmitter->emit('runner.end', [microtime(true) - $start, $result]);
}

private function applyFocus(TestResult $result)
{
$result->setIsFocusedByDsl($this->suite->isFocused());
$focusPattern = $this->configuration->getFocusPattern();
$skipPattern = $this->configuration->getSkipPattern();

if ($focusPattern !== null || $skipPattern !== null) {
$this->suite->applyFocusPatterns($focusPattern, $skipPattern);
}
}
}