diff --git a/lib/middleware/MiddlewareUtil.js b/lib/middleware/MiddlewareUtil.js index 568e472a..79efb54c 100644 --- a/lib/middleware/MiddlewareUtil.js +++ b/lib/middleware/MiddlewareUtil.js @@ -119,15 +119,25 @@ class MiddlewareUtil { * This method is only available to custom server middleware extensions defining * Specification Version 3.0 and above. * - * @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; } @@ -139,7 +149,8 @@ class MiddlewareUtil { * This method is only available to custom server middleware extensions defining * Specification Version 3.0 and above. * - * @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 diff --git a/test/lib/server/middleware/MiddlewareUtil.js b/test/lib/server/middleware/MiddlewareUtil.js index 4a66c7d7..2a1899d5 100644 --- a/test/lib/server/middleware/MiddlewareUtil.js +++ b/test/lib/server/middleware/MiddlewareUtil.js @@ -64,14 +64,11 @@ 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"); @@ -79,7 +76,6 @@ test("getProject", (t) => { 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"); }); @@ -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");