Bug 1462958 - Avoid infinite recursion if resourceIds is empty. r?mossop
`generateContextsForLocale` didn't have a condition for when `resolvedLength`
equals `resourcesLength` which triggeres an infinite recursion when `resourceIds` is
empty.
MozReview-Commit-ID: Csy1ROTfbE2
--- a/intl/l10n/L10nRegistry.jsm
+++ b/intl/l10n/L10nRegistry.jsm
@@ -206,17 +206,17 @@ async function* generateContextsForLocal
// If the number of resolved sources equals the number of resources,
// create the right context and return it if it loads.
if (resolvedLength + 1 === resourcesLength) {
const ctx = await generateContext(locale, order, resourceIds);
if (ctx !== null) {
yield ctx;
}
- } else {
+ } else if (resolvedLength < resourcesLength) {
// otherwise recursively load another generator that walks over the
// partially resolved list of sources.
yield * generateContextsForLocale(locale, sourcesOrder, resourceIds, order);
}
}
}
const MSG_CONTEXT_OPTIONS = {
--- a/intl/l10n/test/test_l10nregistry.js
+++ b/intl/l10n/test/test_l10nregistry.js
@@ -19,16 +19,53 @@ L10nRegistry.load = async function(url)
add_task(function test_methods_presence() {
equal(typeof L10nRegistry.generateContexts, "function");
equal(typeof L10nRegistry.getAvailableLocales, "function");
equal(typeof L10nRegistry.registerSource, "function");
equal(typeof L10nRegistry.updateSource, "function");
});
/**
+ * Test that passing empty resourceIds list works.
+ */
+add_task(async function test_empty_resourceids() {
+ fs = {};
+
+ const source = new FileSource("test", ["en-US"], "/localization/{locale}");
+ L10nRegistry.registerSource(source);
+
+ const ctxs = L10nRegistry.generateContexts(["en-US"], []);
+
+ const done = (await ctxs.next()).done;
+
+ equal(done, true);
+
+ // cleanup
+ L10nRegistry.sources.clear();
+ L10nRegistry.ctxCache.clear();
+});
+
+/**
+ * Test that passing empty sources list works.
+ */
+add_task(async function test_empty_sources() {
+ fs = {};
+
+ const ctxs = L10nRegistry.generateContexts(["en-US"], []);
+
+ const done = (await ctxs.next()).done;
+
+ equal(done, true);
+
+ // cleanup
+ L10nRegistry.sources.clear();
+ L10nRegistry.ctxCache.clear();
+});
+
+/**
* This test tests generation of a proper context for a single
* source scenario
*/
add_task(async function test_methods_calling() {
fs = {
"/localization/en-US/browser/menu.ftl": "key = Value",
};