Skip to content

Commit fa94176

Browse files
matz3RandomByte
authored andcommitted
test(project): Add failing ProjectBuilder test case
1 parent d44a533 commit fa94176

1 file changed

Lines changed: 65 additions & 44 deletions

File tree

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import test from "ava";
22
import sinonGlobal from "sinon";
33
import {fileURLToPath} from "node:url";
4-
import ProjectGraph from "../../../lib/graph/ProjectGraph.js";
4+
import fs from "node:fs/promises";
55
import ProjectBuilder from "../../../lib/build/ProjectBuilder.js";
6-
import Application from "../../../lib/specifications/types/Application.js";
76
import * as taskRepository from "@ui5/builder/internal/taskRepository";
7+
import {graphFromPackageDependencies} from "../../../lib/graph/graph.js";
88

99
test.beforeEach((t) => {
1010
t.context.sinon = sinonGlobal.createSandbox();
@@ -15,64 +15,49 @@ test.afterEach.always((t) => {
1515
delete process.env.UI5_DATA_DIR;
1616
});
1717

18-
test.serial("Build application project twice without changes", async (t) => {
19-
const {sinon} = t.context;
20-
21-
process.env.UI5_DATA_DIR = getTmpPath("application.a/.ui5");
22-
23-
async function createProjectBuilder() {
24-
const customApplicationProject = new Application();
25-
await customApplicationProject.init({
26-
"id": "application.a",
27-
"version": "1.0.0",
28-
"modulePath": getFixturePath("application.a"),
29-
"configuration": {
30-
"specVersion": "5.0",
31-
"type": "application",
32-
"metadata": {
33-
"name": "application.a"
34-
},
35-
"kind": "project"
36-
}
37-
});
18+
test.serial("Build application project multiple times", async (t) => {
19+
const fixtureTester = new FixtureTester(t, "application.a");
3820

39-
const graph = new ProjectGraph({
40-
rootProjectName: customApplicationProject.getName(),
41-
});
42-
graph.addProject(customApplicationProject);
43-
graph.seal(); // Graph needs to be sealed before building
21+
let projectBuilder;
22+
const destPath = fixtureTester.destPath;
4423

45-
const buildConfig = {};
24+
// #1 build (with empty cache)
25+
projectBuilder = await fixtureTester.createProjectBuilder();
26+
await projectBuilder.build({destPath});
4627

47-
const projectBuilder = new ProjectBuilder({
48-
graph,
49-
taskRepository,
50-
buildConfig
51-
});
52-
sinon.spy(projectBuilder, "_buildProject");
53-
return projectBuilder;
54-
}
28+
t.is(projectBuilder._buildProject.callCount, 1);
29+
t.is(
30+
projectBuilder._buildProject.getCall(0).args[0].getProject().getName(),
31+
"application.a",
32+
"application.a built in build #1"
33+
);
34+
35+
// #2 build (with cache, no changes)
36+
projectBuilder = await fixtureTester.createProjectBuilder();
37+
await projectBuilder.build({destPath});
5538

56-
const destPath = getTmpPath("application.a/dist");
39+
t.is(projectBuilder._buildProject.callCount, 0, "No projects built in build #2");
5740

58-
let projectBuilder = await createProjectBuilder();
41+
// Change a source file in application.a
42+
const changedFilePath = `${fixtureTester.fixturePath}/webapp/test.js`;
43+
await fs.appendFile(changedFilePath, `\ntest("line added");\n`);
5944

60-
// First build (with empty cache)
45+
// #3 build (with cache, with changes)
46+
projectBuilder = await fixtureTester.createProjectBuilder();
6147
await projectBuilder.build({destPath});
6248

6349
t.is(projectBuilder._buildProject.callCount, 1);
6450
t.is(
6551
projectBuilder._buildProject.getCall(0).args[0].getProject().getName(),
6652
"application.a",
67-
"application.a built in first build"
53+
"application.a rebuilt in build #3"
6854
);
6955

70-
// Second build (with cache, no changes)
71-
projectBuilder = await createProjectBuilder();
72-
56+
// #4 build (with cache, no changes)
57+
projectBuilder = await fixtureTester.createProjectBuilder();
7358
await projectBuilder.build({destPath});
7459

75-
t.is(projectBuilder._buildProject.callCount, 0, "No projects built in second build");
60+
t.is(projectBuilder._buildProject.callCount, 0, "No projects built in build #4");
7661
});
7762

7863
function getFixturePath(fixtureName) {
@@ -82,3 +67,39 @@ function getFixturePath(fixtureName) {
8267
function getTmpPath(folderName) {
8368
return fileURLToPath(new URL(`../../tmp/${folderName}`, import.meta.url));
8469
}
70+
71+
class FixtureTester {
72+
constructor(t, fixtureName) {
73+
this._sinon = t.context.sinon;
74+
this._fixtureName = fixtureName;
75+
this._initialized = false;
76+
77+
// Public
78+
this.fixturePath = getTmpPath(fixtureName);
79+
this.destPath = getTmpPath(`${fixtureName}/dist`);
80+
}
81+
82+
async _initialize() {
83+
if (this._initialized) {
84+
return;
85+
}
86+
process.env.UI5_DATA_DIR = getTmpPath(`${this._fixtureName}/.ui5`);
87+
await fs.cp(getFixturePath(this._fixtureName), this.fixturePath, {recursive: true});
88+
this._initialized = true;
89+
}
90+
91+
async createProjectBuilder() {
92+
await this._initialize();
93+
const graph = await graphFromPackageDependencies({
94+
cwd: this.fixturePath
95+
});
96+
graph.seal();
97+
const projectBuilder = new ProjectBuilder({
98+
graph,
99+
taskRepository,
100+
buildConfig: {}
101+
});
102+
this._sinon.spy(projectBuilder, "_buildProject");
103+
return projectBuilder;
104+
}
105+
}

0 commit comments

Comments
 (0)