Skip to content

Commit

Permalink
Allow a list of files to be checked
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarshallou committed Sep 25, 2020
1 parent 24ee399 commit 8237794
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
38 changes: 22 additions & 16 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/local/codechecker/locallib.php');

$path = optional_param('path', '', PARAM_PATH);
$pathlist = optional_param('path', '', PARAM_RAW);
$exclude = optional_param('exclude', '', PARAM_NOTAGS);
$includewarnings = optional_param('includewarnings', true, PARAM_BOOL);

$pageparams = array();
if ($path) {
$pageparams['path'] = $path;
if ($pathlist) {
$pageparams['path'] = $pathlist;
}
if ($exclude) {
$pageparams['exclude'] = $exclude;
Expand All @@ -43,27 +43,35 @@

// We are going to need lots of memory and time.
raise_memory_limit(MEMORY_HUGE);
set_time_limit(300);
set_time_limit(600);

$mform = new local_codechecker_form(new moodle_url('/local/codechecker/'));
$mform->set_data((object)$pageparams);
if ($data = $mform->get_data()) {
redirect(new moodle_url('/local/codechecker/', $pageparams));
}

if ($path) {
$fullpath = $CFG->dirroot . '/' . trim($path, '/');
if (!is_file($fullpath) && !is_dir($fullpath)) {
$fullpath = null;
}
}

$output = $PAGE->get_renderer('local_codechecker');

echo $OUTPUT->header();

if ($path) {
if ($fullpath) {
if ($pathlist) {
$paths = preg_split('~[\r\n]+~', $pathlist);

$failed = false;
$fullpaths = [];
foreach ($paths as $path) {
$path = clean_param($path, PARAM_PATH);
$fullpath = $CFG->dirroot . '/' . trim($path, '/');
if (!is_file($fullpath) && !is_dir($fullpath)) {
echo $output->invald_path_message($path);
$failed = true;
continue;
}
$fullpaths[] = local_codechecker_clean_path($fullpath);
}

if ($fullpaths && !$failed) {
$reportfile = make_temp_directory('phpcs') . '/phpcs_' . random_string(10) . '.xml';
$phpcs = new PHP_CodeSniffer();
$phpcs->setAllowedFileExtensions(['php']); // We are only going to process php files ever.
Expand All @@ -73,7 +81,7 @@
$cli->setIncludeWarnings($includewarnings); // Decide if we want to show warnings (defaults yes).
$phpcs->setCli($cli);
$phpcs->setIgnorePatterns(local_codesniffer_get_ignores($exclude));
$phpcs->process(local_codechecker_clean_path($fullpath),
$phpcs->process($fullpaths,
local_codechecker_clean_path($CFG->dirroot . '/local/codechecker/moodle'));
// Save the xml report file to dataroot/temp.
$phpcs->reporting->printReport('local_codechecker', false, $cli->getCommandLineValues(), $reportfile);
Expand All @@ -89,8 +97,6 @@

// And clean the report temp file.
@unlink($reportfile);
} else {
echo $output->invald_path_message($path);
}
}

Expand Down
5 changes: 3 additions & 2 deletions lang/en/local_codechecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@
$string['filesummary'] = '{$a->path} - {$a->count}';
$string['info'] = '<p>Checks code against some aspects of the {$a->link}.</p>
<p>Enter a path relative to the Moodle code root, for example: {$a->path}.</p>
<p>You can enter either a specific PHP file, or to a folder to check all the files it contains.</p>
<p>You can enter either a specific PHP file, or to a folder to check all the files it contains.
Multiple entries are supported (files or folders), one per line.</p>
<p>To exclude files, a comma separated list of substr matching paths can be used, for example: {$a->excludeexample}. Asterisks are allowed as wildchars at any place.</p>';
$string['includewarnings'] = 'Include warnings';
$string['invalidpath'] = 'Invalid path {$a}';
$string['moodlecodingguidelines'] = 'Moodle coding guidelines';
$string['numerrorswarnings'] = '{$a->errors} error(s) and {$a->warnings} warning(s)';
$string['path'] = 'Path to check';
$string['path'] = 'Path(s) to check';
$string['privacy:metadata'] = 'The Code checker plugin does not store any personal data.';
$string['pluginname'] = 'Code checker';
$string['recheckfile'] = 'Re-check just this file';
Expand Down
4 changes: 2 additions & 2 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ protected function definition() {
$a->excludeexample = html_writer::tag('tt', 'db, backup/*1, *lib*');
$mform->addElement('static', '', '', get_string('info', 'local_codechecker', $a));

$mform->addElement('text', 'path', get_string('path', 'local_codechecker'), array('size' => '48'));
$mform->setType('path', PARAM_PATH);
$mform->addElement('textarea', 'path', get_string('path', 'local_codechecker'), ['rows' => '4', 'cols' => 48]);
$mform->setType('path', PARAM_RAW);
$mform->addRule('path', null, 'required', null, 'client');

$mform->addElement('text', 'exclude', get_string('exclude', 'local_codechecker'), array('size' => '48'));
Expand Down
16 changes: 12 additions & 4 deletions tests/behat/ui.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Codechecker UI works as expected
Scenario Outline: Verify that specified paths are checked
Given I log in as "admin"
And I navigate to "Development > Code checker" in site administration
And I set the field "Path to check" to "<path>"
And I set the field "Path(s) to check" to "<path>"
And I set the field "Exclude" to "*/tests/fixtures/*"
When I press "Check code"
Then I should see "<seen>"
Expand All @@ -31,7 +31,7 @@ Feature: Codechecker UI works as expected
Scenario Outline: Verify that specified exclusions are performed
Given I log in as "admin"
And I navigate to "Development > Code checker" in site administration
And I set the field "Path to check" to "<path>"
And I set the field "Path(s) to check" to "<path>"
And I set the field "Exclude" to "<exclude>"
When I press "Check code"
Then I should see "<seen>"
Expand All @@ -50,10 +50,10 @@ Feature: Codechecker UI works as expected

# We use the @javascript tag here because of MDL-53083, causing non-javascript to fail unchecking checkboxes
@javascript
Scenario: Verify that the warnings toogle has effect
Scenario: Verify that the warnings toggle has effect
Given I log in as "admin"
And I navigate to "Development > Code checker" in site administration
And I set the field "Path to check" to "local/codechecker/moodle/tests/fixtures/squiz_php_commentedoutcode.php"
And I set the field "Path(s) to check" to "local/codechecker/moodle/tests/fixtures/squiz_php_commentedoutcode.php"
And I set the field "Exclude" to "dont_exclude_anything"
# Warnings enabled
And I set the field "Include warnings" to "1"
Expand All @@ -68,3 +68,11 @@ Feature: Codechecker UI works as expected
And I should not see "Inline comments must start"
And I should not see "is this commented out code"
And I log out

Scenario: Verify that multiple paths work
Given I log in as "admin"
And I navigate to "Development > Code checker" in site administration
And I set the field "Path(s) to check" to "local/codechecker/version.php\nlocal/codechecker/index.php"
When I press "Check code"
Then I should see "index.php"
And I should see "version.php"

0 comments on commit 8237794

Please sign in to comment.