diff --git a/src/Logger/Logger.php b/src/Logger/Logger.php index 302582568..921d5a229 100644 --- a/src/Logger/Logger.php +++ b/src/Logger/Logger.php @@ -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"); } diff --git a/tests/src/Logger/LoggerTest.php b/tests/src/Logger/LoggerTest.php index 00281ba0b..af9accaec 100644 --- a/tests/src/Logger/LoggerTest.php +++ b/tests/src/Logger/LoggerTest.php @@ -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);