diff --git a/.eslintignore b/.eslintignore index ae97d88107d4..5609d96ff5fe 100644 --- a/.eslintignore +++ b/.eslintignore @@ -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 diff --git a/toolkit/components/osfile/.eslintrc.js b/toolkit/components/osfile/.eslintrc.js new file mode 100644 index 000000000000..e4f5f33a77d7 --- /dev/null +++ b/toolkit/components/osfile/.eslintrc.js @@ -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", + } +}; diff --git a/toolkit/components/osfile/modules/osfile_shared_front.jsm b/toolkit/components/osfile/modules/osfile_shared_front.jsm index a2971991d4cf..61c935d63363 100644 --- a/toolkit/components/osfile/modules/osfile_shared_front.jsm +++ b/toolkit/components/osfile/modules/osfile_shared_front.jsm @@ -152,24 +152,30 @@ AbstractFile.openUnique = function openUnique(path, options = {}) { path: path, file: OS.File.open(path, mode) }; - } catch (ex if ex instanceof OS.File.Error && ex.becauseExists) { - for (let i = 0; i < maxAttempts; ++i) { - try { - if (humanReadable) { - uniquePath = Path.join(dirName, fileName + "-" + (i + 1) + suffix); - } else { - let hexNumber = Math.floor(Math.random() * MAX_HEX_NUMBER).toString(HEX_RADIX); - uniquePath = Path.join(dirName, fileName + "-" + hexNumber + suffix); + } catch (ex) { + if (ex instanceof OS.File.Error && ex.becauseExists) { + for (let i = 0; i < maxAttempts; ++i) { + try { + if (humanReadable) { + uniquePath = Path.join(dirName, fileName + "-" + (i + 1) + suffix); + } else { + 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; try { decoder = new TextDecoder(options.encoding); - } catch (ex if ex instanceof RangeError) { - throw OS.File.Error.invalidArgument("Decode"); + } 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) { - // The file doesn't exist, nothing to backup. + } 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) { - // The file doesn't exist, nothing to backup. + } catch (ex) { + if (ex.becauseNoSuchFile) { + // The file doesn't exist, nothing to backup. + } else { + throw ex; + } } } diff --git a/toolkit/components/osfile/tests/mochi/main_test_osfile_async.js b/toolkit/components/osfile/tests/mochi/main_test_osfile_async.js index d875548efdcf..538efad482cb 100644 --- a/toolkit/components/osfile/tests/mochi/main_test_osfile_async.js +++ b/toolkit/components/osfile/tests/mochi/main_test_osfile_async.js @@ -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) { - exn = ex; - let exists = await iterator.exists(); - test.ok(!exists, "After one iteration, iterator detects that the directory doesn't exist"); + } 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); })(); }); - - diff --git a/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js b/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js index 9fe2d0b4e655..cf1a11def7d5 100644 --- a/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js +++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js @@ -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) { - exceptionRaised = true; + } catch(e) { + if (e instanceof TypeError && e.message.indexOf("open") > -1) { + exceptionRaised = true; + } else { + throw e; + } } ok(exceptionRaised, "test_passing_undefined: exception gets thrown") } - diff --git a/toolkit/components/osfile/tests/mochi/worker_test_osfile_win.js b/toolkit/components/osfile/tests/mochi/worker_test_osfile_win.js index f41fdecfeac5..f4a87c0ac590 100644 --- a/toolkit/components/osfile/tests/mochi/worker_test_osfile_win.js +++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_win.js @@ -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) { - exceptionRaised = true; + } catch(e) { + if (e instanceof TypeError && e.message.indexOf("CreateFile") > -1) { + exceptionRaised = true; + } else { + throw e; + } } ok(exceptionRaised, "test_passing_undefined: exception gets thrown") diff --git a/toolkit/components/osfile/tests/xpcshell/.eslintrc.js b/toolkit/components/osfile/tests/xpcshell/.eslintrc.js index 70fe35407782..d4b574ff14d9 100644 --- a/toolkit/components/osfile/tests/xpcshell/.eslintrc.js +++ b/toolkit/components/osfile/tests/xpcshell/.eslintrc.js @@ -3,5 +3,9 @@ module.exports = { "extends": [ "plugin:mozilla/xpcshell-test" - ] + ], + + "rules": { + "no-shadow": "off", + } }; diff --git a/toolkit/components/osfile/tests/xpcshell/head.js b/toolkit/components/osfile/tests/xpcshell/head.js index 4c826c26e032..657b1ef91ba4 100644 --- a/toolkit/components/osfile/tests/xpcshell/head.js +++ b/toolkit/components/osfile/tests/xpcshell/head.js @@ -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); + } + } +} diff --git a/toolkit/components/osfile/tests/xpcshell/test_exception.js b/toolkit/components/osfile/tests/xpcshell/test_exception.js index 0fab158f72e7..cf4fad1de214 100644 --- a/toolkit/components/osfile/tests/xpcshell/test_exception.js +++ b/toolkit/components/osfile/tests/xpcshell/test_exception.js @@ -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) { - do_print("Wrong encoding caused the correct exception"); + } 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") { - // Note that TypeError doesn't carry across compartments - do_print("Non-string encoding caused the correct exception"); + } 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) { - do_print("Wrong encoding caused the correct exception"); + } 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") { - // Note that TypeError doesn't carry across compartments - do_print("Non-string encoding caused the correct exception"); + } 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") { - // Note that TypeError doesn't carry across compartments - do_print("Non-number bytes caused the correct exception"); + } 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) { - do_print("Correct exceptions"); + } catch (ex) { + if (ex.becauseNoSuchFile) { + do_print("Correct exceptions"); + } else { + throw ex; + } } }); diff --git a/toolkit/components/osfile/tests/xpcshell/test_open.js b/toolkit/components/osfile/tests/xpcshell/test_open.js index a8a449644873..51339e1156b1 100644 --- a/toolkit/components/osfile/tests/xpcshell/test_open.js +++ b/toolkit/components/osfile/tests/xpcshell/test_open.js @@ -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) { - do_print("File opening 1 failed " + err); + } 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"); diff --git a/toolkit/components/osfile/tests/xpcshell/test_osfile_async_append.js b/toolkit/components/osfile/tests/xpcshell/test_osfile_async_append.js index 94cd2dd622c1..dba9b10e586d 100644 --- a/toolkit/components/osfile/tests/xpcshell/test_osfile_async_append.js +++ b/toolkit/components/osfile/tests/xpcshell/test_osfile_async_append.js @@ -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(); diff --git a/toolkit/components/osfile/tests/xpcshell/test_osfile_async_copy.js b/toolkit/components/osfile/tests/xpcshell/test_osfile_async_copy.js index 651669ff5e7a..555e298773aa 100644 --- a/toolkit/components/osfile/tests/xpcshell/test_osfile_async_copy.js +++ b/toolkit/components/osfile/tests/xpcshell/test_osfile_async_copy.js @@ -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); } } diff --git a/toolkit/components/osfile/tests/xpcshell/test_osfile_async_largefiles.js b/toolkit/components/osfile/tests/xpcshell/test_osfile_async_largefiles.js index 16775c061bc6..ab9a5372a29b 100644 --- a/toolkit/components/osfile/tests/xpcshell/test_osfile_async_largefiles.js +++ b/toolkit/components/osfile/tests/xpcshell/test_osfile_async_largefiles.js @@ -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); diff --git a/toolkit/components/osfile/tests/xpcshell/test_read_write.js b/toolkit/components/osfile/tests/xpcshell/test_read_write.js index 1ea0bdb628ea..227010329fff 100644 --- a/toolkit/components/osfile/tests/xpcshell/test_read_write.js +++ b/toolkit/components/osfile/tests/xpcshell/test_read_write.js @@ -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) { - do_print("With noOverwrite, writeAtomic correctly failed (" + suffix + ")"); + } 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"); diff --git a/toolkit/components/osfile/tests/xpcshell/test_reset.js b/toolkit/components/osfile/tests/xpcshell/test_reset.js index 165208d491cd..63531d26fe8f 100644 --- a/toolkit/components/osfile/tests/xpcshell/test_reset.js +++ b/toolkit/components/osfile/tests/xpcshell/test_reset.js @@ -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 ) { - thrown = true; + } 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 ) { - thrown = true; + } 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() { diff --git a/toolkit/components/osfile/tests/xpcshell/test_shutdown.js b/toolkit/components/osfile/tests/xpcshell/test_shutdown.js index 2d50c3b65135..6cd6716ca858 100644 --- a/toolkit/components/osfile/tests/xpcshell/test_shutdown.js +++ b/toolkit/components/osfile/tests/xpcshell/test_shutdown.js @@ -59,8 +59,12 @@ add_task(async function system_shutdown() { try { await deferred.promise; resolved = true; - } catch (ex if ex == "timeout") { - resolved = false; + } catch (ex) { + if (ex == "timeout") { + resolved = false; + } else { + throw ex; + } } Services.console.unregisterListener(observer); Services.prefs.clearUserPref("toolkit.osfile.log");