From 57c8cd5b35130b8290b5a39f683691eb78d62aa4 Mon Sep 17 00:00:00 2001 From: PabloKowalczyk <11366345+PabloKowalczyk@users.noreply.github.com> Date: Sat, 10 Nov 2018 11:39:27 +0100 Subject: [PATCH] Fix command error output (#162) --- CHANGELOG.md | 4 ++++ src/Event.php | 13 ++++++++++++- src/EventRunner.php | 7 +++---- tests/Unit/EventTest.php | 26 ++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d99e1a44..09701320 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Fixed + +- Crunz doesn't provide error log when emailing, issue [#161](https://github.com/lavary/crunz/issues/161) - PR [#162](https://github.com/lavary/crunz/pull/162) + ## 1.11.0-beta.1 - 2018-10-23 ### Added diff --git a/src/Event.php b/src/Event.php index 5c741486..a701b77c 100644 --- a/src/Event.php +++ b/src/Event.php @@ -176,6 +176,8 @@ class Event implements PingableInterface */ private $lastLockRefresh = 0; + private $wholeOutput = []; + /** * Create a new event instance. * @@ -311,6 +313,11 @@ public function filtersPass() return true; } + public function wholeOutput() + { + return \implode('', $this->wholeOutput); + } + /** * Start the event execution. * @@ -319,7 +326,11 @@ public function filtersPass() public function start() { $this->setProcess(new Process($this->buildCommand())); - $this->getProcess()->start(); + $this->getProcess()->start( + function ($type, $content) { + $this->wholeOutput[] = $content; + } + ); if ($this->preventOverlapping) { $this->lock(); diff --git a/src/EventRunner.php b/src/EventRunner.php index c983a934..35dc7743 100644 --- a/src/EventRunner.php +++ b/src/EventRunner.php @@ -166,7 +166,7 @@ protected function manageStartedEvents() $runStatus = 'success'; - $event->outputStream .= $proc->getOutput(); + $event->outputStream .= $event->wholeOutput(); $event->outputStream .= $this->invoke($event->afterCallbacks()); $this->handleOutput($event); @@ -274,8 +274,7 @@ protected function handleError(Event $event) if ($logErrors) { $this->logger->error($this->formatEventError($event)); } else { - $output = $event->getProcess() - ->getOutput(); + $output = $event->wholeOutput(); $this->output ->write("{$output}"); @@ -323,7 +322,7 @@ protected function formatEventError(Event $event) . $event->getCommandForDisplay() . ') ' . PHP_EOL - . $event->getProcess()->getOutput() + . $event->wholeOutput() . PHP_EOL; } diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index 5e3209f0..c7df1eaa 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -345,6 +345,32 @@ public function closureCommandHaveFullBinaryPaths() $this->assertSame(PHP_BINARY . " {$crunzBin} closure:run {$queryClosure}", $command); } + /** @test */ + public function wholeOutputCatchesStdoutAndStderr() + { + $command = "php -r \"echo 'Test output'; throw new \Exception('Exception output');\""; + $event = new Event(\uniqid('c', true), $command); + $event->start(); + $process = $event->getProcess(); + + while ($process->isRunning()) { + \usleep(20000); // wait 20 ms + } + + $wholeOutput = $event->wholeOutput(); + + $this->assertContains( + 'Test output', + $wholeOutput, + 'Missing standard output' + ); + $this->assertContains( + 'Exception output', + $wholeOutput, + 'Missing error output' + ); + } + private function setClockNow(\DateTimeImmutable $dateTime) { $testClock = new TestClock($dateTime);