Add getTempDirectory (async) method to support.js

Returns a promise for a path to a new temporary directory
This commit is contained in:
Aurimas Vinckevicius 2015-04-26 01:31:03 -05:00
parent 2ebce91ecf
commit 409553da2e
2 changed files with 24 additions and 1 deletions

View file

@ -1,5 +1,4 @@
Components.utils.import("resource://gre/modules/osfile.jsm");
Components.utils.import("resource://zotero/q.js");
var EventUtils = Components.utils.import("resource://zotero-unit/EventUtils.jsm");
var ZoteroUnit = Components.classes["@mozilla.org/commandlinehandler/general-startup;1?type=zotero-unit"].

View file

@ -1,3 +1,5 @@
Components.utils.import("resource://zotero/q.js");
// Useful "constants"
var sqlDateTimeRe = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
var isoDateTimeRe = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/;
@ -161,6 +163,28 @@ function getTestDataDirectory() {
QueryInterface(Components.interfaces.nsIFileURL).file;
}
/**
* Returns an absolute path to an empty temporary directory
* (i.e., test/tests/data)
*/
var getTempDirectory = Q.async(function getTempDirectory() {
Components.utils.import("resource://gre/modules/osfile.jsm");
let path,
attempts = 3,
zoteroTmpDirPath = Zotero.getTempDirectory().path;
while (attempts--) {
path = OS.Path.join(zoteroTmpDirPath, Zotero.Utilities.randomString());
try {
yield OS.File.makeDir(path, { ignoreExisting: false });
break;
} catch (e) {
if (!attempts) throw e; // Throw on last attempt
}
}
Q.return(path);
});
/**
* Resets the Zotero DB and restarts Zotero. Returns a promise resolved
* when this finishes.