Skip to content

Commit

Permalink
[INTERNAL] Adapt to SpecVersionComparator renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Nov 28, 2022
1 parent 7289e4c commit 6f76931
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/middleware/MiddlewareManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class MiddlewareManager {
}
};

const specVersion = customMiddleware.getSpecVersionComparator();
const specVersion = customMiddleware.getSpecVersion();
if (specVersion.gte("3.0")) {
params.options.middlewareName = middlewareDef.name;
params.log = logger.getGroupLogger(`server:custom-middleware:${middlewareDef.name}`);
Expand Down
5 changes: 2 additions & 3 deletions lib/middleware/MiddlewareUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ class MiddlewareUtil {
*
* @public
* @typedef {object} @ui5/project/build/helpers/TaskUtil~ProjectInterface
* @property {Function} getSpecVersion Get the project Specification Version
* @property {Function} getType Get the project type
* @property {Function} getName Get the project name
* @property {Function} getVersion Get the project version
Expand Down Expand Up @@ -189,7 +188,7 @@ class MiddlewareUtil {
* Get an interface to an instance of this class that only provides those functions
* that are supported by the given custom middleware extension specification version.
*
* @param {@ui5/project/specifications/utils/SpecVersionComparator} specVersion
* @param {@ui5/project/specifications/SpecificationVersion} specVersion
* SpecVersionComparator instance of the custom server middleware
* @returns {object} An object with bound instance methods supported by the given specification version
*/
Expand All @@ -210,7 +209,7 @@ class MiddlewareUtil {
const project = this.getProject(projectName);
const baseProjectInterface = {};
bindFunctions(project, baseProjectInterface, [
"getSpecVersion", "getType", "getName", "getVersion", "getNamespace",
"getType", "getName", "getVersion", "getNamespace",
"getRootReader", "getReader", "getCustomConfiguration", "isFrameworkProject"
]);
return baseProjectInterface;
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function createMiddleware({resources, middlewareUtil}) {
let propertiesFileSourceEncoding = project?.getPropertiesFileSourceEncoding();

if (!propertiesFileSourceEncoding) {
if (project && ["0.1", "1.0", "1.1"].includes(project.getSpecVersion())) {
if (project && project.getSpecVersion().lte("1.1")) {
// default encoding to "ISO-8859-1" for old specVersions
propertiesFileSourceEncoding = "ISO-8859-1";
} else {
Expand Down
38 changes: 18 additions & 20 deletions test/lib/server/middleware/MiddlewareManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,13 @@ test("addCustomMiddleware: Missing before- or afterMiddleware configuration", as
test("addCustomMiddleware", async (t) => {
const {sinon} = t.context;
const middlewareModuleStub = sinon.stub().returns("ok");
const getSpecVersionStub = sinon.stub().returns("2.6");
const specVersionComparatorGteStub = sinon.stub().returns(false);
const mockSpecVersionComparator = {
gte: specVersionComparatorGteStub
const specVersionGteStub = sinon.stub().returns(false);
const mockSpecificationVersion = {
toString: () => "2.6",
gte: specVersionGteStub
};
const getExtensionStub = sinon.stub().returns({
getSpecVersion: getSpecVersionStub,
getSpecVersionComparator: () => mockSpecVersionComparator,
getSpecVersion: () => mockSpecificationVersion,
getMiddleware: () => middlewareModuleStub
});
const graph = {
Expand Down Expand Up @@ -582,11 +581,11 @@ test("addCustomMiddleware", async (t) => {
});

t.is(res, "ok", "Wrapper callback returned expected value");
t.is(specVersionComparatorGteStub.callCount, 1, "SpecVersionComparator#gte got called once");
t.is(specVersionComparatorGteStub.getCall(0).args[0], "3.0",
"SpecVersionComparator#gte got called with correct arguments");
t.is(specVersionGteStub.callCount, 1, "SpecificationVersion#gte got called once");
t.is(specVersionGteStub.getCall(0).args[0], "3.0",
"SpecificationVersion#gte got called with correct arguments");
t.is(middlewareUtil.getInterface.callCount, 1, "middlewareUtil.getInterface got called once");
t.is(middlewareUtil.getInterface.getCall(0).args[0], mockSpecVersionComparator,
t.is(middlewareUtil.getInterface.getCall(0).args[0], mockSpecificationVersion,
"middlewareUtil.getInterface got called with correct arguments");
t.is(middlewareModuleStub.callCount, 1, "Middleware module got called once");
t.deepEqual(middlewareModuleStub.getCall(0).args[0], {
Expand All @@ -603,14 +602,13 @@ test("addCustomMiddleware", async (t) => {
test("addCustomMiddleware with specVersion 3.0", async (t) => {
const {sinon, MiddlewareManager} = t.context;
const middlewareModuleStub = sinon.stub().returns("ok");
const getSpecVersionStub = sinon.stub().returns("3.0");
const specVersionComparatorGteStub = sinon.stub().returns(true);
const mockSpecVersionComparator = {
gte: specVersionComparatorGteStub
const specVersionGteStub = sinon.stub().returns(true);
const mockSpecificationVersion = {
toString: () => "3.0",
gte: specVersionGteStub
};
const getExtensionStub = sinon.stub().returns({
getSpecVersion: getSpecVersionStub,
getSpecVersionComparator: () => mockSpecVersionComparator,
getSpecVersion: () => mockSpecificationVersion,
getMiddleware: () => middlewareModuleStub
});
const graph = {
Expand Down Expand Up @@ -652,11 +650,11 @@ test("addCustomMiddleware with specVersion 3.0", async (t) => {
});

t.is(res, "ok", "Wrapper callback returned expected value");
t.is(specVersionComparatorGteStub.callCount, 1, "SpecVersionComparator#gte got called once");
t.is(specVersionComparatorGteStub.getCall(0).args[0], "3.0",
"SpecVersionComparator#gte got called with correct arguments");
t.is(specVersionGteStub.callCount, 1, "SpecificationVersion#gte got called once");
t.is(specVersionGteStub.getCall(0).args[0], "3.0",
"SpecificationVersion#gte got called with correct arguments");
t.is(middlewareUtil.getInterface.callCount, 1, "middlewareUtil.getInterface got called once");
t.is(middlewareUtil.getInterface.getCall(0).args[0], mockSpecVersionComparator,
t.is(middlewareUtil.getInterface.getCall(0).args[0], mockSpecificationVersion,
"middlewareUtil.getInterface got called with correct arguments");
t.is(middlewareModuleStub.callCount, 1, "Middleware module got called once");
t.deepEqual(middlewareModuleStub.getCall(0).args[0], {
Expand Down
28 changes: 13 additions & 15 deletions test/lib/server/middleware/MiddlewareUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import sinon from "sinon";
import esmock from "esmock";
import mime from "mime-types";
import MiddlewareUtil from "../../../../lib/middleware/MiddlewareUtil.js";
import SpecVersionComparator from "@ui5/project/specifications/utils/SpecVersionComparator";
import SpecificationVersion from "@ui5/project/specifications/SpecificationVersion";

test.afterEach.always((t) => {
sinon.restore();
});

function getSpecVersionComparator(specVersion) {
return new SpecVersionComparator(specVersion);
function getSpecificationVersion(specVersion) {
return new SpecificationVersion(specVersion);
}

test.serial("getPathname", async (t) => {
Expand Down Expand Up @@ -157,15 +157,15 @@ test.serial("resourceFactory", (t) => {
test("getInterface: specVersion 1.0", (t) => {
const middlewareUtil = new MiddlewareUtil({graph: "graph", project: "project"});

const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecVersionComparator("1.0"));
const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecificationVersion("1.0"));

t.is(interfacedMiddlewareUtil, undefined, "no interface provided");
});

test("getInterface: specVersion 2.0", (t) => {
const middlewareUtil = new MiddlewareUtil({graph: "graph", project: "project"});

const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecVersionComparator("2.0"));
const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecificationVersion("2.0"));

t.deepEqual(Object.keys(interfacedMiddlewareUtil), [
"getPathname",
Expand All @@ -179,7 +179,7 @@ test("getInterface: specVersion 2.0", (t) => {
test("getInterface: specVersion 2.1", (t) => {
const middlewareUtil = new MiddlewareUtil({graph: "graph", project: "project"});

const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecVersionComparator("2.1"));
const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecificationVersion("2.1"));

t.deepEqual(Object.keys(interfacedMiddlewareUtil), [
"getPathname",
Expand All @@ -193,7 +193,7 @@ test("getInterface: specVersion 2.1", (t) => {
test("getInterface: specVersion 2.2", (t) => {
const middlewareUtil = new MiddlewareUtil({graph: "graph", project: "project"});

const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecVersionComparator("2.2"));
const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecificationVersion("2.2"));

t.deepEqual(Object.keys(interfacedMiddlewareUtil), [
"getPathname",
Expand All @@ -207,7 +207,7 @@ test("getInterface: specVersion 2.2", (t) => {
test("getInterface: specVersion 2.3", (t) => {
const middlewareUtil = new MiddlewareUtil({graph: "graph", project: "project"});

const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecVersionComparator("2.3"));
const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecificationVersion("2.3"));

t.deepEqual(Object.keys(interfacedMiddlewareUtil), [
"getPathname",
Expand All @@ -221,7 +221,7 @@ test("getInterface: specVersion 2.3", (t) => {
test("getInterface: specVersion 2.4", (t) => {
const middlewareUtil = new MiddlewareUtil({graph: "graph", project: "project"});

const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecVersionComparator("2.4"));
const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecificationVersion("2.4"));

t.deepEqual(Object.keys(interfacedMiddlewareUtil), [
"getPathname",
Expand All @@ -235,7 +235,7 @@ test("getInterface: specVersion 2.4", (t) => {
test("getInterface: specVersion 2.5", (t) => {
const middlewareUtil = new MiddlewareUtil({graph: "graph", project: "project"});

const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecVersionComparator("2.5"));
const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecificationVersion("2.5"));

t.deepEqual(Object.keys(interfacedMiddlewareUtil), [
"getPathname",
Expand All @@ -249,7 +249,7 @@ test("getInterface: specVersion 2.5", (t) => {
test("getInterface: specVersion 2.6", (t) => {
const middlewareUtil = new MiddlewareUtil({graph: "graph", project: "project"});

const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecVersionComparator("2.6"));
const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecificationVersion("2.6"));

t.deepEqual(Object.keys(interfacedMiddlewareUtil), [
"getPathname",
Expand Down Expand Up @@ -283,7 +283,7 @@ test("getInterface: specVersion 3.0", (t) => {

const middlewareUtil = new MiddlewareUtil({graph: mockGraph, project: "project"});

const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecVersionComparator("3.0"));
const interfacedMiddlewareUtil = middlewareUtil.getInterface(getSpecificationVersion("3.0"));

t.deepEqual(Object.keys(interfacedMiddlewareUtil), [
"getPathname",
Expand All @@ -301,7 +301,6 @@ test("getInterface: specVersion 3.0", (t) => {
// getProject
const interfacedProject = interfacedMiddlewareUtil.getProject("pony");
t.deepEqual(Object.keys(interfacedProject), [
"getSpecVersion",
"getType",
"getName",
"getVersion",
Expand All @@ -312,7 +311,6 @@ test("getInterface: specVersion 3.0", (t) => {
"isFrameworkProject",
], "Correct methods are provided");

t.is(interfacedProject.getSpecVersion(), "specVersion", "getSpecVersion function is bound correctly");
t.is(interfacedProject.getType(), "type", "getType function is bound correctly");
t.is(interfacedProject.getName(), "name", "getName function is bound correctly");
t.is(interfacedProject.getVersion(), "version", "getVersion function is bound correctly");
Expand Down Expand Up @@ -345,7 +343,7 @@ test("getInterface: specVersion 3.0", (t) => {
test("getInterface: specVersion unknown", (t) => {
const middlewareUtil = new MiddlewareUtil({graph: "graph", project: "project"});
const err = t.throws(() => {
middlewareUtil.getInterface(getSpecVersionComparator("1.5"));
middlewareUtil.getInterface(getSpecificationVersion("1.5"));
});

t.is(err.message,
Expand Down
14 changes: 12 additions & 2 deletions test/lib/server/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,12 @@ test.serial("Check if properties file is served properly without property settin
const readerWriter = resourceFactory.createAdapter({virBasePath: "/"});
const project = {
getPropertiesFileSourceEncoding: () => "",
getSpecVersion: () => "1.1"
getSpecVersion: () => {
return {
toString: () => "1.1",
lte: () => true,
};
}
};
const resource = await writeResource(readerWriter, "/myFile3.properties",
1024 * 1024, "key=titel\nfame=straße", "latin1", project
Expand Down Expand Up @@ -209,7 +214,12 @@ test.serial("Check if properties file is served properly without property settin
const readerWriter = resourceFactory.createAdapter({virBasePath: "/"});
const project = {
getPropertiesFileSourceEncoding: () => "",
getSpecVersion: () => "2.0"
getSpecVersion: () => {
return {
toString: () => "2.0",
lte: () => false,
};
}
};
const resource = await writeResource(readerWriter, "/myFile3.properties",
1024 * 1024, "key=titel\nfame=straße", "utf8", project
Expand Down

0 comments on commit 6f76931

Please sign in to comment.