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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial