Skip to content

Commit

Permalink
Merge pull request #259 from NoelDeMartin/code-checker-exclude
Browse files Browse the repository at this point in the history
Implement --exclude option in phpcs command
  • Loading branch information
stronk7 authored Dec 14, 2023
2 parents f783423 + 79c6925 commit 3d85518
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
The format of this change log follows the advice given at [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
### Added
- Added support for the `--exclude` option to the `phpcs` command.

## [4.2.0] - 2023-11-30
### Added
- Added support for the `--tags` and `--name` options to the `behat` command.
Expand Down
12 changes: 11 additions & 1 deletion docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,16 @@ Version or range of version to test with PHPCompatibility
* Is negatable: no
* Default: `0`

#### `--exclude|-x`

Comma separated list of sniff codes to exclude from checking

* Accept value: yes
* Is value required: yes
* Is multiple: no
* Is negatable: no
* Default: `''`

#### `--help|-h`

Display help for the given command. When no command is given display help for the list command
Expand Down Expand Up @@ -2365,4 +2375,4 @@ Do not ask any interactive question
* Is value required: no
* Is multiple: no
* Is negatable: no
* Default: `false`
* Default: `false`
5 changes: 4 additions & 1 deletion src/Command/CodeCheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected function configure(): void
->setAliases(['codechecker'])
->setDescription('Run Moodle CodeSniffer standard on a plugin')
->addOption('standard', 's', InputOption::VALUE_REQUIRED, 'The name or path of the coding standard to use', 'moodle')
->addOption('exclude', 'x', InputOption::VALUE_REQUIRED, 'Comma separated list of sniff codes to exclude from checking', '')
->addOption('max-warnings', null, InputOption::VALUE_REQUIRED,
'Number of warnings to trigger nonzero exit code - default: -1', -1)
->addOption('test-version', null, InputOption::VALUE_REQUIRED,
Expand Down Expand Up @@ -77,13 +78,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
// @codeCoverageIgnoreEnd

$cmd = array_merge($basicCMD, [
$exclude = $input->getOption('exclude');
$cmd = array_merge($basicCMD, [
'--standard=' . ($input->getOption('standard') ?: 'moodle'),
'--extensions=php',
'-p',
'-w',
'-s',
'--no-cache',
empty($exclude) ? '' : ('--exclude=' . $exclude),
$output->isDecorated() ? '--colors' : '--no-colors',
'--report-full',
'--report-width=132',
Expand Down
51 changes: 31 additions & 20 deletions tests/Command/CodeCheckerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function setUp(): void
$this->fs->dumpFile($this->pluginDir . '/.moodle-plugin-ci.yml', Yaml::dump($config));
}

protected function executeCommand($pluginDir = null, $maxWarnings = -1, $testVersion = null): CommandTester
protected function executeCommand($pluginDir = null, array $options = []): CommandTester
{
if ($pluginDir === null) {
$pluginDir = $this->pluginDir;
Expand All @@ -46,14 +46,7 @@ protected function executeCommand($pluginDir = null, $maxWarnings = -1, $testVer
$application = new Application();
$application->add($command);

$options = ['plugin' => $pluginDir];
if ($maxWarnings >= 0) {
$options['--max-warnings'] = $maxWarnings;
}

if (null !== $testVersion) {
$options['--test-version'] = $testVersion;
}
$options = array_merge(['plugin' => $pluginDir], $options);

$commandTester = new CommandTester($application->find('codechecker'));
$commandTester->execute($options);
Expand Down Expand Up @@ -133,19 +126,19 @@ public function testExecuteWithWarningsAndThreshold()
$this->assertSame(0, $commandTester->getStatusCode());

// Allowing 0 warning, it fails.
$commandTester = $this->executeCommand($this->pluginDir, 0);
$commandTester = $this->executeCommand($this->pluginDir, ['--max-warnings' => 0]);
$this->assertSame(1, $commandTester->getStatusCode());

// Allowing 1 warning, it fails.
$commandTester = $this->executeCommand($this->pluginDir, 1);
$commandTester = $this->executeCommand($this->pluginDir, ['--max-warnings' => 1]);
$this->assertSame(1, $commandTester->getStatusCode());

// Allowing 2 warnings, it passes.
$commandTester = $this->executeCommand($this->pluginDir, 2);
$commandTester = $this->executeCommand($this->pluginDir, ['--max-warnings' => 2]);
$this->assertSame(0, $commandTester->getStatusCode());

// Allowing 3 warnings, it passes.
$commandTester = $this->executeCommand($this->pluginDir, 3);
$commandTester = $this->executeCommand($this->pluginDir, ['--max-warnings' => 3]);
$this->assertSame(0, $commandTester->getStatusCode());
}

Expand All @@ -167,48 +160,66 @@ public function testExecuteWithTestVersion()
$this->fs->dumpFile($this->pluginDir . '/test_versions.php', $content);

// By default, without specify test-version, only reports deprecation warnings and returns 0.
$commandTester = $this->executeCommand($this->pluginDir, -1, null);
$commandTester = $this->executeCommand($this->pluginDir);
$output = $commandTester->getDisplay();
$this->assertSame(0, $commandTester->getStatusCode());
$this->assertMatchesRegularExpression('/FOUND 0 ERRORS AND 4 WARNINGS AFFECTING 4 LINES/', $output);

// With test-version 7.4, reports 3 new errors and <= 7.4 specific warnings and returns 1.
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4');
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '7.4']);
$output = $commandTester->getDisplay();
$this->assertSame(1, $commandTester->getStatusCode());
$this->assertMatchesRegularExpression('/FOUND 3 ERRORS AND 1 WARNING AFFECTING 4 LINES/', $output);

// With test-version 8.0, reports 2 new errors and <= 8.0 specific warnings and returns 1.
$commandTester = $this->executeCommand($this->pluginDir, -1, '8.0');
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '8.0']);
$output = $commandTester->getDisplay();
$this->assertSame(1, $commandTester->getStatusCode());
$this->assertMatchesRegularExpression('/FOUND 2 ERRORS AND 2 WARNINGS AFFECTING 4 LINES/', $output);

// With test-version 8.1, reports 1 new errors and <= 8.1 specific warnings and returns 0.
$commandTester = $this->executeCommand($this->pluginDir, -1, '8.1');
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '8.1']);
$output = $commandTester->getDisplay();
$this->assertSame(1, $commandTester->getStatusCode());
$this->assertMatchesRegularExpression('/FOUND 1 ERROR AND 3 WARNINGS AFFECTING 4 LINES/', $output);

// With test-version 7.4-8.0, reports 3 new errors and <= 8.0 specific warnings and returns 1.
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4-8.0');
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '7.4-8.0']);
$output = $commandTester->getDisplay();
$this->assertSame(1, $commandTester->getStatusCode());
$this->assertMatchesRegularExpression('/FOUND 3 ERRORS AND 2 WARNINGS AFFECTING 5 LINES/', $output);

