Skip to content

Commit b924d2a

Browse files
authored
fix: share the deferred blank line between Output and its forStderr() copy (#10673)
1 parent 95e2534 commit b924d2a

2 files changed

Lines changed: 138 additions & 24 deletions

File tree

‎system/CLI/Output.php‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class Output
3030

3131
private bool $newLineDeferred = false;
3232

33+
/**
34+
* The output that a `forStderr()` copy was made from, which holds the deferred blank line for both.
35+
*/
36+
private ?self $original = null;
37+
3338
/**
3439
* Colour support per stream id, detected on first use.
3540
*
@@ -114,7 +119,7 @@ public function newLine(int $num = 1, Verbosity $verbosity = Verbosity::Normal):
114119
*/
115120
public function deferNewLine(): void
116121
{
117-
$this->newLineDeferred = true;
122+
$this->getDeferralHolder()->newLineDeferred = true;
118123
}
119124

120125
/**
@@ -124,11 +129,11 @@ public function deferNewLine(): void
124129
*/
125130
public function writeDeferredNewLine(): void
126131
{
127-
if (! $this->newLineDeferred) {
132+
if (! $this->getDeferralHolder()->newLineDeferred) {
128133
return;
129134
}
130135

131-
$this->newLineDeferred = false;
136+
$this->getDeferralHolder()->newLineDeferred = false;
132137

133138
$this->writeRaw(PHP_EOL);
134139
$this->cursorAtLineStart = true;
@@ -189,13 +194,14 @@ public function isDecorated(): bool
189194
}
190195

191196
/**
192-
* Returns a copy whose STDOUT writes target STDERR.
197+
* Returns a copy whose STDOUT writes target STDERR, sharing the blank line held by `deferNewLine()`.
193198
*/
194199
public function forStderr(): static
195200
{
196201
$stderrOutput = clone $this;
197202

198203
$stderrOutput->toStderr = true;
204+
$stderrOutput->original = $this->getDeferralHolder();
199205

200206
return $stderrOutput;
201207
}
@@ -273,14 +279,20 @@ public function setCursorAtLineStart(bool $cursorAtLineStart): void
273279
*/
274280
private function line(string $text): string
275281
{
276-
$prefix = $this->cursorAtLineStart && ! $this->newLineDeferred ? '' : PHP_EOL;
282+
$prefix = $this->cursorAtLineStart && ! $this->getDeferralHolder()->newLineDeferred ? '' : PHP_EOL;
277283

278284
$this->cursorAtLineStart = true;
279-
$this->newLineDeferred = false;
285+
286+
$this->getDeferralHolder()->newLineDeferred = false;
280287

281288
return $prefix . $text . PHP_EOL;
282289
}
283290

291+
private function getDeferralHolder(): self
292+
{
293+
return $this->original ?? $this;
294+
}
295+
284296
/**
285297
* Whether the level shows text written at the given verbosity, where STDERR is never quieter than normal.
286298
*/

‎tests/system/CLI/OutputTest.php‎

Lines changed: 120 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ public function testWriteStartsOnAFreshLineOnceAndThenContinues(): void
3636
$output->write('first');
3737
$output->write('second');
3838

39-
$this->assertSame([['STDOUT', "\nfirst\n"], ['STDOUT', "second\n"]], $io->writes);
39+
$this->assertSame(['STDOUT', 'STDOUT'], array_column($io->writes, 0));
40+
$this->assertSame(
41+
<<<'EOT'
42+
43+
first
44+
second
45+
46+
EOT,
47+
$this->getWrittenText($io),
48+
);
4049
}
4150

4251
public function testPrintLeavesTheCursorAfterTheText(): void
@@ -78,7 +87,17 @@ public function testErrorWritesToStderrAndSharesTheCursorState(): void
7887
$output->error('second');
7988
$output->write('third');
8089

81-
$this->assertSame([['STDOUT', "\nfirst\n"], ['STDERR', "second\n"], ['STDOUT', "third\n"]], $io->writes);
90+
$this->assertSame(['STDOUT', 'STDERR', 'STDOUT'], array_column($io->writes, 0));
91+
$this->assertSame(
92+
<<<'EOT'
93+
94+
first
95+
second
96+
third
97+
98+
EOT,
99+
$this->getWrittenText($io),
100+
);
82101
}
83102

84103
public function testErrorOutsideTheCliWritesToStdoutWithoutColor(): void
@@ -329,6 +348,37 @@ public function testForStderrTargetsStderrAndKeepsTheCursorState(): void
329348
], $io->writes);
330349
}
331350

