mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-24 21:31:04 +00:00
Bug 1411979 - Share the getTempFile function in xpcshell and browser tests. r=mak
MozReview-Commit-ID: 5hshgOrFqws --HG-- extra : rebase_source : c7b173f7c2685c9522ef0626b74819273352a373 extra : source : 68e85782bbcab3c06e729551643bdc602cf8de71
This commit is contained in:
parent
b8149c0ac2
commit
98b8fb39b7
@ -15,6 +15,7 @@ Cu.import("resource://gre/modules/Services.jsm");
|
||||
Cu.import("resource://gre/modules/NetUtil.jsm");
|
||||
Cu.import("resource://gre/modules/ObjectUtils.jsm");
|
||||
Cu.import("resource://gre/modules/FormLikeFactory.jsm");
|
||||
Cu.import("resource://testing-common/FileTestUtils.jsm");
|
||||
Cu.import("resource://testing-common/MockDocument.jsm");
|
||||
Cu.import("resource://testing-common/TestUtils.jsm");
|
||||
|
||||
@ -51,43 +52,10 @@ if (!extensionDir.exists()) {
|
||||
}
|
||||
Components.manager.addBootstrappedManifestLocation(extensionDir);
|
||||
|
||||
// While the previous test file should have deleted all the temporary files it
|
||||
// used, on Windows these might still be pending deletion on the physical file
|
||||
// system. Thus, start from a new base number every time, to make a collision
|
||||
// with a file that is still pending deletion highly unlikely.
|
||||
let gFileCounter = Math.floor(Math.random() * 1000000);
|
||||
|
||||
/**
|
||||
* Returns a reference to a temporary file, that is guaranteed not to exist, and
|
||||
* to have never been created before.
|
||||
*
|
||||
* @param {string} leafName
|
||||
* Suggested leaf name for the file to be created.
|
||||
*
|
||||
* @returns {nsIFile} pointing to a non-existent file in a temporary directory.
|
||||
*
|
||||
* @note It is not enough to delete the file if it exists, or to delete the file
|
||||
* after calling nsIFile.createUnique, because on Windows the delete
|
||||
* operation in the file system may still be pending, preventing a new
|
||||
* file with the same name to be created.
|
||||
*/
|
||||
// Returns a reference to a temporary file that is guaranteed not to exist and
|
||||
// is cleaned up later. See FileTestUtils.getTempFile for details.
|
||||
function getTempFile(leafName) {
|
||||
// Prepend a serial number to the extension in the suggested leaf name.
|
||||
let [base, ext] = DownloadPaths.splitBaseNameAndExtension(leafName);
|
||||
let finalLeafName = base + "-" + gFileCounter + ext;
|
||||
gFileCounter++;
|
||||
|
||||
// Get a file reference under the temporary directory for this test file.
|
||||
let file = FileUtils.getFile("TmpD", [finalLeafName]);
|
||||
do_check_false(file.exists());
|
||||
|
||||
do_register_cleanup(function() {
|
||||
if (file.exists()) {
|
||||
file.remove(false);
|
||||
}
|
||||
});
|
||||
|
||||
return file;
|
||||
return FileTestUtils.getTempFile(leafName);
|
||||
}
|
||||
|
||||
async function initProfileStorage(fileName, records, collectionName = "addresses") {
|
||||
|
@ -18,6 +18,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
|
||||
"resource://gre/modules/NetUtil.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "Services",
|
||||
"resource://gre/modules/Services.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "FileTestUtils",
|
||||
"resource://testing-common/FileTestUtils.jsm");
|
||||
|
||||
const BackgroundFileSaverOutputStream = Components.Constructor(
|
||||
"@mozilla.org/network/background-file-saver;1?mode=outputstream",
|
||||
@ -61,17 +63,11 @@ const TEST_DATA_LONG = new Array(1 + DESIRED_LENGTH / 256).join(TEST_256_CHARS);
|
||||
do_check_eq(TEST_DATA_LONG.length, DESIRED_LENGTH);
|
||||
|
||||
/**
|
||||
* Returns a reference to a temporary file. If the file is then created, it
|
||||
* will be removed when tests in this file finish.
|
||||
* Returns a reference to a temporary file that is guaranteed not to exist and
|
||||
* is cleaned up later. See FileTestUtils.getTempFile for details.
|
||||
*/
|
||||
function getTempFile(aLeafName) {
|
||||
let file = FileUtils.getFile("TmpD", [aLeafName]);
|
||||
do_register_cleanup(function GTF_cleanup() {
|
||||
if (file.exists()) {
|
||||
file.remove(false);
|
||||
}
|
||||
});
|
||||
return file;
|
||||
function getTempFile(leafName) {
|
||||
return FileTestUtils.getTempFile(leafName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,6 +17,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
|
||||
"resource://gre/modules/FileUtils.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
|
||||
"resource://gre/modules/NetUtil.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "FileTestUtils",
|
||||
"resource://testing-common/FileTestUtils.jsm");
|
||||
|
||||
const BackgroundFileSaverOutputStream = Components.Constructor(
|
||||
"@mozilla.org/network/background-file-saver;1?mode=outputstream",
|
||||
@ -30,17 +32,11 @@ const StringInputStream = Components.Constructor(
|
||||
const TEST_FILE_NAME_1 = "test-backgroundfilesaver-1.txt";
|
||||
|
||||
/**
|
||||
* Returns a reference to a temporary file. If the file is then created, it
|
||||
* will be removed when tests in this file finish.
|
||||
* Returns a reference to a temporary file that is guaranteed not to exist and
|
||||
* is cleaned up later. See FileTestUtils.getTempFile for details.
|
||||
*/
|
||||
function getTempFile(aLeafName) {
|
||||
let file = FileUtils.getFile("TmpD", [aLeafName]);
|
||||
do_register_cleanup(function GTF_cleanup() {
|
||||
if (file.exists()) {
|
||||
file.remove(false);
|
||||
}
|
||||
});
|
||||
return file;
|
||||
function getTempFile(leafName) {
|
||||
return FileTestUtils.getTempFile(leafName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
74
testing/modules/FileTestUtils.jsm
Normal file
74
testing/modules/FileTestUtils.jsm
Normal file
@ -0,0 +1,74 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Provides testing functions dealing with local files and their contents.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
this.EXPORTED_SYMBOLS = [
|
||||
"FileTestUtils",
|
||||
];
|
||||
|
||||
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
|
||||
|
||||
Cu.import("resource://gre/modules/AsyncShutdown.jsm", this);
|
||||
Cu.import("resource://gre/modules/DownloadPaths.jsm", this);
|
||||
Cu.import("resource://gre/modules/FileUtils.jsm", this);
|
||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
|
||||
Cu.import("resource://testing-common/Assert.jsm", this);
|
||||
|
||||
let gFileCounter = 1;
|
||||
|
||||
this.FileTestUtils = {
|
||||
/**
|
||||
* Returns a reference to a temporary file that is guaranteed not to exist and
|
||||
* to have never been created before. If a file or a directory with this name
|
||||
* is created by the test, it will be deleted when all tests terminate.
|
||||
*
|
||||
* @param suggestedName [optional]
|
||||
* Any extension on this template file name will be preserved. If this
|
||||
* is unspecified, the returned file name will have the generic ".dat"
|
||||
* extension, which may indicate either a binary or a text data file.
|
||||
*
|
||||
* @return nsIFile pointing to a non-existent file in a temporary directory.
|
||||
*
|
||||
* @note It is not enough to delete the file if it exists, or to delete the
|
||||
* file after calling nsIFile.createUnique, because on Windows the
|
||||
* delete operation in the file system may still be pending, preventing
|
||||
* a new file with the same name to be created.
|
||||
*/
|
||||
getTempFile(suggestedName = "test.dat") {
|
||||
// Prepend a serial number to the extension in the suggested leaf name.
|
||||
let [base, ext] = DownloadPaths.splitBaseNameAndExtension(suggestedName);
|
||||
let leafName = base + "-" + gFileCounter + ext;
|
||||
gFileCounter++;
|
||||
|
||||
// Get a file reference under the temporary directory for this test file.
|
||||
let file = this._globalTemporaryDirectory.clone();
|
||||
file.append(leafName);
|
||||
Assert.ok(!file.exists(), "Sanity check the temporary file doesn't exist.");
|
||||
return file;
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a reference to a global temporary directory that will be deleted
|
||||
* when all tests terminate.
|
||||
*/
|
||||
XPCOMUtils.defineLazyGetter(FileTestUtils, "_globalTemporaryDirectory", () => {
|
||||
// While previous test runs should have deleted their temporary directories,
|
||||
// on Windows they might still be pending deletion on the physical file
|
||||
// system. This makes a simple nsIFile.createUnique call unreliable, and we
|
||||
// have to use a random number to make a collision unlikely.
|
||||
let randomNumber = Math.floor(Math.random() * 1000000);
|
||||
let dir = FileUtils.getFile("TmpD", ["testdir-" + randomNumber]);
|
||||
dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
|
||||
AsyncShutdown.profileBeforeChange.addBlocker("Removing test files", () => {
|
||||
// Note that this may fail if any test leaves inaccessible files behind.
|
||||
dir.remove(true);
|
||||
});
|
||||
return dir;
|
||||
});
|
@ -2,13 +2,18 @@
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/*
|
||||
* This module implements a number of utilities useful for testing.
|
||||
/**
|
||||
* Contains a limited number of testing functions that are commonly used in a
|
||||
* wide variety of situations, for example waiting for an event loop tick or an
|
||||
* observer notification.
|
||||
*
|
||||
* Additions to this module should be generic and useful to testing multiple
|
||||
* features. Utilities only useful to a sepcific testing framework should live
|
||||
* in a module specific to that framework, such as BrowserTestUtils.jsm in the
|
||||
* case of mochitest-browser-chrome.
|
||||
* More complex functions are likely to belong to a separate test-only module.
|
||||
* Examples include Assert.jsm for generic assertions, FileTestUtils.jsm to work
|
||||
* with local files and their contents, and BrowserTestUtils.jsm to work with
|
||||
* browser windows and tabs.
|
||||
*
|
||||
* Individual components also offer testing functions to other components, for
|
||||
* example LoginTestUtils.jsm.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
@ -13,6 +13,7 @@ TESTING_JS_MODULES += [
|
||||
'AppInfo.jsm',
|
||||
'Assert.jsm',
|
||||
'CoverageUtils.jsm',
|
||||
'FileTestUtils.jsm',
|
||||
'MockRegistrar.jsm',
|
||||
'sinon-2.3.2.js',
|
||||
'StructuredLog.jsm',
|
||||
|
@ -30,48 +30,19 @@ XPCOMUtils.defineLazyModuleGetter(this, "HttpServer",
|
||||
"resource://testing-common/httpd.js");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "OS",
|
||||
"resource://gre/modules/osfile.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "FileTestUtils",
|
||||
"resource://testing-common/FileTestUtils.jsm");
|
||||
|
||||
const TEST_TARGET_FILE_NAME_PDF = "test-download.pdf";
|
||||
|
||||
// Support functions
|
||||
|
||||
// While the previous test file should have deleted all the temporary files it
|
||||
// used, on Windows these might still be pending deletion on the physical file
|
||||
// system. Thus, start from a new base number every time, to make a collision
|
||||
// with a file that is still pending deletion highly unlikely.
|
||||
var gFileCounter = Math.floor(Math.random() * 1000000);
|
||||
|
||||
/**
|
||||
* Returns a reference to a temporary file, that is guaranteed not to exist, and
|
||||
* to have never been created before.
|
||||
*
|
||||
* @param aLeafName
|
||||
* Suggested leaf name for the file to be created.
|
||||
*
|
||||
* @return nsIFile pointing to a non-existent file in a temporary directory.
|
||||
*
|
||||
* @note It is not enough to delete the file if it exists, or to delete the file
|
||||
* after calling nsIFile.createUnique, because on Windows the delete
|
||||
* operation in the file system may still be pending, preventing a new
|
||||
* file with the same name to be created.
|
||||
* Returns a reference to a temporary file that is guaranteed not to exist and
|
||||
* is cleaned up later. See FileTestUtils.getTempFile for details.
|
||||
*/
|
||||
function getTempFile(aLeafName) {
|
||||
// Prepend a serial number to the extension in the suggested leaf name.
|
||||
let [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName);
|
||||
let leafName = base + "-" + gFileCounter + ext;
|
||||
gFileCounter++;
|
||||
|
||||
// Get a file reference under the temporary directory for this test file.
|
||||
let file = FileUtils.getFile("TmpD", [leafName]);
|
||||
ok(!file.exists(), "Temp file does not exist");
|
||||
|
||||
registerCleanupFunction(function() {
|
||||
if (file.exists()) {
|
||||
file.remove(false);
|
||||
}
|
||||
});
|
||||
|
||||
return file;
|
||||
function getTempFile(leafName) {
|
||||
return FileTestUtils.getTempFile(leafName);
|
||||
}
|
||||
|
||||
function promiseBrowserLoaded(browser) {
|
||||
|
@ -37,6 +37,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "Services",
|
||||
"resource://gre/modules/Services.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "OS",
|
||||
"resource://gre/modules/osfile.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "FileTestUtils",
|
||||
"resource://testing-common/FileTestUtils.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "MockRegistrar",
|
||||
"resource://testing-common/MockRegistrar.jsm");
|
||||
|
||||
@ -101,54 +103,12 @@ function httpUrl(aFileName) {
|
||||
aFileName;
|
||||
}
|
||||
|
||||
// While the previous test file should have deleted all the temporary files it
|
||||
// used, on Windows these might still be pending deletion on the physical file
|
||||
// system. Thus, start from a new base number every time, to make a collision
|
||||
// with a file that is still pending deletion highly unlikely.
|
||||
var gFileCounter = Math.floor(Math.random() * 1000000);
|
||||
|
||||
/**
|
||||
* Returns a reference to a temporary file, that is guaranteed not to exist, and
|
||||
* to have never been created before.
|
||||
*
|
||||
* @param aLeafName
|
||||
* Suggested leaf name for the file to be created.
|
||||
*
|
||||
* @return nsIFile pointing to a non-existent file in a temporary directory.
|
||||
*
|
||||
* @note It is not enough to delete the file if it exists, or to delete the file
|
||||
* after calling nsIFile.createUnique, because on Windows the delete
|
||||
* operation in the file system may still be pending, preventing a new
|
||||
* file with the same name to be created.
|
||||
* Returns a reference to a temporary file that is guaranteed not to exist and
|
||||
* is cleaned up later. See FileTestUtils.getTempFile for details.
|
||||
*/
|
||||
function getTempFile(aLeafName) {
|
||||
// Prepend a serial number to the extension in the suggested leaf name.
|
||||
let [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName);
|
||||
let leafName = base + "-" + gFileCounter + ext;
|
||||
gFileCounter++;
|
||||
|
||||
// Get a file reference under the temporary directory for this test file.
|
||||
let file = FileUtils.getFile("TmpD", [leafName]);
|
||||
do_check_false(file.exists());
|
||||
|
||||
do_register_cleanup(function() {
|
||||
try {
|
||||
file.remove(false);
|
||||
} catch (e) {
|
||||
if (!(e instanceof Components.Exception &&
|
||||
(e.result == Cr.NS_ERROR_FILE_ACCESS_DENIED ||
|
||||
e.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST ||
|
||||
e.result == Cr.NS_ERROR_FILE_NOT_FOUND))) {
|
||||
throw e;
|
||||
}
|
||||
// On Windows, we may get an access denied error if the file existed before,
|
||||
// and was recently deleted.
|
||||
// Don't bother checking file.exists() as that may also cause an access
|
||||
// denied error.
|
||||
}
|
||||
});
|
||||
|
||||
return file;
|
||||
function getTempFile(leafName) {
|
||||
return FileTestUtils.getTempFile(leafName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,21 +7,6 @@
|
||||
|
||||
Cu.import("resource://gre/modules/AppConstants.jsm");
|
||||
|
||||
/**
|
||||
* Provides a temporary save directory.
|
||||
*
|
||||
* @returns nsIFile pointing to the new or existing directory.
|
||||
*/
|
||||
function createTemporarySaveDirectory() {
|
||||
var saveDir = Cc["@mozilla.org/file/directory_service;1"].
|
||||
getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
|
||||
saveDir.append("testsavedir");
|
||||
if (!saveDir.exists()) {
|
||||
saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
|
||||
}
|
||||
return saveDir;
|
||||
}
|
||||
|
||||
function testSanitize(leafName, expectedLeafName) {
|
||||
do_check_eq(DownloadPaths.sanitize(leafName), expectedLeafName);
|
||||
}
|
||||
@ -131,39 +116,36 @@ add_task(async function test_splitBaseNameAndExtension() {
|
||||
});
|
||||
|
||||
add_task(async function test_createNiceUniqueFile() {
|
||||
var destDir = createTemporarySaveDirectory();
|
||||
var destDir = FileTestUtils.getTempFile("destdir");
|
||||
destDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
|
||||
|
||||
// Single extension.
|
||||
var tempFile = destDir.clone();
|
||||
tempFile.append("test.txt");
|
||||
testCreateNiceUniqueFile(tempFile, "test.txt");
|
||||
testCreateNiceUniqueFile(tempFile, "test(1).txt");
|
||||
testCreateNiceUniqueFile(tempFile, "test(2).txt");
|
||||
|
||||
// Double extension.
|
||||
tempFile.leafName = "test.tar.gz";
|
||||
testCreateNiceUniqueFile(tempFile, "test.tar.gz");
|
||||
testCreateNiceUniqueFile(tempFile, "test(1).tar.gz");
|
||||
testCreateNiceUniqueFile(tempFile, "test(2).tar.gz");
|
||||
|
||||
// Test automatic shortening of long file names. We don't know exactly how
|
||||
// many characters are removed, because it depends on the name of the folder
|
||||
// where the file is located.
|
||||
tempFile.leafName = new Array(256).join("T") + ".txt";
|
||||
var newFile = DownloadPaths.createNiceUniqueFile(tempFile);
|
||||
do_check_true(newFile.leafName.length < tempFile.leafName.length);
|
||||
do_check_eq(newFile.leafName.slice(-4), ".txt");
|
||||
|
||||
// Creating a valid file name from an invalid one is not always possible.
|
||||
tempFile.append("file-under-long-directory.txt");
|
||||
try {
|
||||
// Single extension.
|
||||
var tempFile = destDir.clone();
|
||||
tempFile.append("test.txt");
|
||||
testCreateNiceUniqueFile(tempFile, "test.txt");
|
||||
testCreateNiceUniqueFile(tempFile, "test(1).txt");
|
||||
testCreateNiceUniqueFile(tempFile, "test(2).txt");
|
||||
|
||||
// Double extension.
|
||||
tempFile.leafName = "test.tar.gz";
|
||||
testCreateNiceUniqueFile(tempFile, "test.tar.gz");
|
||||
testCreateNiceUniqueFile(tempFile, "test(1).tar.gz");
|
||||
testCreateNiceUniqueFile(tempFile, "test(2).tar.gz");
|
||||
|
||||
// Test automatic shortening of long file names. We don't know exactly how
|
||||
// many characters are removed, because it depends on the name of the folder
|
||||
// where the file is located.
|
||||
tempFile.leafName = new Array(256).join("T") + ".txt";
|
||||
var newFile = DownloadPaths.createNiceUniqueFile(tempFile);
|
||||
do_check_true(newFile.leafName.length < tempFile.leafName.length);
|
||||
do_check_eq(newFile.leafName.slice(-4), ".txt");
|
||||
|
||||
// Creating a valid file name from an invalid one is not always possible.
|
||||
tempFile.append("file-under-long-directory.txt");
|
||||
try {
|
||||
DownloadPaths.createNiceUniqueFile(tempFile);
|
||||
do_throw("Exception expected with a long parent directory name.");
|
||||
} catch (e) {
|
||||
// An exception is expected, but we don't know which one exactly.
|
||||
}
|
||||
} finally {
|
||||
// Clean up the temporary directory.
|
||||
destDir.remove(true);
|
||||
DownloadPaths.createNiceUniqueFile(tempFile);
|
||||
do_throw("Exception expected with a long parent directory name.");
|
||||
} catch (e) {
|
||||
// An exception is expected, but we don't know which one exactly.
|
||||
}
|
||||
});
|
||||
|
@ -16,5 +16,4 @@ support-files =
|
||||
[test_Downloads.js]
|
||||
[test_DownloadStore.js]
|
||||
[test_PrivateTemp.js]
|
||||
# coverage flag is for bug 1336730
|
||||
skip-if = (os != 'linux' || coverage)
|
||||
skip-if = true # Bug 1336730
|
||||
|
@ -12,6 +12,7 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
Cu.import("resource://gre/modules/Services.jsm");
|
||||
Cu.import("resource://gre/modules/LoginRecipes.jsm");
|
||||
Cu.import("resource://gre/modules/LoginHelper.jsm");
|
||||
Cu.import("resource://testing-common/FileTestUtils.jsm");
|
||||
Cu.import("resource://testing-common/LoginTestUtils.jsm");
|
||||
Cu.import("resource://testing-common/MockDocument.jsm");
|
||||
|
||||
@ -40,47 +41,12 @@ function run_test()
|
||||
|
||||
// Global helpers
|
||||
|
||||
// Some of these functions are already implemented in other parts of the source
|
||||
// tree, see bug 946708 about sharing more code.
|
||||
|
||||
// While the previous test file should have deleted all the temporary files it
|
||||
// used, on Windows these might still be pending deletion on the physical file
|
||||
// system. Thus, start from a new base number every time, to make a collision
|
||||
// with a file that is still pending deletion highly unlikely.
|
||||
let gFileCounter = Math.floor(Math.random() * 1000000);
|
||||
|
||||
/**
|
||||
* Returns a reference to a temporary file, that is guaranteed not to exist, and
|
||||
* to have never been created before.
|
||||
*
|
||||
* @param aLeafName
|
||||
* Suggested leaf name for the file to be created.
|
||||
*
|
||||
* @return nsIFile pointing to a non-existent file in a temporary directory.
|
||||
*
|
||||
* @note It is not enough to delete the file if it exists, or to delete the file
|
||||
* after calling nsIFile.createUnique, because on Windows the delete
|
||||
* operation in the file system may still be pending, preventing a new
|
||||
* file with the same name to be created.
|
||||
* Returns a reference to a temporary file that is guaranteed not to exist and
|
||||
* is cleaned up later. See FileTestUtils.getTempFile for details.
|
||||
*/
|
||||
function getTempFile(aLeafName)
|
||||
{
|
||||
// Prepend a serial number to the extension in the suggested leaf name.
|
||||
let [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName);
|
||||
let leafName = base + "-" + gFileCounter + ext;
|
||||
gFileCounter++;
|
||||
|
||||
// Get a file reference under the temporary directory for this test file.
|
||||
let file = FileUtils.getFile("TmpD", [leafName]);
|
||||
do_check_false(file.exists());
|
||||
|
||||
do_register_cleanup(function() {
|
||||
if (file.exists()) {
|
||||
file.remove(false);
|
||||
}
|
||||
});
|
||||
|
||||
return file;
|
||||
function getTempFile(leafName) {
|
||||
return FileTestUtils.getTempFile(leafName);
|
||||
}
|
||||
|
||||
const RecipeHelpers = {
|
||||
|
@ -16,6 +16,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
|
||||
"resource://gre/modules/FileUtils.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
|
||||
"resource://gre/modules/NetUtil.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "FileTestUtils",
|
||||
"resource://testing-common/FileTestUtils.jsm");
|
||||
|
||||
const BackgroundFileSaverOutputStream = Components.Constructor(
|
||||
"@mozilla.org/network/background-file-saver;1?mode=outputstream",
|
||||
@ -37,20 +39,6 @@ var gHttpServer = null;
|
||||
const appRepURLPref = "browser.safebrowsing.downloads.remote.url";
|
||||
const remoteEnabledPref = "browser.safebrowsing.downloads.remote.enabled";
|
||||
|
||||
/**
|
||||
* Returns a reference to a temporary file. If the file is then created, it
|
||||
* will be removed when tests in this file finish.
|
||||
*/
|
||||
function getTempFile(aLeafName) {
|
||||
let file = FileUtils.getFile("TmpD", [aLeafName]);
|
||||
do_register_cleanup(function GTF_cleanup() {
|
||||
if (file.exists()) {
|
||||
file.remove(false);
|
||||
}
|
||||
});
|
||||
return file;
|
||||
}
|
||||
|
||||
function readFileToString(aFilename) {
|
||||
let f = do_get_file(aFilename);
|
||||
let stream = Cc["@mozilla.org/network/file-input-stream;1"]
|
||||
@ -318,7 +306,7 @@ add_task(async function test_signature_whitelists() {
|
||||
"http://localhost:4444/throw");
|
||||
|
||||
// Use BackgroundFileSaver to extract the signature on Windows.
|
||||
let destFile = getTempFile(TEST_FILE_NAME_1);
|
||||
let destFile = FileTestUtils.getTempFile(TEST_FILE_NAME_1);
|
||||
|
||||
let data = readFileToString("data/signed_win.exe");
|
||||
let saver = new BackgroundFileSaverOutputStream();
|
||||
|
@ -15,40 +15,15 @@ XPCOMUtils.defineLazyModuleGetter(this, "OS",
|
||||
"resource://gre/modules/osfile.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "JSONFile",
|
||||
"resource://gre/modules/JSONFile.jsm");
|
||||
|
||||
let gFileCounter = Math.floor(Math.random() * 1000000);
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "FileTestUtils",
|
||||
"resource://testing-common/FileTestUtils.jsm");
|
||||
|
||||
/**
|
||||
* Returns a reference to a temporary file, that is guaranteed not to exist, and
|
||||
* to have never been created before.
|
||||
*
|
||||
* @param aLeafName
|
||||
* Suggested leaf name for the file to be created.
|
||||
*
|
||||
* @return nsIFile pointing to a non-existent file in a temporary directory.
|
||||
*
|
||||
* @note It is not enough to delete the file if it exists, or to delete the file
|
||||
* after calling nsIFile.createUnique, because on Windows the delete
|
||||
* operation in the file system may still be pending, preventing a new
|
||||
* file with the same name to be created.
|
||||
* Returns a reference to a temporary file that is guaranteed not to exist and
|
||||
* is cleaned up later. See FileTestUtils.getTempFile for details.
|
||||
*/
|
||||
function getTempFile(aLeafName) {
|
||||
// Prepend a serial number to the extension in the suggested leaf name.
|
||||
let [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName);
|
||||
let leafName = base + "-" + gFileCounter + ext;
|
||||
gFileCounter++;
|
||||
|
||||
// Get a file reference under the temporary directory for this test file.
|
||||
let file = FileUtils.getFile("TmpD", [leafName]);
|
||||
do_check_false(file.exists());
|
||||
|
||||
do_register_cleanup(function() {
|
||||
if (file.exists()) {
|
||||
file.remove(false);
|
||||
}
|
||||
});
|
||||
|
||||
return file;
|
||||
function getTempFile(leafName) {
|
||||
return FileTestUtils.getTempFile(leafName);
|
||||
}
|
||||
|
||||
const TEST_STORE_FILE_NAME = "test-store.json";
|
||||
|
@ -10,58 +10,9 @@
|
||||
|
||||
Cu.import("resource://gre/modules/Downloads.jsm", this);
|
||||
Cu.import("resource://gre/modules/DownloadPaths.jsm", this);
|
||||
Cu.import("resource://testing-common/FileTestUtils.jsm", this);
|
||||
Cu.import("resource://testing-common/MockRegistrar.jsm", this);
|
||||
|
||||
const TEST_TARGET_FILE_NAME = "test-download.txt";
|
||||
|
||||
// While the previous test file should have deleted all the temporary files it
|
||||
// used, on Windows these might still be pending deletion on the physical file
|
||||
// system. Thus, start from a new base number every time, to make a collision
|
||||
// with a file that is still pending deletion highly unlikely.
|
||||
let gFileCounter = Math.floor(Math.random() * 1000000);
|
||||
|
||||
/**
|
||||
* Returns a reference to a temporary file, that is guaranteed not to exist, and
|
||||
* to have never been created before.
|
||||
*
|
||||
* @param aLeafName
|
||||
* Suggested leaf name for the file to be created.
|
||||
*
|
||||
* @return nsIFile pointing to a non-existent file in a temporary directory.
|
||||
*
|
||||
* @note It is not enough to delete the file if it exists, or to delete the file
|
||||
* after calling nsIFile.createUnique, because on Windows the delete
|
||||
* operation in the file system may still be pending, preventing a new
|
||||
* file with the same name to be created.
|
||||
*/
|
||||
function getTempFile(aLeafName) {
|
||||
// Prepend a serial number to the extension in the suggested leaf name.
|
||||
let [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName);
|
||||
let leafName = base + "-" + gFileCounter + ext;
|
||||
gFileCounter++;
|
||||
|
||||
// Get a file reference under the temporary directory for this test file.
|
||||
let file = FileUtils.getFile("TmpD", [leafName]);
|
||||
Assert.ok(!file.exists());
|
||||
|
||||
registerCleanupFunction(() => {
|
||||
try {
|
||||
file.remove(false);
|
||||
} catch (ex) {
|
||||
// On Windows, we may get an access denied error if the file existed
|
||||
// before, and was recently deleted.
|
||||
if (!(ex instanceof Components.Exception &&
|
||||
(ex.result == Cr.NS_ERROR_FILE_ACCESS_DENIED ||
|
||||
ex.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST ||
|
||||
ex.result == Cr.NS_ERROR_FILE_NOT_FOUND))) {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
add_task(async function test_setup() {
|
||||
// Save downloads to disk without showing the dialog.
|
||||
let cid = MockRegistrar.register("@mozilla.org/helperapplauncherdialog;1", {
|
||||
@ -71,7 +22,7 @@ add_task(async function test_setup() {
|
||||
},
|
||||
promptForSaveToFileAsync(launcher) {
|
||||
// The dialog should create the empty placeholder file.
|
||||
let file = getTempFile(TEST_TARGET_FILE_NAME);
|
||||
let file = FileTestUtils.getTempFile();
|
||||
file.create(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
|
||||
launcher.saveDestinationAvailable(file);
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user