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

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:f327a67420b0
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 "use strict";
6
7 Components.utils.import("resource://gre/modules/osfile.jsm");
8 Components.utils.import("resource://gre/modules/Services.jsm");
9
10 let Path = OS.Path;
11 let profileDir;
12
13 do_register_cleanup(function() {
14 Services.prefs.setBoolPref("toolkit.osfile.log", false);
15 });
16
17 function run_test() {
18 run_next_test();
19 }
20
21 /**
22 * Test OS.File.makeDir
23 */
24
25 add_task(function init() {
26 // Set up profile. We create the directory in the profile, because the profile
27 // is removed after every test run.
28 do_get_profile();
29 profileDir = OS.Constants.Path.profileDir;
30 Services.prefs.setBoolPref("toolkit.osfile.log", true);
31 });
32
33 /**
34 * Basic use
35 */
36
37 add_task(function* test_basic() {
38 let dir = Path.join(profileDir, "directory");
39
40 // Sanity checking for the test
41 do_check_false((yield OS.File.exists(dir)));
42
43 // Make a directory
44 yield OS.File.makeDir(dir);
45
46 //check if the directory exists
47 yield OS.File.stat(dir);
48
49 // Make a directory that already exists, this should succeed
50 yield OS.File.makeDir(dir);
51
52 // Make a directory with ignoreExisting
53 yield OS.File.makeDir(dir, {ignoreExisting: true});
54
55 // Make a directory with ignoreExisting false
56 let exception = null;
57 try {
58 yield OS.File.makeDir(dir, {ignoreExisting: false});
59 } catch (ex) {
60 exception = ex;
61 }
62
63 do_check_true(!!exception);
64 do_check_true(exception instanceof OS.File.Error);
65 do_check_true(exception.becauseExists);
66 });
67
68 // Make a root directory that already exists
69 add_task(function* test_root() {
70 if (OS.Constants.Win) {
71 yield OS.File.makeDir("C:");
72 yield OS.File.makeDir("C:\\");
73 } else {
74 yield OS.File.makeDir("/");
75 }
76 });
77
78 /**
79 * Creating subdirectories
80 */
81 add_task(function test_option_from() {
82 let dir = Path.join(profileDir, "a", "b", "c");
83
84 // Sanity checking for the test
85 do_check_false((yield OS.File.exists(dir)));
86
87 // Make a directory
88 yield OS.File.makeDir(dir, {from: profileDir});
89
90 //check if the directory exists
91 yield OS.File.stat(dir);
92
93 // Make a directory that already exists, this should succeed
94 yield OS.File.makeDir(dir);
95
96 // Make a directory with ignoreExisting
97 yield OS.File.makeDir(dir, {ignoreExisting: true});
98
99 // Make a directory with ignoreExisting false
100 let exception = null;
101 try {
102 yield OS.File.makeDir(dir, {ignoreExisting: false});
103 } catch (ex) {
104 exception = ex;
105 }
106
107 do_check_true(!!exception);
108 do_check_true(exception instanceof OS.File.Error);
109 do_check_true(exception.becauseExists);
110
111 // Make a directory without |from| and fail
112 let dir2 = Path.join(profileDir, "g", "h", "i");
113 exception = null;
114 try {
115 yield OS.File.makeDir(dir2);
116 } catch (ex) {
117 exception = ex;
118 }
119
120 do_check_true(!!exception);
121 do_check_true(exception instanceof OS.File.Error);
122 do_check_true(exception.becauseNoSuchFile);
123 });

mercurial