Skip to content
Open
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
8 changes: 4 additions & 4 deletions src/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public function print(Host $host, string $buffer, bool $force = false): void
{
if ($this->output->isVerbose() || $force) {
foreach (explode("\n", rtrim($buffer)) as $line) {
if (empty($line)) {
return;
if ($line === '') {
continue;
}
$this->output->writeln("[{$host->getTag()}] $line");
}
}
foreach (explode("\n", rtrim($buffer)) as $line) {
if (empty($line)) {
return;
if ($line === '') {
continue;
}
$this->fileLog->writeln("[{$host->getAlias()}] $line");
}
Expand Down
44 changes: 44 additions & 0 deletions tests/src/Logger/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,50 @@ public function testPrintWritesToOutputWhenForced(): void
$logger->print($this->host, "output line\n", true);
}

public function testPrintSkipsEmptyLinesWithoutTruncatingOutput(): void
{
$output = $this->createMock(OutputInterface::class);
$output->method('isVerbose')->willReturn(true);
$writelnArgs = [];
$output->expects($this->exactly(4))
->method('writeln')
->willReturnCallback(function ($msg) use (&$writelnArgs) {
$writelnArgs[] = $msg;
});

$fileLog = $this->createStub(HandlerInterface::class);

$logger = new Logger($output, $fileLog);
$logger->print($this->host, "first\n\nsecond\n0\n\nthird\n");

$this->assertStringEndsWith('first', $writelnArgs[0]);
$this->assertStringEndsWith('second', $writelnArgs[1]);
$this->assertStringEndsWith('0', $writelnArgs[2]);
$this->assertStringEndsWith('third', $writelnArgs[3]);
}

public function testPrintWritesAllNonEmptyLinesToFileLog(): void
{
$output = $this->createStub(OutputInterface::class);
$output->method('isVerbose')->willReturn(false);

$fileLog = $this->createMock(HandlerInterface::class);
$writelnArgs = [];
$fileLog->expects($this->exactly(4))
->method('writeln')
->willReturnCallback(function ($msg) use (&$writelnArgs) {
$writelnArgs[] = $msg;
});

$logger = new Logger($output, $fileLog);
$logger->print($this->host, "first\n\nsecond\n0\n\nthird\n");

$this->assertSame('[test-host] first', $writelnArgs[0]);
$this->assertSame('[test-host] second', $writelnArgs[1]);
$this->assertSame('[test-host] 0', $writelnArgs[2]);
$this->assertSame('[test-host] third', $writelnArgs[3]);
}

public function testStartTaskDefault(): void
{
$output = $this->createMock(OutputInterface::class);
Expand Down
Loading