@@ -92,76 +92,88 @@ mod tests {
9292 go_bin
9393 }
9494
95- struct App < ' a > {
95+ struct App {
9696 /// The path to the example application
9797 path : String ,
9898 /// The WIT world to target
99- world : String ,
99+ world : Option < String > ,
100100 /// The output path of the wasm file
101101 wasm_path : String ,
102102 /// The path to the directory containing the WIT files
103- wit_path : String ,
103+ wit_path : Option < String > ,
104104 /// The child process ID of a running wasm app
105105 process : Option < Child > ,
106106 /// Any tests that need to be compiled and run as such
107- tests : Option < & ' a [ Test < ' a > ] > ,
107+ tests : Option < Vec < Test > > ,
108108 }
109109
110- struct Test < ' a > {
110+ #[ derive( Clone ) ]
111+ struct Test {
111112 should_fail : bool ,
112- pkg_path : & ' a str ,
113+ pkg_path : String ,
113114 }
114115
115- impl < ' a > App < ' a > {
116+ impl App {
116117 /// Create a new app runner.
117- fn new ( path : & ' a str , world : & ' a str , tests : Option < & ' a [ Test < ' a > ] > ) -> Self {
118+ fn new (
119+ path : & str ,
120+ world : Option < & str > ,
121+ tests : Option < Vec < Test > > ,
122+ is_component : bool ,
123+ ) -> Self {
118124 let path = componentize_go:: utils:: make_path_absolute ( & PathBuf :: from ( path) )
119125 . expect ( "failed to make app path absolute" ) ;
120126
127+ let world = if is_component {
128+ Some ( world. expect ( "WIT world must be specified" ) . to_string ( ) )
129+ } else {
130+ None
131+ } ;
132+
133+ let wit_path = if is_component {
134+ Some (
135+ path. join ( "wit" )
136+ . to_str ( )
137+ . expect ( "wit_path is not valid unicode" )
138+ . to_string ( ) ,
139+ )
140+ } else {
141+ None
142+ } ;
143+
121144 App {
122145 path : path
123146 . clone ( )
124147 . to_str ( )
125148 . expect ( "app path is not valid unicode" )
126149 . to_string ( ) ,
127- world : world . to_string ( ) ,
150+ world,
128151 wasm_path : path
129152 . join ( "main.wasm" )
130153 . to_str ( )
131154 . expect ( "wasm_path is not valid unicode" )
132155 . to_string ( ) ,
133- wit_path : path
134- . join ( "wit" )
135- . to_str ( )
136- . expect ( "wit_path is not valid unicode" )
137- . to_string ( ) ,
156+ wit_path,
138157 process : None ,
139158 tests,
140159 }
141160 }
142161
143- // Build unit tests with componentize-go
144- fn build_tests ( & self , go : Option < & PathBuf > ) -> Result < ( ) > {
145- let test_pkgs = if let Some ( pkgs) = self . tests {
146- pkgs
147- } else {
148- return Err ( anyhow ! (
149- "Please include the test_pkg_paths when creating App::new()"
150- ) ) ;
151- } ;
162+ fn build_test_modules ( & self , go : Option < & PathBuf > ) -> Result < ( ) > {
163+ let test_pkgs = self . tests . as_ref ( ) . expect ( "missing test_pkg_paths" ) ;
152164
153165 self . generate_bindings ( go) ?;
154166
155167 let mut test_cmd = Command :: new ( COMPONENTIZE_GO_PATH . as_path ( ) ) ;
156168 test_cmd
157- . args ( [ "-w" , & self . world ] )
158- . args ( [ "-d" , & self . wit_path ] )
169+ . args ( [ "-w" , self . world . as_ref ( ) . expect ( "missing WIT world" ) ] )
170+ . args ( [ "-d" , self . wit_path . as_ref ( ) . expect ( "missing WIT path" ) ] )
159171 . arg ( "test" )
160172 . arg ( "--wasip1" ) ;
161173
162174 // Add all the paths to the packages that have unit tests to compile
163175 for test in test_pkgs. iter ( ) {
164- test_cmd. args ( [ "--pkg" , test. pkg_path ] ) ;
176+ test_cmd. args ( [ "--pkg" , & test. pkg_path ] ) ;
165177 }
166178
167179 // `go test -c` needs to be in the same path as the go.mod file.
@@ -183,13 +195,30 @@ mod tests {
183195 Ok ( ( ) )
184196 }
185197
186- fn run_tests ( & self ) -> Result < ( ) > {
198+ fn run_module ( & self ) -> Result < ( ) > {
199+ let output = Command :: new ( "wasmtime" )
200+ . arg ( "run" )
201+ . arg ( & self . wasm_path )
202+ . output ( ) ?;
203+
204+ if !output. status . success ( ) {
205+ return Err ( anyhow ! (
206+ "Failed to run wasm module for application at '{}':\n {} " ,
207+ & self . wasm_path,
208+ String :: from_utf8_lossy( & output. stdout)
209+ ) ) ;
210+ }
211+
212+ Ok ( ( ) )
213+ }
214+
215+ fn run_test_modules ( & self ) -> Result < ( ) > {
187216 let example_dir = PathBuf :: from ( & self . path ) ;
188- if let Some ( tests) = self . tests {
217+ if let Some ( tests) = & self . tests {
189218 let mut test_errors: Vec < String > = vec ! [ ] ;
190219 for test in tests. iter ( ) {
191220 let wasm_file = example_dir. join ( componentize_go:: cmd_test:: get_test_filename (
192- & PathBuf :: from ( test. pkg_path ) ,
221+ & PathBuf :: from ( & test. pkg_path ) ,
193222 ) ) ;
194223 match Command :: new ( "wasmtime" )
195224 . args ( [ "run" , wasm_file. to_str ( ) . unwrap ( ) ] )
@@ -233,15 +262,45 @@ mod tests {
233262 Ok ( ( ) )
234263 }
235264
236- /// Build the app with componentize-go.
237- fn build ( & self , go : Option < & PathBuf > ) -> Result < ( ) > {
265+ fn build_module ( & self , go : Option < & PathBuf > ) -> Result < ( ) > {
266+ // Build component
267+ let mut build_cmd = Command :: new ( COMPONENTIZE_GO_PATH . as_path ( ) ) ;
268+ build_cmd
269+ . arg ( "build" )
270+ . arg ( "--wasip1" )
271+ . args ( [ "-o" , & self . wasm_path ] ) ;
272+
273+ if let Some ( go_path) = go. as_ref ( ) {
274+ build_cmd. args ( [ "--go" , go_path. to_str ( ) . unwrap ( ) ] ) ;
275+ }
276+
277+ // Run `go build` in the same directory as the go.mod file.
278+ build_cmd. current_dir ( & self . path ) ;
279+
280+ let build_output = build_cmd. output ( ) . expect ( & format ! (
281+ "failed to execute componentize-go for \" {}\" " ,
282+ self . path
283+ ) ) ;
284+
285+ if !build_output. status . success ( ) {
286+ return Err ( anyhow ! (
287+ "failed to build application \" {}\" : {}" ,
288+ self . path,
289+ String :: from_utf8_lossy( & build_output. stderr)
290+ ) ) ;
291+ }
292+
293+ Ok ( ( ) )
294+ }
295+
296+ fn build_component ( & self , go : Option < & PathBuf > ) -> Result < ( ) > {
238297 self . generate_bindings ( go) ?;
239298
240299 // Build component
241300 let mut build_cmd = Command :: new ( COMPONENTIZE_GO_PATH . as_path ( ) ) ;
242301 build_cmd
243- . args ( [ "-w" , & self . world ] )
244- . args ( [ "-d" , & self . wit_path ] )
302+ . args ( [ "-w" , self . world . as_ref ( ) . expect ( "missing WIT world" ) ] )
303+ . args ( [ "-d" , self . wit_path . as_ref ( ) . expect ( "missing WIT path" ) ] )
245304 . arg ( "build" )
246305 . args ( [ "-o" , & self . wasm_path ] ) ;
247306
@@ -270,8 +329,8 @@ mod tests {
270329
271330 fn generate_bindings ( & self , go : Option < & PathBuf > ) -> Result < ( ) > {
272331 let bindings_output = Command :: new ( COMPONENTIZE_GO_PATH . as_path ( ) )
273- . args ( [ "-w" , & self . world ] )
274- . args ( [ "-d" , & self . wit_path ] )
332+ . args ( [ "-w" , self . world . as_ref ( ) . expect ( "missing WIT world" ) ] )
333+ . args ( [ "-d" , self . wit_path . as_ref ( ) . expect ( "missing WIT path" ) ] )
275334 . arg ( "bindings" )
276335 . args ( [ "-o" , & self . path ] )
277336 . current_dir ( & self . path )
@@ -307,7 +366,7 @@ mod tests {
307366 }
308367
309368 /// Run the app and check the output.
310- async fn run ( & mut self , route : & str , expected_response : & str ) -> Result < ( ) > {
369+ async fn run_component ( & mut self , route : & str , expected_response : & str ) -> Result < ( ) > {
311370 let listener = TcpListener :: bind ( "127.0.0.1:0" ) . expect ( "Failed to bind to a free port" ) ;
312371 let addr = listener. local_addr ( ) . expect ( "Failed to get local address" ) ;
313372 let port = addr. port ( ) ;
@@ -345,48 +404,60 @@ mod tests {
345404 }
346405 }
347406
348- impl < ' a > Drop for App < ' a > {
407+ impl Drop for App {
349408 fn drop ( & mut self ) {
350409 if let Some ( child) = & mut self . process {
351410 _ = child. kill ( )
352411 }
353412 }
354413 }
355414
415+ #[ test]
416+ fn example_wasip1 ( ) {
417+ let app = App :: new ( "../examples/wasip1" , None , None , false ) ;
418+ app. build_module ( None ) . expect ( "failed to build app module" ) ;
419+ app. run_module ( ) . expect ( "failed to run app module" ) ;
420+ }
421+
356422 #[ tokio:: test]
357423 async fn example_wasip2 ( ) {
358- let unit_tests = [
424+ let unit_tests = vec ! [
359425 Test {
360426 should_fail: false ,
361- pkg_path : "./unit_tests_should_pass" ,
427+ pkg_path: String :: from ( "./unit_tests_should_pass" ) ,
362428 } ,
363429 Test {
364430 should_fail: true ,
365- pkg_path : "./unit_tests_should_fail" ,
431+ pkg_path: String :: from ( "./unit_tests_should_fail" ) ,
366432 } ,
367433 ] ;
368434
369- let mut app = App :: new ( "../examples/wasip2" , "wasip2-example" , Some ( & unit_tests) ) ;
435+ let mut app = App :: new (
436+ "../examples/wasip2" ,
437+ Some ( "wasip2-example" ) ,
438+ Some ( unit_tests) ,
439+ true ,
440+ ) ;
370441
371- app. build ( None ) . expect ( "failed to build app" ) ;
442+ app. build_component ( None ) . expect ( "failed to build app" ) ;
372443
373- app. run ( "/" , "Hello, world!" )
444+ app. run_component ( "/" , "Hello, world!" )
374445 . await
375446 . expect ( "app failed to run" ) ;
376447
377- app. build_tests ( None )
448+ app. build_test_modules ( None )
378449 . expect ( "failed to build app unit tests" ) ;
379450
380- app. run_tests ( )
451+ app. run_test_modules ( )
381452 . expect ( "tests succeeded/failed when they should not have" ) ;
382453 }
383454
384455 #[ tokio:: test]
385456 async fn example_wasip3 ( ) {
386- let mut app = App :: new ( "../examples/wasip3" , "wasip3-example" , None ) ;
387- app. build ( Some ( & patched_go_path ( ) . await ) )
457+ let mut app = App :: new ( "../examples/wasip3" , Some ( "wasip3-example" ) , None , true ) ;
458+ app. build_component ( Some ( & patched_go_path ( ) . await ) )
388459 . expect ( "failed to build app" ) ;
389- app. run ( "/hello" , "Hello, world!" )
460+ app. run_component ( "/hello" , "Hello, world!" )
390461 . await
391462 . expect ( "app failed to run" ) ;
392463 }
0 commit comments