зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1525076 - Part 2: Add `AttributionCode.writeAttributionFile`. r=mixedpuppy
The next step is to have the cached attribution file location vary depending on the platform, and to have the tests run on additional platforms. Differential Revision: https://phabricator.services.mozilla.com/D84264
This commit is contained in:
Родитель
95284b4d0f
Коммит
b436e5cb98
|
@ -37,19 +37,32 @@ const ATTR_CODE_KEYS = [
|
|||
|
||||
let gCachedAttrData = null;
|
||||
|
||||
/**
|
||||
* Returns an nsIFile for the file containing the attribution data.
|
||||
*/
|
||||
function getAttributionFile() {
|
||||
let file = Services.dirsvc.get("LocalAppData", Ci.nsIFile);
|
||||
// appinfo does not exist in xpcshell, so we need defaults.
|
||||
file.append(Services.appinfo.vendor || "mozilla");
|
||||
file.append(AppConstants.MOZ_APP_NAME);
|
||||
file.append("postSigningData");
|
||||
return file;
|
||||
}
|
||||
|
||||
var AttributionCode = {
|
||||
/**
|
||||
* Returns an nsIFile for the file containing the attribution data.
|
||||
*/
|
||||
get attributionFile() {
|
||||
let file = Services.dirsvc.get("LocalAppData", Ci.nsIFile);
|
||||
// appinfo does not exist in xpcshell, so we need defaults.
|
||||
file.append(Services.appinfo.vendor || "mozilla");
|
||||
file.append(AppConstants.MOZ_APP_NAME);
|
||||
file.append("postSigningData");
|
||||
return file;
|
||||
},
|
||||
|
||||
/**
|
||||
* Write the given attribution code to the attribution file.
|
||||
* @param {String} code to write.
|
||||
*/
|
||||
async writeAttributionFile(code) {
|
||||
const file = AttributionCode.attributionFile;
|
||||
await OS.File.makeDir(file.parent.path, {
|
||||
ignoreExisting: true,
|
||||
from: file.parent.parent.path,
|
||||
});
|
||||
await OS.File.writeAtomic(file.path, code);
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns an array of allowed attribution code keys.
|
||||
*/
|
||||
|
@ -111,7 +124,7 @@ var AttributionCode = {
|
|||
if (AppConstants.platform == "win") {
|
||||
let bytes;
|
||||
try {
|
||||
bytes = await OS.File.read(getAttributionFile().path);
|
||||
bytes = await OS.File.read(this.attributionFile.path);
|
||||
} catch (ex) {
|
||||
if (ex instanceof OS.File.Error && ex.becauseNoSuchFile) {
|
||||
return gCachedAttrData;
|
||||
|
@ -176,7 +189,7 @@ var AttributionCode = {
|
|||
*/
|
||||
async deleteFileAsync() {
|
||||
try {
|
||||
await OS.File.remove(getAttributionFile().path);
|
||||
await OS.File.remove(this.attributionFile.path);
|
||||
} catch (ex) {
|
||||
// The attribution file may already have been deleted,
|
||||
// or it may have never been installed at all;
|
||||
|
|
|
@ -9,18 +9,6 @@ const { AttributionCode } = ChromeUtils.import(
|
|||
ChromeUtils.defineModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
|
||||
const { sinon } = ChromeUtils.import("resource://testing-common/Sinon.jsm");
|
||||
|
||||
async function writeAttributionFile(data) {
|
||||
let appDir = Services.dirsvc.get("LocalAppData", Ci.nsIFile);
|
||||
let file = appDir.clone();
|
||||
file.append(Services.appinfo.vendor || "mozilla");
|
||||
file.append(AppConstants.MOZ_APP_NAME);
|
||||
|
||||
await OS.File.makeDir(file.path, { from: appDir.path, ignoreExisting: true });
|
||||
|
||||
file.append("postSigningData");
|
||||
await OS.File.writeAtomic(file.path, data);
|
||||
}
|
||||
|
||||
add_task(function setup() {
|
||||
// Clear cache call is only possible in a testing environment
|
||||
let env = Cc["@mozilla.org/process/environment;1"].getService(
|
||||
|
@ -56,7 +44,7 @@ add_task(async function test_parse_error() {
|
|||
// Write an invalid file to trigger a decode error
|
||||
await AttributionCode.deleteFileAsync();
|
||||
AttributionCode._clearCache();
|
||||
await writeAttributionFile(""); // empty string is invalid
|
||||
await AttributionCode.writeAttributionFile(""); // empty string is invalid
|
||||
result = await AttributionCode.getAttrDataAsync();
|
||||
Assert.deepEqual(result, {}, "Should have failed to parse");
|
||||
|
||||
|
|
|
@ -9,18 +9,6 @@ const { AppConstants } = ChromeUtils.import(
|
|||
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
|
||||
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
||||
|
||||
async function writeAttributionFile(data) {
|
||||
let appDir = Services.dirsvc.get("LocalAppData", Ci.nsIFile);
|
||||
let file = appDir.clone();
|
||||
file.append(Services.appinfo.vendor || "mozilla");
|
||||
file.append(AppConstants.MOZ_APP_NAME);
|
||||
|
||||
await OS.File.makeDir(file.path, { from: appDir.path, ignoreExisting: true });
|
||||
|
||||
file.append("postSigningData");
|
||||
await OS.File.writeAtomic(file.path, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of attribution codes,
|
||||
* to make sure we reject bad ones and accept good ones.
|
||||
|
@ -28,7 +16,7 @@ async function writeAttributionFile(data) {
|
|||
add_task(async function testValidAttrCodes() {
|
||||
for (let entry of validAttrCodes) {
|
||||
AttributionCode._clearCache();
|
||||
await writeAttributionFile(entry.code);
|
||||
await AttributionCode.writeAttributionFile(entry.code);
|
||||
let result = await AttributionCode.getAttrDataAsync();
|
||||
Assert.deepEqual(
|
||||
result,
|
||||
|
@ -45,7 +33,7 @@ add_task(async function testValidAttrCodes() {
|
|||
add_task(async function testInvalidAttrCodes() {
|
||||
for (let code of invalidAttrCodes) {
|
||||
AttributionCode._clearCache();
|
||||
await writeAttributionFile(code);
|
||||
await AttributionCode.writeAttributionFile(code);
|
||||
let result = await AttributionCode.getAttrDataAsync();
|
||||
Assert.deepEqual(result, {}, "Code should have failed to parse: " + code);
|
||||
}
|
||||
|
@ -58,7 +46,7 @@ add_task(async function testInvalidAttrCodes() {
|
|||
*/
|
||||
add_task(async function testDeletedFile() {
|
||||
// Set up the test by clearing the cache and writing a valid file.
|
||||
await writeAttributionFile(validAttrCodes[0].code);
|
||||
await AttributionCode.writeAttributionFile(validAttrCodes[0].code);
|
||||
let result = await AttributionCode.getAttrDataAsync();
|
||||
Assert.deepEqual(
|
||||
result,
|
||||
|
|
|
@ -10,7 +10,6 @@ const { XPCOMUtils } = ChromeUtils.import(
|
|||
XPCOMUtils.defineLazyGlobalGetters(this, ["fetch"]);
|
||||
XPCOMUtils.defineLazyModuleGetters(this, {
|
||||
AppConstants: "resource://gre/modules/AppConstants.jsm",
|
||||
OS: "resource://gre/modules/osfile.jsm",
|
||||
BookmarkPanelHub: "resource://activity-stream/lib/BookmarkPanelHub.jsm",
|
||||
SnippetsTestMessageProvider:
|
||||
"resource://activity-stream/lib/SnippetsTestMessageProvider.jsm",
|
||||
|
@ -1722,18 +1721,7 @@ class _ASRouter {
|
|||
// RTAMO messages. This should only be called from within about:newtab#asrouter
|
||||
/* istanbul ignore next */
|
||||
async _writeAttributionFile(data) {
|
||||
let appDir = Services.dirsvc.get("LocalAppData", Ci.nsIFile);
|
||||
let file = appDir.clone();
|
||||
file.append(Services.appinfo.vendor || "mozilla");
|
||||
file.append(AppConstants.MOZ_APP_NAME);
|
||||
|
||||
await OS.File.makeDir(file.path, {
|
||||
from: appDir.path,
|
||||
ignoreExisting: true,
|
||||
});
|
||||
|
||||
file.append("postSigningData");
|
||||
await OS.File.writeAtomic(file.path, data);
|
||||
await AttributionCode.writeAttributionFile(data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -340,29 +340,16 @@ function spoofPartnerInfo() {
|
|||
}
|
||||
}
|
||||
|
||||
function getAttributionFile() {
|
||||
return FileUtils.getFile("LocalAppData", [
|
||||
"mozilla",
|
||||
AppConstants.MOZ_APP_NAME,
|
||||
"postSigningData",
|
||||
]);
|
||||
}
|
||||
|
||||
function spoofAttributionData() {
|
||||
async function spoofAttributionData() {
|
||||
if (gIsWindows) {
|
||||
AttributionCode._clearCache();
|
||||
let stream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
|
||||
Ci.nsIFileOutputStream
|
||||
);
|
||||
stream.init(getAttributionFile(), -1, -1, 0);
|
||||
stream.write(ATTRIBUTION_CODE, ATTRIBUTION_CODE.length);
|
||||
stream.close();
|
||||
await AttributionCode.writeAttributionFile(ATTRIBUTION_CODE);
|
||||
}
|
||||
}
|
||||
|
||||
function cleanupAttributionData() {
|
||||
if (gIsWindows) {
|
||||
getAttributionFile().remove(false);
|
||||
AttributionCode.attributionFile.remove(false);
|
||||
AttributionCode._clearCache();
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче