Skip to content

Commit cc261ac

Browse files
authored
refactor: cache test case support NEXT_MOVE_DIR_START (#12359)
* refactor: cache test support NEXT_MOVE_DIR_START * test: add test * fix: comment
1 parent 869396f commit cc261ac

File tree

5 files changed

+126
-62
lines changed

5 files changed

+126
-62
lines changed

packages/rspack-test-tools/src/case/cache.ts

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import { cachedStats } from "./runner";
1919

2020
type TTarget = RspackOptions["target"];
2121

22-
const MAX_COMPILER_INDEX = 100;
23-
2422
function createCacheProcessor(
2523
name: string,
2624
src: string,
@@ -35,16 +33,12 @@ function createCacheProcessor(
3533
},
3634
config: async (context: ITestContext) => {
3735
const compiler = context.getCompiler();
38-
let options = defaultOptions(context, temp, target);
39-
options = await config(
36+
const options = await generateOptions(
4037
context,
41-
name,
42-
["rspack.config.js", "webpack.config.js"].map(i =>
43-
path.resolve(temp, i)
44-
),
45-
options
38+
temp,
39+
target,
40+
updatePlugin
4641
);
47-
overrideOptions(options, temp, target, updatePlugin);
4842
compiler.setOptions(options);
4943
},
5044
compiler: async (context: ITestContext) => {
@@ -111,12 +105,13 @@ export function createCacheCase(
111105

112106
const creators: Map<TTarget, BasicCaseCreator> = new Map();
113107

114-
function defaultOptions(
108+
async function generateOptions(
115109
context: ITestContext,
116110
temp: string,
117-
target: TTarget
118-
): RspackOptions {
119-
const options = {
111+
target: TTarget,
112+
updatePlugin: HotUpdatePlugin
113+
): Promise<RspackOptions> {
114+
let options = {
120115
context: temp,
121116
mode: "production",
122117
cache: true,
@@ -147,15 +142,14 @@ function defaultOptions(
147142
options.plugins ??= [];
148143
options.plugins.push(new rspack.HotModuleReplacementPlugin());
149144

150-
return options;
151-
}
145+
options = await config(
146+
context,
147+
"cacheCase",
148+
["rspack.config.js", "webpack.config.js"].map(i => path.resolve(temp, i)),
149+
options
150+
);
152151

153-
function overrideOptions(
154-
options: RspackOptions,
155-
temp: string,
156-
target: TTarget,
157-
updatePlugin: HotUpdatePlugin
158-
): void {
152+
// overwrite
159153
if (!options.entry) {
160154
options.entry = "./index.js";
161155
}
@@ -176,6 +170,8 @@ function overrideOptions(
176170
level: "error"
177171
};
178172
}
173+
174+
return options;
179175
}
180176

181177
function findBundle(
@@ -245,6 +241,7 @@ function createRunner(
245241
moduleScope.COMPILER_INDEX = compilerIndex;
246242
moduleScope.NEXT_HMR = nextHmr;
247243
moduleScope.NEXT_START = nextStart;
244+
moduleScope.NEXT_MOVE_DIR_START = nextMoveDirStart;
248245
return moduleScope;
249246
}
250247
},
@@ -253,34 +250,35 @@ function createRunner(
253250
compilerOptions: options
254251
});
255252
};
256-
const nextHmr = async (m: any, options?: any): Promise<StatsCompilation> => {
257-
await updatePlugin.goNext();
258-
const stats = await compiler.build();
259-
if (!stats) {
260-
throw new Error("Should generate stats during build");
261-
}
262-
const jsonStats = stats.toJson({
263-
// errorDetails: true
264-
});
265-
const compilerOptions = compiler.getOptions();
266253

254+
const checkStats = async (stats: StatsCompilation) => {
255+
const compilerOptions = compiler.getOptions();
267256
const updateIndex = updatePlugin.getUpdateIndex();
268257
await checkArrayExpectation(
269258
source,
270-
jsonStats,
259+
stats,
271260
"error",
272261
`errors${updateIndex}`,
273262
"Error",
274263
compilerOptions
275264
);
276265
await checkArrayExpectation(
277266
source,
278-
jsonStats,
267+
stats,
279268
"warning",
280269
`warnings${updateIndex}`,
281270
"Warning",
282271
compilerOptions
283272
);
273+
};
274+
275+
const nextHmr = async (m: any, options?: any): Promise<StatsCompilation> => {
276+
await updatePlugin.goNext();
277+
const stats = await compiler.build();
278+
const jsonStats = stats.toJson({
279+
// errorDetails: true
280+
});
281+
await checkStats(jsonStats);
284282

285283
const updatedModules = await m.hot.check(options || true);
286284
if (!updatedModules) {
@@ -292,43 +290,48 @@ function createRunner(
292290

293291
const nextStart = async (): Promise<StatsCompilation> => {
294292
await compiler.close();
295-
compiler.createCompiler();
293+
296294
await updatePlugin.goNext();
295+
compilerIndex++;
296+
297+
compiler.createCompiler();
297298
const stats = await compiler.build();
298-
if (!stats) {
299-
throw new Error("Should generate stats during build");
300-
}
301299
const jsonStats = stats.toJson({
302300
// errorDetails: true
303301
});
304-
const compilerOptions = compiler.getOptions();
302+
await checkStats(jsonStats);
305303

306-
const updateIndex = updatePlugin.getUpdateIndex();
307-
await checkArrayExpectation(
308-
source,
309-
jsonStats,
310-
"error",
311-
`errors${updateIndex}`,
312-
"Error",
313-
compilerOptions
314-
);
315-
await checkArrayExpectation(
316-
source,
317-
jsonStats,
318-
"warning",
319-
`warnings${updateIndex}`,
320-
"Warning",
321-
compilerOptions
304+
env.it(`NEXT_START run with compilerIndex==${compilerIndex}`, async () => {
305+
return getWebRunner().run(file);
306+
});
307+
return jsonStats;
308+
};
309+
310+
const nextMoveDirStart = async (): Promise<StatsCompilation> => {
311+
await compiler.close();
312+
313+
const tempDir = await updatePlugin.moveTempDir();
314+
const options = await generateOptions(
315+
context,
316+
tempDir,
317+
compiler.getOptions().target,
318+
updatePlugin
322319
);
320+
compiler.setOptions(options);
321+
await updatePlugin.goNext();
322+
compilerIndex++;
323+
324+
compiler.createCompiler();
325+
const stats = await compiler.build();
326+
const jsonStats = stats.toJson({
327+
// errorDetails: true
328+
});
329+
330+
await checkStats(jsonStats);
331+
323332
env.it(
324-
`NEXT_START run with compilerIndex==${compilerIndex + 1}`,
333+
`NEXT_MOVE_DIR_START run with compilerIndex==${compilerIndex}`,
325334
async () => {
326-
if (compilerIndex > MAX_COMPILER_INDEX) {
327-
throw new Error(
328-
"NEXT_START has been called more than the maximum times"
329-
);
330-
}
331-
compilerIndex++;
332335
return getWebRunner().run(file);
333336
}
334337
);

packages/rspack-test-tools/src/helper/hot-update/plugin.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ async function loopFile(
2323
}
2424

2525
const PLUGIN_NAME = "HotUpdatePlugin";
26+
const MAX_UPDATE_INDEX = 100;
2627

2728
export class HotUpdatePlugin {
2829
private initialized = false;
@@ -107,9 +108,33 @@ export class HotUpdatePlugin {
107108
}, 1);
108109
}
109110
async goNext() {
111+
if (this.updateIndex > MAX_UPDATE_INDEX) {
112+
throw new Error("NEXT_* has been called more than the maximum times");
113+
}
110114
this.updateIndex++;
111115
await this.updateFiles();
112116
}
117+
async moveTempDir() {
118+
// generate next temp dir path.
119+
const nextTempDir =
120+
this.tempDir.replace(/(___[0-9]+)?[/\\]*$/, "") +
121+
"___" +
122+
this.updateIndex;
123+
124+
// update this.files.
125+
for (const key of Object.keys(this.files)) {
126+
const nextKey = key.replace(this.tempDir, nextTempDir);
127+
this.files[nextKey] = this.files[key];
128+
delete this.files[key];
129+
}
130+
131+
// move files.
132+
rimrafSync(nextTempDir);
133+
fs.renameSync(this.tempDir, nextTempDir);
134+
this.tempDir = nextTempDir;
135+
136+
return this.tempDir;
137+
}
113138

114139
apply(compiler: Compiler) {
115140
const options = compiler.options;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default 1;
2+
---
3+
export default 2;
4+
---
5+
export default 3;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import value from "./file";
2+
3+
it("should basic test work", async () => {
4+
if (COMPILER_INDEX == 0) {
5+
expect(value).toBe(1);
6+
await NEXT_START();
7+
}
8+
if (COMPILER_INDEX == 1) {
9+
expect(value).toBe(1);
10+
await NEXT_MOVE_DIR_START();
11+
}
12+
if (COMPILER_INDEX == 2) {
13+
expect(value).toBe(3);
14+
}
15+
});
16+
17+
module.hot.accept("./file");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const path = require("path");
2+
3+
/** @type {import("@rspack/core").Configuration} */
4+
module.exports = {
5+
context: __dirname,
6+
experiments: {
7+
cache: {
8+
type: "persistent",
9+
snapshot: {
10+
immutablePaths: [path.resolve(__dirname, "./file.js")]
11+
}
12+
}
13+
}
14+
};

0 commit comments

Comments
 (0)