11mod redact;
22
3- use std:: { collections:: HashMap , convert:: Infallible , ffi:: OsStr , path:: Path , sync:: Arc } ;
3+ use std:: {
4+ collections:: HashMap ,
5+ convert:: Infallible ,
6+ env:: { self , join_paths, split_paths} ,
7+ ffi:: OsStr ,
8+ fmt:: format,
9+ path:: Path ,
10+ process:: Command ,
11+ sync:: Arc ,
12+ } ;
413
514use copy_dir:: copy_dir;
6- use redact:: redact_snapshot;
15+ use redact:: { redact_e2e_output , redact_snapshot} ;
716use tokio:: runtime:: Runtime ;
817use vite_path:: { AbsolutePath , RelativePathBuf } ;
918use vite_str:: Str ;
@@ -21,6 +30,9 @@ struct Plan {
2130
2231#[ derive( serde:: Deserialize , Debug ) ]
2332struct E2e {
33+ pub name : Str ,
34+ #[ serde( default ) ]
35+ pub cwd : RelativePathBuf ,
2436 pub steps : Vec < Str > ,
2537}
2638
@@ -62,26 +74,47 @@ fn run_case(runtime: &Runtime, tmpdir: &AbsolutePath, fixture_path: &Path) {
6274 Err ( err) => panic ! ( "Failed to read cases.toml for fixture {}: {}" , fixture_name, err) ,
6375 } ;
6476
65- // Add bins to PATH so test programs (such as readfile) in fixtures can be found.
66- let envs: HashMap < Arc < OsStr > , Arc < OsStr > > = [ (
67- Arc :: < OsStr > :: from ( OsStr :: new ( "PATH" ) ) ,
68- Arc :: < OsStr > :: from (
69- std:: env:: current_dir ( )
70- . unwrap ( )
71- . join ( "test_bins" )
72- . join ( "node_modules" )
73- . join ( ".bin" )
74- . into_os_string ( ) ,
77+ let test_bin_path = Arc :: < OsStr > :: from (
78+ std:: env:: current_dir ( )
79+ . unwrap ( )
80+ . join ( "test_bins" )
81+ . join ( "node_modules" )
82+ . join ( ".bin" )
83+ . into_os_string ( ) ,
84+ ) ;
85+
86+ // Add test_bins to PATH so test programs (such as print-file) in fixtures can be found.
87+ let plan_envs: HashMap < Arc < OsStr > , Arc < OsStr > > =
88+ [ ( Arc :: < OsStr > :: from ( OsStr :: new ( "PATH" ) ) , Arc :: clone ( & test_bin_path) ) ]
89+ . into_iter ( )
90+ . collect ( ) ;
91+
92+ // Prepare PATH for e2e tests
93+ let e2e_env_path = join_paths (
94+ [
95+ // Include vite binary path to PATH so that e2e tests can run "vite ..." commands.
96+ {
97+ let vite_path = AbsolutePath :: new ( env ! ( "CARGO_BIN_EXE_vite" ) ) . unwrap ( ) ;
98+ let vite_dir = vite_path. parent ( ) . unwrap ( ) ;
99+ vite_dir. as_path ( ) . as_os_str ( ) . into ( )
100+ } ,
101+ // Include test_bins to PATH so that e2e tests can run utilities such as json-edit.
102+ test_bin_path,
103+ ]
104+ . into_iter ( )
105+ . chain (
106+ // the existing PATH
107+ split_paths ( & env:: var_os ( "PATH" ) . unwrap ( ) )
108+ . map ( |path| Arc :: < OsStr > :: from ( path. into_os_string ( ) ) ) ,
75109 ) ,
76- ) ]
77- . into_iter ( )
78- . collect ( ) ;
110+ )
111+ . unwrap ( ) ;
79112
80113 runtime. block_on ( async {
81114 let workspace_root_str = workspace_root. path . as_path ( ) . to_str ( ) . unwrap ( ) ;
82115 let mut owned_callbacks = vite_task_bin:: OwnedSessionCallbacks :: default ( ) ;
83116 let mut session = Session :: init_with (
84- envs ,
117+ plan_envs ,
85118 Arc :: clone ( & workspace_root. path ) ,
86119 owned_callbacks. as_callbacks ( ) ,
87120 )
@@ -95,6 +128,7 @@ fn run_case(runtime: &Runtime, tmpdir: &AbsolutePath, fixture_path: &Path) {
95128 ) ;
96129 insta:: assert_json_snapshot!( "task graph" , task_graph_json) ;
97130
131+ let mut e2e_count = 0u32 ;
98132 for plan in cases_file. plan_cases {
99133 let snapshot_name = format ! ( "query - {}" , plan. name) ;
100134
@@ -126,29 +160,48 @@ fn run_case(runtime: &Runtime, tmpdir: &AbsolutePath, fixture_path: &Path) {
126160
127161 let plan_json = redact_snapshot ( & plan, workspace_root_str) ;
128162 insta:: assert_json_snapshot!( snapshot_name, & plan_json) ;
129-
130- // let cwd: Arc<AbsolutePath> = case_stage_path.join(&cli_query.cwd).into();
131- // let task_query = match cli_task_query.into_task_query(&cwd) {
132- // Ok(ok) => ok,
133- // Err(err) => {
134- // insta::assert_json_snapshot!(snapshot_name, err);
135- // continue;
136- // }
137- // };
138-
139- // let execution_graph = match indexed_task_graph.query_tasks(task_query) {
140- // Ok(ok) => ok,
141- // Err(err) => {
142- // insta::assert_json_snapshot!(snapshot_name, err);
143- // continue;
144- // }
145- // };
146-
147- // let execution_graph_snapshot =
148- // snapshot_execution_graph(&execution_graph, &indexed_task_graph);
149- // insta::assert_json_snapshot!(snapshot_name, execution_graph_snapshot);
150163 }
151- for e2e in cases_file. e2e_cases { }
164+ for e2e in cases_file. e2e_cases {
165+ let e2e_stage_path = tmpdir. join ( format ! ( "e2e_stage_{}" , e2e_count) ) ;
166+ e2e_count += 1 ;
167+ assert ! ( copy_dir( fixture_path, & e2e_stage_path) . unwrap( ) . is_empty( ) ) ;
168+
169+ let e2e_stage_path_str = e2e_stage_path. as_path ( ) . to_str ( ) . unwrap ( ) ;
170+
171+ let mut e2e_outputs = String :: new ( ) ;
172+ for step in e2e. steps {
173+ let mut cmd = if cfg ! ( windows) {
174+ let mut cmd = Command :: new ( "cmd.exe" ) ;
175+ // https://github.com/nodejs/node/blob/dbd24b165128affb7468ca42f69edaf7e0d85a9a/lib/child_process.js#L633
176+ cmd. args ( [ "/d" , "/s" , "/c" ] ) ;
177+ cmd
178+ } else {
179+ let mut cmd = Command :: new ( "sh" ) ;
180+ cmd. args ( [ "-c" ] ) ;
181+ cmd
182+ } ;
183+ cmd. arg ( step. as_str ( ) )
184+ . env_clear ( )
185+ . env ( "PATH" , & e2e_env_path)
186+ . current_dir ( e2e_stage_path. join ( & e2e. cwd ) ) ;
187+ let output = cmd. output ( ) . unwrap ( ) ;
188+
189+ let exit_code = output. status . code ( ) . unwrap_or ( -1 ) ;
190+ if exit_code != 0 {
191+ e2e_outputs. push_str ( format ! ( "[{}]" , exit_code) . as_str ( ) ) ;
192+ }
193+ e2e_outputs. push_str ( "> " ) ;
194+ e2e_outputs. push_str ( step. as_str ( ) ) ;
195+ e2e_outputs. push ( '\n' ) ;
196+
197+ let stdout = String :: from_utf8 ( output. stdout ) . unwrap ( ) ;
198+ let stderr = String :: from_utf8 ( output. stderr ) . unwrap ( ) ;
199+ e2e_outputs. push_str ( & redact_e2e_output ( stdout, e2e_stage_path_str) ) ;
200+ e2e_outputs. push_str ( & redact_e2e_output ( stderr, e2e_stage_path_str) ) ;
201+ e2e_outputs. push ( '\n' ) ;
202+ }
203+ insta:: assert_snapshot!( e2e. name. as_str( ) , e2e_outputs) ;
204+ }
152205 } ) ;
153206}
154207
0 commit comments