From 421a37577c11c50d938bc59f3b8fca371a703cd3 Mon Sep 17 00:00:00 2001 From: Matthias Osswald Date: Fri, 26 Jul 2024 15:12:05 +0200 Subject: [PATCH] [FIX] manifestEnhancer: Fix fallbackLocale handling fallbackLocale with an empty string was not being handled correctly. --- lib/processors/manifestEnhancer.js | 4 +- test/lib/processors/manifestEnhancer.js | 109 ++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/lib/processors/manifestEnhancer.js b/lib/processors/manifestEnhancer.js index b7152087c..d91fa58e3 100644 --- a/lib/processors/manifestEnhancer.js +++ b/lib/processors/manifestEnhancer.js @@ -212,7 +212,7 @@ class ManifestEnhancer { } const supportedLocales = await this.findSupportedLocales(normalizedBundleUrl); if (!isTerminologyBundle && supportedLocales.length > 0) { - if (fallbackLocale && !supportedLocales.includes(fallbackLocale)) { + if (typeof fallbackLocale === "string" && !supportedLocales.includes(fallbackLocale)) { log.error( `${this.filePath}: ` + `Generated supported locales ('${supportedLocales.join("', '")}') for ` + @@ -221,7 +221,7 @@ class ManifestEnhancer { "properties file for defined fallbackLocale or configure another available fallbackLocale" ); return []; - } else if (!fallbackLocale && !supportedLocales.includes("en")) { + } else if (typeof fallbackLocale === "undefined" && !supportedLocales.includes("en")) { log.warn( `${this.filePath}: ` + `Generated supported locales ('${supportedLocales.join("', '")}') for ` + diff --git a/test/lib/processors/manifestEnhancer.js b/test/lib/processors/manifestEnhancer.js index c09324f30..5a7dc4ce0 100644 --- a/test/lib/processors/manifestEnhancer.js +++ b/test/lib/processors/manifestEnhancer.js @@ -974,6 +974,115 @@ async (t) => { t.true(t.context.logErrorSpy.notCalled, "No errors should be logged"); }); +test("Application: sap.ui5/models: " + + "No warning when fallbackLocale is empty string and is part of supportedLocales", +async (t) => { + const {manifestEnhancer, fs, createResource} = t.context; + const input = JSON.stringify({ + "_version": "1.58.0", + "sap.app": { + "id": "sap.ui.demo.app", + "type": "application" + }, + "sap.ui5": { + "models": { + "i18n": { + "type": "sap.ui.model.resource.ResourceModel", + "settings": { + "bundleName": "sap.ui.demo.app.i18nModel.i18n", + "fallbackLocale": "" + } + } + } + } + }, null, 2); + + const expected = JSON.stringify({ + "_version": "1.58.0", + "sap.app": { + "id": "sap.ui.demo.app", + "type": "application" + }, + "sap.ui5": { + "models": { + "i18n": { + "type": "sap.ui.model.resource.ResourceModel", + "settings": { + "bundleName": "sap.ui.demo.app.i18nModel.i18n", + "fallbackLocale": "", + "supportedLocales": [""] + } + } + } + } + }, null, 2); + + const resource = createResource("/resources/sap/ui/demo/app/manifest.json", true, input); + + fs.readdir.withArgs("/resources/sap/ui/demo/app/i18nModel") + .callsArgWith(1, null, ["i18n.properties"]); + + const processedResources = await manifestEnhancer({ + resources: [resource], + fs + }); + + t.deepEqual(processedResources, [resource], "Input resource is returned"); + + t.is(resource.setString.callCount, 1, "setString should be called once"); + t.deepEqual(resource.setString.getCall(0).args, [expected], "Correct file content should be set"); + + t.true(t.context.logVerboseSpy.notCalled, "No verbose messages should be logged"); + t.true(t.context.logWarnSpy.notCalled, "No warnings should be logged"); + t.true(t.context.logErrorSpy.notCalled, "No errors should be logged"); +}); + +test("Application: sap.ui5/models: " + + "Error when fallbackLocale is empty string and is not part of supportedLocales", +async (t) => { + const {manifestEnhancer, fs, createResource} = t.context; + const input = JSON.stringify({ + "_version": "1.58.0", + "sap.app": { + "id": "sap.ui.demo.app", + "type": "application" + }, + "sap.ui5": { + "models": { + "i18n": { + "type": "sap.ui.model.resource.ResourceModel", + "settings": { + "bundleName": "sap.ui.demo.app.i18nModel.i18n", + "fallbackLocale": "" + } + } + } + } + }, null, 2); + + const resource = createResource("/resources/sap/ui/demo/app/manifest.json", true, input); + + fs.readdir.withArgs("/resources/sap/ui/demo/app/i18nModel") + .callsArgWith(1, null, ["i18n_en.properties"]); + + const processedResources = await manifestEnhancer({ + resources: [resource], + fs + }); + + t.deepEqual(processedResources, [], "Only enhanced resources are returned"); + + t.is(resource.setString.callCount, 0, "setString should not be called"); + + t.true(t.context.logVerboseSpy.notCalled, "No verbose messages should be logged"); + t.true(t.context.logWarnSpy.notCalled, "No warnings should be logged"); + t.is(t.context.logErrorSpy.callCount, 1, "One error should be logged"); + t.deepEqual(t.context.logErrorSpy.getCall(0).args, [ + "/resources/sap/ui/demo/app/manifest.json: Generated supported locales ('en') for " + + "bundle 'i18nModel/i18n.properties' not containing the defined fallback locale ''. " + + "Either provide a properties file for defined fallbackLocale or configure another available fallbackLocale"]); +}); + test("Application: sap.ui5/models: Log verbose if manifest version is not defined at all", async (t) => { const {manifestEnhancer, fs, createResource} = t.context; const input = JSON.stringify({