1- use crate :: util:: files_with_extension;
2- use anyhow:: { ensure, Result } ;
1+ use crate :: util:: { files_with_extension, patched_agave_tools } ;
2+ use anyhow:: { anyhow , ensure, Result } ;
33use assert_cmd:: cargo:: CommandCargoExt ;
44use std:: {
55 collections:: HashSet ,
66 env:: current_dir,
7- fs:: read_to_string,
7+ fs:: { read_to_string, remove_file } ,
88 path:: { Path , PathBuf } ,
99 process:: Command ,
10- sync:: Mutex ,
10+ sync:: { LazyLock , Mutex , MutexGuard } ,
1111} ;
1212
13+ const SBPF_COVERAGE_DOWNLOAD_URL : & str =
14+ "https://github.com/trail-of-forks/sbpf-coverage/releases/download" ;
15+
16+ #[ cfg( target_os = "linux" ) ]
17+ const OS : & str = "Linux" ;
18+
19+ #[ cfg( target_os = "macos" ) ]
20+ const OS : & str = "macOS" ;
21+
22+ #[ cfg( not( any( target_os = "linux" , target_os = "macos" ) ) ) ]
23+ compile_error ! ( "Only Linux and macOS are supported." ) ;
24+
1325const BASIC_DIR : & str = concat ! ( env!( "CARGO_MANIFEST_DIR" ) , "/fixtures/basic" ) ;
1426const MULTIPLE_TEST_CONFIGS_DIR : & str = concat ! (
1527 env!( "CARGO_MANIFEST_DIR" ) ,
@@ -19,14 +31,18 @@ const EXTERNAL_CALL_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/fixtures/e
1931const MULTIPLE_PROGRAMS_DIR : & str =
2032 concat ! ( env!( "CARGO_MANIFEST_DIR" ) , "/fixtures/multiple_programs" ) ;
2133
34+ static AGAVE_TAG : LazyLock < String > = LazyLock :: new ( || {
35+ read_to_string ( "agave_tag.txt" )
36+ . map ( |s| s. trim_end ( ) . to_owned ( ) )
37+ . unwrap ( )
38+ } ) ;
39+
2240// smoelius: Only one Anchor test can be run at a time.
2341static MUTEX : Mutex < ( ) > = Mutex :: new ( ( ) ) ;
2442
2543#[ test]
2644fn basic ( ) {
27- let _lock = MUTEX . lock ( ) . unwrap ( ) ;
28-
29- yarn ( BASIC_DIR ) . unwrap ( ) ;
45+ let _lock = prepare_for_testing ( BASIC_DIR ) . unwrap ( ) ;
3046
3147 let mut command = anchor_coverage_command ( BASIC_DIR ) ;
3248 let status = command. status ( ) . unwrap ( ) ;
@@ -53,9 +69,7 @@ fn basic() {
5369
5470#[ test]
5571fn multiple_test_configs ( ) {
56- let _lock = MUTEX . lock ( ) . unwrap ( ) ;
57-
58- yarn ( MULTIPLE_TEST_CONFIGS_DIR ) . unwrap ( ) ;
72+ let _lock = prepare_for_testing ( MULTIPLE_TEST_CONFIGS_DIR ) . unwrap ( ) ;
5973
6074 for test_config in [ "full" , "just_increment_x" , "just_increment_y" ] {
6175 let mut command = anchor_coverage_command ( MULTIPLE_TEST_CONFIGS_DIR ) ;
@@ -133,9 +147,7 @@ fn multiple_test_configs() {
133147
134148#[ test]
135149fn include_cargo_does_not_change_line_hits ( ) {
136- let _lock = MUTEX . lock ( ) . unwrap ( ) ;
137-
138- yarn ( EXTERNAL_CALL_DIR ) . unwrap ( ) ;
150+ let _lock = prepare_for_testing ( EXTERNAL_CALL_DIR ) . unwrap ( ) ;
139151
140152 let report_without_cargo = run_anchor_coverage_and_read_lcov ( EXTERNAL_CALL_DIR , false ) . unwrap ( ) ;
141153
@@ -159,9 +171,7 @@ fn include_cargo_does_not_change_line_hits() {
159171
160172#[ test]
161173fn multiple_programs ( ) {
162- let _lock = MUTEX . lock ( ) . unwrap ( ) ;
163-
164- yarn ( MULTIPLE_PROGRAMS_DIR ) . unwrap ( ) ;
174+ let _lock = prepare_for_testing ( MULTIPLE_PROGRAMS_DIR ) . unwrap ( ) ;
165175
166176 let mut command = anchor_coverage_command ( MULTIPLE_PROGRAMS_DIR ) ;
167177 let status = command. status ( ) . unwrap ( ) ;
@@ -201,6 +211,42 @@ fn multiple_programs() {
201211 }
202212}
203213
214+ fn prepare_for_testing ( dir : & str ) -> Result < MutexGuard < ' _ , ( ) > > {
215+ download_patched_agave_tools ( dir) ?;
216+
217+ yarn ( dir) ?;
218+
219+ MUTEX . lock ( ) . map_err ( |error| anyhow ! ( "{error}" ) )
220+ }
221+
222+ fn download_patched_agave_tools ( dir : impl AsRef < Path > ) -> Result < ( ) > {
223+ #[ allow( clippy:: redundant_pattern_matching) ]
224+ if let Some ( _) = patched_agave_tools ( & dir) ? {
225+ return Ok ( ( ) ) ;
226+ }
227+
228+ let filename = format ! ( "patched-agave-tools-{}-{}.tar.gz" , * AGAVE_TAG , OS ) ;
229+
230+ let mut command = Command :: new ( "wget" ) ;
231+ command. arg ( format ! (
232+ "{SBPF_COVERAGE_DOWNLOAD_URL}/{}/{}" ,
233+ * AGAVE_TAG , filename
234+ ) ) ;
235+ command. current_dir ( & dir) ;
236+ let status = command. status ( ) ?;
237+ ensure ! ( status. success( ) , "command failed: {command:?}" ) ;
238+
239+ let mut command = Command :: new ( "tar" ) ;
240+ command. args ( [ "xzf" , & filename] ) ;
241+ command. current_dir ( & dir) ;
242+ let status = command. status ( ) ?;
243+ ensure ! ( status. success( ) , "command failed: {command:?}" ) ;
244+
245+ remove_file ( dir. as_ref ( ) . join ( filename) ) ?;
246+
247+ Ok ( ( ) )
248+ }
249+
204250fn yarn ( dir : & str ) -> Result < ( ) > {
205251 let mut command = Command :: new ( "yarn" ) ;
206252 command. current_dir ( dir) ;
0 commit comments