Skip to content

Commit

Permalink
[INTERNAL] MiddlewareUtil#getProject: Retrieve Project for a given Re…
Browse files Browse the repository at this point in the history
…source

For custom middleware this will return a Specification
Version-dependent (=compatible) interface of the Project instance a
Resource is associated with.

This was already described in RFC12[1] but not part of the initial
implementation of MiddlewareUtil#getProject.

[1] SAP/ui5-tooling#664
  • Loading branch information
RandomByte committed Dec 2, 2022
1 parent 70e859f commit 4bca67a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
23 changes: 17 additions & 6 deletions lib/middleware/MiddlewareUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,25 @@ class MiddlewareUtil {
* This method is only available to custom server middleware extensions defining
* <b>Specification Version 3.0 and above</b>.
*
* @param {string} [projectName] Name of the project to retrieve. Defaults to the project currently being built
* @param {string|@ui5/fs/Resource} [projectNameOrResource]
* Name of the project to retrieve or a Resource instance to retrieve the associated project for.
* Defaults to the name of the current root project
* @returns {@ui5/project/build/helpers/MiddlewareUtill~ProjectInterface|undefined}
* project instance or undefined if the project is unknown to the graph
* Specification Version-dependent interface to the Project instance or
* undefined if the project name is unknown or the provided resource is not associated with any project.
* @public
*/
getProject(projectName) {
if (projectName) {
return this._graph.getProject(projectName);
getProject(projectNameOrResource) {
if (projectNameOrResource) {
if (typeof projectNameOrResource === "string" || projectNameOrResource instanceof String) {
// A project name has been provided
return this._graph.getProject(projectNameOrResource);
} else {
// A Resource instance has been provided
return projectNameOrResource.getProject();
}
}
// No parameter has been provided, default to the root project
return this._project;
}

Expand All @@ -139,7 +149,8 @@ class MiddlewareUtil {
* This method is only available to custom server middleware extensions defining
* <b>Specification Version 3.0 and above</b>.
*
* @param {string} [projectName] Name of the project to retrieve. Defaults to the project currently being built
* @param {string} [projectName] Name of the project to retrieve.
* Defaults to the name of the current root project
* @returns {string[]} Names of all direct dependencies
* @throws {Error} If the requested project is unknown to the graph
* @public
Expand Down
25 changes: 20 additions & 5 deletions test/lib/server/middleware/MiddlewareUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,18 @@ test.serial("getMimeInfo: unknown type", (t) => {

test("getProject", (t) => {
const getProjectStub = sinon.stub().returns("Pony farm!");
const getProjectNameStub = sinon.stub().returns("root project name");
const middlewareUtil = new MiddlewareUtil({
graph: {
getProject: getProjectStub
},
project: {
getName: getProjectNameStub
}
project: "root project"
});

const res = middlewareUtil.getProject("pony farm");

t.is(getProjectStub.callCount, 1, "ProjectGraph#getProject got called once");
t.is(getProjectStub.getCall(0).args[0], "pony farm",
"ProjectGraph#getProject got called with correct arguments");
t.is(getProjectNameStub.callCount, 0, "#getName of root project has not been called");
t.is(res, "Pony farm!", "Correct result");
});

Expand All @@ -98,6 +94,25 @@ test("getProject: Default name", (t) => {
t.is(res, "root project", "Correct result");
});

test("getProject: Resource", (t) => {
const getProjectStub = sinon.stub().returns("Pony farm!");
const middlewareUtil = new MiddlewareUtil({
graph: {
getProject: getProjectStub
},
project: "root project"
});

const mockResource = {
getProject: sinon.stub().returns("Pig farm!")
};
const res = middlewareUtil.getProject(mockResource);

t.is(getProjectStub.callCount, 0, "ProjectGraph#getProject never got called");
t.is(mockResource.getProject.callCount, 1, "Resource#getProject got called once");
t.is(res, "Pig farm!", "Correct result");
});

test("getDependencies", (t) => {
const getDependenciesStub = sinon.stub().returns("Pony farm!");
const getProjectNameStub = sinon.stub().returns("root project name");
Expand Down

0 comments on commit 4bca67a

Please sign in to comment.