Bug 1695309 - Replace CommonUtils.{read,write}JSON with IOUtils.{read,write}JSON r=Gijs,markh

Differential Revision: https://phabricator.services.mozilla.com/D159962
This commit is contained in:
Barret Rennie 2022-10-29 03:39:11 +00:00
parent e893d96ac0
commit 4ba0721d59
14 changed files with 28 additions and 143 deletions

View File

@ -1,37 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
function run_test() {
initTestLogging();
run_next_test();
}
add_test(function test_writeJSON_readJSON() {
_("Round-trip some JSON through the promise-based JSON writer.");
let contents = {
a: 12345.67,
b: {
c: "héllö",
},
d: undefined,
e: null,
};
function checkJSON(json) {
Assert.equal(contents.a, json.a);
Assert.equal(contents.b.c, json.b.c);
Assert.equal(contents.d, json.d);
Assert.equal(contents.e, json.e);
run_next_test();
}
function doRead() {
CommonUtils.readJSON(path).then(checkJSON, do_throw);
}
let path = OS.Path.join(OS.Constants.Path.profileDir, "bar.json");
CommonUtils.writeJSON(contents, path).then(doRead, do_throw);
});

View File

@ -20,7 +20,6 @@ tags = remote-settings blocklist
[test_utils_encodeBase32.js]
[test_utils_encodeBase64URL.js]
[test_utils_ensureMillisecondsTimestamp.js]
[test_utils_json.js]
[test_utils_makeURI.js]
[test_utils_namedTimer.js]
[test_utils_sets.js]

View File

