toolkit/components/osfile/tests/xpcshell/test_makeDir.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_makeDir.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,123 @@
     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/Services.jsm");
    1.12 +
    1.13 +let Path = OS.Path;
    1.14 +let profileDir;
    1.15 +
    1.16 +do_register_cleanup(function() {
    1.17 +  Services.prefs.setBoolPref("toolkit.osfile.log", false);
    1.18 +});
    1.19 +
    1.20 +function run_test() {
    1.21 +  run_next_test();
    1.22 +}
    1.23 +
    1.24 +/**
    1.25 + * Test OS.File.makeDir
    1.26 + */
    1.27 +
    1.28 +add_task(function init() {
    1.29 +  // Set up profile. We create the directory in the profile, because the profile
    1.30 +  // is removed after every test run.
    1.31 +  do_get_profile();
    1.32 +  profileDir = OS.Constants.Path.profileDir;
    1.33 +  Services.prefs.setBoolPref("toolkit.osfile.log", true);
    1.34 +});
    1.35 +
    1.36 +/**
    1.37 + * Basic use
    1.38 + */
    1.39 +
    1.40 +add_task(function* test_basic() {
    1.41 +  let dir = Path.join(profileDir, "directory");
    1.42 +
    1.43 +  // Sanity checking for the test
    1.44 +  do_check_false((yield OS.File.exists(dir)));
    1.45 +
    1.46 +  // Make a directory
    1.47 +  yield OS.File.makeDir(dir);
    1.48 +
    1.49 +  //check if the directory exists
    1.50 +  yield OS.File.stat(dir);
    1.51 +
    1.52 +  // Make a directory that already exists, this should succeed
    1.53 +  yield OS.File.makeDir(dir);
    1.54 +
    1.55 +  // Make a directory with ignoreExisting
    1.56 +  yield OS.File.makeDir(dir, {ignoreExisting: true});
    1.57 +
    1.58 +  // Make a directory with ignoreExisting false
    1.59 +  let exception = null;
    1.60 +  try {
    1.61 +    yield OS.File.makeDir(dir, {ignoreExisting: false});
    1.62 +  } catch (ex) {
    1.63 +    exception = ex;
    1.64 +  }
    1.65 +
    1.66 +  do_check_true(!!exception);
    1.67 +  do_check_true(exception instanceof OS.File.Error);
    1.68 +  do_check_true(exception.becauseExists);
    1.69 +});
    1.70 +
    1.71 +// Make a root directory that already exists
    1.72 +add_task(function* test_root() {
    1.73 +  if (OS.Constants.Win) {
    1.74 +    yield OS.File.makeDir("C:");
    1.75 +    yield OS.File.makeDir("C:\\");
    1.76 +  } else {
    1.77 +    yield OS.File.makeDir("/");
    1.78 +  }
    1.79 +});
    1.80 +
    1.81 +/**
    1.82 + * Creating subdirectories
    1.83 + */
    1.84 +add_task(function test_option_from() {
    1.85 +  let dir = Path.join(profileDir, "a", "b", "c");
    1.86 +
    1.87 +  // Sanity checking for the test
    1.88 +  do_check_false((yield OS.File.exists(dir)));
    1.89 +
    1.90 +  // Make a directory
    1.91 +  yield OS.File.makeDir(dir, {from: profileDir});
    1.92 +
    1.93 +  //check if the directory exists
    1.94 +  yield OS.File.stat(dir);
    1.95 +
    1.96 +  // Make a directory that already exists, this should succeed
    1.97 +  yield OS.File.makeDir(dir);
    1.98 +
    1.99 +  // Make a directory with ignoreExisting
   1.100 +  yield OS.File.makeDir(dir, {ignoreExisting: true});
   1.101 +
   1.102 +  // Make a directory with ignoreExisting false
   1.103 +  let exception = null;
   1.104 +  try {
   1.105 +    yield OS.File.makeDir(dir, {ignoreExisting: false});
   1.106 +  } catch (ex) {
   1.107 +    exception = ex;
   1.108 +  }
   1.109 +
   1.110 +  do_check_true(!!exception);
   1.111 +  do_check_true(exception instanceof OS.File.Error);
   1.112 +  do_check_true(exception.becauseExists);
   1.113 +
   1.114 +  // Make a directory without |from| and fail
   1.115 +  let dir2 = Path.join(profileDir, "g", "h", "i");
   1.116 +  exception = null;
   1.117 +  try {
   1.118 +    yield OS.File.makeDir(dir2);
   1.119 +  } catch (ex) {
   1.120 +    exception = ex;
   1.121 +  }
   1.122 +
   1.123 +  do_check_true(!!exception);
   1.124 +  do_check_true(exception instanceof OS.File.Error);
   1.125 +  do_check_true(exception.becauseNoSuchFile);
   1.126 +});

mercurial