351+
public function testForStderrSharesTheDeferredNewLine(): void
352+
{
353+
$io = new RecordingInputOutput();
354+
$output = new Output($io, $this->createUncoloredTerminal());
355+
356+
$output->print("first\n");
357+
$output->deferNewLine();
358+
$output->forStderr()->write('second');
359+
$output->write('third');
360+
361+
$stderr = $output->forStderr();
362+
$output->deferNewLine();
363+
$output->write('fourth');
364+
$stderr->write('fifth');
365+
366+
$this->assertSame(['STDOUT', 'STDERR', 'STDOUT', 'STDOUT', 'STDERR'], array_column($io->writes, 0));
367+
$this->assertSame(
368+
<<<'EOT'
369+
first
370+
371+
second
372+
third
373+
374+
fourth
375+
fifth
376+
377+
EOT,
378+
$this->getWrittenText($io),
379+
);
380+
}
381+
332382
public function testBeepAndClearScreenLeaveTheCursorStateAlone(): void
333383
{
334384
$io = new RecordingInputOutput();
@@ -367,7 +417,16 @@ public function testSetCursorAtLineStartControlsTheNextWrite(): void
367417
$output->setCursorAtLineStart(false);
368418
$output->write('second');
369419

370-
$this->assertSame([['STDOUT', "first\n"], ['STDOUT', "\nsecond\n"]], $io->writes);
420+
$this->assertSame(['STDOUT', 'STDOUT'], array_column($io->writes, 0));
421+
$this->assertSame(
422+
<<<'EOT'
423+
first
424+
425+
second
426+
427+
EOT,
428+
$this->getWrittenText($io),
429+
);
371430
}
372431

373432
public function testDeferredNewLineWritesNothingWhenNothingFollows(): void
@@ -394,13 +453,19 @@ public function testDeferredNewLineIsWrittenOnceBeforeTheNextWrites(): void
394453
$output->deferNewLine();
395454
$output->print('fourth');
396455

397-
$this->assertSame([
398-
['STDOUT', "first\n"],
399-
['STDOUT', "\nsecond\n"],
400-
['STDERR', "\nthird\n"],
401-
['STDOUT', "\n"],
402-
['STDOUT', 'fourth'],
403-
], $io->writes);
456+
$this->assertSame(['STDOUT', 'STDOUT', 'STDERR', 'STDOUT', 'STDOUT'], array_column($io->writes, 0));
457+
$this->assertSame(
458+
<<<'EOT'
459+
first
460+
461+
second
462+
463+
third
464+
465+
fourth
466+
EOT,
467+
$this->getWrittenText($io),
468+
);
404469
}
405470

406471
public function testWriteDeferredNewLineFlushesTheHeldLineOnce(): void
@@ -413,7 +478,15 @@ public function testWriteDeferredNewLineFlushesTheHeldLineOnce(): void
413478
$output->writeDeferredNewLine();
414479
$output->write('next');
415480

416-
$this->assertSame([['STDOUT', "\n"], ['STDOUT', "next\n"]], $io->writes);
481+
$this->assertSame(['STDOUT', 'STDOUT'], array_column($io->writes, 0));
482+
$this->assertSame(
483+
<<<'EOT'
484+
485+
next
486+
487+
EOT,
488+
$this->getWrittenText($io),
489+
);
417490
}
418491

419492
public function testSetInputOutputReplacesTheTransport(): void
@@ -460,11 +533,17 @@ public function testDropsWritesAboveTheLevel(): void
460533
$output->write('very verbose', verbosity: Verbosity::VeryVerbose);
461534
$output->write('debug', verbosity: Verbosity::Debug);
462535

463-
$this->assertSame([
464-
['STDOUT', "\nnormal\n"],
465-
['STDOUT', "quiet\n"],
466-
['STDOUT', "very verbose\n"],
467-
], $io->writes);
536+
$this->assertSame(['STDOUT', 'STDOUT', 'STDOUT'], array_column($io->writes, 0));
537+
$this->assertSame(
538+
<<<'EOT'
539+
540+
normal
541+
quiet
542+
very verbose
543+
544+
EOT,
545+
$this->getWrittenText($io),
546+
);
468547
}
469548

470549
public function testQuietDropsNormalWritesButKeepsErrors(): void
@@ -482,7 +561,16 @@ public function testQuietDropsNormalWritesButKeepsErrors(): void
482561
$output->error('error');
483562
$output->write('quiet', verbosity: Verbosity::Quiet);
484563

485-
$this->assertSame([['STDERR', "\nerror\n"], ['STDOUT', "quiet\n"]], $io->writes);
564+
$this->assertSame(['STDERR', 'STDOUT'], array_column($io->writes, 0));
565+
$this->assertSame(
566+
<<<'EOT'
567+
568+
error
569+
quiet
570+
571+
EOT,
572+
$this->getWrittenText($io),
573+
);
486574
}
487575

488576
public function testSilentDropsEverything(): void
@@ -507,7 +595,16 @@ public function testDroppedWriteLeavesTheCursorAlone(): void
507595
$output->print('dropped', verbosity: Verbosity::Verbose);
508596
$output->write('second');
509597

510-
$this->assertSame([['STDOUT', "\nfirst\n"], ['STDOUT', "second\n"]], $io->writes);
598+
$this->assertSame(['STDOUT', 'STDOUT'], array_column($io->writes, 0));
599+
$this->assertSame(
600+
<<<'EOT'
601+
602+
first
603+
second
604+
605+
EOT,
606+
$this->getWrittenText($io),
607+
);
511608
}
512609

513610
public function testForStderrKeepsTheLevelAndWritesAtQuiet(): void
@@ -561,4 +658,9 @@ private function createUncoloredTerminal(): Terminal
561658

562659
return $terminal;
563660
}
661+
662+
private function getWrittenText(RecordingInputOutput $io): string
663+
{
664+
return implode('', array_column($io->writes, 1));
665+
}
564666
}

0 commit comments

Comments
 (0)