2 module.exports = function ( grunt ) {
3 grunt.loadNpmTasks( 'grunt-banana-checker' );
4 grunt.loadNpmTasks( 'grunt-eslint' );
5 grunt.loadNpmTasks( 'grunt-karma' );
6 grunt.loadNpmTasks( 'grunt-stylelint' );
8 const fs = require( 'fs' );
9 const wgServer = process.env.MW_SERVER;
10 const wgScriptPath = process.env.MW_SCRIPT_PATH;
11 const karmaProxy = {};
13 let qunitURL = wgServer + wgScriptPath + '/index.php?title=Special:JavaScriptTest/qunit/export';
15 // "MediaWiki" for core, or extension/skin name (e.g. "GrowthExperiments")
16 const qunitComponent = grunt.option( 'qunit-component' );
17 const qunitWatch = grunt.option( 'qunit-watch' ) || false;
18 const qunitWatchFiles = [];
19 if ( qunitComponent ) {
20 qunitURL = qunitURL + '&component=' + qunitComponent;
23 if ( !qunitComponent || qunitComponent === 'MediaWiki' ) {
25 qunitWatchFiles.push( 'tests/qunit/**' );
26 qunitWatchFiles.push( 'resources/**' );
28 // one extension or skin
29 const extPath = __dirname + '/extensions/' + qunitComponent + '/extension.json';
30 const skinPath = __dirname + '/skins/' + qunitComponent + '/skin.json';
31 // eslint-disable-next-line security/detect-non-literal-fs-filename
32 if ( fs.existsSync( extPath ) ) {
33 qunitWatchFiles.push( 'extensions/' + qunitComponent + '/extension.json' );
34 qunitWatchFiles.push( 'extensions/' + qunitComponent + '/{modules,resources,tests}/**' );
36 // eslint-disable-next-line security/detect-non-literal-fs-filename
37 if ( fs.existsSync( skinPath ) ) {
38 qunitWatchFiles.push( 'skins/' + qunitComponent + '/skin.json' );
39 qunitWatchFiles.push( 'skins/' + qunitComponent + '/{modules,resources,tests}/**' );
44 karmaProxy[ wgScriptPath ] = {
45 target: wgServer + wgScriptPath,
52 extensions: [ '.js', '.json', '.vue' ],
54 fix: grunt.option( 'fix' )
60 requireLowerCase: false,
61 disallowBlankTranslations: false
63 core: 'languages/i18n/',
64 exif: 'languages/i18n/exif/',
65 api: 'includes/api/i18n/',
66 rest: 'includes/Rest/i18n/',
67 installer: 'includes/installer/i18n/',
68 paramvalidator: 'includes/libs/ParamValidator/i18n/'
72 reportNeedlessDisables: true
74 resources: 'resources/src/**/*.{css,less,vue}',
75 config: 'mw-config/**/*.css'
79 '.{stylelintrc,eslintrc}.json',
81 '!{extensions,node_modules,skins,vendor}/**'
88 '@wikimedia/karma-firefox-launcher',
93 base: 'ChromeHeadless',
94 // Chrome requires --no-sandbox in Docker/CI.
95 // WMF CI images expose CHROMIUM_FLAGS which sets that.
96 flags: process.env.CHROMIUM_FLAGS ? ( process.env.CHROMIUM_FLAGS || '' ).split( ' ' ) : []
106 }, ...qunitWatchFiles.map( ( file ) => ( {
113 logLevel: ( process.env.ZUUL_PROJECT ? 'DEBUG' : 'INFO' ),
114 frameworks: [ 'qunit' ],
115 // Disable autostart because we load modules asynchronously.
121 reporters: [ 'mocha' ],
122 singleRun: !qunitWatch,
123 autoWatch: qunitWatch,
124 // Some tests in extensions don't yield for more than the default 10s (T89075)
125 browserNoActivityTimeout: 60 * 1000,
126 // Karma requires Same-Origin (or CORS) by default since v1.1.1
127 // for better stacktraces. But we load the first request from wgServer
128 crossOriginAttribute: false
131 browsers: [ 'ChromeCustom' ]
134 browsers: [ 'FirefoxHeadless' ]
139 grunt.registerTask( 'assert-mw-env', function () {
141 if ( !process.env.MW_SERVER ) {
142 grunt.log.error( 'Environment variable MW_SERVER must be set.\n' +
143 'Set this like $wgServer, e.g. "http://localhost"'
147 if ( !process.env.MW_SCRIPT_PATH ) {
148 grunt.log.error( 'Environment variable MW_SCRIPT_PATH must be set.\n' +
149 'Set this like $wgScriptPath, e.g. "/w"' );
155 grunt.registerTask( 'lint', [ 'eslint', 'banana', 'stylelint' ] );
156 grunt.registerTask( 'qunit', [ 'assert-mw-env', 'karma:main' ] );