Bug 1811796 - Change Troubleshoot.sys.mjs to use console.error rather than Cu.reportError. r=mossop

Depends on D167517

Differential Revision: https://phabricator.services.mozilla.com/D167518
This commit is contained in:
Mark Banner
2023-01-23 18:09:03 +00:00
parent 752de1ae6e
commit ff6bee72c2
3 changed files with 27 additions and 105 deletions

View File

@@ -356,103 +356,4 @@ const NormandyTestUtils = {
}; };
}; };
}, },
/**
* Creates an nsIConsoleListener that records all console messages. The
* listener will be provided in the options argument to the test as
* `consoleSpy`, and will have methods to assert that expected messages were
* received. */
withConsoleSpy() {
return function(testFunction) {
return async function wrappedTestFunction(args) {
const consoleSpy = new TestConsoleListener();
console.log("Starting to track console messages");
Services.console.registerListener(consoleSpy);
try {
await testFunction({ ...args, consoleSpy });
} finally {
Services.console.unregisterListener(consoleSpy);
console.log("Stopped monitoring console messages");
}
};
};
},
}; };
class TestConsoleListener {
constructor() {
this.messages = [];
}
/**
* Check that every item listed has been received on the console. Items can
* be strings or regexes.
*
* Strings must be exact matches. Regexes must match according to
* `RegExp::test`, which is to say they are not automatically bound to the
* start or end of the message. If this is desired, include `^` and/or `$` in
* your expression.
*
* @param {String|RegExp} expectedMessages
* @param {String} [assertMessage] A message to include in the assertion message.
* @return {boolean}
*/
assertAtLeast(
expectedMessages,
assertMessage = "Console should contain the expected messages."
) {
let expectedSet = new Set(expectedMessages);
for (let { message } of this.messages) {
let found = false;
for (let expected of expectedSet) {
if (expected.test && expected.test(message)) {
found = true;
} else if (expected === message) {
found = true;
}
if (found) {
expectedSet.delete(expected);
break;
}
}
}
if (expectedSet.size) {
let remaining = Array.from(expectedSet);
let errorMessageParts = [];
if (assertMessage) {
errorMessageParts.push(assertMessage);
}
errorMessageParts.push(`"${remaining[0]}"`);
if (remaining.length > 1) {
errorMessageParts.push(`and ${remaining.length - 1} more log messages`);
}
errorMessageParts.push("expected in the console but not found.");
testGlobals.Assert.equal(
expectedSet.size,
0,
errorMessageParts.join(" ")
);
} else {
testGlobals.Assert.equal(expectedSet.size, 0, assertMessage);
}
}
// XPCOM
get QueryInterface() {
return ChromeUtils.generateQI(["nsIConsoleListener"]);
}
// nsIObserver
/**
* Takes all script error messages that do not have an exception attached,
* and emits a "Log.entryAdded" event.
*
* @param {nsIConsoleMessage} message
* Message originating from the nsIConsoleService.
*/
observe(message) {
this.messages.push(message);
}
}

View File

@@ -190,7 +190,7 @@ export var Troubleshoot = {
dataProviders[name](providerDone.bind(null, name)); dataProviders[name](providerDone.bind(null, name));
} catch (err) { } catch (err) {
let msg = "Troubleshoot data provider failed: " + name + "\n" + err; let msg = "Troubleshoot data provider failed: " + name + "\n" + err;
Cu.reportError(msg); console.error(msg);
providerDone(name, msg); providerDone(name, msg);
} }
} }
@@ -920,7 +920,7 @@ var dataProviders = {
].map(promise => ].map(promise =>
promise promise
.catch(error => { .catch(error => {
Cu.reportError(error); console.error(error);
return []; return [];
}) })
.then(items => items.sort((a, b) => a.slug.localeCompare(b.slug))) .then(items => items.sort((a, b) => a.slug.localeCompare(b.slug)))

View File

@@ -222,8 +222,8 @@ add_task(function normandyErrorHandling() {
NormandyTestUtils.withStub(PreferenceRollouts, "getAllActive", { NormandyTestUtils.withStub(PreferenceRollouts, "getAllActive", {
returnValue: Promise.reject("Expected error - PreferenceRollouts"), returnValue: Promise.reject("Expected error - PreferenceRollouts"),
}), }),
NormandyTestUtils.withConsoleSpy(), async function testNormandyErrorHandling() {
async function testNormandyErrorHandling({ consoleSpy }) { let consoleEndFn = TestUtils.listenForConsoleMessages();
let snapshot = await Troubleshoot.snapshot(); let snapshot = await Troubleshoot.snapshot();
let info = snapshot.normandy; let info = snapshot.normandy;
Assert.deepEqual( Assert.deepEqual(
@@ -241,12 +241,33 @@ add_task(function normandyErrorHandling() {
[], [],
"pref rollouts should be an empty list if there is an error" "pref rollouts should be an empty list if there is an error"
); );
let msgs = await consoleEndFn();
consoleSpy.assertAtLeast([ let expectedSet = new Set([
/Expected error - PreferenceExperiments/, /Expected error - PreferenceExperiments/,
/Expected error - AddonStudies/, /Expected error - AddonStudies/,
/Expected error - PreferenceRollouts/, /Expected error - PreferenceRollouts/,
]); ]);
for (let msg of msgs) {
msg = msg.wrappedJSObject;
if (msg.level != "error") {
continue;
}
let msgContents = msg.arguments[0];
for (let expected of expectedSet) {
if (expected.test(msgContents)) {
expectedSet.delete(expected);
break;
}
}
}
Assert.equal(
expectedSet.size,
0,
"Should have no messages left in the expected set"
);
} }
)(); )();
}); });