Bug 1350887 - Fall back to deprecated pref if it exists; r=whimboo, a=test-only
authorAndreas Tolfsen <ato@mozilla.com>
Mon, 27 Mar 2017 14:28:32 +0100
changeset 395690 28a50e73dd6ddd6fb5b4bd8f2a5c505aeed88fd6
parent 395689 7ff4847d897b49ce091540d0d715e5cef3722eb6
child 395691 684f78299750cac7dbf62ea619a5417675c8519a
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswhimboo, test-only
bugs1350887, 1344748
milestone54.0a2
Bug 1350887 - Fall back to deprecated pref if it exists; r=whimboo, a=test-only This is a follow-up to address a fallout caused by bug 1344748 whereby deprecated preferences relevant to Marionette are no longer being picked up. This is preventing trace logs from being emitted in CI. The old logic related to falling back to a deprecated preference is faulty in that it the preferred, new preference always exists through the power of testing/marionette/prefs.js. This patch introduces a new helper method getPref that first looks at whether the preferred pref is set, and only falls back to the deprecated if it isn't set and the deprecation preference exists. MozReview-Commit-ID: 8DeawLAELyK
testing/marionette/components/marionette.js
--- a/testing/marionette/components/marionette.js
+++ b/testing/marionette/components/marionette.js
@@ -20,25 +20,37 @@ const PREF_PORT = "marionette.port";
 const PREF_PORT_FALLBACK = "marionette.defaultPrefs.port";
 const PREF_LOG_LEVEL = "marionette.log.level";
 const PREF_LOG_LEVEL_FALLBACK = "marionette.logging";
 const PREF_FORCE_LOCAL = "marionette.forcelocal";
 const PREF_FORCE_LOCAL_FALLBACK = "marionette.force-local";
 
 const DEFAULT_PORT = 2828;
 const DEFAULT_LOG_LEVEL = "info";
-const LOG_LEVELS = new Map([
-  ["fatal", Log.Level.Fatal],
-  ["error", Log.Level.Error],
-  ["warn", Log.Level.Warn],
-  ["info", Log.Level.Info],
-  ["config", Log.Level.Config],
-  ["debug", Log.Level.Debug],
-  ["trace", Log.Level.Trace],
-]);
+const LOG_LEVELS = new class extends Map {
+  constructor () {
+    super([
+      ["fatal", Log.Level.Fatal],
+      ["error", Log.Level.Error],
+      ["warn", Log.Level.Warn],
+      ["info", Log.Level.Info],
+      ["config", Log.Level.Config],
+      ["debug", Log.Level.Debug],
+      ["trace", Log.Level.Trace],
+    ]);
+  }
+
+  get (level) {
+    let s = new String(level).toLowerCase();
+    if (!this.has(s)) {
+      return DEFAULT_LOG_LEVEL;
+    }
+    return super.get(s);
+  }
+};
 
 // Besides starting based on existing prefs in a profile and a command
 // line flag, we also support inheriting prefs out of an env var, and to
 // start Marionette that way.
 //
 // This allows marionette prefs to persist when we do a restart into
 // a different profile in order to test things like Firefox refresh.
 // The environment variable itself, if present, is interpreted as a
@@ -48,55 +60,42 @@ const LOG_LEVELS = new Map([
 // pref being set to true, thus triggering marionette being enabled for
 // that startup.
 const ENV_PREF_VAR = "MOZ_MARIONETTE_PREF_STATE_ACROSS_RESTARTS";
 
 const ServerSocket = CC("@mozilla.org/network/server-socket;1",
     "nsIServerSocket",
     "initSpecialConnection");
 
+// Get preference value of |preferred|, falling back to |fallback|
+// if |preferred| is not user-modified and |fallback| exists.
+function getPref (preferred, fallback) {
+  if (!Preferences.isSet(preferred) && Preferences.has(fallback)) {
+    return Preferences.get(fallback, Preferences.get(preferred));
+  }
+  return Preferences.get(preferred);
+}
+
 // Marionette preferences recently changed names.  This is an abstraction
 // that first looks for the new name, but falls back to using the old name
 // if the new does not exist.
 //
 // This shim can be removed when Firefox 55 ships.
 const prefs = {
   get port () {
-    let fallback = Preferences.get(PREF_PORT_FALLBACK, DEFAULT_PORT);
-    return Preferences.get(PREF_PORT, fallback);
+    return getPref(PREF_PORT, PREF_PORT_FALLBACK);
   },
 
   get logLevel () {
-    let level = DEFAULT_LOG_LEVEL;
-    let fallback = Preferences.get(PREF_LOG_LEVEL_FALLBACK, level);
-    let p = Preferences.get(PREF_LOG_LEVEL, fallback);
-
-    switch (typeof p) {
-      // Gecko >= 46
-      case "string":
-        let s = p.toLowerCase();
-        if (LOG_LEVELS.has(s)) {
-          level = LOG_LEVELS.get(s);
-        }
-        break;
-
-      // Gecko <= 45
-      case "boolean":
-        if (p) {
-          level = Log.Level.Trace;
-        }
-        break;
-    }
-
-    return level;
+    let s = getPref(PREF_LOG_LEVEL, PREF_LOG_LEVEL_FALLBACK);
+    return LOG_LEVELS.get(s);
   },
 
   get forceLocal () {
-    let fallback = Preferences.get(PREF_FORCE_LOCAL_FALLBACK, true);
-    return Preferences.get(PREF_FORCE_LOCAL, fallback);
+    return getPref(PREF_FORCE_LOCAL, PREF_FORCE_LOCAL_FALLBACK);
   },
 
   readFromEnvironment (key) {
     const env = Cc["@mozilla.org/process/environment;1"]
         .getService(Ci.nsIEnvironment);
 
     if (env.exists(key)) {
       let prefs;