diff --git a/specs/command.spec.php b/specs/command.spec.php index 38b1fff..3d113ea 100644 --- a/specs/command.spec.php +++ b/specs/command.spec.php @@ -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'); + }); + }); }); }); diff --git a/specs/runner.spec.php b/specs/runner.spec.php index 173dddf..b5b27e3 100644 --- a/specs/runner.spec.php +++ b/specs/runner.spec.php @@ -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() { @@ -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() { @@ -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() { @@ -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() { diff --git a/specs/spec-reporter.spec.php b/specs/spec-reporter.spec.php index 5433bbf..d54bb2b 100644 --- a/specs/spec-reporter.spec.php +++ b/specs/spec-reporter.spec.php @@ -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; @@ -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(); }); @@ -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) diff --git a/specs/test-result.spec.php b/specs/test-result.spec.php index 5f4f2dc..56e6799 100644 --- a/specs/test-result.spec.php +++ b/specs/test-result.spec.php @@ -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'); + }); + }); }); diff --git a/src/Console/Command.php b/src/Console/Command.php index a993a8d..6b7f168 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -145,7 +145,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->eventEmitter->emit('peridot.load', [$this, $this->configuration]); - return $this->getResult(); } @@ -179,6 +178,10 @@ protected function getResult() return 1; } + if ($result->isFocusedByDsl()) { + return 2; + } + return 0; } } diff --git a/src/Core/TestResult.php b/src/Core/TestResult.php index 1310750..643c029 100644 --- a/src/Core/TestResult.php +++ b/src/Core/TestResult.php @@ -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 */ @@ -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; + } } diff --git a/src/Reporter/AbstractBaseReporter.php b/src/Reporter/AbstractBaseReporter.php index 8ab680c..201a132 100644 --- a/src/Reporter/AbstractBaseReporter.php +++ b/src/Reporter/AbstractBaseReporter.php @@ -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; @@ -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"], ); @@ -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. * diff --git a/src/Reporter/SpecReporter.php b/src/Reporter/SpecReporter.php index fde4a4e..c88f115 100644 --- a/src/Reporter/SpecReporter.php +++ b/src/Reporter/SpecReporter.php @@ -1,8 +1,9 @@ footer(); + $this->warnings($result); } /** diff --git a/src/Runner/Runner.php b/src/Runner/Runner.php index 813d9a7..afe7259 100644 --- a/src/Runner/Runner.php +++ b/src/Runner/Runner.php @@ -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()) { @@ -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); + } } }