Skip to content

Commit 7928453

Browse files
committed
test: remove simple tester
1 parent 228ec1b commit 7928453

File tree

14 files changed

+407
-367
lines changed

14 files changed

+407
-367
lines changed

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,39 @@ import type { WebpackOptionsNormalized } from 'webpack';
2626
export class BasicCaseCreator<T extends ECompilerType> {
2727
constructor(_options: IBasicCaseCreatorOptions<T>);
2828
// (undocumented)
29-
protected checkSkipped(src: string, testConfig: TTestConfig<T>): boolean | string;
29+
protected checkSkipped(src: string, testConfig: TTestConfig<T>, options: IBasicCaseCreatorOptions<T>): boolean | string;
3030
// (undocumented)
3131
protected clean(folders: string[]): void;
3232
// (undocumented)
33-
create(name: string, src: string, dist: string, temp?: string): ITester | undefined;
33+
create(name: string, src: string, dist: string, temp?: string, caseOptions?: Partial<IBasicCaseCreatorOptions<T>>): ITester | undefined;
3434
// (undocumented)
3535
protected createConcurrentEnv(): ITestEnv & IConcurrentTestEnv;
3636
// (undocumented)
37-
protected createEnv(testConfig: TTestConfig<T>): ITestEnv;
37+
protected createEnv(testConfig: TTestConfig<T>, options: IBasicCaseCreatorOptions<T>): ITestEnv;
3838
// (undocumented)
39-
protected createTester(name: string, src: string, dist: string, temp: string | undefined, testConfig: TTestConfig<T>): ITester;
39+
protected createTester(name: string, src: string, dist: string, temp: string | undefined, testConfig: TTestConfig<T>, options: IBasicCaseCreatorOptions<T>): ITester;
4040
// (undocumented)
4141
protected currentConcurrent: number;
4242
// (undocumented)
43-
protected describe(name: string, tester: ITester, testConfig: TTestConfig<T>): void;
43+
protected describe(name: string, tester: ITester, testConfig: TTestConfig<T>, options: IBasicCaseCreatorOptions<T>): void;
4444
// (undocumented)
45-
protected describeConcurrent(name: string, tester: ITester, testConfig: TTestConfig<T>): void;
45+
protected describeConcurrent(name: string, tester: ITester, testConfig: TTestConfig<T>, options: IBasicCaseCreatorOptions<T>): void;
4646
// (undocumented)
47-
protected getMaxConcurrent(): number;
47+
protected getMaxConcurrent(concurrent?: number): number;
4848
// (undocumented)
4949
protected _options: IBasicCaseCreatorOptions<T>;
5050
// (undocumented)
5151
protected readTestConfig(src: string): TTestConfig<T>;
5252
// (undocumented)
53-
protected registerConcurrentTask(name: string, starter: () => void): () => void;
53+
protected registerConcurrentTask(name: string, starter: () => void, concurrent?: number): () => void;
5454
// (undocumented)
5555
protected shouldRun(name: string): boolean;
5656
// (undocumented)
5757
protected skip(name: string, reason: string | boolean): void;
5858
// (undocumented)
5959
protected tasks: [string, () => void][];
6060
// (undocumented)
61-
protected tryRunTask(): void;
61+
protected tryRunTask(concurrent?: number): void;
6262
}
6363

