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 toolkit/modules/tests/xpcshell/test_task.js
# Not yet updated # Not yet updated
toolkit/components/osfile/**
toolkit/components/url-classifier/** toolkit/components/url-classifier/**
# External code: # External code:
@ -351,6 +350,7 @@ toolkit/components/reader/JSDOMParser.js
# Uses preprocessing # Uses preprocessing
toolkit/content/widgets/wizard.xml toolkit/content/widgets/wizard.xml
toolkit/components/jsdownloads/src/DownloadIntegration.jsm toolkit/components/jsdownloads/src/DownloadIntegration.jsm
toolkit/components/osfile/osfile.jsm
toolkit/components/urlformatter/nsURLFormatter.js toolkit/components/urlformatter/nsURLFormatter.js
toolkit/modules/AppConstants.jsm toolkit/modules/AppConstants.jsm
toolkit/mozapps/downloads/nsHelperAppDlg.js 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,24 +152,30 @@ AbstractFile.openUnique = function openUnique(path, options = {}) {
path: path, path: path,
file: OS.File.open(path, mode) file: OS.File.open(path, mode)
}; };
} catch (ex if ex instanceof OS.File.Error && ex.becauseExists) { } catch (ex) {
for (let i = 0; i < maxAttempts; ++i) { if (ex instanceof OS.File.Error && ex.becauseExists) {
try { for (let i = 0; i < maxAttempts; ++i) {
if (humanReadable) { try {
uniquePath = Path.join(dirName, fileName + "-" + (i + 1) + suffix); if (humanReadable) {
} else { uniquePath = Path.join(dirName, fileName + "-" + (i + 1) + suffix);
let hexNumber = Math.floor(Math.random() * MAX_HEX_NUMBER).toString(HEX_RADIX); } else {
uniquePath = Path.join(dirName, fileName + "-" + hexNumber + suffix); let hexNumber = Math.floor(Math.random() * MAX_HEX_NUMBER).toString(HEX_RADIX);
uniquePath = Path.join(dirName, fileName + "-" + hexNumber + suffix);
}
return {
path: uniquePath,
file: OS.File.open(uniquePath, mode)
};
} catch (ex) {
if (ex instanceof OS.File.Error && ex.becauseExists) {
// keep trying ...
} else {
throw ex;
}
} }
return {
path: uniquePath,
file: OS.File.open(uniquePath, mode)
};
} catch (ex if ex instanceof OS.File.Error && ex.becauseExists) {
// keep trying ...
} }
throw OS.File.Error.exists("could not find an unused file name.", path);
} }
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; let decoder;
try { try {
decoder = new TextDecoder(options.encoding); decoder = new TextDecoder(options.encoding);
} catch (ex if ex instanceof RangeError) { } catch (ex) {
throw OS.File.Error.invalidArgument("Decode"); if (ex instanceof RangeError) {
throw OS.File.Error.invalidArgument("Decode");
} else {
throw ex;
}
} }
return decoder.decode(buffer); return decoder.decode(buffer);
} finally { } finally {
@ -425,8 +435,12 @@ AbstractFile.writeAtomic =
if (options.backupTo) { if (options.backupTo) {
try { try {
OS.File.move(path, options.backupTo, {noCopy: true}); OS.File.move(path, options.backupTo, {noCopy: true});
} catch (ex if ex.becauseNoSuchFile) { } catch (ex) {
// The file doesn't exist, nothing to backup. if (ex.becauseNoSuchFile) {
// The file doesn't exist, nothing to backup.
} else {
throw ex;
}
} }
} }
// Just write, without any renaming trick // Just write, without any renaming trick
@ -458,8 +472,12 @@ AbstractFile.writeAtomic =
if (options.backupTo) { if (options.backupTo) {
try { try {
OS.File.move(path, options.backupTo, {noCopy: true}); OS.File.move(path, options.backupTo, {noCopy: true});
} catch (ex if ex.becauseNoSuchFile) { } catch (ex) {
// The file doesn't exist, nothing to backup. 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; let exn = null;
try { try {
await iterator.next(); await iterator.next();
} catch (ex if ex instanceof OS.File.Error && ex.becauseNoSuchFile) { } catch (ex) {
exn = ex; if (ex instanceof OS.File.Error && ex.becauseNoSuchFile) {
let exists = await iterator.exists(); exn = ex;
test.ok(!exists, "After one iteration, iterator detects that the directory doesn't exist"); 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"); test.ok(exn, "Iterating through a directory that does not exist has failed with becauseNoSuchFile");
} finally { } finally {
@ -420,5 +424,3 @@ var test_debug_test = maketest("debug_test", function debug_test(test) {
toggleDebugTest(false, consoleListener); toggleDebugTest(false, consoleListener);
})(); })();
}); });

View file

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

View file

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

View file

@ -3,5 +3,9 @@
module.exports = { module.exports = {
"extends": [ "extends": [
"plugin:mozilla/xpcshell-test" "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); 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 { try {
await OS.File.read(EXISTING_FILE, { encoding: "baby-speak-encoded" }); await OS.File.read(EXISTING_FILE, { encoding: "baby-speak-encoded" });
do_throw("Should have thrown with an ex.becauseInvalidArgument"); do_throw("Should have thrown with an ex.becauseInvalidArgument");
} catch (ex if ex.becauseInvalidArgument) { } catch (ex) {
do_print("Wrong encoding caused the correct exception"); if (ex.becauseInvalidArgument) {
do_print("Wrong encoding caused the correct exception");
} else {
throw ex;
}
} }
try { try {
await OS.File.read(EXISTING_FILE, { encoding: 4 }); await OS.File.read(EXISTING_FILE, { encoding: 4 });
do_throw("Should have thrown a TypeError"); do_throw("Should have thrown a TypeError");
} catch (ex if ex.constructor.name == "TypeError") { } catch (ex) {
// Note that TypeError doesn't carry across compartments if (ex.constructor.name == "TypeError") {
do_print("Non-string encoding caused the correct exception"); // 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() { add_test_pair(async function test_bad_compression() {
do_print("Testing with a non-existing compression"); do_print("Testing with a non-existing compression");
try { try {
await OS.File.read(EXISTING_FILE, { compression: "mmmh-crunchy" }); await OS.File.read(EXISTING_FILE, { compression: "mmmh-crunchy" });
do_throw("Should have thrown with an ex.becauseInvalidArgument"); do_throw("Should have thrown with an ex.becauseInvalidArgument");
} catch (ex if ex.becauseInvalidArgument) { } catch (ex) {
do_print("Wrong encoding caused the correct exception"); if (ex.becauseInvalidArgument) {
do_print("Wrong encoding caused the correct exception");
} else {
throw ex;
}
} }
do_print("Testing with a bad type for option compression"); do_print("Testing with a bad type for option compression");
try { try {
await OS.File.read(EXISTING_FILE, { compression: 5 }); await OS.File.read(EXISTING_FILE, { compression: 5 });
do_throw("Should have thrown a TypeError"); do_throw("Should have thrown a TypeError");
} catch (ex if ex.constructor.name == "TypeError") { } catch (ex) {
// Note that TypeError doesn't carry across compartments if (ex.constructor.name == "TypeError") {
do_print("Non-string encoding caused the correct exception"); // 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 { try {
await OS.File.read(EXISTING_FILE, { bytes: "five" }); await OS.File.read(EXISTING_FILE, { bytes: "five" });
do_throw("Should have thrown a TypeError"); do_throw("Should have thrown a TypeError");
} catch (ex if ex.constructor.name == "TypeError") { } catch (ex) {
// Note that TypeError doesn't carry across compartments if (ex.constructor.name == "TypeError") {
do_print("Non-number bytes caused the correct exception"); // 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 { try {
await OS.File.read("I/do/not/exist"); await OS.File.read("I/do/not/exist");
do_throw("Should have thrown with an ex.becauseNoSuchFile"); do_throw("Should have thrown with an ex.becauseNoSuchFile");
} catch (ex if ex.becauseNoSuchFile) { } catch (ex) {
do_print("Correct exceptions"); 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 // Attempt to open a file that does not exist, ensure that it yields the
// appropriate error. // appropriate error.
try { 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)"); do_check_true(false, "File opening 1 succeeded (it should fail)");
} catch (err if err instanceof OS.File.Error && err.becauseNoSuchFile) { } catch (err) {
do_print("File opening 1 failed " + 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 // Attempt to open a file with the wrong args, so that it fails before
// serialization, ensure that it yields the appropriate error. // serialization, ensure that it yields the appropriate error.
do_print("Attempting to open a file with wrong arguments"); 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"); "test_osfile_async_append.tmp");
// Clear any left-over files from previous runs. // Clear any left-over files from previous runs.
try { await removeTestFile(path)
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore
}
try { try {
mode = setup_mode(mode); mode = setup_mode(mode);
@ -53,12 +49,8 @@ async function test_append(mode) {
} finally { } finally {
await file.close(); await file.close();
} }
} catch(ex) { } catch (ex) {
try { await removeTestFile(path)
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore.
}
} }
} }
@ -68,11 +60,7 @@ async function test_no_append(mode) {
"test_osfile_async_noappend.tmp"); "test_osfile_async_noappend.tmp");
// Clear any left-over files from previous runs. // Clear any left-over files from previous runs.
try { await removeTestFile(path)
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore
}
try { try {
mode = setup_mode(mode); mode = setup_mode(mode);
@ -95,18 +83,14 @@ async function test_no_append(mode) {
await file.close(); await file.close();
} }
} finally { } finally {
try { await removeTestFile(path)
await OS.File.remove(path);
} catch (ex if ex.becauseNoSuchFile) {
// ignore.
}
} }
} }
var test_flags = [ var test_flags = [
{}, {},
{create:true}, {create: true},
{trunc:true} {trunc: true}
]; ];
function run_test() { function run_test() {
do_test_pending(); do_test_pending();

View file

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

View file

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

View file

@ -62,8 +62,12 @@ add_test_pair(async function read_write_all() {
opt.noOverwrite = true; opt.noOverwrite = true;
await OS.File.writeAtomic(DEST_PATH, view, opt); await OS.File.writeAtomic(DEST_PATH, view, opt);
do_throw("With noOverwrite, writeAtomic should have refused to overwrite file (" + suffix + ")"); do_throw("With noOverwrite, writeAtomic should have refused to overwrite file (" + suffix + ")");
} catch (err if err instanceof OS.File.Error && err.becauseExists) { } catch (err) {
do_print("With noOverwrite, writeAtomic correctly failed (" + suffix + ")"); 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); 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(DEST_PATH);
await OS.File.remove(TMP_PATH); await OS.File.remove(TMP_PATH);
})(); })();
}; }
await test_with_options({tmpPath: TMP_PATH}, "Renaming, not flushing"); await test_with_options({tmpPath: TMP_PATH}, "Renaming, not flushing");
await test_with_options({tmpPath: TMP_PATH, flush: true}, "Renaming, 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; let thrown = false;
try { try {
await OS.File.resetWorker(); await OS.File.resetWorker();
} catch (ex if ex.message.indexOf(OS.Path.basename(TEST_FILE)) != -1 ) { } catch (ex) {
thrown = true; if (ex.message.indexOf(OS.Path.basename(TEST_FILE)) != -1 ) {
thrown = true;
} else {
throw ex;
}
} }
do_check_true(thrown); do_check_true(thrown);
@ -52,8 +56,12 @@ add_task(async function dir_open_cannot_reset() {
let thrown = false; let thrown = false;
try { try {
await OS.File.resetWorker(); await OS.File.resetWorker();
} catch (ex if ex.message.indexOf(OS.Path.basename(TEST_DIR)) != -1 ) { } catch (ex) {
thrown = true; if (ex.message.indexOf(OS.Path.basename(TEST_DIR)) != -1 ) {
thrown = true;
} else {
throw ex;
}
} }
do_check_true(thrown); do_check_true(thrown);
@ -87,7 +95,7 @@ add_task(async function finish_with_a_reset() {
} catch (ex) { } catch (ex) {
} }
// Now reset // Now reset
/*don't yield*/ OS.File.resetWorker(); /* don't yield*/ OS.File.resetWorker();
}); });
function run_test() { function run_test() {

View file

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