michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: Components.utils.import("resource://gre/modules/osfile.jsm"); michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: let Path = OS.Path; michael@0: let profileDir; michael@0: michael@0: do_register_cleanup(function() { michael@0: Services.prefs.setBoolPref("toolkit.osfile.log", false); michael@0: }); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: /** michael@0: * Test OS.File.makeDir michael@0: */ michael@0: michael@0: add_task(function init() { michael@0: // Set up profile. We create the directory in the profile, because the profile michael@0: // is removed after every test run. michael@0: do_get_profile(); michael@0: profileDir = OS.Constants.Path.profileDir; michael@0: Services.prefs.setBoolPref("toolkit.osfile.log", true); michael@0: }); michael@0: michael@0: /** michael@0: * Basic use michael@0: */ michael@0: michael@0: add_task(function* test_basic() { michael@0: let dir = Path.join(profileDir, "directory"); michael@0: michael@0: // Sanity checking for the test michael@0: do_check_false((yield OS.File.exists(dir))); michael@0: michael@0: // Make a directory michael@0: yield OS.File.makeDir(dir); michael@0: michael@0: //check if the directory exists michael@0: yield OS.File.stat(dir); michael@0: michael@0: // Make a directory that already exists, this should succeed michael@0: yield OS.File.makeDir(dir); michael@0: michael@0: // Make a directory with ignoreExisting michael@0: yield OS.File.makeDir(dir, {ignoreExisting: true}); michael@0: michael@0: // Make a directory with ignoreExisting false michael@0: let exception = null; michael@0: try { michael@0: yield OS.File.makeDir(dir, {ignoreExisting: false}); michael@0: } catch (ex) { michael@0: exception = ex; michael@0: } michael@0: michael@0: do_check_true(!!exception); michael@0: do_check_true(exception instanceof OS.File.Error); michael@0: do_check_true(exception.becauseExists); michael@0: }); michael@0: michael@0: // Make a root directory that already exists michael@0: add_task(function* test_root() { michael@0: if (OS.Constants.Win) { michael@0: yield OS.File.makeDir("C:"); michael@0: yield OS.File.makeDir("C:\\"); michael@0: } else { michael@0: yield OS.File.makeDir("/"); michael@0: } michael@0: }); michael@0: michael@0: /** michael@0: * Creating subdirectories michael@0: */ michael@0: add_task(function test_option_from() { michael@0: let dir = Path.join(profileDir, "a", "b", "c"); michael@0: michael@0: // Sanity checking for the test michael@0: do_check_false((yield OS.File.exists(dir))); michael@0: michael@0: // Make a directory michael@0: yield OS.File.makeDir(dir, {from: profileDir}); michael@0: michael@0: //check if the directory exists michael@0: yield OS.File.stat(dir); michael@0: michael@0: // Make a directory that already exists, this should succeed michael@0: yield OS.File.makeDir(dir); michael@0: michael@0: // Make a directory with ignoreExisting michael@0: yield OS.File.makeDir(dir, {ignoreExisting: true}); michael@0: michael@0: // Make a directory with ignoreExisting false michael@0: let exception = null; michael@0: try { michael@0: yield OS.File.makeDir(dir, {ignoreExisting: false}); michael@0: } catch (ex) { michael@0: exception = ex; michael@0: } michael@0: michael@0: do_check_true(!!exception); michael@0: do_check_true(exception instanceof OS.File.Error); michael@0: do_check_true(exception.becauseExists); michael@0: michael@0: // Make a directory without |from| and fail michael@0: let dir2 = Path.join(profileDir, "g", "h", "i"); michael@0: exception = null; michael@0: try { michael@0: yield OS.File.makeDir(dir2); michael@0: } catch (ex) { michael@0: exception = ex; michael@0: } michael@0: michael@0: do_check_true(!!exception); michael@0: do_check_true(exception instanceof OS.File.Error); michael@0: do_check_true(exception.becauseNoSuchFile); michael@0: });