Bug 1617887 - Rework load_via_src_path. r=darktrojan
authorMagnus Melin <mkmelin+mozilla@iki.fi>
Mon, 04 May 2020 13:56:50 +0300
changeset 38124 89648b88a0e37b0444151712165156a7e278f439
parent 38123 87970e80244c9117cba8f522be7d4d25eecfb5cd
child 38125 ad8b33dcd11efd5c9914d9a85e97708f96767158
push id2595
push userclokep@gmail.com
push dateMon, 04 May 2020 19:02:04 +0000
treeherdercomm-beta@f53913797371 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdarktrojan
bugs1617887
Bug 1617887 - Rework load_via_src_path. r=darktrojan
mail/test/browser/attachment/browser_attachment.js
mail/test/browser/attachment/browser_attachmentSize.js
mail/test/browser/composition/browser_charsetEdit.js
mail/test/browser/shared-modules/FolderDisplayHelpers.jsm
mail/test/browser/shared-modules/NNTPHelpers.jsm
mailnews/test/resources/MessageGenerator.jsm
--- a/mail/test/browser/attachment/browser_attachment.js
+++ b/mail/test/browser/attachment/browser_attachment.js
@@ -27,24 +27,26 @@ var {
   close_popup,
   create_folder,
   create_message,
   mc,
   msgGen,
   plan_to_wait_for_folder_events,
   select_click_row,
   select_none,
-  SyntheticPartLeaf,
-  SyntheticPartMultiMixed,
   wait_for_folder_events,
   wait_for_message_display_completion,
   wait_for_popup_to_open,
 } = ChromeUtils.import(
   "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
 );
+var { SyntheticPartLeaf, SyntheticPartMultiMixed } = ChromeUtils.import(
+  "resource://testing-common/mailnews/MessageGenerator.jsm"
+);
+
 var {
   close_window,
   plan_for_modal_dialog,
   plan_for_new_window,
   wait_for_modal_dialog,
   wait_for_new_window,
 } = ChromeUtils.import("resource://testing-common/mozmill/WindowHelpers.jsm");
 
