diff --git a/README.md b/README.md index ecfbaf2..b274446 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,17 @@ Gitlab format: php vendor/bin/phpfci inspect coverage.xml reports/gitlab.errors.json --report=gitlab ``` +## Command line arguments + +| Option | Values | Description | +|---------------------------|------------------------|-------------------------------------------------------| +| `argument 1` | `inspect`, `baseline` | the command to execute. | +| `argument 2` | `coverage.xml` | the phpunit clover coverage input file. | +| `argument 3` | `phpfci.xml` | the output file to write to. | +| `--report=` | `gitlab`, `checkstyle` | the output format. If absent will default to console. | +| `--config=` | `phpfci.xml` | the path to the config file. | +| `--exit-code-on-failure` | - | Set exit code to `1` when there are failures. | + ## About us At 123inkt (Part of Digital Revolution B.V.), every day more than 30 developers are working on improving our internal ERP and our several shops. Do you want to join us? [We are looking for developers](https://www.123inkt.nl/page/werken_ict.html). diff --git a/src/Command/InspectCommand.php b/src/Command/InspectCommand.php index 0caf81f..ca80879 100644 --- a/src/Command/InspectCommand.php +++ b/src/Command/InspectCommand.php @@ -32,7 +32,8 @@ protected function configure(): void ->addArgument('output', InputOption::VALUE_REQUIRED, 'Path to write inspections report file to') ->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Path to configuration file. Optional') ->addOption('baseDir', '', InputOption::VALUE_REQUIRED, 'Base directory from where to determine the relative config paths') - ->addOption('report', '', InputOption::VALUE_REQUIRED, 'output format, either checkstyle or gitlab', 'checkstyle'); + ->addOption('report', '', InputOption::VALUE_REQUIRED, 'output format, either checkstyle or gitlab', 'checkstyle') + ->addOption('exit-code-on-failure', '', InputOption::VALUE_NONE, 'If failures, exit with failure exit code'); } /** @@ -78,6 +79,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int throw new InvalidArgumentException('Invalid report argument: ' . $input->getOption('report')); } + // raise exit code on failure + if (count($failures) > 0 && $input->getOption('exit-code-on-failure') !== false) { + return Command::FAILURE; + } + return Command::SUCCESS; } } diff --git a/src/Renderer/CheckStyleRenderer.php b/src/Renderer/CheckStyleRenderer.php index 1f67094..5b87056 100644 --- a/src/Renderer/CheckStyleRenderer.php +++ b/src/Renderer/CheckStyleRenderer.php @@ -5,7 +5,6 @@ use DigitalRevolution\CodeCoverageInspection\Model\Config\InspectionConfig; use DigitalRevolution\CodeCoverageInspection\Model\Metric\Failure; -use RuntimeException; use XMLWriter; class CheckStyleRenderer diff --git a/tests/Functional/Command/InspectCommand/InspectCommandTest.php b/tests/Functional/Command/InspectCommand/InspectCommandTest.php index df44c4d..dbc6517 100644 --- a/tests/Functional/Command/InspectCommand/InspectCommandTest.php +++ b/tests/Functional/Command/InspectCommand/InspectCommandTest.php @@ -15,8 +15,7 @@ class InspectCommandTest extends TestCase { - /** @var vfsStreamDirectory */ - private $fileSystem; + private vfsStreamDirectory $fileSystem; protected function setUp(): void { @@ -26,9 +25,10 @@ protected function setUp(): void /** * @coversNothing + * @dataProvider dataProvider * @throws Exception */ - public function testInspectCommand(): void + public function testInspectCommand(array $flags, int $exitStatus): void { // prepare data files $configPath = __DIR__ . '/Data/phpfci.xml'; @@ -39,11 +39,11 @@ public function testInspectCommand(): void // prepare command $command = new InspectCommand(); - $input = new ArgvInput(['phpfci', '--config', $configPath, '--baseDir', $baseDir, $coveragePath, $output]); + $input = new ArgvInput(array_merge(['phpfci', '--config', $configPath, '--baseDir', $baseDir, $coveragePath, $output], $flags)); $output = new ConsoleOutput(); // run test case - static::assertSame(Command::SUCCESS, $command->run($input, $output)); + static::assertSame($exitStatus, $command->run($input, $output)); static::assertTrue($this->fileSystem->hasChild('checkstyle.xml')); // check output @@ -53,4 +53,12 @@ public function testInspectCommand(): void static::assertSame($expected, $result); } + + public function dataProvider(): array + { + return [ + 'standard exit code' => [[], Command::SUCCESS], + 'exit code on failure' => [['--exit-code-on-failure'], Command::FAILURE] + ]; + } }