1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15 /* jshint node: true, browser: false */
19 * @copyright 2021 Andrew Nicols
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 module.exports = grunt => {
25 * Get the list of feature files to pass to the gherkin linter.
29 const getGherkinLintTargets = () => {
30 if (grunt.moodleEnv.files) {
31 // Specific files were requested. Only check these.
32 return grunt.moodleEnv.files;
35 if (grunt.moodleEnv.inComponent) {
36 return [`${grunt.moodleEnv.runDir}/tests/behat/*.feature`];
39 return ['**/tests/behat/*.feature'];
42 const handler = function() {
43 const done = this.async();
44 const options = grunt.config('gherkinlint.options');
46 // Grab the gherkin-lint linter and required scaffolding.
47 const linter = require('gherkin-lint/dist/linter.js');
48 const featureFinder = require('gherkin-lint/dist/feature-finder.js');
49 const configParser = require('gherkin-lint/dist/config-parser.js');
50 const formatter = require('gherkin-lint/dist/formatters/stylish.js');
54 featureFinder.getFeatureFiles(grunt.file.expand(options.files)),
55 configParser.getConfiguration(configParser.defaultConfigFileName)
58 // Print the results out uncondtionally.
59 formatter.printResults(results);
64 // Report on the results.
65 // The done function takes a bool whereby a falsey statement causes the task to fail.
66 return results.every(result => result.errors.length === 0);
68 .then(done); // eslint-disable-line promise/no-callback-in-promise
71 grunt.registerTask('gherkinlint', 'Run gherkinlint against the current directory', handler);
73 grunt.config.set('gherkinlint', {
75 files: getGherkinLintTargets(),
82 files: [grunt.moodleEnv.inComponent ? 'tests/behat/*.feature' : '**/tests/behat/*.feature'],
83 tasks: ['gherkinlint'],
88 // Add the 'gherkinlint' task as a startup task.
89 grunt.moodleEnv.startupTasks.push('gherkinlint');