Bug 1245916: Import more head files for xpcshell tests. r=pbrosset draft
authorDave Townsend <dtownsend@oxymoronical.com>
Fri, 05 Feb 2016 13:23:40 -0800
changeset 331376 ef36531641cbd353625019f8deba333cfd352891
parent 331375 a88116d149af76aa35ee0757b3b51b6f97646ebe
child 331377 5c19b92e4242088b6fc7a268f255fe9a795928f6
push id10969
push userdtownsend@mozilla.com
push dateTue, 16 Feb 2016 22:40:19 +0000
reviewerspbrosset
bugs1245916
milestone47.0a1
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
testing/eslint-plugin-mozilla/lib/helpers.js
testing/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js
--- 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);
+        }
+      }
     }
   };
 };