From 867d8477956c66dd563b439ba3437d91b73d5afe Mon Sep 17 00:00:00 2001 From: Matthias Osswald Date: Wed, 16 Aug 2023 13:26:25 +0200 Subject: [PATCH] [FIX] Resolve UI5 data directory relative to project The UI5 data directory (previously called UI5 home directory) should always be resolved relative to the project directory, which is where the package.json file is located at. This change also adds a list of options to the Configuration class to be used by the CLI config command to allow all available config options to be set or retrieved. Follow-up of https://github.com/SAP/ui5-project/pull/635 --- lib/config/Configuration.js | 30 +++++++++++++------ lib/graph/helpers/ui5Framework.js | 14 +++++---- .../graph/helpers/ui5Framework.integration.js | 8 +++++ test/lib/graph/helpers/ui5Framework.js | 7 +++++ 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/lib/config/Configuration.js b/lib/config/Configuration.js index 7e04b0fb4..4ccb0d730 100644 --- a/lib/config/Configuration.js +++ b/lib/config/Configuration.js @@ -10,17 +10,29 @@ import os from "node:os"; * @alias @ui5/project/config/Configuration */ class Configuration { - #mavenSnapshotEndpointUrl; - #ui5DataDir; + /** + * @public + * @static + */ + static availableOptions = [ + "mavenSnapshotEndpointUrl", + "ui5DataDir" + ]; + + #options = new Map(); /** * @param {object} configuration * @param {string} [configuration.mavenSnapshotEndpointUrl] * @param {string} [configuration.ui5DataDir] */ - constructor({mavenSnapshotEndpointUrl, ui5DataDir}) { - this.#mavenSnapshotEndpointUrl = mavenSnapshotEndpointUrl; - this.#ui5DataDir = ui5DataDir; + constructor(configuration) { + Object.entries(configuration).forEach(([key, value]) => { + if (!Configuration.availableOptions.includes(key)) { + throw new Error(`Unknown configuration option '${key}'`); + } + this.#options.set(key, value); + }); } /** @@ -31,7 +43,7 @@ class Configuration { * @returns {string} */ getMavenSnapshotEndpointUrl() { - return this.#mavenSnapshotEndpointUrl; + return this.#options.get("mavenSnapshotEndpointUrl"); } /** @@ -41,7 +53,7 @@ class Configuration { * @returns {string} */ getUi5DataDir() { - return this.#ui5DataDir; + return this.#options.get("ui5DataDir"); } /** @@ -50,8 +62,8 @@ class Configuration { */ toJson() { return { - mavenSnapshotEndpointUrl: this.#mavenSnapshotEndpointUrl, - ui5DataDir: this.#ui5DataDir, + mavenSnapshotEndpointUrl: this.getMavenSnapshotEndpointUrl(), + ui5DataDir: this.getUi5DataDir() }; } diff --git a/lib/graph/helpers/ui5Framework.js b/lib/graph/helpers/ui5Framework.js index 1a02f4c52..b44ccaa16 100644 --- a/lib/graph/helpers/ui5Framework.js +++ b/lib/graph/helpers/ui5Framework.js @@ -365,11 +365,15 @@ export default { }); } - const config = await Configuration.fromFile(); // ENV var should take precedence over the dataDir from the configuration. - const ui5HomeDir = process.env.UI5_DATA_DIR ? - path.resolve(process.env.UI5_DATA_DIR) : - config.getUi5DataDir(); + let ui5DataDir = process.env.UI5_DATA_DIR; + if (!ui5DataDir) { + const config = await Configuration.fromFile(); + ui5DataDir = config.getUi5DataDir(); + } + if (ui5DataDir) { + ui5DataDir = path.resolve(rootProject.getRootPath(), ui5DataDir); + } // Note: version might be undefined here and the Resolver will throw an error when calling // #install and it can't be resolved via the provided library metadata @@ -378,7 +382,7 @@ export default { version, providedLibraryMetadata, cacheMode, - ui5HomeDir + ui5HomeDir: ui5DataDir }); let startTime; diff --git a/test/lib/graph/helpers/ui5Framework.integration.js b/test/lib/graph/helpers/ui5Framework.integration.js index 0c7c5a9ef..dba455801 100644 --- a/test/lib/graph/helpers/ui5Framework.integration.js +++ b/test/lib/graph/helpers/ui5Framework.integration.js @@ -125,11 +125,19 @@ test.beforeEach(async (t) => { "../../../../lib/specifications/Specification.js": t.context.Specification }); + // Stub os homedir to prevent that the actual ~/.ui5rc from being used in tests + t.context.Configuration = await esmock.p("../../../../lib/config/Configuration.js", { + "node:os": { + homedir: sinon.stub().returns(path.join(fakeBaseDir, "homedir")) + } + }); + t.context.ui5Framework = await esmock.p("../../../../lib/graph/helpers/ui5Framework.js", { "@ui5/logger": ui5Logger, "../../../../lib/graph/Module.js": t.context.Module, "../../../../lib/ui5Framework/Openui5Resolver.js": t.context.Openui5Resolver, "../../../../lib/ui5Framework/Sapui5Resolver.js": t.context.Sapui5Resolver, + "../../../../lib/config/Configuration.js": t.context.Configuration }); t.context.projectGraphBuilder = await esmock.p("../../../../lib/graph/projectGraphBuilder.js", { diff --git a/test/lib/graph/helpers/ui5Framework.js b/test/lib/graph/helpers/ui5Framework.js index c7906da58..0a941a119 100644 --- a/test/lib/graph/helpers/ui5Framework.js +++ b/test/lib/graph/helpers/ui5Framework.js @@ -51,10 +51,17 @@ test.beforeEach(async (t) => { t.context.Sapui5MavenSnapshotResolverResolveVersionStub = sinon.stub(); t.context.Sapui5MavenSnapshotResolverStub.resolveVersion = t.context.Sapui5MavenSnapshotResolverResolveVersionStub; + t.context.ConfigurationStub = { + fromFile: sinon.stub().resolves({ + getUi5DataDir: sinon.stub().returns(undefined) + }) + }; + t.context.ui5Framework = await esmock.p("../../../../lib/graph/helpers/ui5Framework.js", { "@ui5/logger": ui5Logger, "../../../../lib/ui5Framework/Sapui5Resolver.js": t.context.Sapui5ResolverStub, "../../../../lib/ui5Framework/Sapui5MavenSnapshotResolver.js": t.context.Sapui5MavenSnapshotResolverStub, + "../../../../lib/config/Configuration.js": t.context.ConfigurationStub, }); t.context.utils = t.context.ui5Framework._utils; });