Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
5 "use strict";
7 Components.utils.import("resource://gre/modules/osfile.jsm");
8 Components.utils.import("resource://gre/modules/Services.jsm");
10 let Path = OS.Path;
11 let profileDir;
13 do_register_cleanup(function() {
14 Services.prefs.setBoolPref("toolkit.osfile.log", false);
15 });
17 function run_test() {
18 run_next_test();
19 }
21 /**
22 * Test OS.File.makeDir
23 */
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 });
33 /**
34 * Basic use
35 */
37 add_task(function* test_basic() {
38 let dir = Path.join(profileDir, "directory");
40 // Sanity checking for the test
41 do_check_false((yield OS.File.exists(dir)));
43 // Make a directory
44 yield OS.File.makeDir(dir);
46 //check if the directory exists
47 yield OS.File.stat(dir);
49 // Make a directory that already exists, this should succeed
50 yield OS.File.makeDir(dir);
52 // Make a directory with ignoreExisting
53 yield OS.File.makeDir(dir, {ignoreExisting: true});
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 }
63 do_check_true(!!exception);
64 do_check_true(exception instanceof OS.File.Error);
65 do_check_true(exception.becauseExists);
66 });
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 });
78 /**
79 * Creating subdirectories
80 */
81 add_task(function test_option_from() {
82 let dir = Path.join(profileDir, "a", "b", "c");
84 // Sanity checking for the test
85 do_check_false((yield OS.File.exists(dir)));
87 // Make a directory
88 yield OS.File.makeDir(dir, {from: profileDir});
90 //check if the directory exists
91 yield OS.File.stat(dir);
93 // Make a directory that already exists, this should succeed
94 yield OS.File.makeDir(dir);
96 // Make a directory with ignoreExisting
97 yield OS.File.makeDir(dir, {ignoreExisting: true});
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 }
107 do_check_true(!!exception);
108 do_check_true(exception instanceof OS.File.Error);
109 do_check_true(exception.becauseExists);
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 }
120 do_check_true(!!exception);
121 do_check_true(exception instanceof OS.File.Error);
122 do_check_true(exception.becauseNoSuchFile);
123 });