|
| 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 | +}; |
0 commit comments