diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 8852b579..48c45562 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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. diff --git a/docs/CLI.md b/docs/CLI.md index 13e86ae6..abe5dddf 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -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 @@ -2365,4 +2375,4 @@ Do not ask any interactive question * Is value required: no * Is multiple: no * Is negatable: no -* Default: `false` \ No newline at end of file +* Default: `false` diff --git a/src/Command/CodeCheckerCommand.php b/src/Command/CodeCheckerCommand.php index 51804f6a..bc153c62 100644 --- a/src/Command/CodeCheckerCommand.php +++ b/src/Command/CodeCheckerCommand.php @@ -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, @@ -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', diff --git a/tests/Command/CodeCheckerCommandTest.php b/tests/Command/CodeCheckerCommandTest.php index e11019dd..85a86666 100644 --- a/tests/Command/CodeCheckerCommandTest.php +++ b/tests/Command/CodeCheckerCommandTest.php @@ -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; @@ -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); @@ -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()); } @@ -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 = "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.