@ -10,8 +10,6 @@ const { XPCOMUtils } = ChromeUtils.importESModule(
const { Log } = ChromeUtils.importESModule(
"resource://gre/modules/Log.sys.mjs"
);
const lazy = {};
ChromeUtils.defineModuleGetter(lazy, "OS", "resource://gre/modules/osfile.jsm");
var CommonUtils = {
/*
@ -425,33 +423,6 @@ var CommonUtils = {
return over ? atob(b64.substr(0, len - over)) : atob(b64);
},
/**
* Parses a JSON file from disk using OS.File and promises.
*
* @param path the file to read. Will be passed to `OS.File.read()`.
* @return a promise that resolves to the JSON contents of the named file.
*/
readJSON(path) {
return lazy.OS.File.read(path, { encoding: "utf-8" }).then(data => {
return JSON.parse(data);
});
},
/**
* Write a JSON object to the named file using OS.File and promises.
*
* @param contents a JS object. Will be serialized.
* @param path the path of the file to write.
* @return a promise, as produced by OS.File.writeAtomic.
*/
writeJSON(contents, path) {
let data = JSON.stringify(contents);
return lazy.OS.File.writeAtomic(path, data, {
encoding: "utf-8",
tmpPath: path + ".tmp",
});
},
/**
* Ensure that the specified value is defined in integer milliseconds since
* UNIX epoch.

View File

@ -19,9 +19,6 @@ const {
FXA_PWDMGR_SECURE_FIELDS,
log,
} = ChromeUtils.import("resource://gre/modules/FxAccountsCommon.js");
const { CommonUtils } = ChromeUtils.import(
"resource://services-common/utils.js"
);
// A helper function so code can check what fields are able to be stored by
// the storage manager without having a reference to a manager instance.
@ -457,7 +454,7 @@ JSONStorage.prototype = {
);
let start = Date.now();
return IOUtils.makeDirectory(this.baseDir, { ignoreExisting: true })
.then(CommonUtils.writeJSON.bind(null, contents, this.path))
.then(IOUtils.writeJSON.bind(null, this.path, contents))
.then(result => {
log.trace(
"finished write of json user data - took",
@ -470,7 +467,7 @@ JSONStorage.prototype = {
get() {
log.trace("starting fetch of json user data");
let start = Date.now();
return CommonUtils.readJSON(this.path).then(result => {
return IOUtils.readJSON(this.path).then(result => {
log.trace("finished fetch of json user data - took", Date.now() - start);
return result;
});

View File

@ -92,7 +92,7 @@ add_task(async function test_simple() {
// This should have stored stuff in both the .json file in the profile
// dir, and the login dir.
let path = PathUtils.join(PathUtils.profileDir, "signedInUser.json");
let data = await CommonUtils.readJSON(path);
let data = await IOUtils.readJSON(path);
Assert.strictEqual(
data.accountData.email,
@ -190,7 +190,7 @@ add_task(async function test_MPLocked() {
// This should have stored stuff in the .json, and the login manager stuff
// will not exist.
let path = PathUtils.join(PathUtils.profileDir, "signedInUser.json");
let data = await CommonUtils.readJSON(path);
let data = await IOUtils.readJSON(path);
Assert.strictEqual(
data.accountData.email,

View File

@ -47,11 +47,6 @@ XPCOMUtils.defineLazyGetter(lazy, "gPingsArchivePath", function() {
XPCOMUtils.defineLazyGetter(lazy, "gAbortedSessionFilePath", function() {
return OS.Path.join(lazy.gDataReportingDir, ABORTED_SESSION_FILE_NAME);
});
ChromeUtils.defineModuleGetter(
lazy,
"CommonUtils",
"resource://services-common/utils.js"
);
ChromeUtils.defineModuleGetter(
lazy,
"TelemetryHealthPing",
@ -828,7 +823,7 @@ var TelemetryStorageImpl = {
SESSION_STATE_FILE_NAME
);
try {
await lazy.CommonUtils.writeJSON(sessionData, filePath);
await IOUtils.writeJSON(filePath, sessionData);
} catch (e) {
this._log.error(
"_saveSessionData - Failed to write session data to " + filePath,

View File

@ -15,9 +15,7 @@ XPCOMUtils.defineLazyModuleGetters(lazy, {
Assert: "resource://testing-common/Assert.jsm",
// AttributionCode is only needed for Firefox
AttributionCode: "resource:///modules/AttributionCode.jsm",
CommonUtils: "resource://services-common/utils.js",
MockRegistrar: "resource://testing-common/MockRegistrar.jsm",
OS: "resource://gre/modules/osfile.jsm",
});
var EXPORTED_SYMBOLS = ["TelemetryEnvironmentTesting"];
@ -129,13 +127,13 @@ var TelemetryEnvironmentTesting = {
},
spoofProfileReset() {
return lazy.CommonUtils.writeJSON(
return IOUtils.writeJSON(
PathUtils.join(PathUtils.profileDir, "times.json"),
{
created: PROFILE_CREATION_DATE_MS,
reset: PROFILE_RESET_DATE_MS,
firstUse: PROFILE_FIRST_USE_MS,
},
lazy.OS.Path.join(lazy.OS.Constants.Path.profileDir, "times.json")
}
);
},

View File

@ -8,9 +8,6 @@
* checked in the second request.
*/
const { CommonUtils } = ChromeUtils.import(
"resource://services-common/utils.js"
);
const { ClientID } = ChromeUtils.import("resource://gre/modules/ClientID.jsm");
const { TelemetryController } = ChromeUtils.import(
"resource://gre/modules/TelemetryController.jsm"
@ -793,7 +790,7 @@ add_task(async function test_sendNewProfile() {
subsessionId: null,
profileSubsessionCounter: 3785,
};
await CommonUtils.writeJSON(sessionState, stateFilePath);
await IOUtils.writeJSON(stateFilePath, sessionState);
await TelemetryController.testReset();
await TelemetryController.testShutdown();

View File

@ -2,10 +2,6 @@
http://creativecommons.org/publicdomain/zero/1.0/
*/
const { CommonUtils } = ChromeUtils.import(
"resource://services-common/utils.js"
);
/**
* Return the path to the definitions file for the events.
*/
@ -97,7 +93,7 @@ add_task(
// Let's write to the definition file to also cover the file
// loading part.
const FILE_PATH = getDefinitionsPath();
await CommonUtils.writeJSON(DYNAMIC_EVENT_SPEC, FILE_PATH);
await IOUtils.writeJSON(FILE_PATH, DYNAMIC_EVENT_SPEC);
// Start TelemetryController to trigger loading the specs.
await TelemetryController.testReset();

View File

@ -2,10 +2,6 @@
http://creativecommons.org/publicdomain/zero/1.0/
*/
const { CommonUtils } = ChromeUtils.import(
"resource://services-common/utils.js"
);
/**
* Return the path to the definitions file for the scalars.
*/
@ -98,7 +94,7 @@ add_task(
// Let's write to the definition file to also cover the file
// loading part.
const FILE_PATH = getDefinitionsPath();
await CommonUtils.writeJSON(DYNAMIC_SCALAR_SPEC, FILE_PATH);
await IOUtils.writeJSON(FILE_PATH, DYNAMIC_SCALAR_SPEC);
// Start TelemetryController to trigger loading the specs.
await TelemetryController.testReset();

View File

@ -8,9 +8,6 @@
* checked in the second request.
*/
const { CommonUtils } = ChromeUtils.import(
"resource://services-common/utils.js"
);
const { ClientID } = ChromeUtils.import("resource://gre/modules/ClientID.jsm");
const { TelemetrySession } = ChromeUtils.import(
"resource://gre/modules/TelemetrySession.jsm"
@ -1510,7 +1507,7 @@ add_task(async function test_savedSessionData() {
subsessionId: null,
profileSubsessionCounter: 3785,
};
await CommonUtils.writeJSON(sessionState, dataFilePath);
await IOUtils.writeJSON(dataFilePath, sessionState);
const PREF_TEST = "toolkit.telemetry.test.pref1";
Preferences.reset(PREF_TEST);
@ -1561,7 +1558,7 @@ add_task(async function test_savedSessionData() {
fakeGenerateUUID(TelemetryUtils.generateUUID, TelemetryUtils.generateUUID);
// Load back the serialised session data.
let data = await CommonUtils.readJSON(dataFilePath);
let data = await IOUtils.readJSON(dataFilePath);
Assert.equal(data.profileSubsessionCounter, expectedSubsessions);
Assert.equal(data.sessionId, expectedSessionUUID);
Assert.equal(data.subsessionId, expectedSubsessionUUID);
@ -1648,7 +1645,7 @@ add_task(async function test_invalidSessionData() {
profileSubsessionCounter: "not-a-number?",
someOtherField: 12,
};
await CommonUtils.writeJSON(sessionState, dataFilePath);
await IOUtils.writeJSON(dataFilePath, sessionState);
// The session data file should not load. Only expect the current subsession.
const expectedSubsessions = 1;
@ -1675,7 +1672,7 @@ add_task(async function test_invalidSessionData() {
fakeGenerateUUID(TelemetryUtils.generateUUID, TelemetryUtils.generateUUID);
// Load back the serialised session data.
let data = await CommonUtils.readJSON(dataFilePath);
let data = await IOUtils.readJSON(dataFilePath);
Assert.equal(data.profileSubsessionCounter, expectedSubsessions);
Assert.equal(data.sessionId, expectedSessionUUID);
Assert.equal(data.subsessionId, expectedSubsessionUUID);

View File

@ -9,9 +9,6 @@
* in other files might interfere with the other tests.
*/
const { CommonUtils } = ChromeUtils.import(
"resource://services-common/utils.js"
);
const { TelemetryStorage } = ChromeUtils.import(
"resource://gre/modules/TelemetryStorage.jsm"
);
@ -153,7 +150,7 @@ add_task(async function test_abortedSession_canary_clientid() {
);
// Set clientID in aborted-session ping to canary value
let abortedPing = await CommonUtils.readJSON(ABORTED_FILE);
let abortedPing = await IOUtils.readJSON(ABORTED_FILE);
abortedPing.clientId = TelemetryUtils.knownClientID;
OS.File.writeAtomic(ABORTED_FILE, JSON.stringify(abortedPing), {
encoding: "utf-8",

View File

@ -4,10 +4,6 @@
"use strict";
const { ClientID } = ChromeUtils.import("resource://gre/modules/ClientID.jsm");
const { CommonUtils } = ChromeUtils.import(
"resource://services-common/utils.js"
);
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const PREF_CACHED_CLIENTID = "toolkit.telemetry.cachedClientID";
@ -17,11 +13,7 @@ const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
function run_test() {
do_get_profile();
drsPath = OS.Path.join(
OS.Constants.Path.profileDir,
"datareporting",
"state.json"
);
drsPath = PathUtils.join(PathUtils.profileDir, "datareporting", "state.json");
Services.prefs.setBoolPref(
"toolkit.telemetry.testing.overrideProductsCheck",
@ -74,7 +66,7 @@ add_task(async function test_client_id() {
let oldClientID = clientID;
for (let [invalidID] of invalidIDs) {
await ClientID._reset();
await CommonUtils.writeJSON({ clientID: invalidID }, drsPath);
await IOUtils.writeJSON(drsPath, { clientID: invalidID });
clientID = await ClientID.getClientID();
Assert.equal(clientID, oldClientID);
if (AppConstants.platform != "android") {
@ -85,7 +77,7 @@ add_task(async function test_client_id() {
// Test that valid DRS actually works.
const validClientID = "5afebd62-a33c-416c-b519-5c60fb988e8e";
await ClientID._reset();
await CommonUtils.writeJSON({ clientID: validClientID }, drsPath);
await IOUtils.writeJSON(drsPath, { clientID: validClientID });
clientID = await ClientID.getClientID();
Assert.equal(clientID, validClientID);
if (AppConstants.platform != "android") {

View File

@ -1,9 +1,6 @@
const { ProfileAge } = ChromeUtils.importESModule(
"resource://gre/modules/ProfileAge.sys.mjs"
);
const { CommonUtils } = ChromeUtils.import(
"resource://services-common/utils.js"
);
const gProfD = do_get_profile();
let ID = 0;
@ -42,12 +39,9 @@ add_task(
const CREATED_TIME = Date.now() - 2000;
const RESET_TIME = Date.now() - 1000;
await CommonUtils.writeJSON(
{
created: CREATED_TIME,
},
PathUtils.join(profile, "times.json")
);
await IOUtils.writeJSON(PathUtils.join(profile, "times.json"), {
created: CREATED_TIME,
});
let times = await ProfileAge(profile);
Assert.equal(
@ -72,9 +66,7 @@ add_task(
);
await promise;
let results = await CommonUtils.readJSON(
PathUtils.join(profile, "times.json")
);
let results = await IOUtils.readJSON(PathUtils.join(profile, "times.json"));
Assert.deepEqual(
results,
{
@ -98,9 +90,7 @@ add_task(
times.recordProfileReset(RESET_TIME2),
]);
let results = await CommonUtils.readJSON(
PathUtils.join(profile, "times.json")
);
let results = await IOUtils.readJSON(PathUtils.join(profile, "times.json"));
delete results.firstUse;
Assert.deepEqual(
results,
@ -116,13 +106,10 @@ add_task(
withDummyProfile(async profile => {
const CREATED_TIME = Date.now() - 1000;
await CommonUtils.writeJSON(
{
created: CREATED_TIME,
firstUse: null,
},
PathUtils.join(profile, "times.json")
);
await IOUtils.writeJSON(PathUtils.join(profile, "times.json"), {
created: CREATED_TIME,
firstUse: null,
});
let times = await ProfileAge(profile);
Assert.ok(