@@ -6,48 +6,48 @@ import { fileURLToPath, pathToFileURL } from "node:url";
66import { expect , onTestFinished } from "vitest" ;
77
88export interface Fixture {
9- root : URL ;
10- resolve : ( ...segments : string [ ] ) => URL ;
11- readFile : ( file : PathLike ) => Promise < string > ;
12- cleanup : ( ) => Promise < void > ;
9+ root : URL ;
10+ resolve : ( ...segments : string [ ] ) => URL ;
11+ readFile : ( file : PathLike ) => Promise < string > ;
12+ cleanup : ( ) => Promise < void > ;
1313}
1414
1515type FileContent = string | Record < string , unknown > | unknown [ ] ;
1616
1717export async function createFixture < const T extends Record < string , FileContent > > ( files : {
18- [ K in keyof T ] : K extends `${string } .json` ? FileContent : string ;
18+ [ K in keyof T ] : K extends `${string } .json` ? FileContent : string ;
1919} ) : Promise < Fixture > {
20- const raw = expect . getState ( ) . currentTestName ?? "bsh" ;
21- const prefix = raw
22- . toLowerCase ( )
23- . replace ( / [ ^ a - z 0 - 9 ] + / g, "-" )
24- . replace ( / ^ - | - $ / g, "" ) ;
25- const root = new URL ( `${ prefix } -` , `file://${ tmpdir ( ) } /` ) ;
26- const path = await mkdtemp ( fileURLToPath ( root ) ) ;
27- const base = pathToFileURL ( path + sep ) ;
20+ const raw = expect . getState ( ) . currentTestName ?? "bsh" ;
21+ const prefix = raw
22+ . toLowerCase ( )
23+ . replace ( / [ ^ a - z 0 - 9 ] + / g, "-" )
24+ . replace ( / ^ - | - $ / g, "" ) ;
25+ const root = new URL ( `${ prefix } -` , `file://${ tmpdir ( ) } /` ) ;
26+ const path = await mkdtemp ( fileURLToPath ( root ) ) ;
27+ const base = pathToFileURL ( path + sep ) ;
2828
29- for ( const [ name , content ] of Object . entries ( files as Record < string , FileContent > ) ) {
30- const url = new URL ( name , base ) ;
31- const dir = new URL ( "./" , url ) ;
32- await mkdir ( dir , { recursive : true } ) ;
33- if ( name . endsWith ( ".json" ) && typeof content !== "string" ) {
34- await writeFile ( url , JSON . stringify ( content , null , 2 ) , "utf8" ) ;
35- } else {
36- await writeFile ( url , content as string , "utf8" ) ;
37- }
38- }
29+ for ( const [ name , content ] of Object . entries ( files as Record < string , FileContent > ) ) {
30+ const url = new URL ( name , base ) ;
31+ const dir = new URL ( "./" , url ) ;
32+ await mkdir ( dir , { recursive : true } ) ;
33+ if ( name . endsWith ( ".json" ) && typeof content !== "string" ) {
34+ await writeFile ( url , JSON . stringify ( content , null , 2 ) , "utf8" ) ;
35+ } else {
36+ await writeFile ( url , content as string , "utf8" ) ;
37+ }
38+ }
3939
40- const cleanup = ( ) => rm ( path , { recursive : true , force : true } ) ;
41- onTestFinished ( cleanup ) ;
40+ const cleanup = ( ) => rm ( path , { recursive : true , force : true } ) ;
41+ onTestFinished ( cleanup ) ;
4242
43- const resolve = ( ...segments : string [ ] ) => new URL ( `./${ segments . join ( "/" ) } ` , base ) ;
44- const readFile = ( file : PathLike ) =>
45- fsReadFile ( new URL ( `./${ file } ` , base ) , { encoding : "utf-8" } ) ;
43+ const resolve = ( ...segments : string [ ] ) => new URL ( `./${ segments . join ( "/" ) } ` , base ) ;
44+ const readFile = ( file : PathLike ) =>
45+ fsReadFile ( new URL ( `./${ file } ` , base ) , { encoding : "utf-8" } ) ;
4646
47- return {
48- root : pathToFileURL ( path ) ,
49- resolve,
50- readFile,
51- cleanup,
52- } ;
47+ return {
48+ root : pathToFileURL ( path ) ,
49+ resolve,
50+ readFile,
51+ cleanup,
52+ } ;
5353}
0 commit comments