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

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial