|
1 "use strict"; |
|
2 |
|
3 Components.utils.import("resource://gre/modules/osfile.jsm"); |
|
4 Components.utils.import("resource://gre/modules/FileUtils.jsm"); |
|
5 Components.utils.import("resource://gre/modules/NetUtil.jsm"); |
|
6 Components.utils.import("resource://gre/modules/Promise.jsm"); |
|
7 Components.utils.import("resource://gre/modules/Task.jsm"); |
|
8 |
|
9 function run_test() { |
|
10 do_test_pending(); |
|
11 run_next_test(); |
|
12 } |
|
13 |
|
14 /** |
|
15 * A file that we know exists and that can be used for reading. |
|
16 */ |
|
17 let EXISTING_FILE = "test_osfile_async_copy.js"; |
|
18 |
|
19 /** |
|
20 * Fetch asynchronously the contents of a file using xpcom. |
|
21 * |
|
22 * Used for comparing xpcom-based results to os.file-based results. |
|
23 * |
|
24 * @param {string} path The _absolute_ path to the file. |
|
25 * @return {promise} |
|
26 * @resolves {string} The contents of the file. |
|
27 */ |
|
28 let reference_fetch_file = function reference_fetch_file(path) { |
|
29 let promise = Promise.defer(); |
|
30 let file = new FileUtils.File(path); |
|
31 NetUtil.asyncFetch(file, |
|
32 function(stream, status) { |
|
33 if (!Components.isSuccessCode(status)) { |
|
34 promise.reject(status); |
|
35 return; |
|
36 } |
|
37 let result, reject; |
|
38 try { |
|
39 result = NetUtil.readInputStreamToString(stream, stream.available()); |
|
40 } catch (x) { |
|
41 reject = x; |
|
42 } |
|
43 stream.close(); |
|
44 if (reject) { |
|
45 promise.reject(reject); |
|
46 } else { |
|
47 promise.resolve(result); |
|
48 } |
|
49 }); |
|
50 return promise.promise; |
|
51 }; |
|
52 |
|
53 /** |
|
54 * Compare asynchronously the contents two files using xpcom. |
|
55 * |
|
56 * Used for comparing xpcom-based results to os.file-based results. |
|
57 * |
|
58 * @param {string} a The _absolute_ path to the first file. |
|
59 * @param {string} b The _absolute_ path to the second file. |
|
60 * |
|
61 * @resolves {null} |
|
62 */ |
|
63 let reference_compare_files = function reference_compare_files(a, b) { |
|
64 let a_contents = yield reference_fetch_file(a); |
|
65 let b_contents = yield reference_fetch_file(b); |
|
66 // Not using do_check_eq to avoid dumping the whole file to the log. |
|
67 // It is OK to === compare here, as both variables contain a string. |
|
68 do_check_true(a_contents === b_contents); |
|
69 }; |
|
70 |
|
71 /** |
|
72 * Test to ensure that OS.File.copy works. |
|
73 */ |
|
74 function test_copymove(options = {}) { |
|
75 let source = OS.Path.join((yield OS.File.getCurrentDirectory()), |
|
76 EXISTING_FILE); |
|
77 let dest = OS.Path.join(OS.Constants.Path.tmpDir, |
|
78 "test_osfile_async_copy_dest.tmp"); |
|
79 let dest2 = OS.Path.join(OS.Constants.Path.tmpDir, |
|
80 "test_osfile_async_copy_dest2.tmp"); |
|
81 try { |
|
82 // 1. Test copy. |
|
83 yield OS.File.copy(source, dest, options); |
|
84 yield reference_compare_files(source, dest); |
|
85 // 2. Test subsequent move. |
|
86 yield OS.File.move(dest, dest2); |
|
87 yield reference_compare_files(source, dest2); |
|
88 // 3. Check that the moved file was really moved. |
|
89 do_check_eq((yield OS.File.exists(dest)), false); |
|
90 } finally { |
|
91 try { |
|
92 yield OS.File.remove(dest); |
|
93 } catch (ex if ex.becauseNoSuchFile) { |
|
94 // ignore |
|
95 } |
|
96 try { |
|
97 yield OS.File.remove(dest2); |
|
98 } catch (ex if ex.becauseNoSuchFile) { |
|
99 // ignore |
|
100 } |
|
101 } |
|
102 } |
|
103 |
|
104 // Regular copy test. |
|
105 add_task(test_copymove); |
|
106 // Userland copy test. |
|
107 add_task(test_copymove.bind(null, {unixUserland: true})); |
|
108 |
|
109 add_task(do_test_finished); |