Skip to content

Commit

Permalink
[FIX] Resolve UI5 data directory relative to project
Browse files Browse the repository at this point in the history
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 #635
  • Loading branch information
matz3 committed Aug 16, 2023
1 parent 23fd49d commit 867d847
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
30 changes: 21 additions & 9 deletions lib/config/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

/**
Expand All @@ -31,7 +43,7 @@ class Configuration {
* @returns {string}
*/
getMavenSnapshotEndpointUrl() {
return this.#mavenSnapshotEndpointUrl;
return this.#options.get("mavenSnapshotEndpointUrl");
}

/**
Expand All @@ -41,7 +53,7 @@ class Configuration {
* @returns {string}
*/
getUi5DataDir() {
return this.#ui5DataDir;
return this.#options.get("ui5DataDir");
}

/**
Expand All @@ -50,8 +62,8 @@ class Configuration {
*/
toJson() {
return {
mavenSnapshotEndpointUrl: this.#mavenSnapshotEndpointUrl,
ui5DataDir: this.#ui5DataDir,
mavenSnapshotEndpointUrl: this.getMavenSnapshotEndpointUrl(),
ui5DataDir: this.getUi5DataDir()
};
}

Expand Down
14 changes: 9 additions & 5 deletions lib/graph/helpers/ui5Framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -378,7 +382,7 @@ export default {
version,
providedLibraryMetadata,
cacheMode,
ui5HomeDir
ui5HomeDir: ui5DataDir
});

let startTime;
Expand Down
8 changes: 8 additions & 0 deletions test/lib/graph/helpers/ui5Framework.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
7 changes: 7 additions & 0 deletions test/lib/graph/helpers/ui5Framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down

0 comments on commit 867d847

Please sign in to comment.