Bug 1564083: The profile command line argument should match a profile with the same root directory and use its local directory. r=froydnj
authorDave Townsend <dtownsend@oxymoronical.com>
Mon, 18 Nov 2019 19:27:51 +0000
changeset 502458 65114b5cb9ee40f8f8a168e56b090ddad54f9465
parent 502457 3ec96c0dd69655f71c67e434e589fe745dc05200
child 502459 b39ba65e2071c2e8f84b166ad367f7fd8c40f7d4
push id114172
push userdluca@mozilla.com
push dateTue, 19 Nov 2019 11:31:10 +0000
treeherdermozilla-inbound@b5c5ba07d3db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1564083
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1564083: The profile command line argument should match a profile with the same root directory and use its local directory. r=froydnj Differential Revision: https://phabricator.services.mozilla.com/D52736
toolkit/profile/nsToolkitProfileService.cpp
toolkit/profile/xpcshell/test_select_profile_argument.js
toolkit/profile/xpcshell/test_select_profile_argument_new.js
toolkit/profile/xpcshell/xpcshell.ini
--- a/toolkit/profile/nsToolkitProfileService.cpp
+++ b/toolkit/profile/nsToolkitProfileService.cpp
@@ -1305,21 +1305,26 @@ nsresult nsToolkitProfileService::Select
             PR_STDERR,
             "Error: argument --profile requires a path to a directory\n");
         return NS_ERROR_FAILURE;
       }
     }
 
     mStartupReason = NS_LITERAL_STRING("argument-profile");
 
-    // If a profile path is specified directly on the command line, then
-    // assume that the temp directory is the same as the given directory.
-    GetProfileByDir(lf, lf, getter_AddRefs(mCurrent));
+    GetProfileByDir(lf, nullptr, getter_AddRefs(mCurrent));
     NS_ADDREF(*aRootDir = lf);
-    lf.forget(aLocalDir);
+    // If the root dir matched a profile then use its local dir, otherwise use
+    // the root dir as the local dir.
+    if (mCurrent) {
+      mCurrent->GetLocalDir(aLocalDir);
+    } else {
+      lf.forget(aLocalDir);
+    }
+
     NS_IF_ADDREF(*aProfile = mCurrent);
     return NS_OK;
   }
 
   // Check the -createprofile command line argument. It accepts a single
   // argument that is either the name for the new profile or the name followed
   // by the path to use.
   ar = CheckArg(*aArgc, aArgv, "createprofile", &arg, CheckArgFlag::RemoveArg);
@@ -1654,19 +1659,26 @@ nsToolkitProfileService::GetProfileByNam
  */
 void nsToolkitProfileService::GetProfileByDir(nsIFile* aRootDir,
                                               nsIFile* aLocalDir,
                                               nsIToolkitProfile** aResult) {
   for (RefPtr<nsToolkitProfile> profile : mProfiles) {
     bool equal;
     nsresult rv = profile->mRootDir->Equals(aRootDir, &equal);
     if (NS_SUCCEEDED(rv) && equal) {
+      if (!aLocalDir) {
+        // If no local directory was given then we will just use the normal
+        // local directory for the profile.
+        profile.forget(aResult);
+        return;
+      }
+
       rv = profile->mLocalDir->Equals(aLocalDir, &equal);
       if (NS_SUCCEEDED(rv) && equal) {
-        NS_ADDREF(*aResult = profile);
+        profile.forget(aResult);
         return;
       }
     }
   }
 }
 
 nsresult NS_LockProfilePath(nsIFile* aPath, nsIFile* aTempPath,
                             nsIProfileUnlocker** aUnlocker,
new file mode 100644
--- /dev/null
+++ b/toolkit/profile/xpcshell/test_select_profile_argument.js
@@ -0,0 +1,50 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/*
+ * Tests that selecting a profile directory with the "profile" argument finds
+ * the matching profile.
+ */
+
+add_task(async () => {
+  let root = makeRandomProfileDir("foo");
+  let local = gDataHomeLocal.clone();
+  local.append("foo");
+
+  let profileData = {
+    options: {
+      startWithLastProfile: true,
+    },
+    profiles: [
+      {
+        name: "Profile1",
+        path: root.leafName,
+      },
+    ],
+  };
+
+  writeProfilesIni(profileData);
+  checkProfileService(profileData);
+
+  let { rootDir, localDir, profile, didCreate } = selectStartupProfile([
+    "-profile",
+    root.path,
+  ]);
+  checkStartupReason("argument-profile");
+
+  Assert.ok(!didCreate, "Should not have created a new profile.");
+  Assert.ok(rootDir.equals(root), "Should have selected the right root dir.");
+  Assert.ok(
+    localDir.equals(local),
+    "Should have selected the right local dir."
+  );
+  Assert.ok(profile, "A named profile matches this.");
+  Assert.equal(profile.name, "Profile1", "The right profile was matched.");
+
+  let service = getProfileService();
+  Assert.notEqual(
+    service.defaultProfile,
+    profile,
+    "Should not be the default profile."
+  );
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/profile/xpcshell/test_select_profile_argument_new.js
@@ -0,0 +1,43 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/*
+ * Tests that selecting a profile directory with the "profile" argument finds
+ * doesn't match the incorrect profile.
+ */
+
+add_task(async () => {
+  let root = makeRandomProfileDir("foo");
+  let local = gDataHomeLocal.clone();
+  local.append("foo");
+  let empty = makeRandomProfileDir("empty");
+
+  let profileData = {
+    options: {
+      startWithLastProfile: true,
+    },
+    profiles: [
+      {
+        name: "Profile1",
+        path: root.leafName,
+      },
+    ],
+  };
+
+  writeProfilesIni(profileData);
+  checkProfileService(profileData);
+
+  let { rootDir, localDir, profile, didCreate } = selectStartupProfile([
+    "-profile",
+    empty.path,
+  ]);
+  checkStartupReason("argument-profile");
+
+  Assert.ok(!didCreate, "Should not have created a new profile.");
+  Assert.ok(rootDir.equals(empty), "Should have selected the right root dir.");
+  Assert.ok(
+    localDir.equals(empty),
+    "Should have selected the right local dir."
+  );
+  Assert.ok(!profile, "No named profile matches this.");
+});
--- a/toolkit/profile/xpcshell/xpcshell.ini
+++ b/toolkit/profile/xpcshell/xpcshell.ini
@@ -37,8 +37,10 @@ skip-if = devedition
 [test_remove.js]
 [test_conflict_profiles.js]
 [test_conflict_installs.js]
 [test_invalid_descriptor.js]
 [test_legacy_empty.js]
 [test_legacy_select.js]
 [test_fix_directory_case.js]
 [test_ignore_legacy_directory.js]
+[test_select_profile_argument.js]
+[test_select_profile_argument_new.js]