Skip to content

Commit

Permalink
Fix command error output (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloKowalczyk authored Nov 10, 2018
1 parent 3014d6a commit 57c8cd5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ class Event implements PingableInterface
*/
private $lastLockRefresh = 0;

private $wholeOutput = [];

/**
* Create a new event instance.
*
Expand Down Expand Up @@ -311,6 +313,11 @@ public function filtersPass()
return true;
}

public function wholeOutput()
{
return \implode('', $this->wholeOutput);
}

/**
* Start the event execution.
*
Expand All @@ -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();
Expand Down
7 changes: 3 additions & 4 deletions src/EventRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected function manageStartedEvents()

$runStatus = '<info>success</info>';

$event->outputStream .= $proc->getOutput();
$event->outputStream .= $event->wholeOutput();
$event->outputStream .= $this->invoke($event->afterCallbacks());

$this->handleOutput($event);
Expand Down Expand Up @@ -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("<error>{$output}</error>");
Expand Down Expand Up @@ -323,7 +322,7 @@ protected function formatEventError(Event $event)
. $event->getCommandForDisplay()
. ') '
. PHP_EOL
. $event->getProcess()->getOutput()
. $event->wholeOutput()
. PHP_EOL;
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 57c8cd5

Please sign in to comment.