Skip to content

Commit 7c809cf

Browse files
committed
test(project): Split BuildServer integration test into focused files
Break the monolithic BuildServer.integration.js into per-topic suites (serving, cacheModes, errorRecovery, abortRetry, taskExclusion, buildSignature) and extract shared setup into a BuildServerFixtureTester helper.
1 parent 942ccbb commit 7c809cf

8 files changed

Lines changed: 1860 additions & 1697 deletions
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import test from "ava";
2+
import esmock from "esmock";
3+
import fs from "node:fs/promises";
4+
import {appendFileSync} from "node:fs";
5+
import {
6+
createParcelWatcherMock, createFixtureTesterFactory, registerBuildHooks,
7+
} from "./__helper__/BuildServerFixtureTester.js";
8+
9+
// Mock @parcel/watcher for the entire import tree reachable from graph.js so the build server's
10+
// WatchHandler does not try to subscribe to real FSEvents/inotify/ReadDirectoryChangesW handles.
11+
// Tests fire watcher events deterministically via FixtureTester#fireWatcherEvent instead of
12+
// waiting for OS-level event delivery, which is both flaky (timing-dependent) and impossible
13+
// inside sandboxed environments where FSEvents/inotify access is restricted.
14+
const watcherMock = createParcelWatcherMock();
15+
const {graphFromPackageDependencies} = await esmock.p("../../../lib/graph/graph.js", {}, {
16+
"@parcel/watcher": {
17+
default: watcherMock.api,
18+
...watcherMock.api,
19+
},
20+
});
21+
22+
const FixtureTester = createFixtureTesterFactory("abortRetry", {graphFromPackageDependencies, watcherMock});
23+
registerBuildHooks(test, {watcherMock});
24+
25+
// ProjectBuildCache's StageCache must be cleared correctly when a build is aborted.
26+
// A task that completed during an aborted attempt has already called recordTaskResult,
27+
// which adds its stage to the in-memory StageCache. On retry,
28+
// prepareTaskExecutionAndValidateCache might finds those entries via #findStageCache if not cleaned up.
29+
// It will then emit task-skip events for tasks that the retry should have actually re-executed.
30+
test.serial("Aborted initial build must not leak in-memory StageCache to retry", async (t) => {
31+
const fixtureTester = t.context.fixtureTester = await FixtureTester.create(t, "library.d");
32+
await fixtureTester.serveProject({
33+
config: {excludedTasks: ["minify"]}
34+
});
35+
const project = fixtureTester.graph.getProject("library.d");
36+
37+
// One-shot trigger: when `replaceBuildtime` (the 4th task for this fixture) ends in the
38+
// initial build, simulate a watcher event by calling _projectResourceChanged directly.
39+
// This invalidates library.d, aborts the running build at the next signal check, and
40+
// re-enqueues it. By that point, tasks 1-4 have completed recordTaskResult and live in
41+
// the in-memory StageCache. Tasks 5+ never started.
42+
let aborted = false;
43+
const abortHandler = (event) => {
44+
if (
45+
!aborted &&
46+
event.projectName === "library.d" &&
47+
event.status === "task-end" &&
48+
event.taskName === "replaceBuildtime"
49+
) {
50+
aborted = true;
51+
fixtureTester.buildServer._projectResourceChanged(
52+
project, "/resources/library/d/some.js", false);
53+
}
54+
};
55+
process.on("ui5.project-build-status", abortHandler);
56+
57+
try {
58+
// byPath returns once the retry succeeds, so all events for both attempts are captured.
59+
await fixtureTester._reader.byPath("/resources/library/d/some.js");
60+
} finally {
61+
process.off("ui5.project-build-status", abortHandler);
62+
}
63+
64+
t.true(aborted, "Test setup precondition: abort trigger should have fired");
65+
66+
// On a fresh fixture the persistent cache is empty. After the fix, the retry's
67+
// prepareTaskExecutionAndValidateCache should find no cached stages (in-memory cache
68+
// from the aborted build is discarded) and execute every task. No task-skip events
69+
// should be emitted for library.d.
70+
const skippedTasks = t.context.projectBuildStatusEventStub.args
71+
.map(([event]) => event)
72+
.filter((e) => e.projectName === "library.d" && e.status === "task-skip")
73+
.map((e) => e.taskName);
74+
75+
t.deepEqual(skippedTasks, [],
76+
"Persistent cache is empty and the in-memory StageCache populated by the aborted " +
77+
"attempt must not be reused on retry");
78+
});
79+
80+
// Same scenario as above but the for a later abort: After `generateLibraryPreload`
81+
test.serial(
82+
"Aborted initial build must not leak in-memory StageCache to retry (late abort)", async (t) => {
83+
const fixtureTester = t.context.fixtureTester = await FixtureTester.create(t, "library.d");
84+
await fixtureTester.serveProject({
85+
config: {excludedTasks: ["minify"]}
86+
});
87+
const project = fixtureTester.graph.getProject("library.d");
88+
89+
let aborted = false;
90+
const abortHandler = (event) => {
91+
if (
92+
!aborted &&
93+
event.projectName === "library.d" &&
94+
event.status === "task-end" &&
95+
event.taskName === "generateLibraryPreload"
96+
) {
97+
aborted = true;
98+
fixtureTester.buildServer._projectResourceChanged(
99+
project, "/resources/library/d/some.js", false);
100+
}
101+
};
102+
process.on("ui5.project-build-status", abortHandler);
103+
104+
try {
105+
await fixtureTester._reader.byPath("/resources/library/d/some.js");
106+
} finally {
107+
process.off("ui5.project-build-status", abortHandler);
108+
}
109+
110+
t.true(aborted, "Test setup precondition: abort trigger should have fired");
111+
112+
const skippedTasks = t.context.projectBuildStatusEventStub.args
113+
.map(([event]) => event)
114+
.filter((e) => e.projectName === "library.d" && e.status === "task-skip")
115+
.map((e) => e.taskName);
116+
117+
t.deepEqual(skippedTasks, [],
118+
"Persistent cache is empty and the in-memory StageCache populated by the aborted " +
119+
"attempt must not be reused on retry");
120+
}
121+
);
122+
123+
// Regression: a build that hits the NO_CACHE state in validateCache({prepareForBuild: true})
124+
// (because the source signature does not match anything in the persistent cache) and then
125+
// throws SourceChangedDuringBuildError from allTasksCompleted used to fail on retry with
126+
// "Unexpected result cache state after restoring dependency indices for project XYZ: no_cache".
127+
// The fix resets #resultCacheState to PENDING_VALIDATION in the source-changed branch.
128+
//
129+
// Repro recipe — must hit *all* of these conditions on the same ProjectBuildCache instance:
130+
// 1. A first build runs to completion, populating the persistent index + result cache.
131+
// 2. The project is invalidated (a real source change observed by the watcher) so the next
132+
// reader request drives a second build.
133+
// 3. The second build's #initSourceIndex finds an existing index cache and transitions to
134+
// RESTORING_DEPENDENCY_INDICES (rather than INITIAL, which short-circuits prepare).
135+
// 4. validateCache({prepareForBuild: true}) sees a source-signature mismatch against the
136+
// persisted result cache and sets #resultCacheState = NO_CACHE.
137+
// 5. A *further* on-disk source change lands during the second build, but the watcher path
138+
// is stubbed so the abort signal is never set. allTasksCompleted's revalidateSourceIndex
139+
// then throws SourceChangedDuringBuildError instead of taking the abort path.
140+
test.serial("Source change during second build retries cleanly without no_cache error", async (t) => {
141+
const fixtureTester = t.context.fixtureTester = await FixtureTester.create(t, "library.d");
142+
await fixtureTester.serveProject({
143+
config: {excludedTasks: ["minify"]}
144+
});
145+
146+
const changedFilePath = `${fixtureTester.fixturePath}/main/src/library/d/some.js`;
147+
const originalContent = await fs.readFile(changedFilePath, {encoding: "utf8"});
148+
149+
// Build 1 — populates the on-disk index + result cache.
150+
await fixtureTester.requestResource({resource: "/resources/library/d/some.js"});
151+
152+
// Invalidate the project for build 2 by touching the source file. Use the live watcher
153+
// path here: a real modification needs to flow through _projectResourceChanged so the
154+
// project transitions to INVALIDATED and the next request enqueues a rebuild.
155+
await fs.writeFile(changedFilePath, originalContent + "\n// pre-build-2 change\n");
156+
await fixtureTester.fireWatcherEvent("update", changedFilePath);
157+
158+
// Now suppress further watcher-driven aborts. The mid-build modification below is meant
159+
// to flow through #revalidateSourceIndex inside allTasksCompleted, *not* through the
160+
// watcher — otherwise the abort path runs first and the no_cache assertion never fires.
161+
t.context.sinon.stub(fixtureTester.buildServer, "_projectResourceChanged");
162+
163+
// During build 2's task pipeline, append a second on-disk change. Hook the first task
164+
// that is *not* short-circuited from cache (replaceCopyright) so the synchronous write
165+
// lands well before allTasksCompleted's #revalidateSourceIndex reads from disk.
166+
let triggered = false;
167+
const handler = (event) => {
168+
if (
169+
!triggered &&
170+
event.projectName === "library.d" &&
171+
event.status === "task-start" &&
172+
event.taskName === "replaceCopyright"
173+
) {
174+
triggered = true;
175+
appendFileSync(changedFilePath, "\n// mid-build-2 change\n");
176+
}
177+
};
178+
process.on("ui5.project-build-status", handler);
179+
180+
let resource;
181+
try {
182+
// Without the fix this rejects with
183+
// "Unexpected result cache state after restoring dependency indices for project XYZ: no_cache".
184+
resource = await fixtureTester._reader.byPath("/resources/library/d/some.js");
185+
} finally {
186+
process.off("ui5.project-build-status", handler);
187+
}
188+
189+
t.true(triggered, "Test setup precondition: source change handler fired during build 2");
190+
191+
const servedContent = await resource.getString();
192+
t.true(servedContent.includes("pre-build-2 change"),
193+
"Retry served content reflecting the pre-build-2 change");
194+
t.true(servedContent.includes("mid-build-2 change"),
195+
"Retry served content reflecting the mid-build-2 change");
196+
});

0 commit comments

Comments
 (0)