Bug 840887 - Test the behavior of DirectoryIterator when the directory doesn't exist. r=froydnj
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Tue, 26 Feb 2013 12:08:51 -0500
changeset 123039 cf2f3b45cb3837773293e38186de7b4cefe58b27
parent 123038 65108c196870bc6a9c7fc787559367caeabc0d2f
child 123040 f613612cb05c3cc8a09524797c689f428aac028b
push id24372
push useremorley@mozilla.com
push dateWed, 27 Feb 2013 13:22:59 +0000
treeherdermozilla-central@0a91da5f5eab [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs840887
milestone22.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 840887 - Test the behavior of DirectoryIterator when the directory doesn't exist. r=froydnj
toolkit/components/osfile/tests/mochi/main_test_osfile_async.js
--- a/toolkit/components/osfile/tests/mochi/main_test_osfile_async.js
+++ b/toolkit/components/osfile/tests/mochi/main_test_osfile_async.js
@@ -119,16 +119,26 @@ let reference_fetch_file = function refe
  */
 let reference_compare_files = function reference_compare_files(a, b, test) {
   test.info("Comparing files " + a + " and " + b);
   let a_contents = yield reference_fetch_file(a, test);
   let b_contents = yield reference_fetch_file(b, test);
   is(a_contents, b_contents, "Contents of files " + a + " and " + b + " match");
 };
 
+let reference_dir_contents = function reference_dir_contents(path) {
+  let result = [];
+  let entries = new FileUtils.File(path).directoryEntries;
+  while (entries.hasMoreElements()) {
+    let entry = entries.getNext().QueryInterface(Components.interfaces.nsILocalFile);
+    result.push(entry.path);
+  }
+  return result;
+};
+
 let test = maketest("Main", function main(test) {
   return Task.spawn(function() {
     SimpleTest.waitForExplicitFinish();
     yield test_constants();
     yield test_path();
     yield test_open();
     yield test_stat();
     yield test_debug();
@@ -526,17 +536,17 @@ let test_copy = maketest("copy", functio
       test.ok(err, "Could not open a file after it has been moved away " + err);
       test.ok(err instanceof OS.File.Error, "Error is an OS.File.Error");
       test.ok(err.becauseNoSuchFile, "Error mentions that the file does not exist");
     }
   });
 });
 
 /**
- * Test OS.File.prototype.{removeEmptyDir, makeDir}
+ * Test OS.File.{removeEmptyDir, makeDir}
  */
 let test_mkdir = maketest("mkdir", function mkdir(test) {
   return Task.spawn(function() {
     const DIRNAME = "test_dir.tmp";
 
     // Cleanup
     yield OS.File.removeEmptyDir(DIRNAME, {ignoreAbsent: true});
 
@@ -607,16 +617,27 @@ let test_iter = maketest("iter", functio
       yield OS.File.remove(temporary_file_name);
     } catch (err) {
       // Ignore errors removing file
     }
     let allFiles1 = yield iterator.nextBatch();
     test.info("Obtained all files through nextBatch");
     test.isnot(allFiles1.length, 0, "There is at least one file");
     test.isnot(allFiles1[0].path, null, "Files have a path");
+
+    // Ensure that we have the same entries with |reference_dir_contents|
+    let referenceEntries = new Set();
+    for (let entry of reference_dir_contents(currentDir)) {
+      referenceEntries.add(entry);
+    }
+    test.is(referenceEntries.size, allFiles1.length, "All the entries in the directory have been listed");
+    for (let entry of allFiles1) {
+      test.ok(referenceEntries.has(entry.path), "File " + entry.path + " effectively exists");
+    }
+
     yield iterator.close();
     test.info("Closed iterator");
 
     test.info("Double closing DirectoryIterator");
     iterator = new OS.File.DirectoryIterator(currentDir);
     yield iterator.close();
     yield iterator.close(); //double closing |DirectoryIterator|
     test.ok(true, "|DirectoryIterator| was closed twice successfully");
@@ -664,19 +685,44 @@ let test_iter = maketest("iter", functio
 
     // Ensuring that we find new files if they appear
     let file = yield OS.File.open(temporary_file_name, { write: true } );
     file.close();
     iterator = new OS.File.DirectoryIterator(currentDir);
     try {
       let files = yield iterator.nextBatch();
       is(files.length, allFiles1.length + 1, "The directory iterator has noticed the new file");
+      let exists = yield iterator.exists();
+      test.ok(exists, "After nextBatch, iterator detects that the directory exists");
     } finally {
       yield iterator.close();
     }
+
+    // Ensuring that opening a non-existing directory fails consistently
+    // once iteration starts.
+    try {
+      iterator = null;
+      iterator = new OS.File.DirectoryIterator("/I do not exist");
+      let exists = yield iterator.exists();
+      test.ok(!exists, "Before any iteration, iterator detects that the directory doesn't exist");
+      let exn = null;
+      try {
+        yield iterator.next();
+      } catch (ex if ex instanceof OS.File.Error && ex.becauseNoSuchFile) {
+        exn = ex;
+        let exists = yield iterator.exists();
+        test.ok(!exists, "After one iteration, iterator detects that the directory doesn't exist");
+      }
+      test.ok(exn, "Iterating through a directory that does not exist has failed with becauseNoSuchFile");
+    } finally {
+      if (iterator) {
+        iterator.close();
+      }
+    }
+    test.ok(!!iterator, "The directory iterator for a non-existing directory was correctly created");
   });
 });
 
 /**
  * Test OS.File.prototype.{exists}
  */
 let test_exists = maketest("exists", function exists(test) {
   return Task.spawn(function() {