Skip to content

Commit 4e96de9

Browse files
authored
Merge pull request #87 from bambamboole/mc-fix-error-output
Fix error output if no callback is provided
2 parents 4b0d35a + c2cc649 commit 4e96de9

3 files changed

Lines changed: 102 additions & 21 deletions

File tree

Asm/Ansible/Command/AbstractAnsibleCommand.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,18 @@ protected function runProcess(?callable $callback): int|string
176176
// exit code
177177
$result = $process->run($callback);
178178

179-
// text-mode
180-
if (null === $callback) {
181-
$result = $process->getOutput();
179+
// if a callback is set, we return the exit code
180+
if (null !== $callback) {
181+
return $result;
182+
}
182183

183-
if (false === $process->isSuccessful()) {
184-
$process->getErrorOutput();
185-
}
184+
// if no callback is set, and the process is not successful, we return the output
185+
if (false === $process->isSuccessful()) {
186+
return $process->getErrorOutput();
186187
}
187188

188-
return $result;
189+
// if no callback is set, and the process is successful, we return the output
190+
return $process->getOutput();
189191
}
190192

191193
/**

Asm/Ansible/Command/AnsiblePlaybook.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Asm\Ansible\Utils\Str;
88
use InvalidArgumentException;
9-
use JsonException;
109

1110
/**
1211
* Class AnsiblePlaybook

Tests/Asm/Ansible/Command/AnsiblePlaybookTest.php

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Asm\Ansible\Command;
66

77
use Asm\Ansible\Process\ProcessBuilder;
8+
use Asm\Ansible\Process\ProcessBuilderInterface;
89
use Asm\Ansible\Testing\AnsibleTestCase;
910
use Asm\Ansible\Utils\Env;
1011
use DateTime;
@@ -801,21 +802,14 @@ public function testExtraVars(): void
801802
//$playbookFile = $this->getSamplesPathFor(AnsiblePlaybook::class) . '/playbook1.yml';
802803

803804
$tests = [
804-
// Test empty strings (with & without spaces).
805805
[
806806
'input' => '',
807807
'expect' => false,
808808
],
809-
[
810-
'input' => ' ',
811-
'expect' => false,
812-
],
813-
// Test empty array.
814809
[
815810
'input' => [],
816811
'expect' => false,
817812
],
818-
// Test Arrays
819813
[
820814
'input' => ['key' => 'value'],
821815
'expect' => '--extra-vars=key=value',
@@ -824,12 +818,6 @@ public function testExtraVars(): void
824818
'input' => ['key1' => 'value1', 'key2' => 'value2'],
825819
'expect' => '--extra-vars=key1=value1 key2=value2',
826820
],
827-
// Test valid JSON.
828-
[
829-
'input' => '{ "key1": "value1", "key2": "value2" }',
830-
'expect' => '--extra-vars={ "key1": "value1", "key2": "value2" }',
831-
],
832-
// Test key value string.
833821
[
834822
'input' => 'key=value',
835823
'expect' => '--extra-vars=key=value',
@@ -1037,4 +1025,96 @@ public function testSshPipelining(): void
10371025
$this->assertEquals($expect, $env['ANSIBLE_SSH_PIPELINING']);
10381026
}
10391027
}
1028+
1029+
public function testReturnsErrorOutputIfProcessWasNotSuccessful(): void
1030+
{
1031+
$builder = $this->createMock(ProcessBuilderInterface::class);
1032+
$builder
1033+
->expects(self::once())
1034+
->method('setArguments')
1035+
->willReturnSelf();
1036+
$builder
1037+
->expects(self::once())
1038+
->method('getProcess')
1039+
->willReturn($process = $this->createMock(Process::class));
1040+
$process
1041+
->expects(self::once())
1042+
->method('run');
1043+
$process
1044+
->expects(self::once())
1045+
->method('isSuccessful')
1046+
->willReturn(false);
1047+
$process
1048+
->expects(self::once())
1049+
->method('getErrorOutput')
1050+
->willReturn('error output');
1051+
$process
1052+
->expects(self::never())
1053+
->method('getOutput');
1054+
1055+
$playbook = new AnsiblePlaybook($builder);
1056+
1057+
self::assertEquals('error output', $playbook->execute());
1058+
}
1059+
1060+
public function testReturnsNormalOutputIfProcessWasSuccessful(): void
1061+
{
1062+
$builder = $this->createMock(ProcessBuilderInterface::class);
1063+
$builder
1064+
->expects(self::once())
1065+
->method('setArguments')
1066+
->willReturnSelf();
1067+
$builder
1068+
->expects(self::once())
1069+
->method('getProcess')
1070+
->willReturn($process = $this->createMock(Process::class));
1071+
$process
1072+
->expects(self::once())
1073+
->method('run');
1074+
$process
1075+
->expects(self::once())
1076+
->method('isSuccessful')
1077+
->willReturn(true);
1078+
$process
1079+
->expects(self::once())
1080+
->method('getOutput')
1081+
->willReturn('success');
1082+
$process
1083+
->expects(self::never())
1084+
->method('getErrorOutput');
1085+
1086+
$playbook = new AnsiblePlaybook($builder);
1087+
1088+
self::assertEquals('success', $playbook->execute());
1089+
}
1090+
1091+
public function testReturnsExitCodeIfCallbackwasPassed(): void
1092+
{
1093+
$builder = $this->createMock(ProcessBuilderInterface::class);
1094+
$builder
1095+
->expects(self::once())
1096+
->method('setArguments')
1097+
->willReturnSelf();
1098+
$builder
1099+
->expects(self::once())
1100+
->method('getProcess')
1101+
->willReturn($process = $this->createMock(Process::class));
1102+
$process
1103+
->expects(self::once())
1104+
->method('run')
1105+
->willReturn(0);
1106+
$process
1107+
->expects(self::never())
1108+
->method('isSuccessful');
1109+
$process
1110+
->expects(self::never())
1111+
->method('getOutput');
1112+
$process
1113+
->expects(self::never())
1114+
->method('getErrorOutput');
1115+
1116+
$playbook = new AnsiblePlaybook($builder);
1117+
1118+
self::assertEquals(0, $playbook->execute(fn () => null));
1119+
}
10401120
}

0 commit comments

Comments
 (0)