Skip to content

Commit 5668a15

Browse files
committed
Fail on log
Currently, when all is well, nothing should be logged at all. We can leverage this to use the CLI tool as a linter, and create CI jobs with it. Here, a new option called --fail-on-log is introduced, and it will use a failure exit code as soon as any log is emitted. This is based on the assumption that the logger is never and will never be called for anything that should not result in a change in the documentation being processed.
1 parent 8bf9af9 commit 5668a15

File tree

6 files changed

+89
-2
lines changed

6 files changed

+89
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"autoload-dev": {
1818
"psr-4": {
1919
"phpDocumentor\\Guides\\": ["packages/guides/tests/unit/", "tests/"],
20+
"phpDocumentor\\Guides\\Cli\\": "packages/guides-cli/tests/unit",
2021
"phpDocumentor\\Guides\\Graphs\\": "packages/guides-graphs/tests/unit",
2122
"phpDocumentor\\Guides\\RestructuredText\\": "packages/guides-restructured-text/tests/unit",
2223
"phpDocumentor\\Guides\\Markdown\\": "packages/guides-markdown/tests/unit"

composer.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/guides-cli/composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
"phpDocumentor\\Guides\\Cli\\": "src/"
1212
}
1313
},
14+
"autoload-dev": {
15+
"psr-4": {
16+
"phpDocumentor\\Guides\\": [
17+
"tests/unit/"
18+
]
19+
}
20+
},
1421
"authors": [
1522
{
1623
"name": "jaapio",

packages/guides-cli/src/Command/Run.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Monolog\Handler\ErrorLogHandler;
1212
use Monolog\Handler\StreamHandler;
1313
use Monolog\Logger;
14+
use phpDocumentor\Guides\Cli\Logger\SpyProcessor;
1415
use phpDocumentor\Guides\Compiler\CompilerContext;
1516
use phpDocumentor\Guides\Handlers\CompileDocumentsCommand;
1617
use phpDocumentor\Guides\Handlers\ParseDirectoryCommand;
@@ -83,6 +84,12 @@ public function __construct(
8384
'Write log to this path',
8485
'php://stder',
8586
);
87+
$this->addOption(
88+
'fail-on-log',
89+
null,
90+
InputOption::VALUE_NONE,
91+
'Use a non-zero exit code as soon as any log is written',
92+
);
8693

8794
$this->addOption(
8895
'theme',
@@ -136,6 +143,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
136143
$this->logger->pushHandler(new StreamHandler($logPath . '/error.log', Logger::ERROR));
137144
}
138145

146+
$failOnLog = $input->getOption('fail-on-log') ?? false;
147+
148+
if ($failOnLog) {
149+
$spyProcessor = new SpyProcessor();
150+
$this->logger->pushProcessor($spyProcessor);
151+
}
152+
139153
$documents = $this->commandBus->handle(
140154
new ParseDirectoryCommand(
141155
$sourceFileSystem,
@@ -185,7 +199,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
185199
'Successfully placed ' . (is_countable($documents) ? count($documents) : 0) . ' rendered ' . $formatsText . ' files into ' . $outputDir,
186200
);
187201

188-
return 0;
202+
if ($failOnLog && $spyProcessor->hasBeenCalled()) {
203+
return Command::FAILURE;
204+
}
205+
206+
return Command::SUCCESS;
189207
}
190208

191209
private function getAbsolutePath(string $path): string
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Guides\Cli\Logger;
6+
7+
use Monolog\Processor\ProcessorInterface;
8+
9+
/**
10+
* This decorator has an extra method to check whether anything was logged
11+
*
12+
* @internal
13+
*/
14+
final class SpyProcessor implements ProcessorInterface
15+
{
16+
private bool $hasBeenCalled = false;
17+
18+
public function hasBeenCalled(): bool
19+
{
20+
return $this->hasBeenCalled;
21+
}
22+
23+
/** @inheritDoc */
24+
public function __invoke(array $record): array
25+
{
26+
$this->hasBeenCalled = true;
27+
28+
return $record;
29+
}
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Guides\Cli\Logger;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
class SpyProcessorTest extends TestCase
10+
{
11+
public function testHasBeenCalledReturnsFalseByDefault(): void
12+
{
13+
$spyProcessor = new SpyProcessor();
14+
15+
$this->assertFalse($spyProcessor->hasBeenCalled());
16+
}
17+
18+
public function testItKnowsWhenALogIsEmitted(): void
19+
{
20+
$process = new SpyProcessor();
21+
$process(['channel' => 'test']);
22+
self::assertTrue($process->hasBeenCalled());
23+
}
24+
}

0 commit comments

Comments
 (0)