Skip to content

Commit

Permalink
fix: return stderr and exit code in exception for standalone error
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Apr 28, 2023
1 parent a4b5a41 commit cfad002
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 54 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ jobs:
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependencies }}
composer-options: ${{ matrix.composer-options }}

- name: Composer test
run: composer test
37 changes: 0 additions & 37 deletions src/PhpPact/Standalone/Broker/Broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public function canIDeploy()
);
$runner->runBlocking();

if ($runner->getExitCode() !== 0) {
throw new \Exception($runner->getStderr());
}

return \json_decode($runner->getOutput(), true);
}

Expand Down Expand Up @@ -92,10 +88,6 @@ public function createOrUpdatePacticipant()
);
$runner->runBlocking();

if ($runner->getExitCode() !== 0) {
throw new \Exception($runner->getStderr());
}

return \json_decode($runner->getOutput(), true);
}

Expand All @@ -121,10 +113,6 @@ public function createOrUpdateWebhook()
);
$runner->runBlocking();

if ($runner->getExitCode() !== 0) {
throw new \Exception($runner->getStderr());
}

return \json_decode($runner->getOutput(), true);
}

Expand All @@ -144,10 +132,6 @@ public function createVersionTag()
);
$runner->runBlocking();

if ($runner->getExitCode() !== 0) {
throw new \Exception($runner->getStderr());
}

return \json_decode($runner->getOutput(), true);
}

Expand All @@ -172,10 +156,6 @@ public function createWebhook()
);
$runner->runBlocking();

if ($runner->getExitCode() !== 0) {
throw new \Exception($runner->getStderr());
}

return \json_decode($runner->getOutput(), true);
}

Expand All @@ -194,10 +174,6 @@ public function describeVersion()
);
$runner->runBlocking();

if ($runner->getExitCode() !== 0) {
throw new \Exception($runner->getStderr());
}

return \json_decode($runner->getOutput(), true);
}

Expand All @@ -215,10 +191,6 @@ public function listLatestPactVersions()
);
$runner->runBlocking();

if ($runner->getExitCode() !== 0) {
throw new \Exception($runner->getStderr());
}

return \json_decode($runner->getOutput(), true);
}

Expand Down Expand Up @@ -246,12 +218,7 @@ public function publish(): void
)
);

try {
$runner->runBlocking();
} finally {
$this->logger->info('out > ' . $runner->getOutput());
$this->logger->error('err > ' . $runner->getStderr());
}
}

public function testWebhook()
Expand All @@ -268,10 +235,6 @@ public function testWebhook()
);
$runner->runBlocking();

if ($runner->getExitCode() !== 0) {
throw new \Exception($runner->getStderr());
}

return \json_decode($runner->getOutput(), true);
}

Expand Down
7 changes: 1 addition & 6 deletions src/PhpPact/Standalone/ProviderVerifier/VerifierProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ public function run(array $arguments, $processTimeout, $processIdleTimeout)

$logger->info("Verifying PACT with script:\n{$processRunner->getCommand()}\n\n");

try {
$processRunner->runBlocking();
} finally {
$logger->info('out > ' . $processRunner->getOutput());
$logger->error('err > ' . $processRunner->getStderr());
}
$processRunner->runBlocking();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/PhpPact/Standalone/Runner/ProcessRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ public function runBlocking(): int
$logger->debug("Exit code: {$this->getExitCode()}");

if ($this->getExitCode() !== 0) {
throw new \Exception("PactPHP Process returned non-zero exit code: {$this->getExitCode()}");
$this->logger->info('out > ' . $this->getOutput());
$this->logger->error('err > ' . $this->getStderr());
throw new \Exception("PactPHP Process returned non-zero exit code: {$this->getExitCode()}", $this->getExitCode());
}

Loop::stop();
Expand Down
18 changes: 18 additions & 0 deletions tests/PhpPact/Standalone/Broker/BrokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,22 @@ public function listLatestPactVersions(): void
$result = $broker->listLatestPactVersions();
$this->assertArrayHasKey('pacts', $result);
}

/**
* @test
*
* @throws \Exception
*/
public function publishLogsStdError(): void
{
$config = new BrokerConfig();
$config->setPactLocations('not a directory');
$broker = new Broker($config);
try {
$broker->publish();
} catch(\Exception $e){
$this->assertEquals(1, $e->getCode());
$this->assertStringContainsString("PactPHP Process returned non-zero exit code: 1", $e->getMessage());
}
}
}
18 changes: 9 additions & 9 deletions tests/PhpPact/Standalone/Runner/ProcessRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public function testBlockingProcess()
try {
$p->runBlocking();
} catch (\Exception $e) {
$exitCode = $p->getExitCode();
$this->assertEquals($exitCode, $e->getCode());
$this->assertStringContainsString("PactPHP Process returned non-zero exit code: $exitCode", $e->getMessage());
$this->assertNotEquals($exitCode, 0, 'Expect the exit code to be non-zero: ' . $exitCode);
$this->assertStringContainsString($expectedErr, $p->getStderr(), "Expect '{$expectedErr}' to be in the stderr");
$this->assertEquals(null, $p->getOutput(), 'Expect a null stdout');
}

$exitCode = $p->getExitCode();

$this->assertNotEquals($exitCode, 0, 'Expect the exit code to be non-zero: ' . $exitCode);
$this->assertStringContainsString($expectedErr, $p->getStderr(), "Expect '{$expectedErr}' to be in the stderr");
$this->assertEquals(null, $p->getOutput(), 'Expect a null stdout');
}

/**
Expand All @@ -62,12 +62,12 @@ public function testProcessRunnerShouldReturnCompleteOutput()
$p = new ProcessRunner($cmd, []);
$expectedOutput = 'third line';
$expectedErr = 'fourth line';

try {
$p->runBlocking();
} catch (\Exception $e) {
}

$this->assertEquals(42, $e->getCode());
$this->assertStringContainsString("PactPHP Process returned non-zero exit code: 42", $e->getMessage());
}
$this->assertTrue((\stripos($p->getOutput(), $expectedOutput) !== false), "Expect '{$expectedOutput}' to be in the output:");
$this->assertTrue((\stripos($p->getStderr(), $expectedErr) !== false), "Expect '{$expectedErr}' to be in the stderr");
}
Expand Down

0 comments on commit cfad002

Please sign in to comment.