Point other profiles to new data dir after migration (+ code reorg)

Look for other profiles, from both apps (Firefox and Standalone), that
point to the data directory being migrated and update prefs.js in those
profiles to point to the new location.

Also reorganize code into Zotero.Profile and Zotero.DataDirectory
namespaces
This commit is contained in:
Dan Stillman 2016-11-26 22:41:26 -05:00
parent c0bf2b91f6
commit 4c0abb6816
13 changed files with 1338 additions and 919 deletions

View file

@ -474,7 +474,6 @@ function getTestDataUrl(path) {
/**
* Returns an absolute path to an empty temporary directory
* (i.e., test/tests/data)
*/
var getTempDirectory = Zotero.Promise.coroutine(function* getTempDirectory() {
Components.utils.import("resource://gre/modules/osfile.jsm");

View file

@ -1,6 +1,6 @@
"use strict";
describe("Zotero Core Functions", function () {
describe("Zotero.DataDirectory", function () {
var tmpDir, oldDir, newDir, dbFilename, oldDBFile, newDBFile, oldStorageDir, newStorageDir,
oldTranslatorsDir, newTranslatorsDir, translatorName1, translatorName2,
oldStorageDir1, newStorageDir1, storageFile1, oldStorageDir2, newStorageDir2, storageFile2,
@ -12,7 +12,7 @@ describe("Zotero Core Functions", function () {
tmpDir = yield getTempDirectory();
oldDir = OS.Path.join(tmpDir, "old");
newDir = OS.Path.join(tmpDir, "new");
dbFilename = Zotero.getDatabaseFilename();
dbFilename = Zotero.DataDirectory.getDatabaseFilename();
oldDBFile = OS.Path.join(oldDir, dbFilename);
newDBFile = OS.Path.join(newDir, dbFilename);
oldStorageDir = OS.Path.join(oldDir, "storage");
@ -33,24 +33,23 @@ describe("Zotero Core Functions", function () {
str4 = '4';
str5 = '5';
str6 = '6';
oldMigrationMarker = OS.Path.join(oldDir, Zotero.DATA_DIR_MIGRATION_MARKER);
newMigrationMarker = OS.Path.join(newDir, Zotero.DATA_DIR_MIGRATION_MARKER);
oldMigrationMarker = OS.Path.join(oldDir, Zotero.DataDirectory.MIGRATION_MARKER);
newMigrationMarker = OS.Path.join(newDir, Zotero.DataDirectory.MIGRATION_MARKER);
stubs.canMigrate = sinon.stub(Zotero, "canMigrateDataDirectory").returns(true);
stubs.canMigrate = sinon.stub(Zotero.DataDirectory, "canMigrate").returns(true);
// A pipe always exists during tests, since Zotero is running
stubs.pipeExists = sinon.stub(Zotero.IPC, "pipeExists").returns(Zotero.Promise.resolve(false));
});
beforeEach(function* () {
// Trigger a call to setDataDirectory() now to avoid affecting the stub call count
Zotero.getZoteroDirectory();
stubs.setDataDir = sinon.stub(Zotero, "setDataDirectory");
stubs.setDataDir = sinon.stub(Zotero.DataDirectory, "set");
});
afterEach(function* () {
yield removeDir(oldDir);
yield removeDir(newDir);
Zotero._cacheDataDirectory(false);
Zotero.DataDirectory._cache(false);
yield Zotero.DataDirectory.init();
stubs.setDataDir.restore();
});
@ -83,7 +82,7 @@ describe("Zotero Core Functions", function () {
let storageDir1 = OS.Path.join(storageDir, 'AAAAAAAA');
let storageDir2 = OS.Path.join(storageDir, 'BBBBBBBB');
let translatorsDir = OS.Path.join(dir, 'translators');
let migrationMarker = OS.Path.join(dir, Zotero.DATA_DIR_MIGRATION_MARKER);
let migrationMarker = OS.Path.join(dir, Zotero.DataDirectory.MIGRATION_MARKER);
// Database
yield Zotero.File.putContentsAsync(OS.Path.join(dir, dbFilename), str1);
@ -141,7 +140,7 @@ describe("Zotero Core Functions", function () {
});
describe("#checkForDataDirectoryMigration()", function () {
describe("#checkForMigration()", function () {
let fileMoveStub;
before(function () {
@ -198,7 +197,7 @@ describe("Zotero Core Functions", function () {
)
);
});
yield Zotero.checkForDataDirectoryMigration(oldDir, newDir);
yield Zotero.DataDirectory.checkForMigration(oldDir, newDir);
yield promise;
yield promise2;
@ -240,7 +239,7 @@ describe("Zotero Core Functions", function () {
)
);
});
yield Zotero.checkForDataDirectoryMigration(oldDir, newDir);
yield Zotero.DataDirectory.checkForMigration(oldDir, newDir);
yield promise;
assert.isTrue(stub2.calledOnce);
@ -267,7 +266,7 @@ describe("Zotero Core Functions", function () {
it("should remove marker if old directory doesn't exist", function* () {
yield populateDataDirectory(newDir, oldDir);
yield Zotero.checkForDataDirectoryMigration(newDir, newDir);
yield Zotero.DataDirectory.checkForMigration(newDir, newDir);
yield checkMigration({
skipSetDataDirectory: true
});
@ -275,7 +274,7 @@ describe("Zotero Core Functions", function () {
});
describe("#migrateDataDirectory()", function () {
describe("#migrate()", function () {
// Define tests and store for running in non-mv mode
var tests = [];
function add(desc, fn) {
@ -285,7 +284,7 @@ describe("Zotero Core Functions", function () {
add("should move all files and folders", function* () {
yield populateDataDirectory(oldDir);
yield Zotero.migrateDataDirectory(oldDir, newDir);
yield Zotero.DataDirectory.migrate(oldDir, newDir);
yield checkMigration();
});
@ -295,7 +294,7 @@ describe("Zotero Core Functions", function () {
yield OS.File.copy(oldMigrationMarker, newMigrationMarker);
yield Zotero.migrateDataDirectory(oldDir, newDir, true);
yield Zotero.DataDirectory.migrate(oldDir, newDir, true);
yield checkMigration();
});
@ -306,7 +305,7 @@ describe("Zotero Core Functions", function () {
yield OS.File.copy(oldMigrationMarker, newMigrationMarker);
yield OS.File.move(OS.Path.join(oldDir, dbFilename), OS.Path.join(newDir, dbFilename));
yield Zotero.migrateDataDirectory(oldDir, newDir, true);
yield Zotero.DataDirectory.migrate(oldDir, newDir, true);
yield checkMigration();
});
@ -322,7 +321,7 @@ describe("Zotero Core Functions", function () {
yield removeDir(newTranslatorsDir);
yield removeDir(newStorageDir2);
yield Zotero.migrateDataDirectory(oldDir, newDir, true);
yield Zotero.DataDirectory.migrate(oldDir, newDir, true);
yield checkMigration();
});
@ -331,7 +330,7 @@ describe("Zotero Core Functions", function () {
yield OS.File.makeDir(newDir, { unixMode: 0o755 });
yield Zotero.File.putContentsAsync(OS.Path.join(newDir, 'existing'), '');
yield Zotero.migrateDataDirectory(oldDir, newDir);
yield Zotero.DataDirectory.migrate(oldDir, newDir);
yield checkMigration();
assert.isTrue(yield OS.File.exists(OS.Path.join(newDir + "-1", 'existing')));
@ -374,7 +373,7 @@ describe("Zotero Core Functions", function () {
}
});
yield Zotero.migrateDataDirectory(oldDir, newDir);
yield Zotero.DataDirectory.migrate(oldDir, newDir);
stub1.restore();

190
test/tests/profileTest.js Normal file
View file

@ -0,0 +1,190 @@
"use strict";
describe("Zotero.Profile", function () {
var tmpDir;
var profile1 = "ht79g2qb.Test1";
var profile2 = "b9auumgf.Test2";
var profile3 = "7sgqhns3.Test3";
beforeEach(function* () {
tmpDir = yield getTempDirectory();
var contents = `[General]
StartWithLastProfile=0
[Profile0]
Name=Test 1
IsRelative=1
Path=Profiles/${profile1}
Default=1
[Profile1]
Name=Test 2
IsRelative=1
Path=Profiles/${profile2}
[Profile1]
Name=Test 3
IsRelative=1
Path=Profiles/${profile3}
`;
yield Zotero.File.putContentsAsync(OS.Path.join(tmpDir, "profiles.ini"), contents);
yield OS.File.makeDir(
OS.Path.join(tmpDir, "Profiles", profile1),
{
unixMode: 0o755,
from: tmpDir
}
);
yield OS.File.makeDir(OS.Path.join(tmpDir, "Profiles", profile2), { unixMode: 0o755 });
yield OS.File.makeDir(OS.Path.join(tmpDir, "Profiles", profile3), { unixMode: 0o755 });
});
describe("#getDefaultInProfilesDir()", function () {
it("should parse a profiles.ini file", function* () {
var [dir, multiple] = yield Zotero.Profile.getDefaultInProfilesDir(tmpDir);
assert.equal(dir, OS.Path.join(tmpDir, "Profiles", profile1));
});
});
describe("#findOtherProfilesUsingDataDirectory()", function () {
it("should find profile with directory as a custom location", function* () {
let dataDir = Zotero.DataDirectory.dir;
let contents1 = `user_pref("extensions.lastAppVersion", "49.0");
user_pref("extensions.shownSelectionUI", true);
user_pref("extensions.ui.locale.hidden", true);
user_pref("loop.copy.ticket", 196);
`;
let contents2 = `user_pref("extensions.lastAppVersion", "50.0");
user_pref("extensions.shownSelectionUI", true);
user_pref("extensions.zotero.dataDir", "${dataDir}");
user_pref("extensions.zotero.useDataDir", true);
user_pref("extensions.ui.locale.hidden", true);
user_pref("loop.copy.ticket", 196);
`;
let otherDir = OS.Path.join(OS.Path.dirname(dataDir), "Other");
let contents3 = `user_pref("extensions.lastAppVersion", "51.0");
user_pref("extensions.shownSelectionUI", true);
user_pref("extensions.zotero.dataDir", "${otherDir}");
user_pref("extensions.zotero.useDataDir", true);
user_pref("extensions.ui.locale.hidden", true);
user_pref("loop.copy.ticket", 196);
`;
let prefsFile1 = OS.Path.join(tmpDir, "Profiles", profile1, "prefs.js");
let prefsFile2 = OS.Path.join(tmpDir, "Profiles", profile2, "prefs.js");
let prefsFile3 = OS.Path.join(tmpDir, "Profiles", profile3, "prefs.js");
yield Zotero.File.putContentsAsync(prefsFile1, contents1);
yield Zotero.File.putContentsAsync(prefsFile2, contents2);
yield Zotero.File.putContentsAsync(prefsFile3, contents3);
var stub = sinon.stub(Zotero.Profile, "getOtherAppProfilesDir")
.returns(OS.Path.join(tmpDir, "Profiles"));
var dirs = yield Zotero.Profile.findOtherProfilesUsingDataDirectory(dataDir);
stub.restore();
assert.sameMembers(dirs, [OS.Path.join(tmpDir, "Profiles", profile2)]);
assert.lengthOf(dirs, 1);
});
it("should find other-app profile with directory as a legacy default location", function* () {
let contents1 = `user_pref("extensions.lastAppVersion", "49.0");
user_pref("extensions.shownSelectionUI", true);
user_pref("extensions.ui.locale.hidden", true);
user_pref("loop.copy.ticket", 196);
`;
let contents2 = `user_pref("extensions.lastAppVersion", "50.0");
user_pref("extensions.shownSelectionUI", true);
user_pref("extensions.ui.locale.hidden", true);
user_pref("loop.copy.ticket", 196);
`;
let prefsFile1 = OS.Path.join(tmpDir, "Profiles", profile1, "prefs.js");
let prefsFile2 = OS.Path.join(tmpDir, "Profiles", profile2, "prefs.js");
yield Zotero.File.putContentsAsync(prefsFile1, contents1);
yield Zotero.File.putContentsAsync(prefsFile2, contents2);
var stub = sinon.stub(Zotero.Profile, "getOtherAppProfilesDir")
.returns(OS.Path.join(tmpDir, "Profiles"));
Components.utils.import("resource://zotero/config.js");
var dirs = yield Zotero.Profile.findOtherProfilesUsingDataDirectory(
OS.Path.join(OS.Path.dirname(prefsFile1), Zotero.DataDirectory.legacyDirName)
);
stub.restore();
assert.sameMembers(dirs, [OS.Path.join(tmpDir, "Profiles", profile1)]);
assert.lengthOf(dirs, 1);
});
});
describe("#updateProfileDataDirectory()", function () {
it("should add new lines to prefs.js", function* () {
let prefsFile = OS.Path.join(tmpDir, "Profiles", profile1, "prefs.js");
let oldDir = OS.Path.join(OS.Path.dirname(tmpDir), "Old", "Zotero");
let newDir = OS.Path.join(OS.Path.dirname(tmpDir), "New", "Zotero");
let contents = `user_pref("extensions.lastAppVersion", "50.0");
user_pref("extensions.shownSelectionUI", true);
user_pref("extensions.ui.locale.hidden", true);
user_pref("loop.copy.ticket", 196);
`;
let addition = `user_pref("extensions.zotero.dataDir", "${newDir}");
user_pref("extensions.zotero.lastDataDir", "${newDir}");
user_pref("extensions.zotero.useDataDir", true);
`;
yield Zotero.File.putContentsAsync(prefsFile, contents);
yield Zotero.Profile.updateProfileDataDirectory(
OS.Path.join(tmpDir, "Profiles", profile1),
oldDir,
newDir
);
let newContents = yield Zotero.File.getContentsAsync(prefsFile);
assert.equal(newContents, contents + addition);
});
it("should replace existing lines in prefs.js", function* () {
let prefsFile = OS.Path.join(tmpDir, "Profiles", profile1, "prefs.js");
let oldDir = OS.Path.join(OS.Path.dirname(tmpDir), "Old", "Zotero");
let newDir = OS.Path.join(OS.Path.dirname(tmpDir), "New", "Zotero");
let contents = `user_pref("extensions.lastAppVersion", "50.0");
user_pref("extensions.shownSelectionUI", true);
user_pref("extensions.zotero.dataDir", "old-mac-persistent-descriptor");
user_pref("extensions.zotero.lastDataDir", "${oldDir}");
user_pref("extensions.zotero.useDataDir", true);
user_pref("extensions.ui.locale.hidden", true);
user_pref("loop.copy.ticket", 196);
`;
let expectedContents = `user_pref("extensions.lastAppVersion", "50.0");
user_pref("extensions.shownSelectionUI", true);
user_pref("extensions.ui.locale.hidden", true);
user_pref("loop.copy.ticket", 196);
user_pref("extensions.zotero.dataDir", "${newDir}");
user_pref("extensions.zotero.lastDataDir", "${newDir}");
user_pref("extensions.zotero.useDataDir", true);
`;
yield Zotero.File.putContentsAsync(prefsFile, contents);
yield Zotero.Profile.updateProfileDataDirectory(
OS.Path.join(tmpDir, "Profiles", profile1),
oldDir,
newDir
);
let newContents = yield Zotero.File.getContentsAsync(prefsFile);
assert.equal(newContents, expectedContents);
});
});
});

View file

@ -79,7 +79,7 @@ describe("Zotero.Sync.Data.Local", function() {
// extra1 functionality not used at the moment
it.skip("should prompt for data reset and allow to choose a new data directory", function* (){
sinon.stub(Zotero, 'forceNewDataDirectory').returns(true);
sinon.stub(Zotero.DataDirectory, 'forceChange').returns(true);
yield Zotero.Users.setCurrentUserID(1);
yield Zotero.Users.setCurrentUsername("A");
@ -88,10 +88,10 @@ describe("Zotero.Sync.Data.Local", function() {
var cont = yield Zotero.Sync.Data.Local.checkUser(window, 2, "B");
var resetDataDirFileExists = yield OS.File.exists(resetDataDirFile);
assert.isTrue(cont);
assert.isTrue(Zotero.forceNewDataDirectory.called);
assert.isTrue(Zotero.DataDirectory.forceChange.called);
assert.isFalse(resetDataDirFileExists);
Zotero.forceNewDataDirectory.restore();
Zotero.DataDirectory.forceChange.restore();
});
});