@@ -41,6 +41,10 @@ interface TestContext {
41
41
}
42
42
}
43
43
type TestCallback = ( context : TestContext ) => Promise < void > | void
44
+ interface TestFlags {
45
+ only ?: boolean
46
+ debug ?: boolean
47
+ }
44
48
45
49
type SpawnActor = { predicate : ( message : string ) => boolean ; resolve : ( ) => void }
46
50
@@ -51,70 +55,41 @@ export function test(
51
55
name : string ,
52
56
config : TestConfig ,
53
57
testCallback : TestCallback ,
54
- { only = false } = { } ,
58
+ { only = false , debug = false } : TestFlags = { } ,
55
59
) {
56
60
return ( only ? defaultTest . only : defaultTest ) (
57
61
name ,
58
62
{ timeout : TEST_TIMEOUT } ,
59
63
async ( options ) => {
60
- let root = await fs . mkdtemp (
61
- // On Windows CI, tmpdir returns a path containing a weird RUNNER~1 folder
62
- // that apparently causes the vite builds to not work.
63
- path . join (
64
- process . env . CI && platform ( ) === 'win32' ? homedir ( ) : tmpdir ( ) ,
65
- 'tailwind-integrations' ,
66
- ) ,
67
- )
68
-
69
- async function write ( filename : string , content : string ) : Promise < void > {
70
- let full = path . join ( root , filename )
71
-
72
- if ( filename . endsWith ( 'package.json' ) ) {
73
- content = await overwriteVersionsInPackageJson ( content )
74
- }
75
-
76
- // Ensure that files written on Windows use \r\n line ending
77
- if ( platform ( ) === 'win32' ) {
78
- content = content . replace ( / \n / g, '\r\n' )
79
- }
80
-
81
- let dir = path . dirname ( full )
82
- await fs . mkdir ( dir , { recursive : true } )
83
- await fs . writeFile ( full , content )
84
- }
85
-
86
- for ( let [ filename , content ] of Object . entries ( config . fs ) ) {
87
- await write ( filename , content )
64
+ let rootDir = debug
65
+ ? path . join ( REPO_ROOT , '.debug' )
66
+ : // On Windows CI, tmpdir returns a path containing a weird RUNNER~1
67
+ // folder that apparently causes the vite builds to not work.
68
+ process . env . CI && platform ( ) === 'win32'
69
+ ? homedir ( )
70
+ : tmpdir ( )
71
+ await fs . mkdir ( rootDir , { recursive : true } )
72
+
73
+ let root = await fs . mkdtemp ( path . join ( rootDir , 'tailwind-integrations' ) )
74
+
75
+ if ( debug ) {
76
+ console . log ( 'Running test in debug mode. File system will be written to:' )
77
+ console . log ( root )
78
+ console . log ( )
88
79
}
89
80
90
- try {
91
- execSync ( 'pnpm install' , { cwd : root } )
92
- } catch ( error : any ) {
93
- console . error ( error . stdout . toString ( ) )
94
- console . error ( error . stderr . toString ( ) )
95
- throw error
96
- }
97
-
98
- let disposables : ( ( ) => Promise < void > ) [ ] = [ ]
99
-
100
- async function dispose ( ) {
101
- await Promise . all ( disposables . map ( ( dispose ) => dispose ( ) ) )
102
- try {
103
- await fs . rm ( root , { recursive : true , maxRetries : 5 , force : true } )
104
- } catch ( err ) {
105
- if ( ! process . env . CI ) {
106
- throw err
107
- }
108
- }
109
- }
110
-
111
- options . onTestFinished ( dispose )
112
-
113
81
let context = {
114
82
root,
115
83
async exec ( command : string , childProcessOptions : ChildProcessOptions = { } ) {
84
+ let cwd = childProcessOptions . cwd ?? root
85
+ if ( debug && cwd !== root ) {
86
+ let relative = path . relative ( root , cwd )
87
+ if ( relative [ 0 ] !== '.' ) relative = `./${ relative } `
88
+ console . log ( `> cd ${ relative } ` )
89
+ }
90
+ if ( debug ) console . log ( `> ${ command } ` )
116
91
return execSync ( command , {
117
- cwd : root ,
92
+ cwd,
118
93
stdio : 'pipe' ,
119
94
...childProcessOptions ,
120
95
} ) . toString ( )
@@ -127,8 +102,15 @@ export function test(
127
102
rejectDisposal = reject
128
103
} )
129
104
105
+ let cwd = childProcessOptions . cwd ?? root
106
+ if ( debug && cwd !== root ) {
107
+ let relative = path . relative ( root , cwd )
108
+ if ( relative [ 0 ] !== '.' ) relative = `./${ relative } `
109
+ console . log ( `> cd ${ relative } ` )
110
+ }
111
+ if ( debug ) console . log ( `>& ${ command } ` )
130
112
let child = spawn ( command , {
131
- cwd : root ,
113
+ cwd,
132
114
shell : true ,
133
115
env : {
134
116
...process . env ,
@@ -177,12 +159,14 @@ export function test(
177
159
178
160
child . stdout . on ( 'data' , ( result ) => {
179
161
let content = result . toString ( )
162
+ if ( debug ) console . log ( content )
180
163
combined . push ( [ 'stdout' , content ] )
181
164
stdoutMessages . push ( content )
182
165
notifyNext ( stdoutActors , stdoutMessages )
183
166
} )
184
167
child . stderr . on ( 'data' , ( result ) => {
185
168
let content = result . toString ( )
169
+ if ( debug ) console . error ( content )
186
170
combined . push ( [ 'stderr' , content ] )
187
171
stderrMessages . push ( content )
188
172
notifyNext ( stderrActors , stderrMessages )
@@ -195,6 +179,9 @@ export function test(
195
179
} )
196
180
197
181
options . onTestFailed ( ( ) => {
182
+ // In debug mode, messages are logged to the console immediatly
183
+ if ( debug ) return
184
+
198
185
for ( let [ type , message ] of combined ) {
199
186
if ( type === 'stdout' ) {
200
187
console . log ( message )
@@ -253,7 +240,22 @@ export function test(
253
240
} )
254
241
} ,
255
242
fs : {
256
- write,
243
+ async write ( filename : string , content : string ) : Promise < void > {
244
+ let full = path . join ( root , filename )
245
+
246
+ if ( filename . endsWith ( 'package.json' ) ) {
247
+ content = await overwriteVersionsInPackageJson ( content )
248
+ }
249
+
250
+ // Ensure that files written on Windows use \r\n line ending
251
+ if ( platform ( ) === 'win32' ) {
252
+ content = content . replace ( / \n / g, '\r\n' )
253
+ }
254
+
255
+ let dir = path . dirname ( full )
256
+ await fs . mkdir ( dir , { recursive : true } )
257
+ await fs . writeFile ( full , content )
258
+ } ,
257
259
read ( filePath : string ) {
258
260
return fs . readFile ( path . resolve ( root , filePath ) , 'utf8' )
259
261
} ,
@@ -277,13 +279,43 @@ export function test(
277
279
} ,
278
280
} satisfies TestContext
279
281
282
+ for ( let [ filename , content ] of Object . entries ( config . fs ) ) {
283
+ await context . fs . write ( filename , content )
284
+ }
285
+
286
+ try {
287
+ context . exec ( 'pnpm install' )
288
+ } catch ( error : any ) {
289
+ console . error ( error )
290
+ throw error
291
+ }
292
+
293
+ let disposables : ( ( ) => Promise < void > ) [ ] = [ ]
294
+
295
+ async function dispose ( ) {
296
+ await Promise . all ( disposables . map ( ( dispose ) => dispose ( ) ) )
297
+ try {
298
+ if ( debug ) return
299
+ await fs . rm ( root , { recursive : true , maxRetries : 5 , force : true } )
300
+ } catch ( err ) {
301
+ if ( ! process . env . CI ) {
302
+ throw err
303
+ }
304
+ }
305
+ }
306
+
307
+ options . onTestFinished ( dispose )
308
+
280
309
await testCallback ( context )
281
310
} ,
282
311
)
283
312
}
284
313
test . only = ( name : string , config : TestConfig , testCallback : TestCallback ) => {
285
314
return test ( name , config , testCallback , { only : true } )
286
315
}
316
+ test . debug = ( name : string , config : TestConfig , testCallback : TestCallback ) => {
317
+ return test ( name , config , testCallback , { only : true , debug : true } )
318
+ }
287
319
288
320
// Maps package names to their tarball filenames. See scripts/pack-packages.ts
289
321
// for more details.
0 commit comments