diff --git a/src/Dplr/TaskReport.php b/src/Dplr/TaskReport.php index b823222..0f22252 100644 --- a/src/Dplr/TaskReport.php +++ b/src/Dplr/TaskReport.php @@ -82,7 +82,7 @@ public function getType(): string public function getOutput(): ?string { - if (!isset($this->data['Stdout'])) { + if (!isset($this->data['Stdout']) && '' !== $this->data['StdOut']) { return null; } @@ -91,12 +91,12 @@ public function getOutput(): ?string public function getErrorOutput(): ?string { - if (isset($this->data['Stderr'])) { + if (isset($this->data['Stderr']) && '' !== $this->data['Stderr']) { return $this->data['Stderr']; } - if (isset($this->data['ErrorMsg'])) { - return $this->data['ErrorMsg']; + if (isset($this->data['ErrMsg']) && '' !== $this->data['ErrMsg']) { + return $this->data['ErrMsg']; } return null; diff --git a/tests/Dplr/Tests/DplrTest.php b/tests/Dplr/Tests/DplrTest.php index 0ae3762..521acac 100644 --- a/tests/Dplr/Tests/DplrTest.php +++ b/tests/Dplr/Tests/DplrTest.php @@ -208,6 +208,71 @@ public function testLimitedConcurrency(): void } } + public function testShellErrorReporting(): void + { + $d = self::getDplr(); + $d->upload(self::getFixturesPath() . '/files/1.txt', '/foo/1.txt', 'job'); + + $this->assertTrue($d->hasTasks()); + + $output = ''; + $d->run(function (string $s) use (&$output) { + $output .= $s; + }); + + $this->assertFalse($d->isSuccessful()); + $this->assertFalse($d->hasTasks()); + $this->assertEquals( + 'CPY ' . self::getFixturesPath() . "/files/1.txt -> /foo/1.txt E\n", + $output + ); + + $report = $d->getReport(); + $this->assertEquals(1, $report['total']); + $this->assertEquals(0, $report['successful']); + $this->assertEquals(1, $report['failed']); + + $report = $d->getReports()[0]; + $this->assertEquals( + $report->getErrorOutput(), + "ash: can't create /foo/1.txt: nonexistent directory\n" + ); + } + + public function testGosshaErrorReporting(): void + { + $d = new Dplr(self::USER, self::GOSSHA_PATH, self::SSH_KEY); + $d + ->addServer('remote_4', ['job', 'all']) + ; + $d->upload(self::getFixturesPath() . '/files/1.txt', '/foo/1.txt', 'job'); + + $this->assertTrue($d->hasTasks()); + + $output = ''; + $d->run(function (string $s) use (&$output) { + $output .= $s; + }); + + $this->assertFalse($d->isSuccessful()); + $this->assertFalse($d->hasTasks()); + $this->assertEquals( + 'CPY ' . self::getFixturesPath() . "/files/1.txt -> /foo/1.txt E\n", + $output + ); + + $report = $d->getReport(); + $this->assertEquals(1, $report['total']); + $this->assertEquals(0, $report['successful']); + $this->assertEquals(1, $report['failed']); + + $report = $d->getReports()[0]; + $this->assertEquals( + $report->getErrorOutput(), + 'dial tcp: lookup remote_4: no such host' + ); + } + private static function getFixturesPath(): string { return realpath(__DIR__ . '/../Fixtures');