Skip to content

Commit

Permalink
Also validate shim configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Mar 20, 2020
1 parent 06cacce commit 56c2627
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 23 deletions.
50 changes: 27 additions & 23 deletions lib/projectPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ProjectPreprocessor {
return this.applyExtension(extProject);
}));
}
this.applyShims(project);
await this.applyShims(project);
if (this.isConfigValid(project)) {
// Do not apply transparent projects.
// Their only purpose might be to have their dependencies processed
Expand Down Expand Up @@ -201,26 +201,7 @@ class ProjectPreprocessor {
return {};
}

// Validate project config, but exclude additional properties
const excludedProperties = [
"id",
"version",
"path",
"dependencies",
"_level"
];
const config = {};
for (const key in project) {
if (project.hasOwnProperty(key) && !excludedProperties.includes(key)) {
config[key] = project[key];
}
}
await validate({
config,
project: {
id: project.id
}
});
await this.validateExistingProject(project);

this.normalizeConfig(project);
return {};
Expand Down Expand Up @@ -521,7 +502,7 @@ class ProjectPreprocessor {
}
}

applyShims(project) {
async applyShims(project) {
const configShim = this.configShims[project.id];
// Apply configuration shims
if (configShim) {
Expand Down Expand Up @@ -553,7 +534,7 @@ class ProjectPreprocessor {
Object.assign(project, configShim);
delete project.shimDependenciesResolved; // Remove shim processing metadata from project

// TODO: validate
await this.validateExistingProject(project);
}

// Apply collections
Expand Down Expand Up @@ -611,6 +592,29 @@ class ProjectPreprocessor {
const middlewarePath = path.join(extension.path, extension.middleware.path);
middlewareRepository.addMiddleware(extension.metadata.name, middlewarePath);
}

async validateExistingProject(project) {
// Validate project config, but exclude additional properties
const excludedProperties = [
"id",
"version",
"path",
"dependencies",
"_level"
];
const config = {};
for (const key in project) {
if (project.hasOwnProperty(key) && !excludedProperties.includes(key)) {
config[key] = project[key];
}
}
await validate({
config,
project: {
id: project.id
}
});
}
}

/**
Expand Down
71 changes: 71 additions & 0 deletions test/lib/extensions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const test = require("ava");
const path = require("path");
const sinon = require("sinon");
const ValidationError = require("../../lib/validation/ValidationError");
const projectPreprocessor = require("../..").projectPreprocessor;
const Preprocessor = require("../..").projectPreprocessor._ProjectPreprocessor;
const applicationAPath = path.join(__dirname, "..", "fixtures", "application.a");
Expand Down Expand Up @@ -111,6 +112,50 @@ test("Project with project-shim extension with dependency configuration", (t) =>
});
});

test("Project with project-shim extension with invalid dependency configuration", async (t) => {
const tree = {
id: "application.a",
path: applicationAPath,
dependencies: [{
id: "extension.a",
path: applicationAPath,
dependencies: [],
version: "1.0.0",
specVersion: "0.1",
kind: "extension",
type: "project-shim",
metadata: {
name: "shims.a"
},
shims: {
configurations: {
"legacy.library.a": {
specVersion: "2.0",
type: "library"
}
}
}
}, {
id: "legacy.library.a",
version: "1.0.0",
path: legacyLibraryAPath,
dependencies: []
}],
version: "1.0.0",
specVersion: "0.1",
type: "application",
metadata: {
name: "xy"
}
};

const validationError = await t.throwsAsync(projectPreprocessor.processTree(tree), {
instanceOf: ValidationError
});
t.true(validationError.message.includes("Configuration should have required property 'metadata'"),
"ValidationError should contain error about missing metadata configuration");
});

test("Project with project-shim extension with dependency declaration and configuration", (t) => {
const tree = {
id: "application.a",
Expand Down Expand Up @@ -761,6 +806,32 @@ test("specVersion: Extension with valid version 1.1", async (t) => {
t.deepEqual(handleShimStub.getCall(0).args[0].specVersion, "1.1", "Correct spec version");
});

test("specVersion: Extension with invalid configuration shim", async (t) => {
const extension = {
id: "extension.a",
path: applicationAPath,
dependencies: [],
version: "1.0.0",
specVersion: "1.1",
kind: "extension",
type: "project-shim",
metadata: {
name: "shims.a"
},
shims: {
configuration: {
dependency: {

}
}
}
};
const preprocessor = new Preprocessor({});
const handleShimStub = sinon.stub(preprocessor, "handleShim");
await preprocessor.applyExtension(extension);
t.deepEqual(handleShimStub.getCall(0).args[0].specVersion, "1.1", "Correct spec version");
});

test("specVersion: Extension with valid version 2.0", async (t) => {
const extension = {
id: "extension.a",
Expand Down

0 comments on commit 56c2627

Please sign in to comment.