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/Task.jsm"); michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); 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: Services.prefs.setBoolPref("toolkit.osfile.log", true); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function() { 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: michael@0: let file = OS.Path.join(OS.Constants.Path.profileDir, "file"); michael@0: let dir = OS.Path.join(OS.Constants.Path.profileDir, "directory"); michael@0: let file1 = OS.Path.join(dir, "file1"); michael@0: let file2 = OS.Path.join(dir, "file2"); michael@0: let subDir = OS.Path.join(dir, "subdir"); michael@0: let fileInSubDir = OS.Path.join(subDir, "file"); michael@0: michael@0: // Sanity checking for the test michael@0: do_check_false((yield OS.File.exists(dir))); michael@0: michael@0: // Remove non-existent directory michael@0: let exception = null; michael@0: try { michael@0: yield OS.File.removeDir(dir, {ignoreAbsent: 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: michael@0: // Remove non-existent directory with ignoreAbsent michael@0: yield OS.File.removeDir(dir, {ignoreAbsent: true}); michael@0: yield OS.File.removeDir(dir); michael@0: michael@0: // Remove file with ignoreAbsent: false michael@0: yield OS.File.writeAtomic(file, "content", { tmpPath: file + ".tmp" }); michael@0: exception = null; michael@0: try { michael@0: yield OS.File.removeDir(file, {ignoreAbsent: 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: michael@0: // Remove empty directory michael@0: yield OS.File.makeDir(dir); michael@0: yield OS.File.removeDir(dir); michael@0: do_check_false((yield OS.File.exists(dir))); michael@0: michael@0: // Remove directory that contains one file michael@0: yield OS.File.makeDir(dir); michael@0: yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" }); michael@0: yield OS.File.removeDir(dir); michael@0: do_check_false((yield OS.File.exists(dir))); michael@0: michael@0: // Remove directory that contains multiple files michael@0: yield OS.File.makeDir(dir); michael@0: yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" }); michael@0: yield OS.File.writeAtomic(file2, "content", { tmpPath: file2 + ".tmp" }); michael@0: yield OS.File.removeDir(dir); michael@0: do_check_false((yield OS.File.exists(dir))); michael@0: michael@0: // Remove directory that contains a file and a directory michael@0: yield OS.File.makeDir(dir); michael@0: yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" }); michael@0: yield OS.File.makeDir(subDir); michael@0: yield OS.File.writeAtomic(fileInSubDir, "content", { tmpPath: fileInSubDir + ".tmp" }); michael@0: yield OS.File.removeDir(dir); michael@0: do_check_false((yield OS.File.exists(dir))); michael@0: }); michael@0: michael@0: add_task(function* test_unix_symlink() { michael@0: // Windows does not implement OS.File.unixSymLink() michael@0: if (OS.Constants.Win) { michael@0: return; michael@0: } michael@0: michael@0: let file = OS.Path.join(OS.Constants.Path.profileDir, "file"); michael@0: let dir = OS.Path.join(OS.Constants.Path.profileDir, "directory"); michael@0: let file1 = OS.Path.join(dir, "file1"); michael@0: michael@0: // This test will create the following directory structure: michael@0: // /file (regular file) michael@0: // /file.link => file (symlink) michael@0: // /directory (directory) michael@0: // /linkdir => directory (directory) michael@0: // /directory/file1 (regular file) michael@0: // /directory3 (directory) michael@0: // /directory3/file3 (directory) michael@0: // /directory/link2 => ../directory3 (regular file) michael@0: michael@0: // Sanity checking for the test michael@0: do_check_false((yield OS.File.exists(dir))); michael@0: michael@0: yield OS.File.writeAtomic(file, "content", { tmpPath: file + ".tmp" }); michael@0: do_check_true((yield OS.File.exists(file))); michael@0: let info = yield OS.File.stat(file, {unixNoFollowingLinks: true}); michael@0: do_check_false(info.isDir); michael@0: do_check_false(info.isSymLink); michael@0: michael@0: yield OS.File.unixSymLink(file, file + ".link"); michael@0: do_check_true((yield OS.File.exists(file + ".link"))); michael@0: info = yield OS.File.stat(file + ".link", {unixNoFollowingLinks: true}); michael@0: do_check_false(info.isDir); michael@0: do_check_true(info.isSymLink); michael@0: info = yield OS.File.stat(file + ".link"); michael@0: do_check_false(info.isDir); michael@0: do_check_false(info.isSymLink); michael@0: yield OS.File.remove(file + ".link"); michael@0: do_check_false((yield OS.File.exists(file + ".link"))); michael@0: michael@0: yield OS.File.makeDir(dir); michael@0: do_check_true((yield OS.File.exists(dir))); michael@0: info = yield OS.File.stat(dir, {unixNoFollowingLinks: true}); michael@0: do_check_true(info.isDir); michael@0: do_check_false(info.isSymLink); michael@0: michael@0: let link = OS.Path.join(OS.Constants.Path.profileDir, "linkdir"); michael@0: michael@0: yield OS.File.unixSymLink(dir, link); michael@0: do_check_true((yield OS.File.exists(link))); michael@0: info = yield OS.File.stat(link, {unixNoFollowingLinks: true}); michael@0: do_check_false(info.isDir); michael@0: do_check_true(info.isSymLink); michael@0: info = yield OS.File.stat(link); michael@0: do_check_true(info.isDir); michael@0: do_check_false(info.isSymLink); michael@0: michael@0: let dir3 = OS.Path.join(OS.Constants.Path.profileDir, "directory3"); michael@0: let file3 = OS.Path.join(dir3, "file3"); michael@0: let link2 = OS.Path.join(dir, "link2"); michael@0: michael@0: yield OS.File.writeAtomic(file1, "content", { tmpPath: file1 + ".tmp" }); michael@0: do_check_true((yield OS.File.exists(file1))); michael@0: yield OS.File.makeDir(dir3); michael@0: do_check_true((yield OS.File.exists(dir3))); michael@0: yield OS.File.writeAtomic(file3, "content", { tmpPath: file3 + ".tmp" }); michael@0: do_check_true((yield OS.File.exists(file3))); michael@0: yield OS.File.unixSymLink("../directory3", link2); michael@0: do_check_true((yield OS.File.exists(link2))); michael@0: michael@0: yield OS.File.removeDir(link); michael@0: do_check_false((yield OS.File.exists(link))); michael@0: do_check_true((yield OS.File.exists(file1))); michael@0: yield OS.File.removeDir(dir); michael@0: do_check_false((yield OS.File.exists(dir))); michael@0: do_check_true((yield OS.File.exists(file3))); michael@0: yield OS.File.removeDir(dir3); michael@0: do_check_false((yield OS.File.exists(dir3))); michael@0: michael@0: // This task will be executed only on Unix-like systems. michael@0: // Please do not add tests independent to operating systems here michael@0: // or implement symlink() on Windows. michael@0: });