Skip to content

Commit ac4112e

Browse files
committed
Use colors when outputting information
1 parent addc105 commit ac4112e

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

Scripts/DocCodeExamples/Check.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,13 @@ public function run(): int
137137
$validationFailedCount = 0;
138138

139139
if (empty($this->xmlFiles)) {
140-
$this->writer->toStderr('ERROR: No XML documentation files found.');
140+
$message = 'ERROR: No XML documentation files found.' . PHP_EOL;
141+
142+
if ($this->config->getProperty('showColored') === true) {
143+
$message = "\033[31m$message\033[0m";
144+
}
145+
146+
$this->writer->toStderr($message);
141147
return 1;
142148
}
143149

@@ -165,9 +171,19 @@ public function run(): int
165171

166172
if ($exitCode === 0) {
167173
$feedback .= ' All code examples are valid.' . PHP_EOL;
174+
175+
if ($this->config->getProperty('showColored') === true) {
176+
$feedback = "\033[32m{$feedback}\033[0m";
177+
}
178+
168179
$this->writer->toStdout($feedback);
169180
} else {
170181
$feedback .= " Found incorrect code examples in {$validationFailedCount}." . PHP_EOL;
182+
183+
if ($this->config->getProperty('showColored') === true) {
184+
$feedback = "\033[31m{$feedback}\033[0m";
185+
}
186+
171187
$this->writer->toStdout($feedback);
172188
}
173189

Scripts/DocCodeExamples/Config.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ class Config
6767
*/
6868
protected $executeCheck = true;
6969

70+
/**
71+
* Whether or not to show colored output.
72+
*
73+
* This is automatically detected.
74+
*
75+
* @var bool
76+
*/
77+
protected $showColored;
78+
7079
/**
7180
* Help texts.
7281
*
@@ -123,7 +132,8 @@ class Config
123132
*/
124133
public function __construct(Writer $writer)
125134
{
126-
$this->writer = $writer;
135+
$this->writer = $writer;
136+
$this->showColored = HelpTextFormatter::isColorSupported();
127137
$this->processCliCommand();
128138
}
129139

@@ -173,7 +183,7 @@ private function processCliCommand()
173183
$argsFlipped = \array_flip($args);
174184

175185
if (isset($argsFlipped['--help'])) {
176-
$helpText = HelpTextFormatter::format($this->helpTexts, HelpTextFormatter::isColorSupported());
186+
$helpText = HelpTextFormatter::format($this->helpTexts, $this->showColored);
177187
$this->writer->toStdout($this->getVersion());
178188
$this->writer->toStdout($helpText);
179189
$this->executeCheck = false;

Tests/DocCodeExamples/CheckTest.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ final class CheckTest extends XTestCase
7171
*/
7272
protected function setUpPrerequisites()
7373
{
74-
$this->config = $this->createCompatibleMockBuilderWithMethods(Config::class, [])
74+
$this->config = $this->createCompatibleMockBuilderWithMethods(Config::class, [])
7575
->disableOriginalConstructor()
7676
->getMock();
77+
$this->setObjectProperty($this->config, 'showColored', false);
7778
$this->extractor = new CodeBlocksExtractor();
7879
$cliArgs = ['--runtime-set', 'installed_paths', \realpath(self::STANDARD_DIR)];
7980
$this->phpcsConfig = PHPCSConfigLoader::getPHPCSConfigInstance($cliArgs);
@@ -195,7 +196,7 @@ public function testGetXmlDocValidator()
195196

196197
/**
197198
* Test the run() method without mocking the Check class. Test only the two happy paths.
198-
* More detailed tests are performed in the testRun() method.
199+
* More detailed tests are performed in the testRun() method. Colored output is disabled.
199200
*
200201
* @dataProvider dataRunWithoutMockingCheckClass
201202
*
@@ -206,7 +207,50 @@ public function testGetXmlDocValidator()
206207
*
207208
* @return void
208209
*/
209-
public function testRunWithoutMockingCheckClass(array $xmlDocs, int $expectedExitCode, string $expectedStdout, string $expectedStderr)
210+
public function testRunWithoutMockingCheckClassWithoutColoredOutput(array $xmlDocs, int $expectedExitCode, string $expectedStdout, string $expectedStderr)
211+
{
212+
$this->executeTestForRunMethodWithoutMockingCheckClass($xmlDocs, $expectedExitCode, $expectedStdout, $expectedStderr);
213+
}
214+
215+
/**
216+
* Test the run() method without mocking the Check class. Test only the two happy paths.
217+
* More detailed tests are performed in the testRun() method. Colored output is enabled.
218+
*
219+
* @dataProvider dataRunWithoutMockingCheckClass
220+
*
221+
* @param array<string> $xmlDocs XML doc files to process.
222+
* @param int $expectedExitCode Expected exit code from the run() method.
223+
* @param string $expectedStdout Expected stdout output.
224+
* @param string $expectedStderr Expected stderr output.
225+
*
226+
* @return void
227+
*/
228+
public function testRunWithoutMockingCheckClassWithColoredOutput(array $xmlDocs, int $expectedExitCode, string $expectedStdout, string $expectedStderr)
229+
{
230+
$this->setObjectProperty($this->config, 'showColored', true);
231+
232+
if (empty($expectedStderr) === true) {
233+
$expectedStdout = "\033[32m{$expectedStdout}\033[0m";
234+
} elseif (empty($expectedStdout) === false) {
235+
$expectedStdout = "\033[31m{$expectedStdout}\033[0m";
236+
}
237+
238+
$this->executeTestForRunMethodWithoutMockingCheckClass($xmlDocs, $expectedExitCode, $expectedStdout, $expectedStderr);
239+
}
240+
241+
/**
242+
* Helper method to execute the run() method of the Check class without mocking it.
243+
* This is used for both testRunWithoutMockingCheckClassWithoutColoredOutput and
244+
* testRunWithoutMockingCheckClassWithColoredOutput.
245+
*
246+
* @param array<string> $xmlDocs XML doc files to process.
247+
* @param int $expectedExitCode Expected exit code from the run() method.
248+
* @param string $expectedStdout Expected stdout output.
249+
* @param string $expectedStderr Expected stderr output.
250+
*
251+
* @return void
252+
*/
253+
protected function executeTestForRunMethodWithoutMockingCheckClass(array $xmlDocs, int $expectedExitCode, string $expectedStdout, string $expectedStderr)
210254
{
211255
$this->setObjectProperty($this->config, 'projectRoot', \realpath(self::FIXTURE_DIR));
212256
$this->setObjectProperty($this->config, 'targetPaths', $xmlDocs);

0 commit comments

Comments
 (0)