--- a/mail/test/browser/attachment/browser_attachmentSize.js
+++ b/mail/test/browser/attachment/browser_attachmentSize.js
@@ -23,22 +23,24 @@ var {
 var {
   add_message_to_folder,
   be_in_folder,
   create_folder,
   create_message,
   mc,
   msgGen,
   select_click_row,
-  SyntheticPartLeaf,
-  SyntheticPartMultiMixed,
 } = ChromeUtils.import(
   "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
 );
 
+var { SyntheticPartLeaf, SyntheticPartMultiMixed } = ChromeUtils.import(
+  "resource://testing-common/mailnews/MessageGenerator.jsm"
+);
+
 var textAttachment =
   "Can't make the frug contest, Helen; stomach's upset. I'll fix you, " +
   "Ubik! Ubik drops you back in the thick of things fast. Taken as " +
   "directed, Ubik speeds relief to head and stomach. Remember: Ubik is " +
   "only seconds away. Avoid prolonged use.";
 
 var binaryAttachment = textAttachment;
 
--- a/mail/test/browser/composition/browser_charsetEdit.js
+++ b/mail/test/browser/composition/browser_charsetEdit.js
@@ -24,20 +24,22 @@ var {
   add_message_to_folder,
   assert_selected_and_displayed,
   be_in_folder,
   create_message,
   get_special_folder,
   mc,
   press_delete,
   select_click_row,
-  SyntheticPartLeaf,
 } = ChromeUtils.import(
   "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
 );
+var { SyntheticPartLeaf } = ChromeUtils.import(
+  "resource://testing-common/mailnews/MessageGenerator.jsm"
+);
 var { wait_for_notification_to_show } = ChromeUtils.import(
   "resource://testing-common/mozmill/NotificationBoxHelpers.jsm"
 );
 var { plan_for_new_window } = ChromeUtils.import(
   "resource://testing-common/mozmill/WindowHelpers.jsm"
 );
 
 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
--- a/mail/test/browser/shared-modules/FolderDisplayHelpers.jsm
+++ b/mail/test/browser/shared-modules/FolderDisplayHelpers.jsm
@@ -83,17 +83,16 @@ const EXPORTED_SYMBOLS = [
   "gDefaultWindowHeight",
   "gDefaultWindowWidth",
   "get_smart_folder_named",
   "get_special_folder",
   "inboxFolder",
   "kClassicMailLayout",
   "kVerticalMailLayout",
   "kWideMailLayout",
-  "load_via_src_path",
   "make_display_grouped",
   "make_display_threaded",
   "make_display_unthreaded",
   "make_folder_with_sets",
   "make_new_sets_in_folder",
   "make_new_sets_in_folders",
   "make_virtual_folder",
   "mark_action",
@@ -134,19 +133,16 @@ const EXPORTED_SYMBOLS = [
   "set_open_message_behavior",
   "set_pane_layout",
   "set_show_unread_only",
   "show_folder_pane",
   "smimeUtils_ensureNSS",
   "smimeUtils_loadCertificateAndKey",
   "smimeUtils_loadPEMCertificate",
   "switch_tab",
-  "SyntheticPartLeaf",
-  "SyntheticPartMultiMixed",
-  "SyntheticPartMultiRelated",
   "throw_and_dump_view_state",
   "toggle_main_menu",
   "toggle_message_pane",
   "toggle_thread_row",
   "wait_for_all_messages_to_load",
   "wait_for_blank_content_pane",
   "wait_for_folder_events",
   "wait_for_message_display_completion",
@@ -155,17 +151,16 @@ const EXPORTED_SYMBOLS = [
 
 var EventUtils = ChromeUtils.import(
   "resource://testing-common/mozmill/EventUtils.jsm"
 );
 var controller = ChromeUtils.import(
   "resource://testing-common/mozmill/controller.jsm"
 );
 var frame = ChromeUtils.import("resource://testing-common/mozmill/frame.jsm");
-var os = ChromeUtils.import("resource://testing-common/mozmill/os.jsm");
 var utils = ChromeUtils.import("resource://testing-common/mozmill/utils.jsm");
 
 // the windowHelper module
 var windowHelper = ChromeUtils.import(
   "resource://testing-common/mozmill/WindowHelpers.jsm"
 );
 
 var { Assert } = ChromeUtils.import("resource://testing-common/Assert.jsm");
@@ -173,18 +168,22 @@ var { Log4Moz } = ChromeUtils.import("re
 
 var nsMsgViewIndex_None = 0xffffffff;
 var { MailConsts } = ChromeUtils.import("resource:///modules/MailConsts.jsm");
 var { MailServices } = ChromeUtils.import(
   "resource:///modules/MailServices.jsm"
 );
 var { MailUtils } = ChromeUtils.import("resource:///modules/MailUtils.jsm");
 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
-
-var FILE_LOAD_PATHS = ["../../mochitest/", "../../testing/mochitest/"];
+var { MessageGenerator, MessageScenarioFactory } = ChromeUtils.import(
+  "resource://testing-common/mailnews/MessageGenerator.jsm"
+);
+var { SmimeUtils } = ChromeUtils.import(
+  "resource://testing-common/mailnews/smimeUtils.jsm"
+);
 
 /**
  * Server hostname as set in runtest.py
  */
 var FAKE_SERVER_HOSTNAME = "tinderbox123";
 
 /** The controller for the main 3-pane window. */
 var mc;
@@ -231,17 +230,20 @@ function setupModule() {
     do_check_neq() {},
     gDEPTH: "../../",
   };
 
   // -- logging
 
   // The xpcshell test resources assume they are loaded into a single global
   //  namespace, so we need to help them out to maintain their delusion.
-  load_via_src_path("resources/logHelper.js", testHelperModule);
+  load_via_src_path(
+    "../../../testing/mochitest/resources/logHelper.js",
+    testHelperModule
+  );
   mark_action = testHelperModule.mark_action;
 
   // Remove the dump appender that got appended; it just adds noise.
   testHelperModule._mailnewsTestLogger.removeAppender(
     testHelperModule._mailnewsTestLogger.ownAppenders[
       testHelperModule._mailnewsTestLogger.ownAppenders.length - 1
     ]
   );
@@ -316,42 +318,50 @@ function setupModule() {
     } catch (ex) {
       dump("!!!!!!!!EX: " + ex);
       mark_action("fdh", "fail fail marking!", [ex]);
     }
   });
 
   // -- the rest of the asyncTestUtils framework (but not actually async)
 
-  load_via_src_path("resources/asyncTestUtils.js", testHelperModule);
-  load_via_src_path("resources/MessageGenerator.jsm", testHelperModule);
-  load_via_src_path("resources/messageModifier.js", testHelperModule);
-  load_via_src_path("resources/messageInjection.js", testHelperModule);
-  load_via_src_path("resources/viewWrapperTestUtils.js", testHelperModule);
-  load_via_src_path("resources/smimeUtils.jsm", testHelperModule);
+  load_via_src_path(
+    "../../../testing/mochitest/resources/asyncTestUtils.js",
+    testHelperModule
+  );
+  load_via_src_path(
+    "../../../testing/mochitest/resources/messageModifier.js",
+    testHelperModule
+  );
+  load_via_src_path(
+    "../../../testing/mochitest/resources/messageInjection.js",
+    testHelperModule
+  );
+  load_via_src_path(
+    "../../../testing/mochitest/resources/viewWrapperTestUtils.js",
+    testHelperModule
+  );
 
   // provide super helpful folder event info (when logHelper cares)
-  load_via_src_path("resources/folderEventLogHelper.js", testHelperModule);
+  load_via_src_path(
+    "../../../testing/mochitest/resources/folderEventLogHelper.js",
+    testHelperModule
+  );
   testHelperModule.registerFolderEventLogHelper();
 
   // messageInjection wants a gMessageGenerator (and so do we)
-  msgGen = new testHelperModule.MessageGenerator();
+  msgGen = new MessageGenerator();
   testHelperModule.gMessageGenerator = msgGen;
-  testHelperModule.gMessageScenarioFactory = new testHelperModule.MessageScenarioFactory(
-    msgGen
-  );
+  testHelperModule.gMessageScenarioFactory = new MessageScenarioFactory(msgGen);
 
   make_new_sets_in_folders = make_new_sets_in_folder =
     testHelperModule.make_new_sets_in_folders;
   add_sets_to_folders = testHelperModule.add_sets_to_folders;
   make_folder_with_sets = testHelperModule.make_folder_with_sets;
   make_virtual_folder = testHelperModule.make_virtual_folder;
-  SyntheticPartLeaf = testHelperModule.SyntheticPartLeaf;
-  SyntheticPartMultiMixed = testHelperModule.SyntheticPartMultiMixed;
-  SyntheticPartMultiRelated = testHelperModule.SyntheticPartMultiRelated;
 
   delete_message_set = testHelperModule.async_delete_messages;
 
   // use window-helper's augment_controller method to get our extra good stuff
   //  we need.
   mc = windowHelper.wait_for_existing_window("mail:3pane");
   windowHelper.augment_controller(mc);
 
@@ -374,25 +384,25 @@ function setupModule() {
   // account wizard is open on an initial startup type test.
   try {
     mc.folderTreeView.toggleOpenState(1);
   } catch (ex) {}
 }
 setupModule();
 
 function smimeUtils_ensureNSS() {
-  testHelperModule.SmimeUtils.ensureNSS();
+  SmimeUtils.ensureNSS();
 }
 
 function smimeUtils_loadPEMCertificate(file, certType, loadKey = false) {
-  testHelperModule.SmimeUtils.loadPEMCertificate(file, certType, loadKey);
+  SmimeUtils.loadPEMCertificate(file, certType, loadKey);
 }
 
 function smimeUtils_loadCertificateAndKey(file) {
-  testHelperModule.SmimeUtils.loadCertificateAndKey(file);
+  SmimeUtils.loadCertificateAndKey(file);
 }
 
 function setupAccountStuff() {
   inboxFolder = testHelperModule.configure_message_injection({ mode: "local" });
 }
 
 /*
  * Although we all agree that the use of generators when dealing with async
@@ -3348,72 +3358,62 @@ function assert_default_window_size() {
  */
 function restore_default_window_size() {
   windowHelper.resize_to(mc, gDefaultWindowWidth, gDefaultWindowHeight);
 }
 
 /**
  * Toggle visibility of the Main menu bar.
  *
- * @param aEnabled {boolean}  Whether the menu should be shown or not.
+ * @param {boolean} aEnabled - Whether the menu should be shown or not.
  */
 function toggle_main_menu(aEnabled = true) {
   let menubar = mc.e("mail-toolbar-menubar2");
   let state = menubar.getAttribute("autohide") != "true";
   menubar.setAttribute("autohide", !aEnabled);
   mc.sleep(0);
   return state;
 }
 
-/** exported from messageInjection */
+/** exported from messageInjection.js */
 var make_new_sets_in_folders;
 var make_new_sets_in_folder;
 var add_sets_to_folders;
 var delete_message_set;
 var make_folder_with_sets;
 var make_virtual_folder;
-var SyntheticPartLeaf;
-var SyntheticPartMultiMixed;
-var SyntheticPartMultiRelated;
 
 /**
- * Load a file in its own 'module' based on the effective location of the staged copy of
- * test-folder-helpers.js - if you get an error in this function, probably an appropriate relative
- * path in FILE_LOAD_PATHS is missing for your setup.
- *
- * @param aPath A path relative to the comm-central source path (can be just a file name)
+ * Load a file in its own 'module' (scope really), based on the effective
+ * location of the staged FolderDisplayHelpers.jsm module.
+ * @param {String} aPath - A path relative to the module (can be just a file name)
+ * @param {Object} aScope - Scope to load the file into.
  *
  * @return An object that serves as the global scope for the loaded file.
  */
-function load_via_src_path(aPath, aModule) {
+function load_via_src_path(aPath, aScope) {
   let thisFileURL = Cc["@mozilla.org/network/protocol;1?name=resource"]
     .getService(Ci.nsIResProtocolHandler)
     .resolveURI(
       Services.io.newURI(
         "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
       )
     );
   let thisFile = Services.io.newURI(thisFileURL).QueryInterface(Ci.nsIFileURL)
     .file;
 
-  for (let i = 0; i < FILE_LOAD_PATHS.length; ++i) {
-    let srcPath = os.abspath(FILE_LOAD_PATHS[i], thisFile);
-    let fullPath = os.abspath(aPath, os.getFileForPath(srcPath));
-
-    let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
-    file.initWithPath(fullPath);
-
-    if (file.exists()) {
-      try {
-        let uri = Services.io.newFileURI(file).spec;
-        Services.scriptloader.loadSubScript(uri, aModule);
-        return;
-      } catch (ex) {
-        throw new Error(
-          "Unable to load file: " + fullPath + " exception: " + ex
-        );
-      }
-    }
+  thisFile.setRelativePath;
+  let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
+  file.setRelativePath(thisFile, aPath);
+  // The files are at different paths when tests are run locally vs. CI.
+  // Plain js files shouldn't really be loaded from a module, but while we
+  // work on resolving that, try both locations...
+  if (!file.exists()) {
+    file.setRelativePath(thisFile, aPath.replace("/testing", ""));
   }
-
-  // If we've got this far, then we weren't successful, fail out.
-  throw new Error("Could not find " + aPath + " in available paths");
+  if (!file.exists()) {
+    throw new Error(
+      `Could not resolve file ${file.path} for path ${aPath} relative to ${thisFile.path}`
+    );
+  }
+  let uri = Services.io.newFileURI(file).spec;
+  Services.scriptloader.loadSubScript(uri, aScope);
 }
--- a/mail/test/browser/shared-modules/NNTPHelpers.jsm
+++ b/mail/test/browser/shared-modules/NNTPHelpers.jsm
@@ -7,24 +7,26 @@
 const EXPORTED_SYMBOLS = [
   "setupNNTPDaemon",
   "NNTP_PORT",
   "setupLocalServer",
   "startupNNTPServer",
   "shutdownNNTPServer",
 ];
 
-var folderDisplayHelper = ChromeUtils.import(
-  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
-);
-
 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
 var { MailServices } = ChromeUtils.import(
   "resource:///modules/MailServices.jsm"
 );
+var { newsArticle, NNTP_RFC977_handler, nntpDaemon } = ChromeUtils.import(
+  "resource://testing-common/mailnews/Nntpd.jsm"
+);
+var { nsMailServer } = ChromeUtils.import(
+  "resource://testing-common/mailnews/Maild.jsm"
+);
 
 var kSimpleNewsArticle =
   "From: John Doe <john.doe@example.com>\n" +
   "Date: Sat, 24 Mar 1990 10:59:24 -0500\n" +
   "Newsgroups: test.subscribe.simple\n" +
   "Subject: H2G2 -- What does it mean?\n" +
   "Message-ID: <TSS1@nntp.invalid>\n" +
   "\n" +
@@ -35,63 +37,39 @@ var kSimpleNewsArticle =
 // second element is whether or not we should subscribe to it.
 var groups = [
   ["test.empty", false],
   ["test.subscribe.empty", true],
   ["test.subscribe.simple", true],
   ["test.filter", true],
 ];
 
-var testHelperModule;
-
-function setupModule() {
-  testHelperModule = {
-    Cc,
-    Ci,
-    Cu,
-    // fake some xpcshell stuff
-    _TEST_FILE: ["mozmill"],
-    do_throw(aMsg) {
-      throw new Error(aMsg);
-    },
-  };
-  folderDisplayHelper.load_via_src_path(
-    "fakeserver/Nntpd.jsm",
-    testHelperModule
-  );
-  folderDisplayHelper.load_via_src_path(
-    "fakeserver/Maild.jsm",
-    testHelperModule
-  );
-}
-setupModule();
-
 // Sets up the NNTP daemon object for use in fake server
 function setupNNTPDaemon() {
-  var daemon = new testHelperModule.nntpDaemon();
+  var daemon = new nntpDaemon();
 
   groups.forEach(function(element) {
     daemon.addGroup(element[0]);
   });
 
-  var article = new testHelperModule.newsArticle(kSimpleNewsArticle);
+  var article = new newsArticle(kSimpleNewsArticle);
   daemon.addArticleToGroup(article, "test.subscribe.simple", 1);
 
   return daemon;
 }
 
 // Startup server
 function startupNNTPServer(daemon, port) {
-  var handler = testHelperModule.NNTP_RFC977_handler;
+  var handler = NNTP_RFC977_handler;
 
   function createHandler(daemon) {
     return new handler(daemon);
   }
 
-  var server = new testHelperModule.nsMailServer(createHandler, daemon);
+  var server = new nsMailServer(createHandler, daemon);
   server.start(port);
   return server;
 }
 
 // Shutdown server
 function shutdownNNTPServer(server) {
   server.stop();
 }
--- a/mailnews/test/resources/MessageGenerator.jsm
+++ b/mailnews/test/resources/MessageGenerator.jsm
@@ -1,13 +1,28 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, you can obtain one at http://mozilla.org/MPL/2.0/. */
 
-this.EXPORTED_SYMBOLS = ["MessageGenerator", "addMessagesToFolder"];
+this.EXPORTED_SYMBOLS = [
+  "MessageGenerator",
+  "addMessagesToFolder",
+  "MessageScenarioFactory",
+  "SyntheticPartLeaf",
+  "SyntheticDegeneratePartEmpty",
+  "SyntheticPartMulti",
+  "SyntheticPartMultiMixed",
+  "SyntheticPartMultiParallel",
+  "SyntheticPartMultiDigest",
+  "SyntheticPartMultiAlternative",
+  "SyntheticPartMultiRelated",
+  "SyntheticPartMultiSignedSMIME",
+  "SyntheticPartMultiSignedPGP",
+  "SyntheticMessage",
+];
 
 /**
  * A list of first names for use by MessageGenerator to create deterministic,
  *  reversible names.  To keep things easily reversible, if you add names, make
  *  sure they have no spaces in them!
  */
 var FIRST_NAMES = [
   "Andy",