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 path = require( 'path' );
10 const wgServer = process.env.MW_SERVER;
11 const wgScriptPath = process.env.MW_SCRIPT_PATH;
12 const karmaProxy = {};
14 let qunitPattern = wgServer + wgScriptPath + '/index.php?title=Special:JavaScriptTest/qunit/export';
16 // "MediaWiki" for core, or extension/skin name (e.g. "GrowthExperiments")
17 const qunitComponent = grunt.option( 'qunit-component' );
18 const qunitWatch = grunt.option( 'qunit-watch' ) || false;
19 const qunitWatchFiles = [];
20 if ( qunitComponent ) {
21 let qunitWatchSourcePattern;
22 let qunitWatchTestPattern;
23 qunitPattern = qunitPattern + '&component=' + qunitComponent;
25 // Special-case MediaWiki core.
26 if ( qunitComponent === 'MediaWiki' ) {
27 qunitWatchTestPattern = 'tests/qunit/**/*.js';
28 qunitWatchSourcePattern = 'resources/**/*.js';
33 basePath = 'extensions';
34 // eslint-disable-next-line security/detect-non-literal-fs-filename
35 settingsJson = fs.readFileSync(
36 path.resolve( __dirname + '/' + basePath + '/' + qunitComponent + '/extension.json' )
40 // eslint-disable-next-line security/detect-non-literal-fs-filename
41 settingsJson = fs.readFileSync(
42 path.resolve( __dirname + '/' + basePath + '/' + qunitComponent + '/skin.json' )
45 settingsJson = JSON.parse( settingsJson );
46 qunitWatchSourcePattern =
47 path.resolve( __dirname + '/' + basePath + '/' + qunitComponent + '/' + settingsJson.ResourceFileModulePaths.localBasePath + '/**/*.js' );
48 qunitWatchTestPattern = path.resolve( __dirname + '/' + basePath + '/' + qunitComponent + '/tests/qunit/**/*.js' );
50 qunitWatchFiles.push( {
51 pattern: qunitWatchSourcePattern,
57 qunitWatchFiles.push( {
58 pattern: qunitWatchTestPattern,
67 karmaProxy[ wgScriptPath ] = {
68 target: wgServer + wgScriptPath,
75 extensions: [ '.js', '.json', '.vue' ],
77 fix: grunt.option( 'fix' )
83 requireLowerCase: false,
84 disallowBlankTranslations: false
86 core: 'languages/i18n/',
87 exif: 'languages/i18n/exif/',
88 api: 'includes/api/i18n/',
89 rest: 'includes/Rest/i18n/',
90 installer: 'includes/installer/i18n/',
91 paramvalidator: 'includes/libs/ParamValidator/i18n/'
95 reportNeedlessDisables: true
97 resources: 'resources/src/**/*.{css,less,vue}',
98 config: 'mw-config/**/*.css'
102 '.{stylelintrc,eslintrc}.json',
104 '!{extensions,node_modules,skins,vendor}/**'
112 base: 'ChromeHeadless',
113 // Chrome requires --no-sandbox in Docker/CI.
114 // WMF CI images expose CHROMIUM_FLAGS which sets that.
115 flags: process.env.CHROMIUM_FLAGS ? ( process.env.CHROMIUM_FLAGS || '' ).split( ' ' ) : []
120 pattern: qunitPattern,
125 }, ...qunitWatchFiles ],
126 logLevel: ( process.env.ZUUL_PROJECT ? 'DEBUG' : 'INFO' ),
127 frameworks: [ 'qunit' ],
128 reporters: [ 'mocha' ],
129 singleRun: !qunitWatch,
130 autoWatch: qunitWatch,
131 // Some tests in extensions don't yield for more than the default 10s (T89075)
132 browserNoActivityTimeout: 60 * 1000,
133 // Karma requires Same-Origin (or CORS) by default since v1.1.1
134 // for better stacktraces. But we load the first request from wgServer
135 crossOriginAttribute: false
138 browsers: [ 'ChromeCustom' ]
141 browsers: [ 'FirefoxHeadless' ]
146 grunt.registerTask( 'assert-mw-env', function () {
148 if ( !process.env.MW_SERVER ) {
149 grunt.log.error( 'Environment variable MW_SERVER must be set.\n' +
150 'Set this like $wgServer, e.g. "http://localhost"'
154 if ( !process.env.MW_SCRIPT_PATH ) {
155 grunt.log.error( 'Environment variable MW_SCRIPT_PATH must be set.\n' +
156 'Set this like $wgScriptPath, e.g. "/w"' );
162 grunt.registerTask( 'lint', [ 'eslint', 'banana', 'stylelint' ] );
163 grunt.registerTask( 'qunit', [ 'assert-mw-env', 'karma:main' ] );