From d450ff9b15714eff0e68e2678a95182c3837c5b6 Mon Sep 17 00:00:00 2001 From: Dave Townsend Date: Fri, 5 Feb 2016 13:23:40 -0800 Subject: [PATCH] Bug 1245916: Import more head files for xpcshell tests. r=pbrosset xpcshell tests used to use head_*.js files so this adds those for global discovery. MozReview-Commit-ID: BOsoGIpwdgu --HG-- extra : rebase_source : ef36531641cbd353625019f8deba333cfd352891 extra : source : 70eca07367f40a9b4fe8c6e23ec0aef73bf2a962 --- testing/eslint-plugin-mozilla/lib/helpers.js | 20 ++++++++++++-- .../lib/rules/import-headjs-globals.js | 27 ++++++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/testing/eslint-plugin-mozilla/lib/helpers.js b/testing/eslint-plugin-mozilla/lib/helpers.js index 55515e0654d1..560a56b69cae 100644 --- a/testing/eslint-plugin-mozilla/lib/helpers.js +++ b/testing/eslint-plugin-mozilla/lib/helpers.js @@ -308,7 +308,7 @@ module.exports = { * * @param {RuleContext} scope * You should pass this from within a rule - * e.g. helpers.getIsBrowserMochitest(this) + * e.g. helpers.getIsHeadFile(this) * * @return {Boolean} * True or false @@ -319,6 +319,22 @@ module.exports = { return /.*[\\/]head(_.+)?\.js$/.test(pathAndFilename); }, + /** + * Check whether we might be in an xpcshell test. + * + * @param {RuleContext} scope + * You should pass this from within a rule + * e.g. helpers.getIsXpcshellTest(this) + * + * @return {Boolean} + * True or false + */ + getIsXpcshellTest: function(scope) { + var pathAndFilename = scope.getFilename(); + + return /.*[\\/]test_.+\.js$/.test(pathAndFilename); + }, + /** * Check whether we are in a browser mochitest. * @@ -348,7 +364,7 @@ module.exports = { getIsTest: function(scope) { var pathAndFilename = scope.getFilename(); - if (/.*[\\/]test_.+\.js$/.test(pathAndFilename)) { + if (this.getIsXpcshellTest(scope)) { return true; } diff --git a/testing/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js b/testing/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js index 34cfd6fa2484..828eabe43cb3 100644 --- a/testing/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js +++ b/testing/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js @@ -20,6 +20,20 @@ var globals = require("../globals"); module.exports = function(context) { + function importHead(path, node) { + try { + let stats = fs.statSync(path); + if (!stats.isFile()) { + return; + } + } catch (e) { + return; + } + + let newGlobals = globals.getGlobalsForFile(path); + helpers.addGlobals(newGlobals, context.getScope()); + } + // --------------------------------------------------------------------------- // Public // --------------------------------------------------------------------------- @@ -32,13 +46,18 @@ module.exports = function(context) { var currentFilePath = helpers.getAbsoluteFilePath(context); var dirName = path.dirname(currentFilePath); - var fullHeadjsPath = path.resolve(dirName, "head.js"); - if (!fs.existsSync(fullHeadjsPath)) { + importHead(path.resolve(dirName, "head.js"), node); + + if (!helpers.getIsXpcshellTest(this)) { return; } - let newGlobals = globals.getGlobalsForFile(fullHeadjsPath); - helpers.addGlobals(newGlobals, context.getScope()); + let names = fs.readdirSync(dirName); + for (let name of names) { + if (name.startsWith("head_") && name.endsWith(".js")) { + importHead(path.resolve(dirName, name), node); + } + } } }; };