1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_osfile_async_copy.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,109 @@ 1.4 +"use strict"; 1.5 + 1.6 +Components.utils.import("resource://gre/modules/osfile.jsm"); 1.7 +Components.utils.import("resource://gre/modules/FileUtils.jsm"); 1.8 +Components.utils.import("resource://gre/modules/NetUtil.jsm"); 1.9 +Components.utils.import("resource://gre/modules/Promise.jsm"); 1.10 +Components.utils.import("resource://gre/modules/Task.jsm"); 1.11 + 1.12 +function run_test() { 1.13 + do_test_pending(); 1.14 + run_next_test(); 1.15 +} 1.16 + 1.17 +/** 1.18 + * A file that we know exists and that can be used for reading. 1.19 + */ 1.20 +let EXISTING_FILE = "test_osfile_async_copy.js"; 1.21 + 1.22 +/** 1.23 + * Fetch asynchronously the contents of a file using xpcom. 1.24 + * 1.25 + * Used for comparing xpcom-based results to os.file-based results. 1.26 + * 1.27 + * @param {string} path The _absolute_ path to the file. 1.28 + * @return {promise} 1.29 + * @resolves {string} The contents of the file. 1.30 + */ 1.31 +let reference_fetch_file = function reference_fetch_file(path) { 1.32 + let promise = Promise.defer(); 1.33 + let file = new FileUtils.File(path); 1.34 + NetUtil.asyncFetch(file, 1.35 + function(stream, status) { 1.36 + if (!Components.isSuccessCode(status)) { 1.37 + promise.reject(status); 1.38 + return; 1.39 + } 1.40 + let result, reject; 1.41 + try { 1.42 + result = NetUtil.readInputStreamToString(stream, stream.available()); 1.43 + } catch (x) { 1.44 + reject = x; 1.45 + } 1.46 + stream.close(); 1.47 + if (reject) { 1.48 + promise.reject(reject); 1.49 + } else { 1.50 + promise.resolve(result); 1.51 + } 1.52 + }); 1.53 + return promise.promise; 1.54 +}; 1.55 + 1.56 +/** 1.57 + * Compare asynchronously the contents two files using xpcom. 1.58 + * 1.59 + * Used for comparing xpcom-based results to os.file-based results. 1.60 + * 1.61 + * @param {string} a The _absolute_ path to the first file. 1.62 + * @param {string} b The _absolute_ path to the second file. 1.63 + * 1.64 + * @resolves {null} 1.65 + */ 1.66 +let reference_compare_files = function reference_compare_files(a, b) { 1.67 + let a_contents = yield reference_fetch_file(a); 1.68 + let b_contents = yield reference_fetch_file(b); 1.69 + // Not using do_check_eq to avoid dumping the whole file to the log. 1.70 + // It is OK to === compare here, as both variables contain a string. 1.71 + do_check_true(a_contents === b_contents); 1.72 +}; 1.73 + 1.74 +/** 1.75 + * Test to ensure that OS.File.copy works. 1.76 + */ 1.77 +function test_copymove(options = {}) { 1.78 + let source = OS.Path.join((yield OS.File.getCurrentDirectory()), 1.79 + EXISTING_FILE); 1.80 + let dest = OS.Path.join(OS.Constants.Path.tmpDir, 1.81 + "test_osfile_async_copy_dest.tmp"); 1.82 + let dest2 = OS.Path.join(OS.Constants.Path.tmpDir, 1.83 + "test_osfile_async_copy_dest2.tmp"); 1.84 + try { 1.85 + // 1. Test copy. 1.86 + yield OS.File.copy(source, dest, options); 1.87 + yield reference_compare_files(source, dest); 1.88 + // 2. Test subsequent move. 1.89 + yield OS.File.move(dest, dest2); 1.90 + yield reference_compare_files(source, dest2); 1.91 + // 3. Check that the moved file was really moved. 1.92 + do_check_eq((yield OS.File.exists(dest)), false); 1.93 + } finally { 1.94 + try { 1.95 + yield OS.File.remove(dest); 1.96 + } catch (ex if ex.becauseNoSuchFile) { 1.97 + // ignore 1.98 + } 1.99 + try { 1.100 + yield OS.File.remove(dest2); 1.101 + } catch (ex if ex.becauseNoSuchFile) { 1.102 + // ignore 1.103 + } 1.104 + } 1.105 +} 1.106 + 1.107 +// Regular copy test. 1.108 +add_task(test_copymove); 1.109 +// Userland copy test. 1.110 +add_task(test_copymove.bind(null, {unixUserland: true})); 1.111 + 1.112 +add_task(do_test_finished);