-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INTERNAL] Add tests for BuildContext and ProjectBuildContext
Also split them into separate files
- Loading branch information
1 parent
2f50261
commit 31fc208
Showing
4 changed files
with
188 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |