Fix 'false' filename after case-only rename on Macs

Zotero.File.move() now forces `overwrite` if the old and new filenames
differ only by case, since otherwise on a case-insensitive filesystem
OS.File.move() does an existence check and thinks that the target file
exists.
This commit is contained in:
Dan Stillman 2020-04-22 00:01:28 -04:00
parent 595ba396ef
commit 81e2bee452
3 changed files with 35 additions and 2 deletions

View file

@ -473,7 +473,8 @@ Zotero.File = new function(){
* @param {Object} [options]
* @param {Boolean} [options.overwrite=false] - Overwrite file if one exists
* @param {Boolean} [options.unique=false] - Add suffix to create unique filename if necessary
* @return {String|false} - New filename, or false if destination file exists and `overwrite` not set
* @return {String|false} - New filename, or false if destination file exists and `overwrite`
* and `unique` not set
*/
this.rename = async function (file, newName, options = {}) {
var overwrite = options.overwrite || false;
@ -489,6 +490,12 @@ Zotero.File = new function(){
return origName;
}
// If only the case changed, we need to overwrite so move() doesn't think the destination
// file already exists
if (origName.toLowerCase() === newName.toLowerCase()) {
overwrite = true;
}
var parentDir = OS.Path.dirname(origPath);
var destPath = OS.Path.join(parentDir, newName);
var destName = OS.Path.basename(destPath);

View file

@ -132,6 +132,17 @@ describe("Zotero.File", function () {
assert.isTrue(await OS.File.exists(destFile));
});
// Only relevant on a case-insensitive filesystem
it("should rename a file with a case-only change (Mac)", async function () {
var tmpDir = await getTempDirectory();
var sourceFile = OS.Path.join(tmpDir, 'a');
var destFile = OS.Path.join(tmpDir, 'A');
await Zotero.File.putContentsAsync(sourceFile, 'foo');
var newFilename = await Zotero.File.rename(sourceFile, 'A');
assert.equal(newFilename, 'A');
assert.equal(await Zotero.File.getContentsAsync(destFile), 'foo');
});
it("should overwrite an existing file if `overwrite` is true", async function () {
var tmpDir = await getTempDirectory();
var sourceFile = OS.Path.join(tmpDir, 'a');

View file

@ -985,7 +985,22 @@ describe("Zotero.Item", function () {
// DEBUG: Is this necessary?
assert.equal(item.attachmentSyncState, Zotero.Sync.Storage.Local.SYNC_STATE_TO_UPLOAD);
assert.isNull(item.attachmentSyncedHash);
})
});
// Only relevant on a case-insensitive filesystem
it("should rename an attached file with a case-only change (Mac)", async function () {
var file = getTestDataDirectory();
file.append('test.png');
var item = await Zotero.Attachments.importFromFile({
file: file
});
var newName = 'Test.png';
await item.renameAttachmentFile(newName);
assert.equal(item.attachmentFilename, newName);
var path = await item.getFilePathAsync();
assert.equal(OS.Path.basename(path), newName)
await OS.File.exists(path);
});
it("should rename a linked file", function* () {
var filename = 'test.png';