44use std:: str:: FromStr ;
55use std:: sync:: Arc ;
66
7- use anyhow:: Result ;
7+ use anyhow:: { Context , Result } ;
88use clap:: Args ;
99use spk_build:: BuildSource ;
1010use spk_cli_common:: { flags, CommandArgs , Run } ;
1111use spk_schema:: foundation:: format:: { FormatIdent , FormatOptionMap } ;
1212use spk_schema:: foundation:: ident_build:: Build ;
1313use spk_schema:: foundation:: option_map:: { host_options, OptionMap } ;
14+ use spk_schema:: prelude:: * ;
1415use spk_schema:: { Recipe , TestStage , Variant } ;
1516
1617use crate :: test:: { PackageBuildTester , PackageInstallTester , PackageSourceTester , Tester } ;
@@ -23,7 +24,7 @@ mod cmd_test_test;
2324///
2425/// In order to run install tests the package must have been built already
2526#[ derive( Args ) ]
26- pub struct Test {
27+ pub struct CmdTest {
2728 #[ clap( flatten) ]
2829 pub options : flags:: Options ,
2930 #[ clap( flatten) ]
@@ -57,7 +58,7 @@ pub struct Test {
5758}
5859
5960#[ async_trait:: async_trait]
60- impl Run for Test {
61+ impl Run for CmdTest {
6162 async fn run ( & mut self ) -> Result < i32 > {
6263 let options = self . options . get_options ( ) ?;
6364 let ( _runtime, repos) = tokio:: try_join!(
@@ -123,11 +124,15 @@ impl Run for Test {
123124 continue ;
124125 }
125126
126- for ( index, test) in recipe. get_tests ( & opts) ?. into_iter ( ) . enumerate ( ) {
127- if test. stage != stage {
128- continue ;
129- }
130-
127+ let selected = recipe
128+ . get_tests ( stage, & opts)
129+ . context ( "Failed to select tests for this variant" ) ?;
130+ tracing:: info!(
131+ variant=%opts. format_option_map( ) ,
132+ "Running {} relevant tests for this variant" ,
133+ selected. len( )
134+ ) ;
135+ for ( index, test) in selected. into_iter ( ) . enumerate ( ) {
131136 let mut builder = self
132137 . formatter_settings
133138 . get_formatter_builder ( self . verbose ) ?;
@@ -140,31 +145,27 @@ impl Run for Test {
140145
141146 let mut tester: Box < dyn Tester > = match stage {
142147 TestStage :: Sources => {
143- let mut tester = PackageSourceTester :: new (
144- ( * recipe) . clone ( ) ,
145- test. script . join ( "\n " ) ,
146- ) ;
148+ let mut tester =
149+ PackageSourceTester :: new ( ( * recipe) . clone ( ) , test. script ( ) ) ;
147150
148151 tester
149152 . with_options ( opts. clone ( ) )
150153 . with_repositories ( repos. iter ( ) . cloned ( ) )
151- . with_requirements ( test. requirements . clone ( ) )
154+ . with_requirements ( test. additional_requirements ( ) )
152155 . with_source ( source. clone ( ) )
153156 . watch_environment_resolve ( & src_formatter) ;
154157
155158 Box :: new ( tester)
156159 }
157160
158161 TestStage :: Build => {
159- let mut tester = PackageBuildTester :: new (
160- ( * recipe) . clone ( ) ,
161- test. script . join ( "\n " ) ,
162- ) ;
162+ let mut tester =
163+ PackageBuildTester :: new ( ( * recipe) . clone ( ) , test. script ( ) ) ;
163164
164165 tester
165166 . with_options ( opts. clone ( ) )
166167 . with_repositories ( repos. iter ( ) . cloned ( ) )
167- . with_requirements ( test. requirements . clone ( ) )
168+ . with_requirements ( test. additional_requirements ( ) )
168169 . with_source (
169170 source. clone ( ) . map ( BuildSource :: LocalPath ) . unwrap_or_else (
170171 || {
@@ -186,40 +187,24 @@ impl Run for Test {
186187 TestStage :: Install => {
187188 let mut tester = PackageInstallTester :: new (
188189 ( * recipe) . clone ( ) ,
189- test. script . join ( " \n " ) ,
190+ test. script ( ) ,
190191 variant,
191192 ) ;
192193
193194 tester
194195 . with_options ( opts. clone ( ) )
195196 . with_repositories ( repos. iter ( ) . cloned ( ) )
196- . with_requirements ( test. requirements . clone ( ) )
197+ . with_requirements ( test. additional_requirements ( ) )
197198 . with_source ( source. clone ( ) )
198199 . watch_environment_resolve ( & install_formatter) ;
199200
200201 Box :: new ( tester)
201202 }
202203 } ;
203204
204- let mut selected = false ;
205- for selector in test. selectors . iter ( ) {
206- let mut selected_opts = opts. clone ( ) ;
207- selected_opts. extend ( selector. clone ( ) ) ;
208- if selected_opts. digest ( ) == digest {
209- selected = true ;
210- }
211- }
212- if !selected && !test. selectors . is_empty ( ) {
213- tracing:: info!(
214- "SKIP #{index}: variant not selected: {}" ,
215- opts. format_option_map( )
216- ) ;
217- continue ;
218- }
219-
220205 tracing:: info!(
221- "Running test #{index} variant={}" ,
222- opts . format_option_map ( )
206+ variant=%opts . format_option_map ( ) ,
207+ "Running selected test #{index}" ,
223208 ) ;
224209
225210 tester. test ( ) . await ?
@@ -231,7 +216,7 @@ impl Run for Test {
231216 }
232217}
233218
234- impl CommandArgs for Test {
219+ impl CommandArgs for CmdTest {
235220 fn get_positional_args ( & self ) -> Vec < String > {
236221 // The important positional args for a test are the packages
237222 self . packages . clone ( )
0 commit comments