Skip to content

Commit 36dde18

Browse files
committed
[console] Normalize structured command output
1 parent c4407d1 commit 36dde18

5 files changed

Lines changed: 42 additions & 19 deletions

File tree

‎src/Console/Command/CodeStyleCommand.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
164164
return $this->success('Code style checks completed successfully.', $input, [
165165
'fix' => $fix,
166166
'config' => self::CONFIG,
167-
'process_output' => $processOutput instanceof BufferedOutput ? $processOutput->fetch() : null,
167+
'output' => $processOutput,
168168
]);
169169
}
170170

171171
return $this->failure('Code style checks failed.', $input, [
172172
'fix' => $fix,
173173
'config' => self::CONFIG,
174-
'process_output' => $processOutput instanceof BufferedOutput ? $processOutput->fetch() : null,
174+
'output' => $processOutput,
175175
]);
176176
}
177177
}

‎src/Console/Command/TestsCommand.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ private function resolveStructuredProcessResultPayload(OutputInterface $processO
366366
];
367367
}
368368

369-
if (null !== $supplementalOutput) {
369+
if (null !== $supplementalOutput && ! \is_array($decoded)) {
370370
$payload['raw_output'] = $supplementalOutput;
371371
}
372372

‎tests/Console/Command/CodeStyleCommandTest.php‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,10 @@ public function executeWillReturnSuccessWhenProcessQueueSucceeds(): void
129129
$this->logger->log(
130130
'info',
131131
'Code style checks completed successfully.',
132-
[
133-
'input' => $this->input->reveal(),
134-
'fix' => false,
135-
'config' => CodeStyleCommand::CONFIG,
136-
'process_output' => null,
137-
],
132+
Argument::that(fn(array $context): bool => $this->input->reveal() === $context['input']
133+
&& false === $context['fix']
134+
&& CodeStyleCommand::CONFIG === $context['config']
135+
&& $context['output'] instanceof OutputInterface),
138136
)->shouldBeCalled();
139137

140138
self::assertSame(CodeStyleCommand::SUCCESS, $this->executeCommand());
@@ -155,14 +153,12 @@ public function executeWillReturnFailureWhenProcessQueueFails(): void
155153
->shouldBeCalled();
156154
$this->logger->error(
157155
'Code style checks failed.',
158-
[
159-
'input' => $this->input->reveal(),
160-
'file' => null,
161-
'line' => null,
162-
'fix' => false,
163-
'config' => CodeStyleCommand::CONFIG,
164-
'process_output' => null,
165-
],
156+
Argument::that(fn(array $context): bool => $this->input->reveal() === $context['input']
157+
&& null === $context['file']
158+
&& null === $context['line']
159+
&& false === $context['fix']
160+
&& CodeStyleCommand::CONFIG === $context['config']
161+
&& $context['output'] instanceof OutputInterface),
166162
)->shouldBeCalled();
167163

168164
self::assertSame(CodeStyleCommand::FAILURE, $this->executeCommand());
@@ -190,7 +186,7 @@ public function executeWillCaptureBufferedOutputWhenJsonIsRequested(): void
190186
'info',
191187
'Code style checks completed successfully.',
192188
Argument::that(fn(array $context): bool => $this->input->reveal() === $context['input']
193-
&& \is_string($context['process_output'])),
189+
&& $context['output'] instanceof OutputInterface),
194190
)->shouldBeCalled();
195191

196192
self::assertSame(CodeStyleCommand::SUCCESS, $this->executeCommand());

‎tests/Console/Command/TestsCommandTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ public function executeWillCaptureStructuredPhpUnitSummaryAfterCoveragePreludeWh
439439
&& isset($context['output'])
440440
&& 'success' === $context['output']['result']
441441
&& 5 === $context['output']['summary']['assertions']
442-
&& 'Generating code coverage report in PHP format ... done [00:00.002]' === $context['output']['raw_output']),
442+
&& ! isset($context['output']['raw_output'])),
443443
)->shouldBeCalled();
444444
$this->output->writeln(Argument::cetera())->shouldNotBeCalled();
445445

‎tests/Console/Logger/Processor/CommandOutputProcessorTest.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,33 @@ public function processWillDiscardPlainTextPreambleBeforeStructuredJsonOutput():
190190
], $context['output']);
191191
}
192192

193+
/**
194+
* @return void
195+
*/
196+
#[Test]
197+
public function processWillDecodeTheFinalStructuredJsonAfterPlainTextToolOutput(): void
198+
{
199+
$processor = new CommandOutputProcessor();
200+
$output = new BufferedOutput();
201+
$output->write(
202+
"composer-normalize warning before machine output.\n"
203+
. "Another advisory line.\n"
204+
. "{\"totals\":{\"changed_files\":0,\"errors\":0},\"changed_files\":[\"src/Foo.php\"]}\n"
205+
);
206+
207+
$context = $processor->process([
208+
'output' => $output,
209+
]);
210+
211+
self::assertSame([
212+
'totals' => [
213+
'changed_files' => 0,
214+
'errors' => 0,
215+
],
216+
'changed_files' => [],
217+
], $context['output']);
218+
}
219+
193220
/**
194221
* @return void
195222
*/

0 commit comments

Comments
 (0)