toolkit/components/osfile/tests/xpcshell/test_read_write.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_read_write.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +"use strict";
     1.8 +
     1.9 +let {utils: Cu} = Components;
    1.10 +
    1.11 +let SHARED_PATH;
    1.12 +
    1.13 +let EXISTING_FILE = do_get_file("xpcshell.ini").path;
    1.14 +
    1.15 +add_task(function* init() {
    1.16 +  do_get_profile();
    1.17 +  SHARED_PATH = OS.Path.join(OS.Constants.Path.profileDir, "test_osfile_read.tmp");
    1.18 +});
    1.19 +
    1.20 +
    1.21 +// Check that OS.File.read() is executed after the previous operation
    1.22 +add_test_pair(function* ordering() {
    1.23 +  let string1 = "Initial state " + Math.random();
    1.24 +  let string2 = "After writing " + Math.random();
    1.25 +  yield OS.File.writeAtomic(SHARED_PATH, string1);
    1.26 +  OS.File.writeAtomic(SHARED_PATH, string2);
    1.27 +  let string3 = yield OS.File.read(SHARED_PATH, { encoding: "utf-8" });
    1.28 +  do_check_eq(string3, string2);
    1.29 +});
    1.30 +
    1.31 +add_test_pair(function* read_write_all() {
    1.32 +  let DEST_PATH = SHARED_PATH + Math.random();
    1.33 +  let TMP_PATH = DEST_PATH + ".tmp";
    1.34 +
    1.35 +  let test_with_options = function(options, suffix) {
    1.36 +    return Task.spawn(function*() {
    1.37 +      do_print("Running test read_write_all with options " + JSON.stringify(options));
    1.38 +      let TEST = "read_write_all " + suffix;
    1.39 +
    1.40 +      let optionsBackup = JSON.parse(JSON.stringify(options));
    1.41 +
    1.42 +      // Check that read + writeAtomic performs a correct copy
    1.43 +      let currentDir = yield OS.File.getCurrentDirectory();
    1.44 +      let pathSource = OS.Path.join(currentDir, EXISTING_FILE);
    1.45 +      let contents = yield OS.File.read(pathSource);
    1.46 +      do_check_true(!!contents); // Content is not empty
    1.47 +
    1.48 +      let bytesWritten = yield OS.File.writeAtomic(DEST_PATH, contents, options);
    1.49 +      do_check_eq(contents.byteLength, bytesWritten); // Correct number of bytes written
    1.50 +
    1.51 +      // Check that options are not altered
    1.52 +      do_check_eq(JSON.stringify(options), JSON.stringify(optionsBackup));
    1.53 +      yield reference_compare_files(pathSource, DEST_PATH, TEST);
    1.54 +
    1.55 +      // Check that temporary file was removed or never created exist
    1.56 +      do_check_false(new FileUtils.File(TMP_PATH).exists());
    1.57 +
    1.58 +      // Check that writeAtomic fails if noOverwrite is true and the destination
    1.59 +      // file already exists!
    1.60 +      let view = new Uint8Array(contents.buffer, 10, 200);
    1.61 +      try {
    1.62 +        let opt = JSON.parse(JSON.stringify(options));
    1.63 +        opt.noOverwrite = true;
    1.64 +        yield OS.File.writeAtomic(DEST_PATH, view, opt);
    1.65 +        do_throw("With noOverwrite, writeAtomic should have refused to overwrite file (" + suffix + ")");
    1.66 +      } catch (err if err instanceof OS.File.Error && err.becauseExists) {
    1.67 +        do_print("With noOverwrite, writeAtomic correctly failed (" + suffix + ")");
    1.68 +      }
    1.69 +      yield reference_compare_files(pathSource, DEST_PATH, TEST);
    1.70 +
    1.71 +      // Check that temporary file was removed or never created
    1.72 +      do_check_false(new FileUtils.File(TMP_PATH).exists());
    1.73 +
    1.74 +      // Now write a subset
    1.75 +      let START = 10;
    1.76 +      let LENGTH = 100;
    1.77 +      view = new Uint8Array(contents.buffer, START, LENGTH);
    1.78 +      bytesWritten = yield OS.File.writeAtomic(DEST_PATH, view, options);
    1.79 +      do_check_eq(bytesWritten, LENGTH);
    1.80 +
    1.81 +      let array2 = yield OS.File.read(DEST_PATH);
    1.82 +      let view1 = new Uint8Array(contents.buffer, START, LENGTH);
    1.83 +      do_check_eq(view1.length, array2.length);
    1.84 +      let decoder = new TextDecoder();
    1.85 +      do_check_eq(decoder.decode(view1), decoder.decode(array2));
    1.86 +
    1.87 +
    1.88 +      // Cleanup.
    1.89 +      yield OS.File.remove(DEST_PATH);
    1.90 +      yield OS.File.remove(TMP_PATH);
    1.91 +    });
    1.92 +  };
    1.93 +
    1.94 +  yield test_with_options({tmpPath: TMP_PATH}, "Renaming, not flushing");
    1.95 +  yield test_with_options({tmpPath: TMP_PATH, flush: true}, "Renaming, flushing");
    1.96 +  yield test_with_options({}, "Not renaming, not flushing");
    1.97 +  yield test_with_options({flush: true}, "Not renaming, flushing");
    1.98 +});
    1.99 +
   1.100 +
   1.101 +function run_test() {
   1.102 +  run_next_test();
   1.103 +}

mercurial