Bug 1386666 - Remove try..catch(ex if ex) statements from toolkit/components/osfile/ so that ESLint can parse the files. r=mossop

MozReview-Commit-ID: 786dBxzN3Uc

--HG--
extra : rebase_source : bfb2c488d598f528bdbe0383051322c964fb1497
This commit is contained in:
Dan Banner 2017-07-31 16:34:04 +01:00
parent bcadbae76c
commit 2514f6e1e4
16 changed files with 193 additions and 118 deletions

View File

@ -339,7 +339,6 @@ toolkit/components/workerloader/tests/moduleF-syntax-error.js
toolkit/modules/tests/xpcshell/test_task.js
# Not yet updated
toolkit/components/osfile/**
toolkit/components/url-classifier/**
# External code:
@ -351,6 +350,7 @@ toolkit/components/reader/JSDOMParser.js
# Uses preprocessing
toolkit/content/widgets/wizard.xml
toolkit/components/jsdownloads/src/DownloadIntegration.jsm
toolkit/components/osfile/osfile.jsm
toolkit/components/urlformatter/nsURLFormatter.js
toolkit/modules/AppConstants.jsm
toolkit/mozapps/downloads/nsHelperAppDlg.js

View File

@ -0,0 +1,32 @@
"use strict";
module.exports = {
"rules": {
"brace-style": "off",
"comma-spacing": "off",
"consistent-return": "off",
"eol-last": "off",
"func-call-spacing": "off",
"key-spacing": "off",
"keyword-spacing": "off",
"no-else-return": "off",
"no-extra-semi": "off",
"no-irregular-whitespace": "off",
"no-lone-blocks": "off",
"no-lonely-if": "off",
"no-multi-spaces": "off",
"no-redeclare": "off",
"no-self-assign": "off",
"no-shadow": "off",
"no-trailing-spaces": "off",
"no-undef": "off",
"no-unsafe-negation": "off",
"no-unused-vars": "off",
"no-useless-return": "off",
"object-shorthand": "off",
"quotes": "off",
"space-before-function-paren": "off",
"space-infix-ops": "off",
"spaced-comment": "off",
}
};

View File

@ -152,7 +152,8 @@ AbstractFile.openUnique = function openUnique(path, options = {}) {
path: path,
file: OS.File.open(path, mode)
};
} catch (ex if ex instanceof OS.File.Error && ex.becauseExists) {
} catch (ex) {
if (ex instanceof OS.File.Error && ex.becauseExists) {
for (let i = 0; i < maxAttempts; ++i) {
try {
if (humanReadable) {
@ -165,12 +166,17 @@ AbstractFile.openUnique = function openUnique(path, options = {}) {
path: uniquePath,
file: OS.File.open(uniquePath, mode)
};
} catch (ex if ex instanceof OS.File.Error && ex.becauseExists) {
} catch (ex) {
if (ex instanceof OS.File.Error && ex.becauseExists) {
// keep trying ...
} else {
throw ex;
}
}
}
throw OS.File.Error.exists("could not find an unused file name.", path);
}
}
};
/**
@ -339,8 +345,12 @@ AbstractFile.read = function read(path, bytes, options = {}) {
let decoder;
try {
decoder = new TextDecoder(options.encoding);
} catch (ex if ex instanceof RangeError) {
} catch (ex) {
if (ex instanceof RangeError) {
throw OS.File.Error.invalidArgument("Decode");
} else {
throw ex;
}
}
return decoder.decode(buffer);
} finally {
@ -425,8 +435,12 @@ AbstractFile.writeAtomic =
if (options.backupTo) {
try {
OS.File.move(path, options.backupTo, {noCopy: true});
} catch (ex if ex.becauseNoSuchFile) {
} catch (ex) {
if (ex.becauseNoSuchFile) {
// The file doesn't exist, nothing to backup.
} else {
throw ex;
}
}
}
// Just write, without any renaming trick
@ -458,8 +472,12 @@ AbstractFile.writeAtomic =
if (options.backupTo) {
try {
OS.File.move(path, options.backupTo, {noCopy: true});
} catch (ex if ex.becauseNoSuchFile) {
} catch (ex) {
if (ex.becauseNoSuchFile) {
// The file doesn't exist, nothing to backup.
} else {
throw ex;
}
}
}

View File

@ -344,10 +344,14 @@ var test_iter = maketest("iter", function iter(test) {
let exn = null;
try {
await iterator.next();
} catch (ex if ex instanceof OS.File.Error && ex.becauseNoSuchFile) {
} catch (ex) {
if (ex instanceof OS.File.Error && ex.becauseNoSuchFile) {
exn = ex;
let exists = await iterator.exists();
test.ok(!exists, "After one iteration, iterator detects that the directory doesn't exist");
} else {
throw ex;
}
}
test.ok(exn, "Iterating through a directory that does not exist has failed with becauseNoSuchFile");
} finally {
@ -420,5 +424,3 @@ var test_debug_test = maketest("debug_test", function debug_test(test) {
toggleDebugTest(false, consoleListener);
})();
});

View File

@ -192,10 +192,13 @@ function test_passing_undefined()
| OS.Constants.libc.O_CREAT
| OS.Constants.libc.O_TRUNC,
OS.Constants.libc.S_IRWXU);
} catch(e if e instanceof TypeError && e.message.indexOf("open") > -1) {
} catch(e) {
if (e instanceof TypeError && e.message.indexOf("open") > -1) {
exceptionRaised = true;
} else {
throw e;
}
}
ok(exceptionRaised, "test_passing_undefined: exception gets thrown")
}

View File

@ -203,8 +203,12 @@ function test_passing_undefined()
OS.Constants.Win.OPEN_EXISTING,
0,
null);
} catch(e if e instanceof TypeError && e.message.indexOf("CreateFile") > -1) {
} catch(e) {
if (e instanceof TypeError && e.message.indexOf("CreateFile") > -1) {
exceptionRaised = true;
} else {
throw e;
}
}
ok(exceptionRaised, "test_passing_undefined: exception gets thrown")

View File

@ -3,5 +3,9 @@
module.exports = {
"extends": [
"plugin:mozilla/xpcshell-test"
]
],
"rules": {
"no-shadow": "off",
}
};

View File

@ -95,3 +95,13 @@ function reference_compare_files(a, b, test) {
do_check_eq(a_contents, b_contents);
})();
};
async function removeTestFile(filePath, ignoreNoSuchFile = true) {
try {
await OS.File.remove(filePath);
} catch (ex) {
if (!ignoreNoSuchFile || !ex.becauseNoSuchFile) {
do_throw(ex);
}
}
}

View File

@ -31,35 +31,51 @@ add_test_pair(async function test_bad_encoding() {
try {
await OS.File.read(EXISTING_FILE, { encoding: "baby-speak-encoded" });
do_throw("Should have thrown with an ex.becauseInvalidArgument");
} catch (ex if ex.becauseInvalidArgument) {
} catch (ex) {
if (ex.becauseInvalidArgument) {
do_print("Wrong encoding caused the correct exception");
} else {
throw ex;
}
}
try {
await OS.File.read(EXISTING_FILE, { encoding: 4 });
do_throw("Should have thrown a TypeError");
} catch (ex if ex.constructor.name == "TypeError") {
} catch (ex) {
if (ex.constructor.name == "TypeError") {
// Note that TypeError doesn't carry across compartments
do_print("Non-string encoding caused the correct exception");
} else {
throw ex;
}
});
}
});
add_test_pair(async function test_bad_compression() {
do_print("Testing with a non-existing compression");
try {
await OS.File.read(EXISTING_FILE, { compression: "mmmh-crunchy" });
do_throw("Should have thrown with an ex.becauseInvalidArgument");
} catch (ex if ex.becauseInvalidArgument) {
} catch (ex) {
if (ex.becauseInvalidArgument) {
do_print("Wrong encoding caused the correct exception");
} else {
throw ex;
}
}
do_print("Testing with a bad type for option compression");
try {
await OS.File.read(EXISTING_FILE, { compression: 5 });
do_throw("Should have thrown a TypeError");
} catch (ex if ex.constructor.name == "TypeError") {
} catch (ex) {
if (ex.constructor.name == "TypeError") {
// Note that TypeError doesn't carry across compartments
do_print("Non-string encoding caused the correct exception");
} else {
throw ex;
}
}
});
@ -68,9 +84,13 @@ add_test_pair(async function test_bad_bytes() {
try {
await OS.File.read(EXISTING_FILE, { bytes: "five" });
do_throw("Should have thrown a TypeError");
} catch (ex if ex.constructor.name == "TypeError") {
} catch (ex) {
if (ex.constructor.name == "TypeError") {
// Note that TypeError doesn't carry across compartments
do_print("Non-number bytes caused the correct exception");
} else {
throw ex;
}
}
});
@ -79,8 +99,12 @@ add_test_pair(async function read_non_existent() {
try {
await OS.File.read("I/do/not/exist");
do_throw("Should have thrown with an ex.becauseNoSuchFile");
} catch (ex if ex.becauseNoSuchFile) {
} catch (ex) {
if (ex.becauseNoSuchFile) {
do_print("Correct exceptions");
} else {
throw ex;
}
}
});

View File

@ -19,12 +19,15 @@ add_task(async function() {
// Attempt to open a file that does not exist, ensure that it yields the
// appropriate error.
try {
let fd = await OS.File.open(OS.Path.join(".", "This file does not exist"));
await OS.File.open(OS.Path.join(".", "This file does not exist"));
do_check_true(false, "File opening 1 succeeded (it should fail)");
} catch (err if err instanceof OS.File.Error && err.becauseNoSuchFile) {
} catch (err) {
if (err instanceof OS.File.Error && err.becauseNoSuchFile) {
do_print("File opening 1 failed " + err);
} else {
throw err;
}
}
// Attempt to open a file with the wrong args, so that it fails before
// serialization, ensure that it yields the appropriate error.
do_print("Attempting to open a file with wrong arguments");

View File

@ -27,11 +27,7 @@ async function test_append(mode) {
"test_osfile_async_append.tmp");
// Clear any left-over files from previous runs.
try {
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore
}
await removeTestFile(path)
try {
mode = setup_mode(mode);
@ -53,12 +49,8 @@ async function test_append(mode) {
} finally {
await file.close();
}
} catch(ex) {
try {
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore.
}
} catch (ex) {
await removeTestFile(path)
}
}
@ -68,11 +60,7 @@ async function test_no_append(mode) {
"test_osfile_async_noappend.tmp");
// Clear any left-over files from previous runs.
try {
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore
}
await removeTestFile(path)
try {
mode = setup_mode(mode);
@ -95,18 +83,14 @@ async function test_no_append(mode) {
await file.close();
}
} finally {
try {
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore.
}
await removeTestFile(path)
}
}
var test_flags = [
{},
{create:true},
{trunc:true}
{create: true},
{trunc: true}
];
function run_test() {
do_test_pending();

View File

@ -90,16 +90,8 @@ async function test_copymove(options = {}) {
// 3. Check that the moved file was really moved.
do_check_eq((await OS.File.exists(dest)), false);
} finally {
try {
await OS.File.remove(dest);
} catch (ex if ex.becauseNoSuchFile) {
// ignore
}
try {
await OS.File.remove(dest2);
} catch (ex if ex.becauseNoSuchFile) {
// ignore
}
await removeTestFile(dest);
await removeTestFile(dest2);
}
}

View File

@ -17,11 +17,7 @@ async function test_setPosition(forward, current, backward) {
"test_osfile_async_largefiles.tmp");
// Clear any left-over files from previous runs.
try {
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore
}
await removeTestFile(path);
try {
let file = await OS.File.open(path, {write:true, append:false});
@ -51,12 +47,7 @@ async function test_setPosition(forward, current, backward) {
await file.close();
}
} catch(ex) {
try {
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore.
}
do_throw(ex);
await removeTestFile(path);
}
}
@ -66,11 +57,7 @@ async function test_setPosition_failures() {
"test_osfile_async_largefiles.tmp");
// Clear any left-over files from previous runs.
try {
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore
}
await removeTestFile(path);
try {
let file = await OS.File.open(path, {write:true, append:false});
@ -114,11 +101,7 @@ async function test_setPosition_failures() {
} finally {
await file.setPosition(0, OS.File.POS_START);
await file.close();
try {
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore.
}
await removeTestFile(path);
}
} catch(ex) {
do_throw(ex);

View File

@ -62,8 +62,12 @@ add_test_pair(async function read_write_all() {
opt.noOverwrite = true;
await OS.File.writeAtomic(DEST_PATH, view, opt);
do_throw("With noOverwrite, writeAtomic should have refused to overwrite file (" + suffix + ")");
} catch (err if err instanceof OS.File.Error && err.becauseExists) {
} catch (err) {
if (err instanceof OS.File.Error && err.becauseExists) {
do_print("With noOverwrite, writeAtomic correctly failed (" + suffix + ")");
} else {
throw err;
}
}
await reference_compare_files(pathSource, DEST_PATH, TEST);
@ -89,7 +93,7 @@ add_test_pair(async function read_write_all() {
await OS.File.remove(DEST_PATH);
await OS.File.remove(TMP_PATH);
})();
};
}
await test_with_options({tmpPath: TMP_PATH}, "Renaming, not flushing");
await test_with_options({tmpPath: TMP_PATH, flush: true}, "Renaming, flushing");

View File

@ -35,8 +35,12 @@ add_task(async function file_open_cannot_reset() {
let thrown = false;
try {
await OS.File.resetWorker();
} catch (ex if ex.message.indexOf(OS.Path.basename(TEST_FILE)) != -1 ) {
} catch (ex) {
if (ex.message.indexOf(OS.Path.basename(TEST_FILE)) != -1 ) {
thrown = true;
} else {
throw ex;
}
}
do_check_true(thrown);
@ -52,8 +56,12 @@ add_task(async function dir_open_cannot_reset() {
let thrown = false;
try {
await OS.File.resetWorker();
} catch (ex if ex.message.indexOf(OS.Path.basename(TEST_DIR)) != -1 ) {
} catch (ex) {
if (ex.message.indexOf(OS.Path.basename(TEST_DIR)) != -1 ) {
thrown = true;
} else {
throw ex;
}
}
do_check_true(thrown);
@ -87,7 +95,7 @@ add_task(async function finish_with_a_reset() {
} catch (ex) {
}
// Now reset
/*don't yield*/ OS.File.resetWorker();
/* don't yield*/ OS.File.resetWorker();
});
function run_test() {

View File

@ -59,8 +59,12 @@ add_task(async function system_shutdown() {
try {
await deferred.promise;
resolved = true;
} catch (ex if ex == "timeout") {
} catch (ex) {
if (ex == "timeout") {
resolved = false;
} else {
throw ex;
}
}
Services.console.unregisterListener(observer);
Services.prefs.clearUserPref("toolkit.osfile.log");