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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_removeDir.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,172 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +Components.utils.import("resource://gre/modules/osfile.jsm");
    1.11 +Components.utils.import("resource://gre/modules/Task.jsm");
    1.12 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.13 +
    1.14 +do_register_cleanup(function() {
    1.15 +  Services.prefs.setBoolPref("toolkit.osfile.log", false);
    1.16 +});
    1.17 +
    1.18 +function run_test() {
    1.19 +  Services.prefs.setBoolPref("toolkit.osfile.log", true);
    1.20 +
    1.21 +  run_next_test();
    1.22 +}
    1.23 +
    1.24 +add_task(function() {
    1.25 +  // Set up profile. We create the directory in the profile, because the profile
    1.26 +  // is removed after every test run.
    1.27 +  do_get_profile();
    1.28 +
    1.29 +  let file = OS.Path.join(OS.Constants.Path.profileDir, "file");
    1.30 +  let dir = OS.Path.join(OS.Constants.Path.profileDir, "directory");
    1.31 +  let file1 = OS.Path.join(dir, "file1");
    1.32 +  let file2 = OS.Path.join(dir, "file2");
    1.33 +  let subDir = OS.Path.join(dir, "subdir");
    1.34 +  let fileInSubDir = OS.Path.join(subDir, "file");
    1.35 +
    1.36 +  // Sanity checking for the test
    1.37 +  do_check_false((yield OS.File.exists(dir)));
    1.38 +
    1.39 +  // Remove non-existent directory
    1.40 +  let exception = null;
    1.41 +  try {
    1.42 +    yield OS.File.removeDir(dir, {ignoreAbsent: false});
    1.43 +  } catch (ex) {
    1.44 +    exception = ex;
    1.45 +  }
    1.46 +
    1.47 +  do_check_true(!!exception);
    1.48 +  do_check_true(exception instanceof OS.File.Error);
    1.49 +
    1.50 +  // Remove non-existent directory with ignoreAbsent
    1.51 +  yield OS.File.removeDir(dir, {ignoreAbsent: true});
    1.52 +  yield OS.File.removeDir(dir);
    1.53 +
    1.54 +  // Remove file with ignoreAbsent: false
    1.55 +  yield OS.File.writeAtomic(file, "content", { tmpPath: file + ".tmp" });
    1.56 +  exception = null;
    1.57 +  try {
    1.58 +    yield OS.File.removeDir(file, {ignoreAbsent: false});
    1.59 +  } catch (ex) {
    1.60 +    exception = ex;
    1.61 +  }
    1.62 +
    1.63 +  do_check_true(!!exception);
    1.64 +  do_check_true(exception instanceof OS.File.Error);
    1.65 +
    1.66 +  // Remove empty directory
    1.67 +  yield OS.File.makeDir(dir);
    1.68 +  yield OS.File.removeDir(dir);
    1.69 +  do_check_false((yield OS.File.exists(dir)));
    1.70 +
    1.71 +  // Remove directory that contains one file
    1.72 +  yield OS.File.makeDir(dir);
    1.73 +  yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" });
    1.74 +  yield OS.File.removeDir(dir);
    1.75 +  do_check_false((yield OS.File.exists(dir)));
    1.76 +
    1.77 +  // Remove directory that contains multiple files
    1.78 +  yield OS.File.makeDir(dir);
    1.79 +  yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" });
    1.80 +  yield OS.File.writeAtomic(file2, "content", { tmpPath: file2 + ".tmp" });
    1.81 +  yield OS.File.removeDir(dir);
    1.82 +  do_check_false((yield OS.File.exists(dir)));
    1.83 +
    1.84 +  // Remove directory that contains a file and a directory
    1.85 +  yield OS.File.makeDir(dir);
    1.86 +  yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" });
    1.87 +  yield OS.File.makeDir(subDir);
    1.88 +  yield OS.File.writeAtomic(fileInSubDir, "content", { tmpPath: fileInSubDir + ".tmp" });
    1.89 +  yield OS.File.removeDir(dir);
    1.90 +  do_check_false((yield OS.File.exists(dir)));
    1.91 +});
    1.92 +
    1.93 +add_task(function* test_unix_symlink() {
    1.94 +  // Windows does not implement OS.File.unixSymLink()
    1.95 +  if (OS.Constants.Win) {
    1.96 +    return;
    1.97 +  }
    1.98 +
    1.99 +  let file = OS.Path.join(OS.Constants.Path.profileDir, "file");
   1.100 +  let dir = OS.Path.join(OS.Constants.Path.profileDir, "directory");
   1.101 +  let file1 = OS.Path.join(dir, "file1");
   1.102 +
   1.103 +  // This test will create the following directory structure:
   1.104 +  // <profileDir>/file                             (regular file)
   1.105 +  // <profileDir>/file.link => file                (symlink)
   1.106 +  // <profileDir>/directory                        (directory)
   1.107 +  // <profileDir>/linkdir => directory             (directory)
   1.108 +  // <profileDir>/directory/file1                  (regular file)
   1.109 +  // <profileDir>/directory3                       (directory)
   1.110 +  // <profileDir>/directory3/file3                 (directory)
   1.111 +  // <profileDir>/directory/link2 => ../directory3 (regular file)
   1.112 +
   1.113 +  // Sanity checking for the test
   1.114 +  do_check_false((yield OS.File.exists(dir)));
   1.115 +
   1.116 +  yield OS.File.writeAtomic(file, "content", { tmpPath: file + ".tmp" });
   1.117 +  do_check_true((yield OS.File.exists(file)));
   1.118 +  let info = yield OS.File.stat(file, {unixNoFollowingLinks: true});
   1.119 +  do_check_false(info.isDir);
   1.120 +  do_check_false(info.isSymLink);
   1.121 +
   1.122 +  yield OS.File.unixSymLink(file, file + ".link");
   1.123 +  do_check_true((yield OS.File.exists(file + ".link")));
   1.124 +  info = yield OS.File.stat(file + ".link", {unixNoFollowingLinks: true});
   1.125 +  do_check_false(info.isDir);
   1.126 +  do_check_true(info.isSymLink);
   1.127 +  info = yield OS.File.stat(file + ".link");
   1.128 +  do_check_false(info.isDir);
   1.129 +  do_check_false(info.isSymLink);
   1.130 +  yield OS.File.remove(file + ".link");
   1.131 +  do_check_false((yield OS.File.exists(file + ".link")));
   1.132 +
   1.133 +  yield OS.File.makeDir(dir);
   1.134 +  do_check_true((yield OS.File.exists(dir)));
   1.135 +  info = yield OS.File.stat(dir, {unixNoFollowingLinks: true});
   1.136 +  do_check_true(info.isDir);
   1.137 +  do_check_false(info.isSymLink);
   1.138 +
   1.139 +  let link = OS.Path.join(OS.Constants.Path.profileDir, "linkdir");
   1.140 +
   1.141 +  yield OS.File.unixSymLink(dir, link);
   1.142 +  do_check_true((yield OS.File.exists(link)));
   1.143 +  info = yield OS.File.stat(link, {unixNoFollowingLinks: true});
   1.144 +  do_check_false(info.isDir);
   1.145 +  do_check_true(info.isSymLink);
   1.146 +  info = yield OS.File.stat(link);
   1.147 +  do_check_true(info.isDir);
   1.148 +  do_check_false(info.isSymLink);
   1.149 +
   1.150 +  let dir3 = OS.Path.join(OS.Constants.Path.profileDir, "directory3");
   1.151 +  let file3 = OS.Path.join(dir3, "file3");
   1.152 +  let link2 = OS.Path.join(dir, "link2");
   1.153 +
   1.154 +  yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" });
   1.155 +  do_check_true((yield OS.File.exists(file1)));
   1.156 +  yield OS.File.makeDir(dir3);
   1.157 +  do_check_true((yield OS.File.exists(dir3)));
   1.158 +  yield OS.File.writeAtomic(file3, "content", { tmpPath: file3 + ".tmp" });
   1.159 +  do_check_true((yield OS.File.exists(file3)));
   1.160 +  yield OS.File.unixSymLink("../directory3", link2);
   1.161 +  do_check_true((yield OS.File.exists(link2)));
   1.162 +
   1.163 +  yield OS.File.removeDir(link);
   1.164 +  do_check_false((yield OS.File.exists(link)));
   1.165 +  do_check_true((yield OS.File.exists(file1)));
   1.166 +  yield OS.File.removeDir(dir);
   1.167 +  do_check_false((yield OS.File.exists(dir)));
   1.168 +  do_check_true((yield OS.File.exists(file3)));
   1.169 +  yield OS.File.removeDir(dir3);
   1.170 +  do_check_false((yield OS.File.exists(dir3)));
   1.171 +
   1.172 +  // This task will be executed only on Unix-like systems.
   1.173 +  // Please do not add tests independent to operating systems here
   1.174 +  // or implement symlink() on Windows.
   1.175 +});

mercurial