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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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/Task.jsm");
michael@0 9 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 10
michael@0 11 do_register_cleanup(function() {
michael@0 12 Services.prefs.setBoolPref("toolkit.osfile.log", false);
michael@0 13 });
michael@0 14
michael@0 15 function run_test() {
michael@0 16 Services.prefs.setBoolPref("toolkit.osfile.log", true);
michael@0 17
michael@0 18 run_next_test();
michael@0 19 }
michael@0 20
michael@0 21 add_task(function() {
michael@0 22 // Set up profile. We create the directory in the profile, because the profile
michael@0 23 // is removed after every test run.
michael@0 24 do_get_profile();
michael@0 25
michael@0 26 let file = OS.Path.join(OS.Constants.Path.profileDir, "file");
michael@0 27 let dir = OS.Path.join(OS.Constants.Path.profileDir, "directory");
michael@0 28 let file1 = OS.Path.join(dir, "file1");
michael@0 29 let file2 = OS.Path.join(dir, "file2");
michael@0 30 let subDir = OS.Path.join(dir, "subdir");
michael@0 31 let fileInSubDir = OS.Path.join(subDir, "file");
michael@0 32
michael@0 33 // Sanity checking for the test
michael@0 34 do_check_false((yield OS.File.exists(dir)));
michael@0 35
michael@0 36 // Remove non-existent directory
michael@0 37 let exception = null;
michael@0 38 try {
michael@0 39 yield OS.File.removeDir(dir, {ignoreAbsent: false});
michael@0 40 } catch (ex) {
michael@0 41 exception = ex;
michael@0 42 }
michael@0 43
michael@0 44 do_check_true(!!exception);
michael@0 45 do_check_true(exception instanceof OS.File.Error);
michael@0 46
michael@0 47 // Remove non-existent directory with ignoreAbsent
michael@0 48 yield OS.File.removeDir(dir, {ignoreAbsent: true});
michael@0 49 yield OS.File.removeDir(dir);
michael@0 50
michael@0 51 // Remove file with ignoreAbsent: false
michael@0 52 yield OS.File.writeAtomic(file, "content", { tmpPath: file + ".tmp" });
michael@0 53 exception = null;
michael@0 54 try {
michael@0 55 yield OS.File.removeDir(file, {ignoreAbsent: false});
michael@0 56 } catch (ex) {
michael@0 57 exception = ex;
michael@0 58 }
michael@0 59
michael@0 60 do_check_true(!!exception);
michael@0 61 do_check_true(exception instanceof OS.File.Error);
michael@0 62
michael@0 63 // Remove empty directory
michael@0 64 yield OS.File.makeDir(dir);
michael@0 65 yield OS.File.removeDir(dir);
michael@0 66 do_check_false((yield OS.File.exists(dir)));
michael@0 67
michael@0 68 // Remove directory that contains one file
michael@0 69 yield OS.File.makeDir(dir);
michael@0 70 yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" });
michael@0 71 yield OS.File.removeDir(dir);
michael@0 72 do_check_false((yield OS.File.exists(dir)));
michael@0 73
michael@0 74 // Remove directory that contains multiple files
michael@0 75 yield OS.File.makeDir(dir);
michael@0 76 yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" });
michael@0 77 yield OS.File.writeAtomic(file2, "content", { tmpPath: file2 + ".tmp" });
michael@0 78 yield OS.File.removeDir(dir);
michael@0 79 do_check_false((yield OS.File.exists(dir)));
michael@0 80
michael@0 81 // Remove directory that contains a file and a directory
michael@0 82 yield OS.File.makeDir(dir);
michael@0 83 yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" });
michael@0 84 yield OS.File.makeDir(subDir);
michael@0 85 yield OS.File.writeAtomic(fileInSubDir, "content", { tmpPath: fileInSubDir + ".tmp" });
michael@0 86 yield OS.File.removeDir(dir);
michael@0 87 do_check_false((yield OS.File.exists(dir)));
michael@0 88 });
michael@0 89
michael@0 90 add_task(function* test_unix_symlink() {
michael@0 91 // Windows does not implement OS.File.unixSymLink()
michael@0 92 if (OS.Constants.Win) {
michael@0 93 return;
michael@0 94 }
michael@0 95
michael@0 96 let file = OS.Path.join(OS.Constants.Path.profileDir, "file");
michael@0 97 let dir = OS.Path.join(OS.Constants.Path.profileDir, "directory");
michael@0 98 let file1 = OS.Path.join(dir, "file1");
michael@0 99
michael@0 100 // This test will create the following directory structure:
michael@0 101 // <profileDir>/file (regular file)
michael@0 102 // <profileDir>/file.link => file (symlink)
michael@0 103 // <profileDir>/directory (directory)
michael@0 104 // <profileDir>/linkdir => directory (directory)
michael@0 105 // <profileDir>/directory/file1 (regular file)
michael@0 106 // <profileDir>/directory3 (directory)
michael@0 107 // <profileDir>/directory3/file3 (directory)
michael@0 108 // <profileDir>/directory/link2 => ../directory3 (regular file)
michael@0 109
michael@0 110 // Sanity checking for the test
michael@0 111 do_check_false((yield OS.File.exists(dir)));
michael@0 112
michael@0 113 yield OS.File.writeAtomic(file, "content", { tmpPath: file + ".tmp" });
michael@0 114 do_check_true((yield OS.File.exists(file)));
michael@0 115 let info = yield OS.File.stat(file, {unixNoFollowingLinks: true});
michael@0 116 do_check_false(info.isDir);
michael@0 117 do_check_false(info.isSymLink);
michael@0 118
michael@0 119 yield OS.File.unixSymLink(file, file + ".link");
michael@0 120 do_check_true((yield OS.File.exists(file + ".link")));
michael@0 121 info = yield OS.File.stat(file + ".link", {unixNoFollowingLinks: true});
michael@0 122 do_check_false(info.isDir);
michael@0 123 do_check_true(info.isSymLink);
michael@0 124 info = yield OS.File.stat(file + ".link");
michael@0 125 do_check_false(info.isDir);
michael@0 126 do_check_false(info.isSymLink);
michael@0 127 yield OS.File.remove(file + ".link");
michael@0 128 do_check_false((yield OS.File.exists(file + ".link")));
michael@0 129
michael@0 130 yield OS.File.makeDir(dir);
michael@0 131 do_check_true((yield OS.File.exists(dir)));
michael@0 132 info = yield OS.File.stat(dir, {unixNoFollowingLinks: true});
michael@0 133 do_check_true(info.isDir);
michael@0 134 do_check_false(info.isSymLink);
michael@0 135
michael@0 136 let link = OS.Path.join(OS.Constants.Path.profileDir, "linkdir");
michael@0 137
michael@0 138 yield OS.File.unixSymLink(dir, link);
michael@0 139 do_check_true((yield OS.File.exists(link)));
michael@0 140 info = yield OS.File.stat(link, {unixNoFollowingLinks: true});
michael@0 141 do_check_false(info.isDir);
michael@0 142 do_check_true(info.isSymLink);
michael@0 143 info = yield OS.File.stat(link);
michael@0 144 do_check_true(info.isDir);
michael@0 145 do_check_false(info.isSymLink);
michael@0 146
michael@0 147 let dir3 = OS.Path.join(OS.Constants.Path.profileDir, "directory3");
michael@0 148 let file3 = OS.Path.join(dir3, "file3");
michael@0 149 let link2 = OS.Path.join(dir, "link2");
michael@0 150
michael@0 151 yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" });
michael@0 152 do_check_true((yield OS.File.exists(file1)));
michael@0 153 yield OS.File.makeDir(dir3);
michael@0 154 do_check_true((yield OS.File.exists(dir3)));
michael@0 155 yield OS.File.writeAtomic(file3, "content", { tmpPath: file3 + ".tmp" });
michael@0 156 do_check_true((yield OS.File.exists(file3)));
michael@0 157 yield OS.File.unixSymLink("../directory3", link2);
michael@0 158 do_check_true((yield OS.File.exists(link2)));
michael@0 159
michael@0 160 yield OS.File.removeDir(link);
michael@0 161 do_check_false((yield OS.File.exists(link)));
michael@0 162 do_check_true((yield OS.File.exists(file1)));
michael@0 163 yield OS.File.removeDir(dir);
michael@0 164 do_check_false((yield OS.File.exists(dir)));
michael@0 165 do_check_true((yield OS.File.exists(file3)));
michael@0 166 yield OS.File.removeDir(dir3);
michael@0 167 do_check_false((yield OS.File.exists(dir3)));
michael@0 168
michael@0 169 // This task will be executed only on Unix-like systems.
michael@0 170 // Please do not add tests independent to operating systems here
michael@0 171 // or implement symlink() on Windows.
michael@0 172 });

mercurial