// With test-version 7.4-8.1, reports 3 new errors and <= 8.1 specific warnings and returns 1.
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4-8.1');
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '7.4-8.1']);
$output = $commandTester->getDisplay();
$this->assertSame(1, $commandTester->getStatusCode());
$this->assertMatchesRegularExpression('/FOUND 3 ERRORS AND 3 WARNINGS AFFECTING 6 LINES/', $output);

// With test-version 7.4- (open range), reports 3 new errors and <= 8.2 specific warnings and returns 1.
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4-');
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '7.4-']);
$output = $commandTester->getDisplay();
$this->assertSame(1, $commandTester->getStatusCode());
$this->assertMatchesRegularExpression('/FOUND 3 ERRORS AND 4 WARNINGS AFFECTING 7 LINES/', $output);
}

public function testExecuteWithExclusions()
{
// Add a file with errors and warnings, and verify that they are suppressed with the exclusions.
$content = "<?php require(__DIR__.'/../../config.php');\n";

$this->fs->dumpFile($this->pluginDir . '/warnings.php', $content);

// Without exclusions.
$commandTester = $this->executeCommand($this->pluginDir);
$output = $commandTester->getDisplay();
$this->assertSame(1, $commandTester->getStatusCode());
$this->assertMatchesRegularExpression('/FOUND 9 ERRORS AND 1 WARNING AFFECTING 1 LINE/', $output);

// With exclusions.
$commandTester = $this->executeCommand($this->pluginDir, ['--exclude' => 'moodle.Files.RequireLogin,moodle.Files.BoilerplateComment']);
$this->assertSame(0, $commandTester->getStatusCode());
}

public function testExecuteNoFiles()
{
// Just random directory with no PHP files.
Expand Down

0 comments on commit 3d85518

Please sign in to comment.