Skip to content

Commit bf42288

Browse files
authored
Merge pull request #9 from 123inkt/Add_exit_code_on_failures
Add ability to raise the exit code when there are failures found.
2 parents 0fbd3d9 + 4942480 commit bf42288

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ Gitlab format:
6464
php vendor/bin/phpfci inspect coverage.xml reports/gitlab.errors.json --report=gitlab
6565
```
6666

67+
## Command line arguments
68+
69+
| Option | Values | Description |
70+
|---------------------------|------------------------|-------------------------------------------------------|
71+
| `argument 1` | `inspect`, `baseline` | the command to execute. |
72+
| `argument 2` | `coverage.xml` | the phpunit clover coverage input file. |
73+
| `argument 3` | `phpfci.xml` | the output file to write to. |
74+
| `--report=<report-style>` | `gitlab`, `checkstyle` | the output format. If absent will default to console. |
75+
| `--config=<path-to-file>` | `phpfci.xml` | the path to the config file. |
76+
| `--exit-code-on-failure` | - | Set exit code to `1` when there are failures. |
77+
6778
## About us
6879

6980
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).

src/Command/InspectCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ protected function configure(): void
3232
->addArgument('output', InputOption::VALUE_REQUIRED, 'Path to write inspections report file to')
3333
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Path to configuration file. Optional')
3434
->addOption('baseDir', '', InputOption::VALUE_REQUIRED, 'Base directory from where to determine the relative config paths')
35-
->addOption('report', '', InputOption::VALUE_REQUIRED, 'output format, either checkstyle or gitlab', 'checkstyle');
35+
->addOption('report', '', InputOption::VALUE_REQUIRED, 'output format, either checkstyle or gitlab', 'checkstyle')
36+
->addOption('exit-code-on-failure', '', InputOption::VALUE_NONE, 'If failures, exit with failure exit code');
3637
}
3738

3839
/**
@@ -78,6 +79,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7879
throw new InvalidArgumentException('Invalid report argument: ' . $input->getOption('report'));
7980
}
8081

82+
// raise exit code on failure
83+
if (count($failures) > 0 && $input->getOption('exit-code-on-failure') !== false) {
84+
return Command::FAILURE;
85+
}
86+
8187
return Command::SUCCESS;
8288
}
8389
}

src/Renderer/CheckStyleRenderer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use DigitalRevolution\CodeCoverageInspection\Model\Config\InspectionConfig;
77
use DigitalRevolution\CodeCoverageInspection\Model\Metric\Failure;
8-
use RuntimeException;
98
use XMLWriter;
109

1110
class CheckStyleRenderer

tests/Functional/Command/InspectCommand/InspectCommandTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
class InspectCommandTest extends TestCase
1717
{
18-
/** @var vfsStreamDirectory */
19-
private $fileSystem;
18+
private vfsStreamDirectory $fileSystem;
2019

2120
protected function setUp(): void
2221
{
@@ -26,9 +25,10 @@ protected function setUp(): void
2625

2726
/**
2827
* @coversNothing
28+
* @dataProvider dataProvider
2929
* @throws Exception
3030
*/
31-
public function testInspectCommand(): void
31+
public function testInspectCommand(array $flags, int $exitStatus): void
3232
{
3333
// prepare data files
3434
$configPath = __DIR__ . '/Data/phpfci.xml';
@@ -39,11 +39,11 @@ public function testInspectCommand(): void
3939

4040
// prepare command
4141
$command = new InspectCommand();
42-
$input = new ArgvInput(['phpfci', '--config', $configPath, '--baseDir', $baseDir, $coveragePath, $output]);
42+
$input = new ArgvInput(array_merge(['phpfci', '--config', $configPath, '--baseDir', $baseDir, $coveragePath, $output], $flags));
4343
$output = new ConsoleOutput();
4444

4545
// run test case
46-
static::assertSame(Command::SUCCESS, $command->run($input, $output));
46+
static::assertSame($exitStatus, $command->run($input, $output));
4747
static::assertTrue($this->fileSystem->hasChild('checkstyle.xml'));
4848

4949
// check output
@@ -53,4 +53,12 @@ public function testInspectCommand(): void
5353

5454
static::assertSame($expected, $result);
5555
}
56+
57+
public function dataProvider(): array
58+
{
59+
return [
60+
'standard exit code' => [[], Command::SUCCESS],
61+
'exit code on failure' => [['--exit-code-on-failure'], Command::FAILURE]
62+
];
63+
}
5664
}

0 commit comments

Comments
 (0)