michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: let {utils: Cu, interfaces: Ci} = Components; michael@0: michael@0: let {OS} = Cu.import("resource://gre/modules/osfile.jsm", {}); michael@0: let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); michael@0: let {Promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); michael@0: let {Task} = Cu.import("resource://gre/modules/Task.jsm", {}); michael@0: let {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", {}); michael@0: let {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", {}); michael@0: michael@0: Services.prefs.setBoolPref("toolkit.osfile.log", true); michael@0: michael@0: /** michael@0: * As add_task, but execute the test both with native operations and michael@0: * without. michael@0: */ michael@0: function add_test_pair(generator) { michael@0: add_task(function*() { michael@0: do_print("Executing test " + generator.name + " with native operations"); michael@0: Services.prefs.setBoolPref("toolkit.osfile.native", true); michael@0: return Task.spawn(generator); michael@0: }); michael@0: add_task(function*() { michael@0: do_print("Executing test " + generator.name + " without native operations"); michael@0: Services.prefs.setBoolPref("toolkit.osfile.native", false); michael@0: return Task.spawn(generator); michael@0: }); michael@0: } 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: function reference_fetch_file(path, test) { michael@0: do_print("Fetching file " + path); michael@0: let deferred = 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: deferred.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: deferred.reject(reject); michael@0: } else { michael@0: deferred.resolve(result); michael@0: } michael@0: }); michael@0: return deferred.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: function reference_compare_files(a, b, test) { michael@0: return Task.spawn(function*() { michael@0: do_print("Comparing files " + a + " and " + b); michael@0: let a_contents = yield reference_fetch_file(a, test); michael@0: let b_contents = yield reference_fetch_file(b, test); michael@0: do_check_eq(a_contents, b_contents); michael@0: }); michael@0: };