profileTest.js (6688B)
1 "use strict"; 2 3 describe("Zotero.Profile", function () { 4 var tmpDir; 5 var profile1 = "ht79g2qb.Test1"; 6 var profile2 = "b9auumgf.Test2"; 7 var profile3 = "7sgqhns3.Test3"; 8 9 beforeEach(function* () { 10 tmpDir = yield getTempDirectory(); 11 var contents = `[General] 12 StartWithLastProfile=0 13 14 [Profile0] 15 Name=Test 1 16 IsRelative=1 17 Path=Profiles/${profile1} 18 Default=1 19 20 [Profile1] 21 Name=Test 2 22 IsRelative=1 23 Path=Profiles/${profile2} 24 25 [Profile1] 26 Name=Test 3 27 IsRelative=1 28 Path=Profiles/${profile3} 29 `; 30 yield Zotero.File.putContentsAsync(OS.Path.join(tmpDir, "profiles.ini"), contents); 31 yield OS.File.makeDir( 32 OS.Path.join(tmpDir, "Profiles", profile1), 33 { 34 unixMode: 0o755, 35 from: tmpDir 36 } 37 ); 38 yield OS.File.makeDir(OS.Path.join(tmpDir, "Profiles", profile2), { unixMode: 0o755 }); 39 yield OS.File.makeDir(OS.Path.join(tmpDir, "Profiles", profile3), { unixMode: 0o755 }); 40 }); 41 42 43 describe("#getDefaultInProfilesDir()", function () { 44 it("should parse a profiles.ini file", function* () { 45 var [dir, multiple] = yield Zotero.Profile.getDefaultInProfilesDir(tmpDir); 46 assert.equal(dir, OS.Path.join(tmpDir, "Profiles", profile1)); 47 }); 48 }); 49 50 51 describe("#findOtherProfilesUsingDataDirectory()", function () { 52 it("should find profile with directory as a custom location", function* () { 53 let dataDir = Zotero.DataDirectory.dir; 54 let contents1 = `user_pref("extensions.lastAppVersion", "49.0"); 55 user_pref("extensions.shownSelectionUI", true); 56 user_pref("extensions.ui.locale.hidden", true); 57 user_pref("loop.copy.ticket", 196); 58 `; 59 let contents2 = `user_pref("extensions.lastAppVersion", "50.0"); 60 user_pref("extensions.shownSelectionUI", true); 61 user_pref("extensions.zotero.dataDir", "${dataDir}"); 62 user_pref("extensions.zotero.useDataDir", true); 63 user_pref("extensions.ui.locale.hidden", true); 64 user_pref("loop.copy.ticket", 196); 65 `; 66 let otherDir = OS.Path.join(OS.Path.dirname(dataDir), "Other"); 67 let contents3 = `user_pref("extensions.lastAppVersion", "51.0"); 68 user_pref("extensions.shownSelectionUI", true); 69 user_pref("extensions.zotero.dataDir", "${otherDir}"); 70 user_pref("extensions.zotero.useDataDir", true); 71 user_pref("extensions.ui.locale.hidden", true); 72 user_pref("loop.copy.ticket", 196); 73 `; 74 75 let prefsFile1 = OS.Path.join(tmpDir, "Profiles", profile1, "prefs.js"); 76 let prefsFile2 = OS.Path.join(tmpDir, "Profiles", profile2, "prefs.js"); 77 let prefsFile3 = OS.Path.join(tmpDir, "Profiles", profile3, "prefs.js"); 78 yield Zotero.File.putContentsAsync(prefsFile1, contents1); 79 yield Zotero.File.putContentsAsync(prefsFile2, contents2); 80 yield Zotero.File.putContentsAsync(prefsFile3, contents3); 81 82 var stub = sinon.stub(Zotero.Profile, "getOtherAppProfilesDir") 83 .returns(OS.Path.join(tmpDir, "Profiles")); 84 85 var dirs = yield Zotero.Profile.findOtherProfilesUsingDataDirectory(dataDir); 86 87 stub.restore(); 88 89 assert.sameMembers(dirs, [OS.Path.join(tmpDir, "Profiles", profile2)]); 90 assert.lengthOf(dirs, 1); 91 }); 92 93 94 it("should find other-app profile with directory as a legacy default location", function* () { 95 let contents1 = `user_pref("extensions.lastAppVersion", "49.0"); 96 user_pref("extensions.shownSelectionUI", true); 97 user_pref("extensions.ui.locale.hidden", true); 98 user_pref("loop.copy.ticket", 196); 99 `; 100 let contents2 = `user_pref("extensions.lastAppVersion", "50.0"); 101 user_pref("extensions.shownSelectionUI", true); 102 user_pref("extensions.ui.locale.hidden", true); 103 user_pref("loop.copy.ticket", 196); 104 `; 105 106 let prefsFile1 = OS.Path.join(tmpDir, "Profiles", profile1, "prefs.js"); 107 let prefsFile2 = OS.Path.join(tmpDir, "Profiles", profile2, "prefs.js"); 108 yield Zotero.File.putContentsAsync(prefsFile1, contents1); 109 yield Zotero.File.putContentsAsync(prefsFile2, contents2); 110 111 var stub = sinon.stub(Zotero.Profile, "getOtherAppProfilesDir") 112 .returns(OS.Path.join(tmpDir, "Profiles")); 113 114 Components.utils.import("resource://zotero/config.js"); 115 var dirs = yield Zotero.Profile.findOtherProfilesUsingDataDirectory( 116 OS.Path.join(OS.Path.dirname(prefsFile1), Zotero.DataDirectory.legacyDirName) 117 ); 118 119 stub.restore(); 120 121 assert.sameMembers(dirs, [OS.Path.join(tmpDir, "Profiles", profile1)]); 122 assert.lengthOf(dirs, 1); 123 }); 124 }); 125 126 127 describe("#updateProfileDataDirectory()", function () { 128 it("should add new lines to prefs.js", function* () { 129 let prefsFile = OS.Path.join(tmpDir, "Profiles", profile1, "prefs.js"); 130 let oldDir = OS.Path.join(OS.Path.dirname(tmpDir), "Old", "Zotero"); 131 let newDir = OS.Path.join(OS.Path.dirname(tmpDir), "New", "Zotero"); 132 133 let contents = `user_pref("extensions.lastAppVersion", "50.0"); 134 user_pref("extensions.shownSelectionUI", true); 135 user_pref("extensions.ui.locale.hidden", true); 136 user_pref("loop.copy.ticket", 196); 137 `; 138 let addition = `user_pref("extensions.zotero.dataDir", "${newDir}"); 139 user_pref("extensions.zotero.lastDataDir", "${newDir}"); 140 user_pref("extensions.zotero.useDataDir", true); 141 `; 142 143 yield Zotero.File.putContentsAsync(prefsFile, contents); 144 yield Zotero.Profile.updateProfileDataDirectory( 145 OS.Path.join(tmpDir, "Profiles", profile1), 146 oldDir, 147 newDir 148 ); 149 150 let newContents = yield Zotero.File.getContentsAsync(prefsFile); 151 152 assert.equal(newContents, contents + addition); 153 }); 154 155 156 it("should replace existing lines in prefs.js", function* () { 157 let prefsFile = OS.Path.join(tmpDir, "Profiles", profile1, "prefs.js"); 158 let oldDir = OS.Path.join(OS.Path.dirname(tmpDir), "Old", "Zotero"); 159 let newDir = OS.Path.join(OS.Path.dirname(tmpDir), "New", "Zotero"); 160 161 let contents = `user_pref("extensions.lastAppVersion", "50.0"); 162 user_pref("extensions.shownSelectionUI", true); 163 user_pref("extensions.zotero.dataDir", "old-mac-persistent-descriptor"); 164 user_pref("extensions.zotero.lastDataDir", "${oldDir}"); 165 user_pref("extensions.zotero.useDataDir", true); 166 user_pref("extensions.ui.locale.hidden", true); 167 user_pref("loop.copy.ticket", 196); 168 `; 169 let expectedContents = `user_pref("extensions.lastAppVersion", "50.0"); 170 user_pref("extensions.shownSelectionUI", true); 171 user_pref("extensions.ui.locale.hidden", true); 172 user_pref("loop.copy.ticket", 196); 173 user_pref("extensions.zotero.dataDir", "${newDir}"); 174 user_pref("extensions.zotero.lastDataDir", "${newDir}"); 175 user_pref("extensions.zotero.useDataDir", true); 176 `; 177 178 yield Zotero.File.putContentsAsync(prefsFile, contents); 179 yield Zotero.Profile.updateProfileDataDirectory( 180 OS.Path.join(tmpDir, "Profiles", profile1), 181 oldDir, 182 newDir 183 ); 184 185 let newContents = yield Zotero.File.getContentsAsync(prefsFile); 186 187 assert.equal(newContents, expectedContents); 188 }); 189 }); 190 });