Skip to content

Commit 5cf4b64

Browse files
authored
Merge pull request #267 from stronk7/prevent_todo_sniff_for_plugins
Prevent plugin runs to use the moodle.Commenting.TodoComment sniff
2 parents 8f23cf5 + 2b00727 commit 5cf4b64

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

docs/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99
The format of this change log follows the advice given at [Keep a CHANGELOG](http://keepachangelog.com).
1010

1111
## [Unreleased]
12+
### Added
13+
- Added support for the `--todo-comment-regex` option to the `phpcs` command. When specified, all the todo comments (`TODO` and `@todo`) are inspected to ensure that they contain some text matching the regular expression (a tracker issue key: `MDL-[0-9]+`, a link to GitHub: `github.com/moodle/moodle/pull/[0-9]+`, ... or any other valid alternative).
14+
1215
### Changed
1316
- Updated project dependencies to current [moodle-cs](https://github.com/moodlehq/moodle-cs) and [moodle-local_ci](https://github.com/moodlehq/moodle-local_ci) versions.
1417

docs/CLI.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ Run Moodle CodeSniffer standard on a plugin
15721572

15731573
### Usage
15741574

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

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

1632+
#### `--todo-comment-regex`
1633+
1634+
Regex to use to match TODO/@todo comments
1635+
1636+
* Accept value: yes
1637+
* Is value required: yes
1638+
* Is multiple: no
1639+
* Is negatable: no
1640+
* Default: `''`
1641+
16321642
#### `--help|-h`
16331643

16341644
Display help for the given command. When no command is given display help for the list command

src/Command/CodeCheckerCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ protected function configure(): void
4343
->addOption('max-warnings', null, InputOption::VALUE_REQUIRED,
4444
'Number of warnings to trigger nonzero exit code - default: -1', -1)
4545
->addOption('test-version', null, InputOption::VALUE_REQUIRED,
46-
'Version or range of version to test with PHPCompatibility', 0);
46+
'Version or range of version to test with PHPCompatibility', 0)
47+
->addOption('todo-comment-regex', null, InputOption::VALUE_REQUIRED,
48+
'Regex to use to match TODO/@todo comments', '');
4749
}
4850

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

8183
$exclude = $input->getOption('exclude');
84+
8285
$cmd = array_merge($basicCMD, [
8386
'--standard=' . ($input->getOption('standard') ?: 'moodle'),
8487
'--extensions=php',
@@ -109,6 +112,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
109112
array_push($cmd, '--runtime-set', 'testVersion', $testVersion);
110113
}
111114

115+
// Set the regex to use to match TODO/@todo comments.
116+
// Note that the option defaults to an empty string,
117+
// meaning that no checks will be performed. Configure it
118+
// to a valid regex ('MDL-[0-9]+', 'https:', ...) to enable the checks.
119+
$todoCommentRegex = $input->getOption('todo-comment-regex');
120+
array_push($cmd, '--runtime-set', 'moodleTodoCommentRegex', $todoCommentRegex);
121+
112122
// Add the files to process.
113123
foreach ($files as $file) {
114124
$cmd[] = $file;

tests/Command/CodeCheckerCommandTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,39 @@ public function testExecuteWithExclusions()
220220
$this->assertSame(0, $commandTester->getStatusCode());
221221
}
222222

223+
public function testExecuteWithTodoCommentRegex()
224+
{
225+
// Let's add a file with some comments having links and some without
226+
$content = <<<'EOT'
227+
<?php
228+
// phpcs:disable moodle.Files.BoilerplateComment
229+
// Without any CUSTOM-[0-9]+ reference.
230+
231+
// TODO: This is the simplest TODO comment.
232+
/** @todo This is also the simplest, but within a phpdoc block */
233+
// With a CUSTOM-[0-9]+ reference.
234+
235+
// TODO: This is the simplest TODO comment. CUSTOM-123.
236+
/** @todo This is also the simplest, but within a phpdoc block. CUSTOM-123 */
237+
238+
EOT;
239+
240+
$this->fs->dumpFile($this->pluginDir . '/test_comment_todos.php', $content);
241+
242+
// Without any regex configured.
243+
$commandTester = $this->executeCommand($this->pluginDir);
244+
$output = $commandTester->getDisplay();
245+
$this->assertMatchesRegularExpression('/\.{10} 10 \/ 10 \(100%\)/', $output);
246+
$this->assertSame(0, $commandTester->getStatusCode());
247+
248+
// With a "CUSTOM-[0-9]+" regex configured.
249+
$commandTester = $this->executeCommand($this->pluginDir, ['--todo-comment-regex' => 'CUSTOM-[0-9]+']);
250+
$output = $commandTester->getDisplay();
251+
$this->assertSame(0, $commandTester->getStatusCode());
252+
$this->assertMatchesRegularExpression('/FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES/', $output);
253+
$this->assertMatchesRegularExpression('/Missing required "CUSTOM-\[0-9\]\+"/', $output);
254+
}
255+
223256
public function testExecuteNoFiles()
224257
{
225258
// Just random directory with no PHP files.

tests/Fixture/moodle-local_ci/lib.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

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

0 commit comments

Comments
 (0)