Merge branch 'MDL-78849' of https://github.com/paulholden/moodle
[moodle.git] / .grunt / tasks / gherkinlint.js
blob658c752928c8dc16b511d7e3d0ce8aba199e13fe
1 // This file is part of Moodle - http://moodle.org/
2 //
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.
7 //
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 */
16 /* eslint-env node */
18 /**
19  * @copyright  2021 Andrew Nicols
20  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21  */
23 module.exports = grunt => {
24     /**
25      * Get the list of feature files to pass to the gherkin linter.
26      *
27      * @returns {Array}
28      */
29     const getGherkinLintTargets = () => {
30         if (grunt.moodleEnv.files) {
31             // Specific files were requested. Only check these.
32             return grunt.moodleEnv.files;
33         }
35         if (grunt.moodleEnv.inComponent) {
36             return [`${grunt.moodleEnv.runDir}/tests/behat/*.feature`];
37         }
39         return ['**/tests/behat/*.feature'];
40     };
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');
52         // Run the linter.
53         return linter.lint(
54             featureFinder.getFeatureFiles(grunt.file.expand(options.files)),
55             configParser.getConfiguration(configParser.defaultConfigFileName)
56         )
57         .then(results => {
58             // Print the results out uncondtionally.
59             formatter.printResults(results);
61             return results;
62         })
63         .then(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);
67         })
68         .then(done); // eslint-disable-line promise/no-callback-in-promise
69     };
71     grunt.registerTask('gherkinlint', 'Run gherkinlint against the current directory', handler);
73     grunt.config.set('gherkinlint', {
74         options: {
75             files: getGherkinLintTargets(),
76         }
77     });
79     grunt.config.merge({
80         watch: {
81             gherkinlint: {
82                 files: [grunt.moodleEnv.inComponent ? 'tests/behat/*.feature' : '**/tests/behat/*.feature'],
83                 tasks: ['gherkinlint'],
84             },
85         },
86     });
88     // Add the 'gherkinlint' task as a startup task.
89     grunt.moodleEnv.startupTasks.push('gherkinlint');
91     return handler;