1
1
import type { OutputFileSystem } from "@rspack/core" ;
2
- import { getSimpleProcessorRunner } from "../test/simple " ;
2
+ import { BasicCaseCreator } from "../test/creator " ;
3
3
import type {
4
4
ECompilerType ,
5
5
ITestContext ,
6
6
ITestEnv ,
7
+ ITestProcessor ,
7
8
TCompilation ,
8
9
TCompiler ,
9
10
TCompilerOptions ,
@@ -12,6 +13,151 @@ import type {
12
13
} from "../type" ;
13
14
import { getCompiler } from "./common" ;
14
15
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
+
15
161
export function createCompilerCase (
16
162
name : string ,
17
163
src : string ,
@@ -24,139 +170,15 @@ export function createCompilerCase(
24
170
if ( ! Array . isArray ( caseConfigList ) ) {
25
171
caseConfigList = [ caseConfigList ] ;
26
172
}
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
160
182
} ) ;
161
183
}
162
184
}
0 commit comments