Bug 786211 - OS.File atomic write (test suite). r=froydnj
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Thu, 27 Sep 2012 23:06:00 -0400
changeset 108475 480fc4fb5ff7192e999f87dc44a89dafc79127ec
parent 108474 73a24144ec2d516d9bfd4c9d33648a101e0d0731
child 108476 b1a2469303333a4939bb8e25bd1914d1693e7573
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewersfroydnj
bugs786211
milestone18.0a1
Bug 786211 - OS.File atomic write (test suite). r=froydnj
toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
--- a/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
+++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
@@ -331,16 +331,69 @@ function test_readall_writeall_file()
   dest.write(readResult.buffer, {bytes: readResult.bytes});
 
   ok(true, "test_readall_writeall_file: copy complete (auto allocation)");
   source.close();
   dest.close();
 
   compare_files("test_readall_writeall_file (auto allocation)", src_file_name, tmp_file_name);
   OS.File.remove(tmp_file_name);
+
+  // File.readAll
+  readResult = OS.File.read(src_file_name);
+  is(readResult.bytes, size, "test_readall_writeall_file: read the right number of bytes (OS.File.readAll)");
+ 
+  // File.writeAtomic on top of nothing
+  OS.File.writeAtomic(tmp_file_name, readResult.buffer,
+    {bytes: readResult.bytes,
+     tmpPath: tmp_file_name + ".tmp"});
+  try {
+    let stat = OS.File.stat(tmp_file_name);
+    ok(true, "readAll + writeAtomic created a file");
+    is(stat.size, size, "readAll + writeAtomic created a file of the right size");
+  } catch (x) {
+    ok(false, "readAll + writeAtomic somehow failed");
+    if(x.becauseNoSuchFile) {
+      ok(false, "readAll + writeAtomic did not create file");
+    }
+  }
+  compare_files("test_readall_writeall_file (OS.File.readAll + writeAtomic)",
+                src_file_name, tmp_file_name);
+  exn = null;
+  try {
+    let stat = OS.File.stat(tmp_file_name + ".tmp");
+  } catch (x) {
+    exn = x;
+  }
+  ok(!!exn, "readAll + writeAtomic cleaned up after itself");
+
+
+  // File.writeAtomic on top of existing file
+  // Remove content and set arbitrary size, to avoid potential false negatives
+  dest = OS.File.open(tmp_file_name, {write: true, trunc:true});
+  dest.setPosition(1234);
+  dest.close();
+
+  OS.File.writeAtomic(tmp_file_name, readResult.buffer,
+    {bytes: readResult.bytes,
+     tmpPath: tmp_file_name + ".tmp"});
+  compare_files("test_readall_writeall_file (OS.File.readAll + writeAtomic 2)",
+                src_file_name, tmp_file_name);
+
+  // Ensure that File.writeAtomic fails if no temporary file name is provided
+  // (FIXME: Remove this test as part of bug 793660)
+
+  exn = null;
+  try {
+    OS.File.writeAtomic(tmp_file_name, readResult.buffer,
+      {bytes: readResult.bytes});
+  } catch (x) {
+    exn = x;
+  }
+  ok(!!exn && exn instanceof TypeError, "wrietAtomic fails if tmpPath is not provided");
 }
 
 /**
  * Test that copying a file using |copy| works.
  */
 function test_copy_existing_file()
 {
   let src_file_name = "chrome/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js";