Skip to content

Commit

Permalink
[INTERNAL] Add tests for BuildContext and ProjectBuildContext
Browse files Browse the repository at this point in the history
Also split them into separate files
  • Loading branch information
RandomByte committed Aug 11, 2020
1 parent 2f50261 commit 31fc208
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 34 deletions.
40 changes: 6 additions & 34 deletions lib/builder/BuildContext.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const ProjectBuildContext = require("./ProjectBuildContext");

/**
* Context of a build process
*
Expand All @@ -6,8 +8,11 @@
*/
class BuildContext {
constructor({rootProject}) {
this.projectBuildContexts = [];
if (!rootProject) {
throw new Error(`Missing parameter 'rootProject'`);
}
this.rootProject = rootProject;
this.projectBuildContexts = [];
}

getRootProject() {
Expand All @@ -31,37 +36,4 @@ class BuildContext {
}
}


/**
* Build context of a single project. Always part of an overall
* [Build Context]{@link module:@ui5/builder.builder.BuildContext}
*
* @private
* @memberof module:@ui5/builder.builder
*/
class ProjectBuildContext {
constructor({buildContext, project, resources}) {
this._buildContext = buildContext;
this._project = project;
// this.resources = resources;
this.queues = {
cleanup: []
};
}

isRootProject() {
return this._project === this._buildContext.getRootProject();
}

registerCleanupTask(callback) {
this.queues.cleanup.push(callback);
}

async executeCleanupTasks() {
await Promise.all(this.queues.cleanup.map((callback) => {
return callback();
}));
}
}

module.exports = BuildContext;
36 changes: 36 additions & 0 deletions lib/builder/ProjectBuildContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Build context of a single project. Always part of an overall
* [Build Context]{@link module:@ui5/builder.builder.BuildContext}
*
* @private
* @memberof module:@ui5/builder.builder
*/
class ProjectBuildContext {
constructor({buildContext, project, resources}) {
if (!buildContext || !project || !resources) {
throw new Error(`One or more mandatory parameters are missing`);
}
this._buildContext = buildContext;
this._project = project;
// this.resources = resources;
this.queues = {
cleanup: []
};
}

isRootProject() {
return this._project === this._buildContext.getRootProject();
}

registerCleanupTask(callback) {
this.queues.cleanup.push(callback);
}

async executeCleanupTasks() {
await Promise.all(this.queues.cleanup.map((callback) => {
return callback();
}));
}
}

module.exports = ProjectBuildContext;
71 changes: 71 additions & 0 deletions test/lib/builder/BuildContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const test = require("ava");
const sinon = require("sinon");
const mock = require("mock-require");

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

const BuildContext = require("../../../lib/builder/BuildContext");

test("Missing parameters", (t) => {
const error = t.throws(() => {
new BuildContext({});
});

t.is(error.message, `Missing parameter 'rootProject'`, "Threw with expected error message");
});

test("getRootProject", (t) => {
const buildContext = new BuildContext({
rootProject: "pony"
});

t.is(buildContext.getRootProject(), "pony", "Returned correct value");
});

test.serial("createProjectContext", (t) => {
class DummyProjectContext {
constructor({buildContext, project, resources}) {
t.is(buildContext, testBuildContext, "Correct buildContext parameter");
t.is(project, "project", "Correct project parameter");
t.is(resources, "resources", "Correct resources parameter");
}
}
mock("../../../lib/builder/ProjectBuildContext", DummyProjectContext);

const BuildContext = mock.reRequire("../../../lib/builder/BuildContext");
const testBuildContext = new BuildContext({
rootProject: "pony"
});

const projectContext = testBuildContext.createProjectContext({
project: "project",
resources: "resources"
});

t.true(projectContext instanceof DummyProjectContext,
"Project context is an instance of DummyProjectContext");
t.is(testBuildContext.projectBuildContexts[0], projectContext,
"BuildContext stored correct ProjectBuildContext");
});

test("executeCleanupTasks", async (t) => {
const buildContext = new BuildContext({
rootProject: "pony"
});

const executeCleanupTasks = sinon.stub().resolves();

buildContext.projectBuildContexts.push({
executeCleanupTasks
});
buildContext.projectBuildContexts.push({
executeCleanupTasks
});

await buildContext.executeCleanupTasks();

t.is(executeCleanupTasks.callCount, 2,
"Project context executeCleanupTasks got called twice");
});
75 changes: 75 additions & 0 deletions test/lib/builder/ProjectBuildContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const test = require("ava");
const sinon = require("sinon");

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

const ProjectBuildContext = require("../../../lib/builder/ProjectBuildContext");

test("Missing parameters", (t) => {
const error = t.throws(() => {
new ProjectBuildContext({});
});

t.is(error.message, `One or more mandatory parameters are missing`, "Threw with expected error message");
});

test("isRootProject: true", (t) => {
const projectBuildContext = new ProjectBuildContext({
buildContext: {
getRootProject: () => "root project"
},
project: "root project",
resources: "resources"
});

t.true(projectBuildContext.isRootProject(), "Correctly identified root project");
});

test("isRootProject: false", (t) => {
const projectBuildContext = new ProjectBuildContext({
buildContext: {
getRootProject: () => "root project"
},
project: "no root project",
resources: "resources"
});

t.false(projectBuildContext.isRootProject(), "Correctly identified non-root project");
});

test("registerCleanupTask", (t) => {
const projectBuildContext = new ProjectBuildContext({
buildContext: {
getRootProject: () => "root project"
},
project: "no root project",
resources: "resources"
});
projectBuildContext.registerCleanupTask("my task 1");
projectBuildContext.registerCleanupTask("my task 2");

t.is(projectBuildContext.queues.cleanup[0], "my task 1", "Cleanup task registered");
t.is(projectBuildContext.queues.cleanup[1], "my task 2", "Cleanup task registered");
});


test("executeCleanupTasks", (t) => {
const projectBuildContext = new ProjectBuildContext({
buildContext: {
getRootProject: () => "root project"
},
project: "no root project",
resources: "resources"
});
const task1 = sinon.stub().resolves();
const task2 = sinon.stub().resolves();
projectBuildContext.registerCleanupTask(task1);
projectBuildContext.registerCleanupTask(task2);

projectBuildContext.executeCleanupTasks();

t.is(task1.callCount, 1, "Cleanup task 1 got called");
t.is(task2.callCount, 1, "my task 2", "Cleanup task 2 got called");
});

0 comments on commit 31fc208

Please sign in to comment.