Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow phpcs annotations + report wrong testcase classes in class lines instead of line 0 #172

Merged
merged 2 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions moodle/Sniffs/Files/BoilerplateCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@

namespace MoodleCodeSniffer\moodle\Sniffs\Files;

// phpcs:disable moodle.NamingConventions

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

class BoilerplateCommentSniff implements Sniff {
protected static $comment = array(
Expand Down Expand Up @@ -62,24 +65,29 @@ public function process(File $file, $stackptr) {

$tokens = $file->getTokens();

// Allow T_PHPCS_XXX comment annotations in the first line (skip them).
if ($commentptr = $file->findNext(Tokens::$phpcsCommentTokens, $stackptr + 1, $stackptr + 3)) {
$stackptr = $commentptr;
}

// Find count the number of newlines after the opening <?PHP. We only
// count enough to see if the number is right.
// Note that the opening PHP tag includes one newline.
$numnewlines = 0;
for ($i = 1; $i <= 5; ++$i) {
for ($i = $stackptr + 1; $i <= $stackptr + 5; ++$i) {
if ($tokens[$i]['code'] == T_WHITESPACE && $tokens[$i]['content'] == "\n") {
$numnewlines = $i;
$numnewlines++;
} else {
break;
}
}

if ($numnewlines > 0) {
$file->addError('The opening <?php tag must be followed by exactly one newline.',
1, 'WrongWhitespace');
$stackptr + 1, 'WrongWhitespace');
return;
}
$offset = $numnewlines + 1;
$offset = $stackptr + $numnewlines + 1;

// Now check the text of the comment.
foreach (self::$comment as $lineindex => $line) {
Expand Down
7 changes: 6 additions & 1 deletion moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public function process(File $file, $pointer) {
// verify that it extends something and that has a test_ method.
$class = '';
$classFound = false;
$classPointers = []; // Save all class pointers to report later if no class is found.
while ($cStart = $file->findNext(T_CLASS, $pointer)) {
$classPointers[] = $cStart;
$pointer = $cStart + 1; // Move the pointer to the class start.

// Only if the class is extending something.
Expand Down Expand Up @@ -146,7 +148,10 @@ public function process(File $file, $pointer) {

// No testcase class found, this is plain-wrong.
if (!$classFound) {
$file->addError('PHPUnit test file missing any valid testcase class declaration', 0, 'Missing');
$classPointers = $classPointers ?: [0];
foreach ($classPointers as $classPointer) {
$file->addError('PHPUnit test file missing any valid testcase class declaration', $classPointer, 'Missing');
}
return; // If arrived here we don't have a valid class, we are finished.
}

Expand Down
139 changes: 139 additions & 0 deletions moodle/tests/files_boilerplatecomment_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace local_codechecker;

defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . '/../../tests/local_codechecker_testcase.php');

// phpcs:disable moodle.NamingConventions

/**
* Test the BoilerplateCommentSniff sniff.
*
* @package local_codechecker
* @category test
* @copyright 2022 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @covers \MoodleCodeSniffer\moodle\Sniffs\Files\BoilerplateCommentSniff
*/
class files_boilerplatecomment_test extends local_codechecker_testcase {

public function test_moodle_files_boilerplatecomment_ok() {
$this->set_standard('moodle');
$this->set_sniff('moodle.Files.BoilerplateComment');
$this->set_fixture(__DIR__ . '/fixtures/files/boilerplatecomment/ok.php');

// Define expected results (errors and warnings). Format, array of:
// - line => number of problems, or
// - line => array of contents for message / source problem matching.
// - line => string of contents for message / source problem matching (only 1).
$this->set_errors([]);
$this->set_warnings([]);

$this->verify_cs_results();

// Also try with the <?php line having some // phpcs:xxxx annotations.
$this->set_fixture(__DIR__ . '/fixtures/files/boilerplatecomment/ok2.php');

$this->set_errors([]);
$this->set_warnings([]);

$this->verify_cs_results();
}

public function test_moodle_files_boilerplatecomment_nophp() {
$this->set_standard('moodle');
$this->set_sniff('moodle.Files.BoilerplateComment');
$this->set_fixture(__DIR__ . '/fixtures/files/boilerplatecomment/nophp.php');

$this->set_errors([
1 => 'moodle.Files.BoilerplateComment.NoPHP',
]);
$this->set_warnings([]);

$this->verify_cs_results();
}

public function test_moodle_files_boilerplatecomment_blank() {
$this->set_standard('moodle');
$this->set_sniff('moodle.Files.BoilerplateComment');
$this->set_fixture(__DIR__ . '/fixtures/files/boilerplatecomment/blank.php');

$this->set_errors([
2 => 'followed by exactly one newline',
]);
$this->set_warnings([]);

$this->verify_cs_results();
}

public function test_moodle_files_boilerplatecomment_short() {
$this->set_standard('moodle');
$this->set_sniff('moodle.Files.BoilerplateComment');
$this->set_fixture(__DIR__ . '/fixtures/files/boilerplatecomment/short.php');

$this->set_errors([
14 => 'FileTooShort',
]);
$this->set_warnings([]);

$this->verify_cs_results();
}

public function test_moodle_files_boilerplatecomment_wrongline() {
$this->set_standard('moodle');
$this->set_sniff('moodle.Files.BoilerplateComment');
$this->set_fixture(__DIR__ . '/fixtures/files/boilerplatecomment/wrongline.php');

$this->set_errors([
6 => 'version 3',
11 => 'FITNESS',
]);
$this->set_warnings([]);

$this->verify_cs_results();
}

public function test_moodle_files_boilerplatecomment_gnu_http() {

$this->set_standard('moodle');
$this->set_sniff('moodle.Files.BoilerplateComment');
$this->set_fixture(__DIR__ . '/fixtures/files/boilerplatecomment/gnu_http.php');

$this->set_errors([]);
$this->set_warnings([]);

$this->verify_cs_results();
}

/**
* Assert that www.gnu.org can be referred to via https URL in the boilerplate.
*/
public function test_moodle_files_boilerplatecomment_gnu_https() {

$this->set_standard('moodle');
$this->set_sniff('moodle.Files.BoilerplateComment');
$this->set_fixture(__DIR__ . '/fixtures/files/boilerplatecomment/gnu_https.php');

$this->set_errors([]);
$this->set_warnings([]);

$this->verify_cs_results();
}
}
18 changes: 18 additions & 0 deletions moodle/tests/fixtures/files/boilerplatecomment/blank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

class someclass { }
18 changes: 18 additions & 0 deletions moodle/tests/fixtures/files/boilerplatecomment/nophp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//something
<?php // phpcs:disable moodle.PHPUnit.TestCaseNames.Missing
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

class someclass { }
17 changes: 17 additions & 0 deletions moodle/tests/fixtures/files/boilerplatecomment/ok.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

class someclass { }
17 changes: 17 additions & 0 deletions moodle/tests/fixtures/files/boilerplatecomment/ok2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php // phpcs:disable moodle.PHPUnit.TestCaseNames.Missing
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

class someclass { }
14 changes: 14 additions & 0 deletions moodle/tests/fixtures/files/boilerplatecomment/short.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
17 changes: 17 additions & 0 deletions moodle/tests/fixtures/files/boilerplatecomment/wrongline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version N of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or UTILITY FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

class someclass { }
30 changes: 0 additions & 30 deletions moodle/tests/moodlestandard_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,34 +831,4 @@ public function test_moodle_files_requirelogin_nomoodlecookies_ok() {

$this->verify_cs_results();
}

/**
* Assert that www.gnu.org can be referred to via http URL in the boilerplate.
*/
public function test_moodle_files_boilerplate_gnu_http() {

$this->set_standard('moodle');
$this->set_sniff('moodle.Files.BoilerplateComment');
$this->set_fixture(__DIR__ . '/fixtures/moodle_files_boilerplate/gnu_http.php');

$this->set_errors([]);
$this->set_warnings([]);

$this->verify_cs_results();
}

/**
* Assert that www.gnu.org can be referred to via https URL in the boilerplate.
*/
public function test_moodle_files_boilerplate_gnu_https() {

$this->set_standard('moodle');
$this->set_sniff('moodle.Files.BoilerplateComment');
$this->set_fixture(__DIR__ . '/fixtures/moodle_files_boilerplate/gnu_https.php');

$this->set_errors([]);
$this->set_warnings([]);

$this->verify_cs_results();
}
}
3 changes: 2 additions & 1 deletion moodle/tests/phpunit_testcasenames_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function provider_phpunit_testcasenames() {
'Missing' => [
'fixture' => 'fixtures/phpunit/testcasenames_missing.php',
'errors' => [
1 => '@Message: PHPUnit test file missing any valid testcase class',
7 => '@Message: PHPUnit test file missing any valid testcase class',
14 => 'Missing',
],
'warnings' => [],
],
Expand Down
4 changes: 2 additions & 2 deletions tests/behat/ui.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ Feature: Codechecker UI works as expected

Examples:
| path | exclude | seen | notseen |
| local/codechecker/moodle/tests | */tests/fixtures/* | Files found: 4 | Invalid path |
| local/codechecker/moodle/tests | */tests/fixtures/* | Files found: 5 | Invalid path |
| local/codechecker/moodle/tests | */tests/fixtures/* | moodlestandard_test.php | Invalid path |
| local/codechecker/moodle/tests/ | *PHPC*, *moodle_* | Files found: 43 | Invalid path |
| local/codechecker/moodle/tests/ | *PHPC*, *moodle_* | Files found: 52 | Invalid path |
| local/codechecker/moodle/tests/ | *PHPC*, *moodle_* | Line 1 of the opening comment | moodle_php |
| local/codechecker/moodle/tests/ | *PHPC*, *moodle_* | Inline comments must end | /phpcompat |
| local/codechecker/moodle/tests/ | *PHPC*, *moodle_* | Inline comments must end | /phpcompat |
Expand Down