michael@0: "use strict"; michael@0: michael@0: Components.utils.import("resource://gre/modules/osfile.jsm"); michael@0: Components.utils.import("resource://gre/modules/FileUtils.jsm"); michael@0: Components.utils.import("resource://gre/modules/NetUtil.jsm"); michael@0: Components.utils.import("resource://gre/modules/Promise.jsm"); michael@0: Components.utils.import("resource://gre/modules/Task.jsm"); michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: run_next_test(); michael@0: } michael@0: michael@0: /** michael@0: * A file that we know exists and that can be used for reading. michael@0: */ michael@0: let EXISTING_FILE = "test_osfile_async_copy.js"; michael@0: michael@0: /** michael@0: * Fetch asynchronously the contents of a file using xpcom. michael@0: * michael@0: * Used for comparing xpcom-based results to os.file-based results. michael@0: * michael@0: * @param {string} path The _absolute_ path to the file. michael@0: * @return {promise} michael@0: * @resolves {string} The contents of the file. michael@0: */ michael@0: let reference_fetch_file = function reference_fetch_file(path) { michael@0: let promise = Promise.defer(); michael@0: let file = new FileUtils.File(path); michael@0: NetUtil.asyncFetch(file, michael@0: function(stream, status) { michael@0: if (!Components.isSuccessCode(status)) { michael@0: promise.reject(status); michael@0: return; michael@0: } michael@0: let result, reject; michael@0: try { michael@0: result = NetUtil.readInputStreamToString(stream, stream.available()); michael@0: } catch (x) { michael@0: reject = x; michael@0: } michael@0: stream.close(); michael@0: if (reject) { michael@0: promise.reject(reject); michael@0: } else { michael@0: promise.resolve(result); michael@0: } michael@0: }); michael@0: return promise.promise; michael@0: }; michael@0: michael@0: /** michael@0: * Compare asynchronously the contents two files using xpcom. michael@0: * michael@0: * Used for comparing xpcom-based results to os.file-based results. michael@0: * michael@0: * @param {string} a The _absolute_ path to the first file. michael@0: * @param {string} b The _absolute_ path to the second file. michael@0: * michael@0: * @resolves {null} michael@0: */ michael@0: let reference_compare_files = function reference_compare_files(a, b) { michael@0: let a_contents = yield reference_fetch_file(a); michael@0: let b_contents = yield reference_fetch_file(b); michael@0: // Not using do_check_eq to avoid dumping the whole file to the log. michael@0: // It is OK to === compare here, as both variables contain a string. michael@0: do_check_true(a_contents === b_contents); michael@0: }; michael@0: michael@0: /** michael@0: * Test to ensure that OS.File.copy works. michael@0: */ michael@0: function test_copymove(options = {}) { michael@0: let source = OS.Path.join((yield OS.File.getCurrentDirectory()), michael@0: EXISTING_FILE); michael@0: let dest = OS.Path.join(OS.Constants.Path.tmpDir, michael@0: "test_osfile_async_copy_dest.tmp"); michael@0: let dest2 = OS.Path.join(OS.Constants.Path.tmpDir, michael@0: "test_osfile_async_copy_dest2.tmp"); michael@0: try { michael@0: // 1. Test copy. michael@0: yield OS.File.copy(source, dest, options); michael@0: yield reference_compare_files(source, dest); michael@0: // 2. Test subsequent move. michael@0: yield OS.File.move(dest, dest2); michael@0: yield reference_compare_files(source, dest2); michael@0: // 3. Check that the moved file was really moved. michael@0: do_check_eq((yield OS.File.exists(dest)), false); michael@0: } finally { michael@0: try { michael@0: yield OS.File.remove(dest); michael@0: } catch (ex if ex.becauseNoSuchFile) { michael@0: // ignore michael@0: } michael@0: try { michael@0: yield OS.File.remove(dest2); michael@0: } catch (ex if ex.becauseNoSuchFile) { michael@0: // ignore michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Regular copy test. michael@0: add_task(test_copymove); michael@0: // Userland copy test. michael@0: add_task(test_copymove.bind(null, {unixUserland: true})); michael@0: michael@0: add_task(do_test_finished);