toolkit/components/osfile/tests/xpcshell/test_open.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:ced5bf37a6a6
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 "use strict";
5
6 Components.utils.import("resource://gre/modules/osfile.jsm");
7
8 function run_test() {
9 run_next_test();
10 }
11
12 /**
13 * Test OS.File.open for reading:
14 * - with an existing file (should succeed);
15 * - with a non-existing file (should fail);
16 * - with inconsistent arguments (should fail).
17 */
18 add_task(function() {
19 // Attempt to open a file that does not exist, ensure that it yields the
20 // appropriate error.
21 try {
22 let fd = yield OS.File.open(OS.Path.join(".", "This file does not exist"));
23 do_check_true(false, "File opening 1 succeeded (it should fail)");
24 } catch (err if err instanceof OS.File.Error && err.becauseNoSuchFile) {
25 do_print("File opening 1 failed " + err);
26 }
27
28 // Attempt to open a file with the wrong args, so that it fails before
29 // serialization, ensure that it yields the appropriate error.
30 do_print("Attempting to open a file with wrong arguments");
31 try {
32 let fd = yield OS.File.open(1, 2, 3);
33 do_check_true(false, "File opening 2 succeeded (it should fail)" + fd);
34 } catch (err) {
35 do_print("File opening 2 failed " + err);
36 do_check_false(err instanceof OS.File.Error,
37 "File opening 2 returned something that is not a file error");
38 do_check_true(err.constructor.name == "TypeError",
39 "File opening 2 returned a TypeError");
40 }
41
42 // Attempt to open a file correctly
43 do_print("Attempting to open a file correctly");
44 let openedFile = yield OS.File.open(OS.Path.join(do_get_cwd().path, "test_open.js"));
45 do_print("File opened correctly");
46
47 do_print("Attempting to close a file correctly");
48 yield openedFile.close();
49
50 do_print("Attempting to close a file again");
51 yield openedFile.close();
52 });
53
54 /**
55 * Test the error thrown by OS.File.open when attempting to open a directory
56 * that does not exist.
57 */
58 add_task(function test_error_attributes () {
59
60 let dir = OS.Path.join(do_get_profile().path, "test_osfileErrorAttrs");
61 let fpath = OS.Path.join(dir, "test_error_attributes.txt");
62
63 try {
64 yield OS.File.open(fpath, {truncate: true}, {});
65 do_check_true(false, "Opening path suceeded (it should fail) " + fpath);
66 } catch (err) {
67 do_check_true(err instanceof OS.File.Error);
68 do_check_true(err.becauseNoSuchFile);
69 }
70 });

mercurial