mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 897023 - [OS.File] Be smarter with functions that can fail with becauseAbsent/becauseExists. r=Yoric
This commit is contained in:
parent
7c2476f6f2
commit
77fdb11ee6
@ -337,13 +337,14 @@
|
||||
*
|
||||
* @param {string} path The name of the directory to remove.
|
||||
* @param {*=} options Additional options.
|
||||
* - {bool} ignoreAbsent If |true|, do not fail if the
|
||||
* directory does not exist yet.
|
||||
* - {bool} ignoreAbsent If |false|, throw an error if the directory
|
||||
* does not exist. |true| by default
|
||||
*/
|
||||
File.removeEmptyDir = function removeEmptyDir(path, options = {}) {
|
||||
let result = UnixFile.rmdir(path);
|
||||
if (result == -1) {
|
||||
if (options.ignoreAbsent && ctypes.errno == Const.ENOENT) {
|
||||
if ((!("ignoreAbsent" in options) || options.ignoreAbsent) &&
|
||||
ctypes.errno == Const.ENOENT) {
|
||||
return;
|
||||
}
|
||||
throw new File.Error("removeEmptyDir");
|
||||
@ -366,17 +367,19 @@
|
||||
* as per libc function |mkdir|. If unspecified, dirs are
|
||||
* created with a default mode of 0700 (dir is private to
|
||||
* the user, the user can read, write and execute).
|
||||
* - {bool} ignoreExisting If |true|, do not fail if the
|
||||
* directory already exists.
|
||||
* - {bool} ignoreExisting If |false|, throw error if the directory
|
||||
* already exists. |true| by default
|
||||
*/
|
||||
File.makeDir = function makeDir(path, options = {}) {
|
||||
let omode = options.unixMode !== undefined ? options.unixMode : DEFAULT_UNIX_MODE_DIR;
|
||||
let result = UnixFile.mkdir(path, omode);
|
||||
if (result != -1 ||
|
||||
options.ignoreExisting && ctypes.errno == Const.EEXIST) {
|
||||
return;
|
||||
if (result == -1) {
|
||||
if ((!("ignoreExisting" in options) || options.ignoreExisting) &&
|
||||
ctypes.errno == Const.EEXIST) {
|
||||
return;
|
||||
}
|
||||
throw new File.Error("makeDir");
|
||||
}
|
||||
throw new File.Error("makeDir");
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -410,13 +410,13 @@
|
||||
*
|
||||
* @param {string} path The name of the directory to remove.
|
||||
* @param {*=} options Additional options.
|
||||
* - {bool} ignoreAbsent If |true|, do not fail if the
|
||||
* directory does not exist yet.
|
||||
* - {bool} ignoreAbsent If |false|, throw an error if the directory
|
||||
* does not exist. |true| by default
|
||||
*/
|
||||
File.removeEmptyDir = function removeEmptyDir(path, options = {}) {
|
||||
let result = WinFile.RemoveDirectory(path);
|
||||
if (!result) {
|
||||
if (options.ignoreAbsent &&
|
||||
if ((!("ignoreAbsent" in options) || options.ignoreAbsent) &&
|
||||
ctypes.winLastError == Const.ERROR_FILE_NOT_FOUND) {
|
||||
return;
|
||||
}
|
||||
@ -435,18 +435,19 @@
|
||||
* as per winapi function |CreateDirectory|. If unspecified,
|
||||
* use the default security descriptor, inherited from the
|
||||
* parent directory.
|
||||
* - {bool} ignoreExisting If |true|, do not fail if the
|
||||
* directory already exists.
|
||||
* - {bool} ignoreExisting If |false|, throw an error if the directory
|
||||
* already exists. |true| by default
|
||||
*/
|
||||
File.makeDir = function makeDir(path, options = {}) {
|
||||
let security = options.winSecurity || null;
|
||||
let result = WinFile.CreateDirectory(path, security);
|
||||
if (result ||
|
||||
options.ignoreExisting &&
|
||||
ctypes.winLastError == Const.ERROR_ALREADY_EXISTS) {
|
||||
return;
|
||||
if (!result) {
|
||||
if ((!("ignoreExisting" in options) || options.ignoreExisting) &&
|
||||
ctypes.winLastError == Const.ERROR_ALREADY_EXISTS) {
|
||||
return;
|
||||
}
|
||||
throw new File.Error("makeDir");
|
||||
}
|
||||
throw new File.Error("makeDir");
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -157,7 +157,6 @@ let test = maketest("Main", function main(test) {
|
||||
yield test_read_write();
|
||||
yield test_read_write_all();
|
||||
yield test_position();
|
||||
yield test_mkdir();
|
||||
yield test_iter();
|
||||
yield test_exists();
|
||||
yield test_debug_test();
|
||||
@ -429,68 +428,6 @@ let test_position = maketest("position", function position(test) {
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Test OS.File.{removeEmptyDir, makeDir}
|
||||
*/
|
||||
let test_mkdir = maketest("mkdir", function mkdir(test) {
|
||||
return Task.spawn(function() {
|
||||
const DIRNAME = "test_dir.tmp";
|
||||
|
||||
// Cleanup
|
||||
yield OS.File.removeEmptyDir(DIRNAME, {ignoreAbsent: true});
|
||||
|
||||
// Remove an absent directory with ignoreAbsent
|
||||
yield OS.File.removeEmptyDir(DIRNAME, {ignoreAbsent: true});
|
||||
test.ok(true, "Removing an absent directory with ignoreAbsent succeeds");
|
||||
|
||||
// Remove an absent directory without ignoreAbsent
|
||||
try {
|
||||
yield OS.File.removeEmptyDir(DIRNAME);
|
||||
test.fail("Removing an absent directory without ignoreAbsent should have failed");
|
||||
} catch (err) {
|
||||
test.ok(err, "Removing an absent directory without ignoreAbsent throws the right error");
|
||||
test.ok(err instanceof OS.File.Error, "Error is an OS.File.Error");
|
||||
test.ok(err.becauseNoSuchFile, "Error mentions that the file does not exist");
|
||||
}
|
||||
|
||||
// Creating a directory (should succeed)
|
||||
test.ok(true, "Creating a directory");
|
||||
yield OS.File.makeDir(DIRNAME);
|
||||
let stat = yield OS.File.stat(DIRNAME);
|
||||
test.ok(stat.isDir, "I have effectively created a directory");
|
||||
|
||||
// Creating a directory with ignoreExisting (should succeed)
|
||||
try {
|
||||
yield OS.File.makeDir(DIRNAME, {ignoreExisting: true});
|
||||
test.ok(true, "Creating a directory with ignoreExisting succeeds");
|
||||
} catch(err) {
|
||||
test.ok(false, "Creating a directory with ignoreExisting fails");
|
||||
}
|
||||
|
||||
// Creating a directory (should fail)
|
||||
try {
|
||||
yield OS.File.makeDir(DIRNAME);
|
||||
test.fail("Creating over an existing directory should have failed");
|
||||
} catch (err) {
|
||||
test.ok(err, "Creating over an existing directory throws the right error");
|
||||
test.ok(err instanceof OS.File.Error, "Error is an OS.File.Error");
|
||||
test.ok(err.becauseExists, "Error mentions that the file already exists");
|
||||
}
|
||||
|
||||
// Remove a directory and check the result
|
||||
yield OS.File.removeEmptyDir(DIRNAME);
|
||||
test.ok(true, "Removing empty directory suceeded");
|
||||
try {
|
||||
yield OS.File.stat(DIRNAME);
|
||||
test.fail("Removing directory should have failed");
|
||||
} catch (err) {
|
||||
test.ok(err, "Directory was effectively removed");
|
||||
test.ok(err instanceof OS.File.Error, "Error is an OS.File.Error");
|
||||
test.ok(err.becauseNoSuchFile, "Error mentions that the file does not exist");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Test OS.File.prototype.{DirectoryIterator}
|
||||
*/
|
||||
|
56
toolkit/components/osfile/tests/xpcshell/test_makeDir.js
Normal file
56
toolkit/components/osfile/tests/xpcshell/test_makeDir.js
Normal file
@ -0,0 +1,56 @@
|
||||
/* 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/. */
|
||||
|
||||
"use strict";
|
||||
|
||||
Components.utils.import("resource://gre/modules/osfile.jsm");
|
||||
Components.utils.import("resource://gre/modules/Services.jsm");
|
||||
|
||||
do_register_cleanup(function() {
|
||||
Services.prefs.setBoolPref("toolkit.osfile.log", false);
|
||||
});
|
||||
|
||||
function run_test() {
|
||||
Services.prefs.setBoolPref("toolkit.osfile.log", true);
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test OS.File.makeDir
|
||||
*/
|
||||
add_task(function() {
|
||||
// Set up profile. We create the directory in the profile, because the profile
|
||||
// is removed after every test run.
|
||||
do_get_profile();
|
||||
|
||||
let dir = OS.Path.join(OS.Constants.Path.profileDir, "directory");
|
||||
|
||||
// Sanity checking for the test
|
||||
do_check_false((yield OS.File.exists(dir)));
|
||||
|
||||
// Make a directory
|
||||
yield OS.File.makeDir(dir);
|
||||
|
||||
//check if the directory exists
|
||||
yield OS.File.stat(dir);
|
||||
|
||||
// Make a directory that already exists
|
||||
yield OS.File.makeDir(dir);
|
||||
|
||||
// Make a directory with ignoreExisting
|
||||
yield OS.File.makeDir(dir, {ignoreExisting: true});
|
||||
|
||||
// Make a directory with ignoreExisting false
|
||||
let exception = null;
|
||||
try {
|
||||
yield OS.File.makeDir(dir, {ignoreExisting: false});
|
||||
} catch (ex) {
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
do_check_true(!!exception);
|
||||
do_check_true(exception instanceof OS.File.Error);
|
||||
do_check_true(exception.becauseExists);
|
||||
});
|
@ -0,0 +1,55 @@
|
||||
/* 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/. */
|
||||
|
||||
"use strict";
|
||||
|
||||
Components.utils.import("resource://gre/modules/osfile.jsm");
|
||||
Components.utils.import("resource://gre/modules/Services.jsm");
|
||||
|
||||
do_register_cleanup(function() {
|
||||
Services.prefs.setBoolPref("toolkit.osfile.log", false);
|
||||
});
|
||||
|
||||
function run_test() {
|
||||
Services.prefs.setBoolPref("toolkit.osfile.log", true);
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test OS.File.removeEmptyDir
|
||||
*/
|
||||
add_task(function() {
|
||||
// Set up profile. We create the directory in the profile, because the profile
|
||||
// is removed after every test run.
|
||||
do_get_profile();
|
||||
|
||||
let dir = OS.Path.join(OS.Constants.Path.profileDir, "directory");
|
||||
|
||||
// Sanity checking for the test
|
||||
do_check_false((yield OS.File.exists(dir)));
|
||||
|
||||
// Remove non-existent directory
|
||||
yield OS.File.removeEmptyDir(dir);
|
||||
|
||||
// Remove non-existent directory with ignoreAbsent
|
||||
yield OS.File.removeEmptyDir(dir, {ignoreAbsent: true});
|
||||
|
||||
// Remove non-existent directory with ignoreAbsent false
|
||||
let exception = null;
|
||||
try {
|
||||
yield OS.File.removeEmptyDir(dir, {ignoreAbsent: false});
|
||||
} catch (ex) {
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
do_check_true(!!exception);
|
||||
do_check_true(exception instanceof OS.File.Error);
|
||||
do_check_true(exception.becauseNoSuchFile);
|
||||
|
||||
// Remove empty directory
|
||||
yield OS.File.makeDir(dir);
|
||||
yield OS.File.removeEmptyDir(dir);
|
||||
do_check_false((yield OS.File.exists(dir)));
|
||||
});
|
@ -10,6 +10,8 @@ tail =
|
||||
[test_osfile_async_copy.js]
|
||||
[test_osfile_async_flush.js]
|
||||
[test_osfile_async_setDates.js]
|
||||
[test_removeEmptyDir.js]
|
||||
[test_makeDir.js]
|
||||
[test_profiledir.js]
|
||||
[test_logging.js]
|
||||
[test_creationDate.js]
|
||||
|
Loading…
Reference in New Issue
Block a user