diff --git a/src/Command/ErrorsConsoleStyle.php b/src/Command/ErrorsConsoleStyle.php index 18301f5ab88..509f3eed356 100644 --- a/src/Command/ErrorsConsoleStyle.php +++ b/src/Command/ErrorsConsoleStyle.php @@ -4,6 +4,7 @@ use OndraM\CiDetector\CiDetector; use Override; +use PHPStan\Internal\AgentDetector; use Symfony\Component\Console\Helper\Helper; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\TableSeparator; @@ -95,9 +96,10 @@ public function createProgressBar(int $max = 0): ProgressBar } $ci = $this->isCiDetected(); - $this->progressBar->setOverwrite(!$ci); + $agent = AgentDetector::isRunningInAgent(); + $this->progressBar->setOverwrite(!$ci && !$agent); - if ($ci) { + if ($ci || $agent) { $this->progressBar->minSecondsBetweenRedraws(15); $this->progressBar->maxSecondsBetweenRedraws(30); } elseif (DIRECTORY_SEPARATOR === '\\') { diff --git a/tests/PHPStan/Command/ErrorsConsoleStyleTest.php b/tests/PHPStan/Command/ErrorsConsoleStyleTest.php new file mode 100644 index 00000000000..921e1443d00 --- /dev/null +++ b/tests/PHPStan/Command/ErrorsConsoleStyleTest.php @@ -0,0 +1,92 @@ + */ + private array $originalEnvVars = []; + + #[Override] + protected function setUp(): void + { + foreach ([...AgentDetector::ENV_VARS, 'GITHUB_ACTIONS'] as $var) { + $this->originalEnvVars[$var] = getenv($var); + putenv($var); + } + } + + #[Override] + protected function tearDown(): void + { + foreach ($this->originalEnvVars as $var => $value) { + putenv($var . ($value !== false ? '=' . $value : '')); + } + } + + public function testProgressOutputInAgentDoesNotOverwrite(): void + { + $agentOutput = $this->renderProgressOutput(true); + $regularOutput = $this->renderProgressOutput(false); + + self::assertSame( + rtrim(<<<'EOT' + 0/2 [>---------------------------] 0% + 2/2 [============================] 100% + EOT), + $agentOutput, + ); + self::assertSame( + " 0/2 [>---------------------------] 0%\033[1G\033[2K 2/2 [============================] 100%", + $regularOutput, + ); + } + + private function renderProgressOutput(bool $isAgent): string + { + if ($isAgent) { + putenv('AI_AGENT=1'); + } else { + putenv('AI_AGENT'); + } + + $stream = fopen('php://memory', 'w+'); + self::assertNotFalse($stream); + + $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, true); + $errorStyle = new ErrorsConsoleStyle(new StringInput(''), $output); + + $progressBar = $errorStyle->createProgressBar(2); + $progressBar->setBarCharacter('='); + $progressBar->setEmptyBarCharacter('-'); + $progressBar->setProgressCharacter('>'); + $progressBar->setProgress(0); + $progressBar->display(); + $progressBar->setProgress(2); + $progressBar->display(); + + rewind($stream); + $contents = stream_get_contents($stream); + fclose($stream); + + self::assertIsString($contents); + + return str_replace(["\r\n", "\r"], "\n", $contents); + } + +}