Skip to content

Commit

Permalink
Prevent plugin runs to use the moodle.Commenting.TodoComment sniff
Browse files Browse the repository at this point in the history
A new `todo-comment-regex` option has been added to the phpcs command.

It allows to specify the regex that will be used with inspecting
todo (TODO and @todo) comments.

By default, an empty string is used for the option and that makes
the Sniff to stop checking. Whoever wants to check for anything
(tracker issue, github issue, arbitrary url, ...) can us the new
option to configure it.

Fixes moodlehq#266
  • Loading branch information
stronk7 committed Jan 18, 2024
1 parent 1c2c65c commit a347beb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
12 changes: 11 additions & 1 deletion docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ Run Moodle CodeSniffer standard on a plugin

### Usage

* `phpcs [-s|--standard STANDARD] [-x|--exclude EXCLUDE] [--max-warnings MAX-WARNINGS] [--test-version TEST-VERSION] [--] <plugin>`
* `phpcs [-s|--standard STANDARD] [-x|--exclude EXCLUDE] [--max-warnings MAX-WARNINGS] [--test-version TEST-VERSION] [--todo-comment-regex TODO-COMMENT-REGEX] [--] <plugin>`
* `codechecker`

Run Moodle CodeSniffer standard on a plugin
Expand Down Expand Up @@ -1629,6 +1629,16 @@ Version or range of version to test with PHPCompatibility
* Is negatable: no
* Default: `0`

#### `--todo-comment-regex`

Regex to use to match TODO/@todo comments

* 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
12 changes: 11 additions & 1 deletion src/Command/CodeCheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ protected function configure(): void
->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,
'Version or range of version to test with PHPCompatibility', 0);
'Version or range of version to test with PHPCompatibility', 0)
->addOption('todo-comment-regex', null, InputOption::VALUE_REQUIRED,
'Regex to use to match TODO/@todo comments', '');
}

protected function initialize(InputInterface $input, OutputInterface $output): void
Expand Down Expand Up @@ -79,6 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// @codeCoverageIgnoreEnd

$exclude = $input->getOption('exclude');

$cmd = array_merge($basicCMD, [
'--standard=' . ($input->getOption('standard') ?: 'moodle'),
'--extensions=php',
Expand Down Expand Up @@ -109,6 +112,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
array_push($cmd, '--runtime-set', 'testVersion', $testVersion);
}

// Set the regex to use to match TODO/@todo comments.
// Note that the option defaults to an empty string,
// meaning that no checks will be performed. Configure it
// to a valid regex ('MDL-[0-9]+', 'https:', ...) to enable the checks.
$todoCommentRegex = $input->getOption('todo-comment-regex');
array_push($cmd, '--runtime-set', 'moodleTodoCommentRegex', $todoCommentRegex);

// Add the files to process.
foreach ($files as $file) {
$cmd[] = $file;
Expand Down
33 changes: 33 additions & 0 deletions tests/Command/CodeCheckerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,39 @@ public function testExecuteWithExclusions()
$this->assertSame(0, $commandTester->getStatusCode());
}

public function testExecuteWithTodoCommentRegex()
{
// Let's add a file with some comments having links and some without
$content = <<<'EOT'
<?php
// phpcs:disable moodle.Files.BoilerplateComment
// Without any CUSTOM-[0-9]+ reference.
// TODO: This is the simplest TODO comment.
/** @todo This is also the simplest, but within a phpdoc block */
// With a CUSTOM-[0-9]+ reference.
// TODO: This is the simplest TODO comment. CUSTOM-123.
/** @todo This is also the simplest, but within a phpdoc block. CUSTOM-123 */

EOT;

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

// Without any regex configured.
$commandTester = $this->executeCommand($this->pluginDir);
$output = $commandTester->getDisplay();
$this->assertMatchesRegularExpression('/\.{10} 10 \/ 10 \(100%\)/', $output);
$this->assertSame(0, $commandTester->getStatusCode());

// With a "CUSTOM-[0-9]+" regex configured.
$commandTester = $this->executeCommand($this->pluginDir, ['--todo-comment-regex' => 'CUSTOM-[0-9]+']);
$output = $commandTester->getDisplay();
$this->assertSame(0, $commandTester->getStatusCode());
$this->assertMatchesRegularExpression('/FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES/', $output);
$this->assertMatchesRegularExpression('/Missing required "CUSTOM-\[0-9\]\+"/', $output);
}

public function testExecuteNoFiles()
{
// Just random directory with no PHP files.
Expand Down
6 changes: 6 additions & 0 deletions tests/Fixture/moodle-local_ci/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

// TODO: This todo comment without any MDL link is good for moodle-plugin-ci
// (because, by default, the moodle.Commenting.TodoComment Sniff
// isn't checked - empty todo-comment-regex option is applied). But if it's
// set then can check for anything, like CUSTOM-123 or https://github.com
// or whatever.

/**
* Add
*
Expand Down

0 comments on commit a347beb

Please sign in to comment.