Skip to content

Commit d1f1565

Browse files
authored
test: run webpack multi compiler test cases with test tools (#11787)
1 parent 4d772e7 commit d1f1565

File tree

15 files changed

+1091
-173
lines changed

15 files changed

+1091
-173
lines changed

packages/rspack-test-tools/etc/test-tools.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ export function createHotNormalCase(name: string, src: string, dist: string): vo
121121
// @public (undocumented)
122122
export function createHotStepCase(name: string, src: string, dist: string, target: TCompilerOptions<ECompilerType.Rspack>["target"]): void;
123123

124+
// @public (undocumented)
125+
export function createMultiCompilerCase(name: string, src: string, dist: string, testConfig: string): void;
126+
124127
// @public (undocumented)
125128
export function createNativeWatcher(name: string, src: string, dist: string, temp: string): void;
126129

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export {
1515
createHotIncrementalCase,
1616
createWatchIncrementalCase
1717
} from "./incremental";
18+
export { createMultiCompilerCase } from "./multi-compiler";
1819
export { createNativeWatcher } from "./native-watcher";
1920
export { createHotNormalCase, createNormalCase } from "./normal";
2021
export { createSerialCase } from "./serial";
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import path from "node:path";
2+
import type { OutputFileSystem, WatchFileSystem } from "@rspack/core";
3+
import { createFsFromVolume, Volume } from "memfs";
4+
import { BasicCaseCreator } from "../test/creator";
5+
import type {
6+
ECompilerType,
7+
ITestContext,
8+
ITestEnv,
9+
ITestProcessor,
10+
TCompilation,
11+
TCompiler,
12+
TCompilerOptions,
13+
TCompilerStats,
14+
TCompilerStatsCompilation
15+
} from "../type";
16+
import { getCompiler } from "./common";
17+
18+
function createMultiCompilerProcessor(
19+
name: string,
20+
caseConfig: TMultiCompilerCaseConfig
21+
) {
22+
return {
23+
config: async (context: ITestContext) => {
24+
const compiler = getCompiler(context, name);
25+
const options = Object.assign(
26+
[
27+
{
28+
name: "a",
29+
context: path.join(__dirname, "fixtures"),
30+
entry: "./a.js"
31+
},
32+
{
33+
name: "b",
34+
context: path.join(__dirname, "fixtures"),
35+
entry: "./b.js"
36+
}
37+
],
38+
caseConfig.options?.(context) || {}
39+
);
40+
compiler.setOptions(options);
41+
},
42+
compiler: async (context: ITestContext) => {
43+
const compiler = getCompiler(context, name);
44+
if (caseConfig.compilerCallback) {
45+
compiler.createCompilerWithCallback(caseConfig.compilerCallback);
46+
} else {
47+
compiler.createCompiler();
48+
}
49+
const c = compiler.getCompiler()!;
50+
c.outputFileSystem = createFsFromVolume(new Volume()) as OutputFileSystem;
51+
c.watchFileSystem = {
52+
watch() {
53+
// watch should return a watcher instance
54+
// watcher instance should have close, pause and getInfo methods
55+
return {
56+
close: () => {},
57+
pause: () => {},
58+
getInfo: () => {
59+
return {
60+
changes: new Set(),
61+
removals: new Set(),
62+
fileTimeInfoEntries: new Map(),
63+
directoryTimeInfoEntries: new Map()
64+
};
65+
}
66+
};
67+
}
68+
} as unknown as WatchFileSystem;
69+
await caseConfig.compiler?.(context, c);
70+
},
71+
build: async (context: ITestContext) => {
72+
const compiler = getCompiler(context, name);
73+
if (typeof caseConfig.build === "function") {
74+
await caseConfig.build?.(context, compiler.getCompiler()!);
75+
} else {
76+
await compiler.build();
77+
}
78+
},
79+
run: async (env: ITestEnv, context: ITestContext) => {},
80+
check: async (env: ITestEnv, context: ITestContext) => {}
81+
} as ITestProcessor;
82+
}
83+
84+
const creator = new BasicCaseCreator({
85+
clean: true,
86+
describe: false,
87+
steps: ({ name, caseConfig }) => {
88+
return [
89+
createMultiCompilerProcessor(name, caseConfig as TMultiCompilerCaseConfig)
90+
];
91+
},
92+
concurrent: false
93+
});
94+
95+
export function createMultiCompilerCase(
96+
name: string,
97+
src: string,
98+
dist: string,
99+
testConfig: string
100+
) {
101+
let caseConfigList:
102+
| TMultiCompilerCaseConfig
103+
| TMultiCompilerCaseConfig[] = require(testConfig);
104+
if (!Array.isArray(caseConfigList)) {
105+
caseConfigList = [caseConfigList];
106+
}
107+
for (let i = 0; i < caseConfigList.length; i++) {
108+
const caseConfig = caseConfigList[i];
109+
if (caseConfig.skip) {
110+
it.skip(`${name}[${i}]`, () => {});
111+
continue;
112+
}
113+
creator.create(`${name}[${i}]`, src, dist, undefined, {
114+
caseConfig,
115+
description: () => caseConfig.description
116+
});
117+
}
118+
}
119+
120+
export type TMultiCompilerCaseConfig = {
121+
description: string;
122+
error?: boolean;
123+
skip?: boolean;
124+
options?: (context: ITestContext) => TCompilerOptions<ECompilerType.Rspack>;
125+
compiler?: (
126+
context: ITestContext,
127+
compiler: TCompiler<ECompilerType.Rspack>
128+
) => Promise<void>;
129+
build?: (
130+
context: ITestContext,
131+
compiler: TCompiler<ECompilerType.Rspack>
132+
) => Promise<void>;
133+
check?: ({
134+
context,
135+
stats,
136+
files,
137+
compiler,
138+
compilation
139+
}: {
140+
context: ITestContext;
141+
stats?: TCompilerStatsCompilation<ECompilerType.Rspack>;
142+
files?: Record<string, string>;
143+
compiler: TCompiler<ECompilerType.Rspack>;
144+
compilation?: TCompilation<ECompilerType.Rspack>;
145+
}) => Promise<void>;
146+
compilerCallback?: (
147+
error: Error | null,
148+
stats: TCompilerStats<ECompilerType.Rspack> | null
149+
) => void;
150+
};

packages/rspack/etc/core.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6345,6 +6345,7 @@ declare namespace rspackExports {
63456345
LoaderOptionsPlugin,
63466346
LoaderTargetPlugin,
63476347
OutputFileSystem,
6348+
WatchFileSystem,
63486349
web,
63496350
node,
63506351
electron,
@@ -9004,7 +9005,7 @@ type WatchFiles = {
90049005
};
90059006

90069007
// @public (undocumented)
9007-
interface WatchFileSystem {
9008+
export interface WatchFileSystem {
90089009
// (undocumented)
90099010
watch(files: Iterable<string> & {
90109011
added?: Iterable<String>;

packages/rspack/src/exports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export { default as EntryOptionPlugin } from "./lib/EntryOptionPlugin";
130130
export { EnvironmentPlugin } from "./lib/EnvironmentPlugin";
131131
export { LoaderOptionsPlugin } from "./lib/LoaderOptionsPlugin";
132132
export { LoaderTargetPlugin } from "./lib/LoaderTargetPlugin";
133-
export type { OutputFileSystem } from "./util/fs";
133+
export type { OutputFileSystem, WatchFileSystem } from "./util/fs";
134134

135135
import {
136136
EsmLibraryPlugin,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const path = require("path");
2+
const { createMultiCompilerCase, describeByWalk } = require("@rspack/test-tools");
3+
const srcDir = path.resolve(__dirname, "./fixtures");
4+
5+
describeByWalk(
6+
__filename,
7+
(name, testConfig, dist) => {
8+
createMultiCompilerCase(name, srcDir, dist, testConfig);
9+
},
10+
{
11+
level: 1,
12+
type: "file"
13+
}
14+
);

tests/rspack-test/legacy-test/MultiCompiler.test.js

Lines changed: 0 additions & 170 deletions
This file was deleted.

0 commit comments

Comments
 (0)