Skip to content

Commit

Permalink
Log stdout/stderr after running wkhtml (even if an exception is thrown)
Browse files Browse the repository at this point in the history
See #223.
  • Loading branch information
Albin Kerouanton committed Sep 13, 2017
1 parent 3e9f97f commit 921e452
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 10 deletions.
27 changes: 23 additions & 4 deletions src/Knp/Snappy/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,32 @@ public function generate($input, $output, array $options = [], $overwrite = fals

$inputFiles = is_array($input) ? implode('", "', $input) : $input;

$this->logger->debug(sprintf('Generate from file(s) "%s" to file "%s".', $inputFiles, $output), [
$this->logger->info(sprintf('Generate from file(s) "%s" to file "%s".', $inputFiles, $output), [
'command' => $command,
'env' => $this->env,
'timeout' => $this->timeout,
]);

list($status, $stdout, $stderr) = $this->executeCommand($command);
$this->checkProcessStatus($status, $stdout, $stderr, $command);
$this->checkOutput($output, $command);
try {
list($status, $stdout, $stderr) = $this->executeCommand($command);
$this->checkProcessStatus($status, $stdout, $stderr, $command);
$this->checkOutput($output, $command);
} catch (\Exception $e) { // @TODO: should be replaced by \Throwable when support for php5.6 is dropped
$this->logger->error(sprintf('An error happened while generating "%s".', $output), [
'command' => $command,
'status' => isset($status) ? $status : null,
'stdout' => isset($stdout) ? $stdout : null,
'stderr' => isset($stderr) ? $stderr : null,
]);

throw $e;
}

$this->logger->info(sprintf('File "%s" has been successfully generated.', $output), [
'command' => $command,
'stdout' => $stdout,
'stderr' => $stderr,
]);
}

/**
Expand Down
92 changes: 86 additions & 6 deletions test/Knp/Snappy/AbstractGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,18 @@ public function testGenerate()
;
$media->setLogger($logger);
$logger
->expects($this->once())
->method('debug')
->with('Generate from file(s) "the_input_file" to file "the_output_file".', [
'command' => "the command",
])
->expects($this->exactly(2))
->method('info')
->with(
$this->logicalOr(
'Generate from file(s) "the_input_file" to file "the_output_file".',
'File "the_output_file" has been successfully generated.'
),
$this->logicalOr(
['command' => "the command"],
['command' => "the command", 'stdout' => 'stdout', 'stderr' => 'stderr']
)
)
;

$media
Expand All @@ -206,11 +213,12 @@ public function testGenerate()
->expects($this->once())
->method('executeCommand')
->with($this->equalTo('the command'))
->willReturn([0, 'stdout', 'stderr'])
;
$media
->expects($this->once())
->method('checkProcessStatus')
->with(null, '', '', 'the command')
->with(0, 'stdout', 'stderr', 'the command')
;
$media
->expects($this->once())
Expand All @@ -224,6 +232,78 @@ public function testGenerate()
$media->generate('the_input_file', 'the_output_file', ['foo' => 'bar']);
}

public function testFailingGenerate()
{
$media = $this->getMock(
'Knp\Snappy\AbstractGenerator',
[
'configure',
'prepareOutput',
'getCommand',
'executeCommand',
'checkOutput',
'checkProcessStatus',
],
[
'the_binary',
[]
]
);

$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$media->setLogger($logger);

$logger
->expects($this->once())
->method('info')
->with(
$this->equalTo('Generate from file(s) "the_input_file" to file "the_output_file".'),
$this->equalTo(['command' => "the command"])
)
;

$logger
->expects($this->once())
->method('error')
->with(
$this->equalTo('An error happened while generating "the_output_file".'),
$this->equalTo(['command' => "the command", 'status' => 1, 'stdout' => 'stdout', 'stderr' => 'stderr'])
)
;

$media
->expects($this->once())
->method('prepareOutput')
->with($this->equalTo('the_output_file'))
;
$media
->expects($this->any())
->method('getCommand')
->with(
$this->equalTo('the_input_file'),
$this->equalTo('the_output_file')
)
->will($this->returnValue('the command'))
;
$media
->expects($this->once())
->method('executeCommand')
->with($this->equalTo('the command'))
->willReturn([1, 'stdout', 'stderr'])
;
$media
->expects($this->once())
->method('checkProcessStatus')
->with(1, 'stdout', 'stderr', 'the command')
->willThrowException(new \RuntimeException())
;

$this->setExpectedException(\RuntimeException::class);


$media->generate('the_input_file', 'the_output_file', ['foo' => 'bar']);
}

public function testGenerateFromHtml()
{
$media = $this->getMock(
Expand Down

0 comments on commit 921e452

Please sign in to comment.