michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: Components.utils.import("resource://gre/modules/osfile.jsm"); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: /** michael@0: * Test OS.File.open for reading: michael@0: * - with an existing file (should succeed); michael@0: * - with a non-existing file (should fail); michael@0: * - with inconsistent arguments (should fail). michael@0: */ michael@0: add_task(function() { michael@0: // Attempt to open a file that does not exist, ensure that it yields the michael@0: // appropriate error. michael@0: try { michael@0: let fd = yield OS.File.open(OS.Path.join(".", "This file does not exist")); michael@0: do_check_true(false, "File opening 1 succeeded (it should fail)"); michael@0: } catch (err if err instanceof OS.File.Error && err.becauseNoSuchFile) { michael@0: do_print("File opening 1 failed " + err); michael@0: } michael@0: michael@0: // Attempt to open a file with the wrong args, so that it fails before michael@0: // serialization, ensure that it yields the appropriate error. michael@0: do_print("Attempting to open a file with wrong arguments"); michael@0: try { michael@0: let fd = yield OS.File.open(1, 2, 3); michael@0: do_check_true(false, "File opening 2 succeeded (it should fail)" + fd); michael@0: } catch (err) { michael@0: do_print("File opening 2 failed " + err); michael@0: do_check_false(err instanceof OS.File.Error, michael@0: "File opening 2 returned something that is not a file error"); michael@0: do_check_true(err.constructor.name == "TypeError", michael@0: "File opening 2 returned a TypeError"); michael@0: } michael@0: michael@0: // Attempt to open a file correctly michael@0: do_print("Attempting to open a file correctly"); michael@0: let openedFile = yield OS.File.open(OS.Path.join(do_get_cwd().path, "test_open.js")); michael@0: do_print("File opened correctly"); michael@0: michael@0: do_print("Attempting to close a file correctly"); michael@0: yield openedFile.close(); michael@0: michael@0: do_print("Attempting to close a file again"); michael@0: yield openedFile.close(); michael@0: }); michael@0: michael@0: /** michael@0: * Test the error thrown by OS.File.open when attempting to open a directory michael@0: * that does not exist. michael@0: */ michael@0: add_task(function test_error_attributes () { michael@0: michael@0: let dir = OS.Path.join(do_get_profile().path, "test_osfileErrorAttrs"); michael@0: let fpath = OS.Path.join(dir, "test_error_attributes.txt"); michael@0: michael@0: try { michael@0: yield OS.File.open(fpath, {truncate: true}, {}); michael@0: do_check_true(false, "Opening path suceeded (it should fail) " + fpath); michael@0: } catch (err) { michael@0: do_check_true(err instanceof OS.File.Error); michael@0: do_check_true(err.becauseNoSuchFile); michael@0: } michael@0: });