1- #!/usr/bin/env node
2-
31import cp from 'node:child_process' ;
42import { randomUUID } from 'node:crypto' ;
53import fs from 'node:fs' ;
64import fsPromises from 'node:fs/promises' ;
75import { tmpdir } from 'node:os' ;
86import path from 'node:path' ;
9- import { debuglog , promisify } from 'node:util' ;
7+ import { debuglog , parseArgs , promisify } from 'node:util' ;
8+
9+ import { isPassThroughEnv , replaceUnstableOutput } from './utils' ;
1010
1111const debug = debuglog ( 'vite-plus/snap-test' ) ;
1212const cpExec = promisify ( cp . exec ) ;
@@ -16,37 +16,42 @@ const exec = async (command: string, options: cp.ExecOptionsWithStringEncoding)
1616 process . platform === 'win32' ? { ...options , shell : 'pwsh.exe' } : options ,
1717 ) ;
1818
19- import { isPassThroughEnv , replaceUnstableOutput } from './utils.ts' ;
19+ export async function snapTest ( ) {
20+ const { positionals } = parseArgs ( {
21+ allowPositionals : true ,
22+ args : process . argv . slice ( 3 ) ,
23+ } ) ;
2024
21- // Create a unique temporary directory for testing
22- // On macOS, `tmpdir()` is a symlink. Resolve it so that we can replace the resolved cwd in outputs.
23- const tempTmpDir = `${ fs . realpathSync ( tmpdir ( ) ) } /vite-plus-test-${ randomUUID ( ) } ` ;
24- fs . mkdirSync ( tempTmpDir , { recursive : true } ) ;
25+ const filter = positionals [ 0 ] ?? '' ; // Optional filter to run specific test cases
2526
26- // Make dependencies available in the test cases
27- fs . symlinkSync (
28- path . resolve ( 'node_modules' ) ,
29- path . join ( tempTmpDir , 'node_modules' ) ,
30- process . platform === 'win32' ? 'junction' : 'dir' ,
31- ) ;
27+ // Create a unique temporary directory for testing
28+ // On macOS, `tmpdir()` is a symlink. Resolve it so that we can replace the resolved cwd in outputs.
29+ const tempTmpDir = `${ fs . realpathSync ( tmpdir ( ) ) } /vite-plus-test-${ randomUUID ( ) } ` ;
30+ fs . mkdirSync ( tempTmpDir , { recursive : true } ) ;
3231
33- // Clean up the temporary directory on exit
34- process . on ( 'exit' , ( ) => fs . rmSync ( tempTmpDir , { recursive : true , force : true } ) ) ;
32+ // Make dependencies available in the test cases
33+ fs . symlinkSync (
34+ path . resolve ( 'node_modules' ) ,
35+ path . join ( tempTmpDir , 'node_modules' ) ,
36+ process . platform === 'win32' ? 'junction' : 'dir' ,
37+ ) ;
3538
36- const casesDir = path . resolve ( 'snap-tests' ) ;
39+ // Clean up the temporary directory on exit
40+ process . on ( 'exit' , ( ) => fs . rmSync ( tempTmpDir , { recursive : true , force : true } ) ) ;
3741
38- const filter = process . argv [ 2 ] ?? '' ; // Optional filter to run specific test cases
42+ const casesDir = path . resolve ( 'snap-tests' ) ;
3943
40- const tasks : Promise < void > [] = [];
41- for (const caseName of fs.readdirSync(casesDir)) {
42- if ( caseName . startsWith ( '.' ) ) continue ; // Skip hidden files like .DS_Store
43- if ( caseName . includes ( filter ) ) {
44- tasks . push ( runTestCase ( caseName ) ) ;
44+ const tasks : Promise < void > [ ] = [ ] ;
45+ for ( const caseName of fs . readdirSync ( casesDir ) ) {
46+ if ( caseName . startsWith ( '.' ) ) continue ; // Skip hidden files like .DS_Store
47+ if ( caseName . includes ( filter ) ) {
48+ tasks . push ( runTestCase ( caseName , tempTmpDir , casesDir ) ) ;
49+ }
4550 }
46- }
4751
48- if ( tasks . length > 0 ) {
49- await Promise . all ( tasks ) ;
52+ if ( tasks . length > 0 ) {
53+ await Promise . all ( tasks ) ;
54+ }
5055}
5156
5257interface Steps {
@@ -55,7 +60,7 @@ interface Steps {
5560 commands : string [ ] ;
5661}
5762
58- async function runTestCase ( name : string ) {
63+ async function runTestCase ( name : string , tempTmpDir : string , casesDir : string ) {
5964 const steps : Steps = JSON . parse ( await fsPromises . readFile ( `${ casesDir } /${ name } /steps.json` , 'utf-8' ) ) ;
6065 if ( steps . ignoredPlatforms !== undefined && steps . ignoredPlatforms . includes ( process . platform ) ) {
6166 console . log ( '%s skipped on platform %s' , name , process . platform ) ;
@@ -67,7 +72,7 @@ async function runTestCase(name: string) {
6772 await fsPromises . cp ( `${ casesDir } /${ name } ` , caseTmpDir , { recursive : true , errorOnExist : true } ) ;
6873
6974 const passThroughEnvs = Object . fromEntries ( Object . entries ( process . env ) . filter ( ( [ key ] ) => isPassThroughEnv ( key ) ) ) ;
70- const env = {
75+ const env : Record < string , string > = {
7176 ...passThroughEnvs ,
7277 // Indicate CLI is running in test mode, so that it prints more detailed outputs.
7378 VITE_PLUS_CLI_TEST : '1' ,
@@ -104,7 +109,7 @@ async function runTestCase(name: string) {
104109 if ( stderr ) {
105110 newSnap . push ( replaceUnstableOutput ( stderr , caseTmpDir ) ) ;
106111 }
107- } catch ( error ) {
112+ } catch ( error : any ) {
108113 // add error exit code to the command
109114 newSnap . push ( `[${ error . code } ]> ${ command } ` ) ;
110115 if ( error . stdout ) {
0 commit comments