6464
// @public (undocumented)
@@ -235,12 +235,6 @@ export function formatCode(name: string, raw: string, options: IFormatCodeOption
235235
// @public (undocumented)
236236
export function getRspackDefaultConfig(cwd: string, config: TCompilerOptions<ECompilerType>): TCompilerOptions<ECompilerType>;
237237

238-
// @public (undocumented)
239-
export function getSimpleProcessorRunner(src: string, dist: string, options?: {
240-
env?: () => ITestEnv;
241-
context?: (src: string, dist: string) => ITestContext;
242-
}): (name: string, processor: ITestProcessor) => Promise<void>;
243-
244238
// @public (undocumented)
245239
export interface IBasicCaseCreatorOptions<T extends ECompilerType> {
246240
// (undocumented)
@@ -252,6 +246,8 @@ export interface IBasicCaseCreatorOptions<T extends ECompilerType> {
252246
// (undocumented)
253247
contextValue?: Record<string, unknown>;
254248
// (undocumented)
249+
createContext?: (config: ITesterConfig) => ITestContext;
250+
// (undocumented)
255251
describe?: boolean;
256252
// (undocumented)
257253
description?: (name: string, step: number) => string;
@@ -527,6 +523,8 @@ export interface ITesterConfig {
527523
// (undocumented)
528524
contextValue?: Record<string, unknown>;
529525
// (undocumented)
526+
createContext?: (config: ITesterConfig) => ITestContext;
527+
// (undocumented)
530528
dist: string;
531529
// (undocumented)
532530
name: string;

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

Lines changed: 156 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { OutputFileSystem } from "@rspack/core";
2-
import { getSimpleProcessorRunner } from "../test/simple";
2+
import { BasicCaseCreator } from "../test/creator";
33
import type {
44
ECompilerType,
55
ITestContext,
66
ITestEnv,
7+
ITestProcessor,
78
TCompilation,
89
TCompiler,
910
TCompilerOptions,
@@ -12,6 +13,151 @@ import type {
1213
} from "../type";
1314
import { getCompiler } from "./common";
1415

16+
function createCompilerProcessor(
17+
name: string,
18+
caseConfig: TCompilerCaseConfig
19+
) {
20+
const logs = {
21+
mkdir: [] as string[],
22+
writeFile: [] as (string | number | Buffer<ArrayBufferLike>)[]
23+
};
24+
const files = {} as Record<string, string>;
25+
return {
26+
config: async (context: ITestContext) => {
27+
const compiler = getCompiler(context, name);
28+
const options = caseConfig.options?.(context) || {};
29+
options.mode ??= "production";
30+
options.context ??= context.getSource();
31+
options.entry ??= "./a.js";
32+
options.output ??= {};
33+
options.output.path ??= "/";
34+
options.output.pathinfo ??= true;
35+
options.optimization ??= {};
36+
options.optimization.minimize ??= false;
37+
compiler.setOptions(options);
38+
},
39+
compiler: async (context: ITestContext) => {
40+
const compiler = getCompiler(context, name);
41+
if (caseConfig.compilerCallback) {
42+
compiler.createCompilerWithCallback(caseConfig.compilerCallback);
43+
} else {
44+
compiler.createCompiler();
45+
}
46+
const c = compiler.getCompiler()!;
47+
c.outputFileSystem = {
48+
// CHANGE: Added support for the `options` parameter to enable recursive directory creation,
49+
// accommodating Rspack's requirement that differs from webpack's usage
50+
mkdir(
51+
path: string,
52+
callback: (
53+
err?: Error & {
54+
code?: string;
55+
}
56+
) => void
57+
) {
58+
const recursive = false;
59+
// if (typeof options === "function") {
60+
// callback = options;
61+
// } else if (options) {
62+
// if (options.recursive !== undefined) recursive = options.recursive;
63+
// }
64+
logs.mkdir.push(path);
65+
if (recursive) {
66+
callback();
67+
} else {
68+
const err = new Error() as Error & {
69+
code?: string;
70+
};
71+
err.code = "EEXIST";
72+
callback(err);
73+
}
74+
},
75+
writeFile(name, content, callback) {
76+
logs.writeFile.push(name, content);
77+
files[name] = content.toString("utf-8");
78+
callback();
79+
},
80+
stat(path, callback) {
81+
callback(new Error("ENOENT"));
82+
}
83+
} as OutputFileSystem;
84+
c.hooks.compilation.tap(
85+
"CompilerTest",
86+
compilation => ((compilation as any).bail = true)
87+
);
88+
await caseConfig.compiler?.(context, c);
89+
},
90+
build: async (context: ITestContext) => {
91+
const compiler = getCompiler(context, name);
92+
if (typeof caseConfig.build === "function") {
93+
await caseConfig.build?.(context, compiler.getCompiler()!);
94+
} else {
95+
await compiler.build();
96+
}
97+
},
98+
run: async (env: ITestEnv, context: ITestContext) => {},
99+
check: async (env: ITestEnv, context: ITestContext) => {
100+
const compiler = getCompiler(context, name);
101+
const c = compiler.getCompiler()!;
102+
const stats = compiler.getStats() as TCompilerStats<ECompilerType.Rspack>;
103+
if (caseConfig.error) {
104+
const statsJson = stats?.toJson({
105+
modules: true,
106+
reasons: true
107+
});
108+
const compilation = stats?.compilation;
109+
await caseConfig.check?.({
110+
context,
111+
compiler: c,
112+
stats: statsJson,
113+
compilation,
114+
files
115+
});
116+
} else if (stats) {
117+
expect(typeof stats).toBe("object");
118+
const compilation = stats.compilation;
119+
const statsJson = stats.toJson({
120+
modules: true,
121+
reasons: true
122+
});
123+
expect(typeof statsJson).toBe("object");
124+
expect(statsJson).toHaveProperty("errors");
125+
expect(Array.isArray(statsJson.errors)).toBe(true);
126+
if (statsJson.errors!.length > 0) {
127+
expect(statsJson.errors![0]).toBeInstanceOf(Object);
128+
throw statsJson.errors![0];
129+
}
130+
statsJson.logs = logs;
131+
await caseConfig.check?.({
132+
context,
133+
stats: statsJson,
134+
files,
135+
compiler: c,
136+
compilation
137+
});
138+
} else {
139+
await caseConfig.check?.({
140+
context,
141+
files,
142+
compiler: c
143+
});
144+
}
145+
},
146+
after: async (context: ITestContext) => {
147+
await context.closeCompiler(name);
148+
}
149+
} as ITestProcessor;
150+
}
151+
152+
const creator = new BasicCaseCreator({
153+
clean: true,
154+
describe: false,
155+
steps: ({ name, caseConfig }) => {
156+
return [createCompilerProcessor(name, caseConfig as TCompilerCaseConfig)];
157+
},
158+
concurrent: false
159+
});
160+
15161
export function createCompilerCase(
16162
name: string,
17163
src: string,
@@ -24,139 +170,15 @@ export function createCompilerCase(
24170
if (!Array.isArray(caseConfigList)) {
25171
caseConfigList = [caseConfigList];
26172
}
27-
const runner = getSimpleProcessorRunner(src, dist);
28-
29-
for (const caseConfig of caseConfigList) {
30-
const testFn = caseConfig.skip ? it.skip : it;
31-
testFn(caseConfig.description, async () => {
32-
const logs = {
33-
mkdir: [] as string[],
34-
writeFile: [] as (string | number | Buffer<ArrayBufferLike>)[]
35-
};
36-
const files = {} as Record<string, string>;
37-
await runner(name, {
38-
config: async (context: ITestContext) => {
39-
const compiler = getCompiler(context, name);
40-
const options = caseConfig.options?.(context) || {};
41-
options.mode ??= "production";
42-
options.context ??= context.getSource();
43-
options.entry ??= "./a.js";
44-
options.output ??= {};
45-
options.output.path ??= "/";
46-
options.output.pathinfo ??= true;
47-
options.optimization ??= {};
48-
options.optimization.minimize ??= false;
49-
compiler.setOptions(options);
50-
},
51-
compiler: async (context: ITestContext) => {
52-
const compiler = getCompiler(context, name);
53-
if (caseConfig.compilerCallback) {
54-
compiler.createCompilerWithCallback(caseConfig.compilerCallback);
55-
} else {
56-
compiler.createCompiler();
57-
}
58-
const c = compiler.getCompiler()!;
59-
c.outputFileSystem = {
60-
// CHANGE: Added support for the `options` parameter to enable recursive directory creation,
61-
// accommodating Rspack's requirement that differs from webpack's usage
62-
mkdir(
63-
path: string,
64-
callback: (
65-
err?: Error & {
66-
code?: string;
67-
}
68-
) => void
69-
) {
70-
const recursive = false;
71-
// if (typeof options === "function") {
72-
// callback = options;
73-
// } else if (options) {
74-
// if (options.recursive !== undefined) recursive = options.recursive;
75-
// }
76-
logs.mkdir.push(path);
77-
if (recursive) {
78-
callback();
79-
} else {
80-
const err = new Error() as Error & {
81-
code?: string;
82-
};
83-
err.code = "EEXIST";
84-
callback(err);
85-
}
86-
},
87-
writeFile(name, content, callback) {
88-
logs.writeFile.push(name, content);
89-
files[name] = content.toString("utf-8");
90-
callback();
91-
},
92-
stat(path, callback) {
93-
callback(new Error("ENOENT"));
94-
}
95-
} as OutputFileSystem;
96-
c.hooks.compilation.tap(
97-
"CompilerTest",
98-
compilation => ((compilation as any).bail = true)
99-
);
100-
await caseConfig.compiler?.(context, c);
101-
},
102-
build: async (context: ITestContext) => {
103-
const compiler = getCompiler(context, name);
104-
if (typeof caseConfig.build === "function") {
105-
await caseConfig.build?.(context, compiler.getCompiler()!);
106-
} else {
107-
await compiler.build();
108-
}
109-
},
110-
run: async (env: ITestEnv, context: ITestContext) => {},
111-
check: async (env: ITestEnv, context: ITestContext) => {
112-
const compiler = getCompiler(context, name);
113-
const c = compiler.getCompiler()!;
114-
const stats =
115-
compiler.getStats() as TCompilerStats<ECompilerType.Rspack>;
116-
if (caseConfig.error) {
117-
const statsJson = stats?.toJson({
118-
modules: true,
119-
reasons: true
120-
});
121-
const compilation = stats?.compilation;
122-
await caseConfig.check?.({
123-
context,
124-
compiler: c,
125-
stats: statsJson,
126-
compilation,
127-
files
128-
});
129-
} else if (stats) {
130-
expect(typeof stats).toBe("object");
131-
const compilation = stats.compilation;
132-
const statsJson = stats.toJson({
133-
modules: true,
134-
reasons: true
135-
});
136-
expect(typeof statsJson).toBe("object");
137-
expect(statsJson).toHaveProperty("errors");
138-
expect(Array.isArray(statsJson.errors)).toBe(true);
139-
if (statsJson.errors!.length > 0) {
140-
expect(statsJson.errors![0]).toBeInstanceOf(Object);
141-
throw statsJson.errors![0];
142-
}
143-
statsJson.logs = logs;
144-
await caseConfig.check?.({
145-
context,
146-
stats: statsJson,
147-
files,
148-
compiler: c,
149-
compilation
150-
});
151-
} else {
152-
await caseConfig.check?.({
153-
context,
154-
files,
155-
compiler: c
156-
});
157-
}
158-
}
159-
});
173+
for (let i = 0; i < caseConfigList.length; i++) {
174+
const caseConfig = caseConfigList[i];
175+
if (caseConfig.skip) {
176+
it.skip(`${name}[${i}]`, () => {});
177+
continue;
178+
}
179+
creator.create(`${name}[${i}]`, src, dist, undefined, {
180+
caseConfig,
181+
description: () => caseConfig.description
160182
});
161183
}
162184
}

0 commit comments

Comments
 (0)