Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 let {utils: Cu, interfaces: Ci} = Components;
8 let {OS} = Cu.import("resource://gre/modules/osfile.jsm", {});
9 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
10 let {Promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
11 let {Task} = Cu.import("resource://gre/modules/Task.jsm", {});
12 let {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", {});
13 let {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", {});
15 Services.prefs.setBoolPref("toolkit.osfile.log", true);
17 /**
18 * As add_task, but execute the test both with native operations and
19 * without.
20 */
21 function add_test_pair(generator) {
22 add_task(function*() {
23 do_print("Executing test " + generator.name + " with native operations");
24 Services.prefs.setBoolPref("toolkit.osfile.native", true);
25 return Task.spawn(generator);
26 });
27 add_task(function*() {
28 do_print("Executing test " + generator.name + " without native operations");
29 Services.prefs.setBoolPref("toolkit.osfile.native", false);
30 return Task.spawn(generator);
31 });
32 }
34 /**
35 * Fetch asynchronously the contents of a file using xpcom.
36 *
37 * Used for comparing xpcom-based results to os.file-based results.
38 *
39 * @param {string} path The _absolute_ path to the file.
40 * @return {promise}
41 * @resolves {string} The contents of the file.
42 */
43 function reference_fetch_file(path, test) {
44 do_print("Fetching file " + path);
45 let deferred = Promise.defer();
46 let file = new FileUtils.File(path);
47 NetUtil.asyncFetch(file,
48 function(stream, status) {
49 if (!Components.isSuccessCode(status)) {
50 deferred.reject(status);
51 return;
52 }
53 let result, reject;
54 try {
55 result = NetUtil.readInputStreamToString(stream, stream.available());
56 } catch (x) {
57 reject = x;
58 }
59 stream.close();
60 if (reject) {
61 deferred.reject(reject);
62 } else {
63 deferred.resolve(result);
64 }
65 });
66 return deferred.promise;
67 };
69 /**
70 * Compare asynchronously the contents two files using xpcom.
71 *
72 * Used for comparing xpcom-based results to os.file-based results.
73 *
74 * @param {string} a The _absolute_ path to the first file.
75 * @param {string} b The _absolute_ path to the second file.
76 *
77 * @resolves {null}
78 */
79 function reference_compare_files(a, b, test) {
80 return Task.spawn(function*() {
81 do_print("Comparing files " + a + " and " + b);
82 let a_contents = yield reference_fetch_file(a, test);
83 let b_contents = yield reference_fetch_file(b, test);
84 do_check_eq(a_contents, b_contents);
85 });
86 };