Skip to content

Commit 92c2930

Browse files
paulbalandanondrejmirtes
authored andcommitted
Fix output in agent tool use
1 parent fe750c5 commit 92c2930

2 files changed

Lines changed: 96 additions & 2 deletions

File tree

src/Command/ErrorsConsoleStyle.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use OndraM\CiDetector\CiDetector;
66
use Override;
7+
use PHPStan\Internal\AgentDetector;
78
use Symfony\Component\Console\Helper\Helper;
89
use Symfony\Component\Console\Helper\ProgressBar;
910
use Symfony\Component\Console\Helper\TableSeparator;
@@ -95,9 +96,10 @@ public function createProgressBar(int $max = 0): ProgressBar
9596
}
9697

9798
$ci = $this->isCiDetected();
98-
$this->progressBar->setOverwrite(!$ci);
99+
$agent = AgentDetector::isRunningInAgent();
100+
$this->progressBar->setOverwrite(!$ci && !$agent);
99101

100-
if ($ci) {
102+
if ($ci || $agent) {
101103
$this->progressBar->minSecondsBetweenRedraws(15);
102104
$this->progressBar->maxSecondsBetweenRedraws(30);
103105
} elseif (DIRECTORY_SEPARATOR === '\\') {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Command;
4+
5+
use Override;
6+
use PHPStan\Internal\AgentDetector;
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Console\Input\StringInput;
9+
use Symfony\Component\Console\Output\StreamOutput;
10+
use function fclose;
11+
use function fopen;
12+
use function getenv;
13+
use function putenv;
14+
use function rewind;
15+
use function rtrim;
16+
use function str_replace;
17+
use function stream_get_contents;
18+
19+
class ErrorsConsoleStyleTest extends TestCase
20+
{
21+
22+
/** @var array<string, string|false> */
23+
private array $originalEnvVars = [];
24+
25+
#[Override]
26+
protected function setUp(): void
27+
{
28+
foreach ([...AgentDetector::ENV_VARS, 'GITHUB_ACTIONS'] as $var) {
29+
$this->originalEnvVars[$var] = getenv($var);
30+
putenv($var);
31+
}
32+
}
33+
34+
#[Override]
35+
protected function tearDown(): void
36+
{
37+
foreach ($this->originalEnvVars as $var => $value) {
38+
putenv($var . ($value !== false ? '=' . $value : ''));
39+
}
40+
}
41+
42+
public function testProgressOutputInAgentDoesNotOverwrite(): void
43+
{
44+
$agentOutput = $this->renderProgressOutput(true);
45+
$regularOutput = $this->renderProgressOutput(false);
46+
47+
self::assertSame(
48+
rtrim(<<<'EOT'
49+
0/2 [>---------------------------] 0%
50+
2/2 [============================] 100%
51+
EOT),
52+
$agentOutput,
53+
);
54+
self::assertSame(
55+
" 0/2 [>---------------------------] 0%\033[1G\033[2K 2/2 [============================] 100%",
56+
$regularOutput,
57+
);
58+
}
59+
60+
private function renderProgressOutput(bool $isAgent): string
61+
{
62+
if ($isAgent) {
63+
putenv('AI_AGENT=1');
64+
} else {
65+
putenv('AI_AGENT');
66+
}
67+
68+
$stream = fopen('php://memory', 'w+');
69+
self::assertNotFalse($stream);
70+
71+
$output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, true);
72+
$errorStyle = new ErrorsConsoleStyle(new StringInput(''), $output);
73+
74+
$progressBar = $errorStyle->createProgressBar(2);
75+
$progressBar->setBarCharacter('=');
76+
$progressBar->setEmptyBarCharacter('-');
77+
$progressBar->setProgressCharacter('>');
78+
$progressBar->setProgress(0);
79+
$progressBar->display();
80+
$progressBar->setProgress(2);
81+
$progressBar->display();
82+
83+
rewind($stream);
84+
$contents = stream_get_contents($stream);
85+
fclose($stream);
86+
87+
self::assertIsString($contents);
88+
89+
return str_replace(["\r\n", "\r"], "\n", $contents);
90+
}
91+
92+
}

0 commit comments

Comments
 (0)