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
--- a/testing/eslint-plugin-mozilla/lib/helpers.js
+++ b/testing/eslint-plugin-mozilla/lib/helpers.js
@@ -303,28 +303,44 @@ module.exports = {
return true;
},
/**
* Check whether we might be in a test head file.
*
* @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
*/
getIsHeadFile: function(scope) {
var pathAndFilename = scope.getFilename();
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.
*
* @param {RuleContext} scope
* You should pass this from within a rule
* e.g. helpers.getIsBrowserMochitest(this)
*
* @return {Boolean}
* True or false
@@ -343,17 +359,17 @@ module.exports = {
* e.g. helpers.getIsTest(this)
*
* @return {Boolean}
* True or false
*/
getIsTest: function(scope) {
var pathAndFilename = scope.getFilename();
- if (/.*[\\/]test_.+\.js$/.test(pathAndFilename)) {
+ if (this.getIsXpcshellTest(scope)) {
return true;
}
return this.getIsBrowserMochitest(scope);
},
/**
* Gets the root directory of the repository by walking up directories until
--- a/testing/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js
+++ b/testing/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js
@@ -15,30 +15,49 @@
var fs = require("fs");
var path = require("path");
var helpers = require("../helpers");
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
// ---------------------------------------------------------------------------
return {
Program: function(node) {
if (!helpers.getIsTest(this)) {
return;
}
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);
+ }
+ }
}
};
};