Bug 1350887 - Fall back to deprecated pref if it exists; r=whimboo draft
authorAndreas Tolfsen <ato@mozilla.com>
Mon, 27 Mar 2017 14:28:32 +0100
changeset 556067 bc16298be5cfba70f8687b7932586945053bb3c1
parent 556066 cb8d7ac746540d2f7ad50bf57dd27ab5be1761df
child 556068 f7b7752a227dadbfe91990a79c71b1da4d9e0311
push id52427
push userbmo:ato@mozilla.com
push dateWed, 05 Apr 2017 09:39:48 +0000
reviewerswhimboo
bugs1350887, 1344748
milestone55.0a1
Bug 1350887 - Fall back to deprecated pref if it exists; r=whimboo 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;