Bug 644680 - Tests for FileUtils.jsm. r=sdwilsh
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Mon, 18 Apr 2011 15:41:09 -0700
changeset 68501 e3e1ade248ca62fe89b9aec2908a45aee2f0ee54
parent 68500 37ad2e03e38e8c07b10ba51eb88194af5844106c
child 68502 b3dbcd2947648bb48bdbb3b75e7dfe83579214a8
push id76
push userbzbarsky@mozilla.com
push dateTue, 05 Jul 2011 17:00:57 +0000
treeherdermozilla-beta@d3a2732c35f1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssdwilsh
bugs644680
milestone6.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 644680 - Tests for FileUtils.jsm. r=sdwilsh
toolkit/mozapps/shared/FileUtils.jsm
toolkit/mozapps/shared/Makefile.in
toolkit/mozapps/shared/test/unit/test_FileUtils.js
--- a/toolkit/mozapps/shared/FileUtils.jsm
+++ b/toolkit/mozapps/shared/FileUtils.jsm
@@ -54,17 +54,17 @@ var FileUtils = {
   MODE_CREATE   : 0x08,
   MODE_APPEND   : 0x10,
   MODE_TRUNCATE : 0x20,
 
   PERMS_FILE      : 0644,
   PERMS_DIRECTORY : 0755,
 
   /**
-   * Gets the file at the specified hierarchy under a Directory Service key.
+   * Gets a file at the specified hierarchy under a nsIDirectoryService key.
    * @param   key
    *          The Directory Service Key to start from
    * @param   pathArray
    *          An array of path components to locate beneath the directory
    *          specified by |key|. The last item in this array must be the
    *          leaf name of a file.
    * @return  nsIFile object for the file specified. The file is NOT created
    *          if it does not exist, however all required directories along
@@ -72,31 +72,29 @@ var FileUtils = {
    */
   getFile: function FileUtils_getFile(key, pathArray, followLinks) {
     var file = this.getDir(key, pathArray.slice(0, -1), true, followLinks);
     file.append(pathArray[pathArray.length - 1]);
     return file;
   },
 
   /**
-   * Gets the specified directory at the specified hierarchy under a
-   * Directory Service key.
+   * Gets a directory at the specified hierarchy under a nsIDirectoryService
+   * key.
    * @param   key
    *          The Directory Service Key to start from
    * @param   pathArray
    *          An array of path components to locate beneath the directory
    *          specified by |key|
    * @param   shouldCreate
    *          true if the directory hierarchy specified in |pathArray|
    *          should be created if it does not exist, false otherwise.
    * @param   followLinks (optional)
    *          true if links should be followed, false otherwise.
-   * @return  nsIFile object for the location specified. If the directory
-   *          requested does not exist, it is created, along with any
-   *          parent directories that need to be created.
+   * @return  nsIFile object for the location specified.
    */
   getDir: function FileUtils_getDir(key, pathArray, shouldCreate, followLinks) {
     var dir = gDirService.get(key, Ci.nsILocalFile);
     for (var i = 0; i < pathArray.length; ++i) {
       dir.append(pathArray[i]);
       if (shouldCreate && !dir.exists())
         dir.create(Ci.nsILocalFile.DIRECTORY_TYPE, this.PERMS_DIRECTORY);
     }
--- a/toolkit/mozapps/shared/Makefile.in
+++ b/toolkit/mozapps/shared/Makefile.in
@@ -34,23 +34,25 @@
 # the terms of any one of the MPL, the GPL or the LGPL.
 #
 # ***** END LICENSE BLOCK *****
 
 DEPTH     = ../../..
 topsrcdir = @top_srcdir@
 srcdir    = @srcdir@
 VPATH     = @srcdir@
+relativesrcdir = toolkit/mozapps/shared
 
 include $(DEPTH)/config/autoconf.mk
 
 MODULE = toolkitShared
 
 EXTRA_PP_JS_MODULES = \
   CertUtils.jsm \
   FileUtils.jsm \
   $(NULL)
 
 ifdef ENABLE_TESTS
 DIRS += test/chrome
+XPCSHELL_TESTS = test/unit
 endif
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/toolkit/mozapps/shared/test/unit/test_FileUtils.js
@@ -0,0 +1,146 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+Components.utils.import("resource://gre/modules/FileUtils.jsm");
+
+function do_check_throws(f, result, stack) {
+  if (!stack)
+    stack = Components.stack.caller;
+
+  try {
+    f();
+  } catch (exc) {
+    if (exc.result == result)
+      return;
+    do_throw("expected result " + result + ", caught " + exc, stack);
+  }
+  do_throw("expected result " + result + ", none thrown", stack);
+}
+
+const gProfD = do_get_profile();
+
+add_test(function test_getFile() {
+  let file = FileUtils.getFile("ProfD", ["foobar"]);
+  do_check_true(file instanceof Components.interfaces.nsIFile);
+  do_check_false(file.exists());
+
+  let other = gProfD.clone();
+  other.append("foobar");
+  do_check_true(file.equals(other));
+
+  run_next_test();
+});
+
+add_test(function test_getFile_nonexistentDir() {
+  do_check_throws(function () {
+    let file = FileUtils.getFile("NonexistentD", ["foobar"]);
+  }, Components.results.NS_ERROR_FAILURE);
+
+  run_next_test();
+});
+
+add_test(function test_getFile_createDirs() {
+  let file = FileUtils.getFile("ProfD", ["a", "b", "foobar"]);
+  do_check_true(file instanceof Components.interfaces.nsIFile);
+  do_check_false(file.exists());
+
+  let other = gProfD.clone();
+  other.append("a");
+  do_check_true(other.isDirectory());
+  other.append("b");
+  do_check_true(other.isDirectory());
+  other.append("foobar");
+  do_check_true(file.equals(other));
+
+  run_next_test();
+});
+
+add_test(function test_getDir() {
+  let dir = FileUtils.getDir("ProfD", ["foodir"]);
+  do_check_true(dir instanceof Components.interfaces.nsIFile);
+  do_check_false(dir.exists());
+
+  let other = gProfD.clone();
+  other.append("foodir");
+  do_check_true(dir.equals(other));
+
+  run_next_test();
+});
+
+add_test(function test_getDir_nonexistentDir() {
+  do_check_throws(function () {
+    let file = FileUtils.getDir("NonexistentD", ["foodir"]);
+  }, Components.results.NS_ERROR_FAILURE);
+
+  run_next_test();
+});
+
+add_test(function test_getDir_shouldCreate() {
+  let dir = FileUtils.getDir("ProfD", ["c", "d", "foodir"], true);
+  do_check_true(dir instanceof Components.interfaces.nsIFile);
+  do_check_true(dir.exists());
+
+  let other = gProfD.clone();
+  other.append("c");
+  do_check_true(other.isDirectory());
+  other.append("d");
+  do_check_true(other.isDirectory());
+  other.append("foodir");
+  do_check_true(dir.equals(other));
+
+  run_next_test();
+});
+
+add_test(function test_openSafeFileOutputStream_defaultFlags() {
+  let file = FileUtils.getFile("ProfD", ["john"]);
+  let fos = FileUtils.openSafeFileOutputStream(file);
+  do_check_true(fos instanceof Components.interfaces.nsIFileOutputStream);
+  do_check_true(fos instanceof Components.interfaces.nsISafeOutputStream);
+
+  //TODO FileUtils.openSafeFileOutputStream() should use DEFER_OPEN in which
+  // case we want to verify that the file hasn't been created yet and gets
+  // created once we write to it.
+  do_check_true(file.exists());
+
+  // No nsIXULRuntime in xpcshell, so use this trick to determine whether we're
+  // on Windows.
+  if ("@mozilla.org/windows-registry-key;1" in Components.classes) {
+    do_check_eq(file.permissions, 0666);
+  } else {
+    do_check_eq(file.permissions, FileUtils.PERMS_FILE);
+  }
+
+  run_next_test();
+});
+
+// openSafeFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE
+// as the default mode flags, but we can pass in our own if we want to.
+add_test(function test_openSafeFileOutputStream_modeFlags() {
+  let file = FileUtils.getFile("ProfD", ["paul"]);
+  do_check_throws(function () {
+    FileUtils.openSafeFileOutputStream(file, FileUtils.MODE_WRONLY);
+  }, Components.results.NS_ERROR_FILE_NOT_FOUND);
+  do_check_false(file.exists());
+
+  run_next_test();
+});
+
+add_test(function test_closeSafeFileOutputStream() {
+  let file = FileUtils.getFile("ProfD", ["george"]);
+  let fos = FileUtils.openSafeFileOutputStream(file);
+
+  // We can write data to the stream just fine while it's open.
+  let data = "here comes the sun";
+  fos.write(data, data.length);
+
+  // But once we close it, we can't anymore.
+  FileUtils.closeSafeFileOutputStream(fos);
+  do_check_throws(function () {
+    fos.write(data, data.length);
+  }, Components.results.NS_BASE_STREAM_CLOSED);
+  run_next_test();
+});
+
+function run_test() {
+  run_next_test();
+}