1- use crate :: { Cover , find_target} ;
2- use anyhow:: { Context , Result , bail} ;
3- use cargo_metadata:: camino:: Utf8PathBuf ;
1+ use crate :: { Common , Cover , util:: Context , util:: Utf8PathBuf } ;
2+ use anyhow:: { Context as _, Result , bail} ;
43use glob:: glob;
54use indicatif:: { ProgressBar , ProgressStyle } ;
65use rayon:: iter:: { IntoParallelIterator , ParallelIterator } ;
@@ -16,37 +15,35 @@ thread_local! {
1615}
1716
1817impl Cover {
19- pub fn generate_coverage ( & mut self ) -> Result < ( ) , anyhow:: Error > {
18+ pub fn generate_coverage ( & self , common : & Common ) -> Result < ( ) , anyhow:: Error > {
19+ let cx = Context :: new ( common, self . target . clone ( ) ) ?;
2020 process:: Command :: new ( "grcov" )
2121 . arg ( "--version" )
2222 . output ( )
2323 . context ( "grcov not found - please install by running `cargo install grcov`" ) ?;
2424
2525 eprintln ! ( "Generating coverage" ) ;
2626
27- self . target =
28- find_target ( & self . target ) . context ( "⚠️ couldn't find the target to start coverage" ) ?;
29-
3027 if let Some ( path) = & self . source
3128 && !path. try_exists ( ) ?
3229 {
3330 bail ! ( "Source directory specified, but path does not exist!" ) ;
3431 }
3532
3633 // build the runner
37- Self :: build_runner ( ) ?;
34+ Self :: build_runner ( common ) ?;
3835
3936 if !self . keep {
4037 // We remove the previous coverage files
41- Self :: clean_old_cov ( ) ?;
38+ Self :: clean_old_cov ( & cx ) ?;
4239 }
4340
4441 let input_path = PathBuf :: from (
4542 self . input
4643 . display ( )
4744 . to_string ( )
4845 . replace ( "{ziggy_output}" , & self . ziggy_output . display ( ) . to_string ( ) )
49- . replace ( "{target_name}" , & self . target ) ,
46+ . replace ( "{target_name}" , & cx . bin_target ) ,
5047 ) ;
5148
5249 let coverage_corpus = if input_path. is_dir ( ) {
@@ -71,16 +68,16 @@ impl Cover {
7168 . display ( )
7269 . to_string ( )
7370 . replace ( "{ziggy_output}" , & self . ziggy_output . display ( ) . to_string ( ) )
74- . replace ( "{target_name}" , & self . target ) ;
71+ . replace ( "{target_name}" , & cx . bin_target ) ;
7572
7673 delete_dir_or_file ( & coverage_dir) ?;
7774
7875 // Get the absolute path for the coverage directory to ensure .profraw files
7976 // are created in the correct location, even in workspace scenarios
80- let base_dir = super :: target_dir ( ) . join ( "coverage/debug" ) ;
77+ let base_dir = cx . target_dir . join ( "coverage/debug" ) ;
8178 let coverage_target_dir = base_dir. join ( "deps" ) ;
8279 let cfg = Cfg :: new (
83- base_dir. join ( & self . target ) ,
80+ base_dir. join ( & cx . bin_target ) ,
8481 coverage_target_dir. join ( "coverage-%p-%m.profraw" ) ,
8582 ) ;
8683
@@ -93,7 +90,7 @@ impl Cover {
9390 . unwrap ( )
9491 . progress_chars ( "#>-" ) ,
9592 ) ;
96- let log_dir = self . ziggy_output . join ( format ! ( "{}/logs" , & self . target ) ) ;
93+ let log_dir = self . ziggy_output . join ( format ! ( "{}/logs" , & cx . bin_target ) ) ;
9794 fs:: create_dir_all ( & log_dir) ?;
9895 let log_file = std:: sync:: Mutex :: new ( std:: fs:: File :: create ( log_dir. join ( "coverage.log" ) ) ?) ;
9996 coverage_corpus. into_par_iter ( ) . for_each ( |file| {
@@ -121,7 +118,7 @@ impl Cover {
121118 // We generate the code coverage report
122119 eprintln ! ( "\n Generating coverage report" ) ;
123120 Self :: run_grcov (
124- & self . target ,
121+ & cx ,
125122 output_types,
126123 & coverage_dir,
127124 & source_or_workspace_root,
@@ -130,22 +127,25 @@ impl Cover {
130127 }
131128
132129 /// Build the runner with the appropriate flags for coverage
133- pub fn build_runner ( ) -> Result < ( ) , anyhow:: Error > {
134- // The cargo executable
135- let cargo = env:: var ( "CARGO" ) . unwrap_or_else ( |_| String :: from ( "cargo" ) ) ;
130+ pub fn build_runner ( common : & Common ) -> Result < ( ) , anyhow:: Error > {
131+ let target_dir = common. target_dir ( ) ?;
136132
137133 let mut coverage_rustflags =
138134 env:: var ( "COVERAGE_RUSTFLAGS" ) . unwrap_or_else ( |_| "-Cinstrument-coverage" . to_string ( ) ) ;
139135 coverage_rustflags. push ( ' ' ) ;
140136 coverage_rustflags. push_str ( & env:: var ( "RUSTFLAGS" ) . unwrap_or_default ( ) ) ;
141- let target_dir = format ! ( "--target-dir={}" , super :: target_dir( ) . join( "coverage" ) ) ;
142137
143- let build = process:: Command :: new ( & cargo)
144- . args ( [ "rustc" , "--features=ziggy/coverage" , & target_dir] )
138+ let build = common
139+ . cargo ( )
140+ . args ( [
141+ "rustc" ,
142+ "--features=ziggy/coverage" ,
143+ & format ! ( "--target-dir={}" , target_dir. join( "coverage" ) ) ,
144+ ] )
145145 . env ( "RUSTFLAGS" , coverage_rustflags)
146146 . env (
147147 "LLVM_PROFILE_FILE" ,
148- super :: target_dir ( ) . join ( "coverage/debug/deps/build-%p-%m.profraw" ) ,
148+ target_dir. join ( "coverage/debug/deps/build-%p-%m.profraw" ) ,
149149 )
150150 . spawn ( )
151151 . context ( "⚠️ couldn't spawn rustc for coverage" ) ?
@@ -158,16 +158,16 @@ impl Cover {
158158 }
159159
160160 pub fn run_grcov (
161- target : & str ,
161+ cx : & Context ,
162162 output_types : & str ,
163163 coverage_dir : & str ,
164164 source_or_workspace_root : & str ,
165165 threads : Option < usize > ,
166166 ) -> Result < ( ) , anyhow:: Error > {
167167 let coverage = process:: Command :: new ( "grcov" )
168168 . args ( [
169- crate :: target_dir ( ) . join ( "coverage/debug/deps" ) . as_str ( ) ,
170- & format ! ( "-b={}/coverage/debug/{target }" , super :: target_dir( ) ) ,
169+ cx . target_dir . join ( "coverage/debug/deps" ) . as_str ( ) ,
170+ & format ! ( "-b={}/coverage/debug/{}" , cx . target_dir, cx . bin_target ) ,
171171 & format ! ( "-s={source_or_workspace_root}" ) ,
172172 & format ! ( "-t={output_types}" ) ,
173173 "--llvm" ,
@@ -186,10 +186,9 @@ impl Cover {
186186 Ok ( ( ) )
187187 }
188188
189- pub fn clean_old_cov ( ) -> Result < ( ) , anyhow:: Error > {
189+ pub fn clean_old_cov ( cx : & Context ) -> Result < ( ) , anyhow:: Error > {
190190 // Use absolute path to ensure we clean the correct location in workspaces
191- let coverage_deps_dir = super :: target_dir ( ) . join ( "coverage/debug/deps" ) ;
192- let pattern = coverage_deps_dir. join ( "*.profraw" ) ;
191+ let pattern = cx. target_dir . join ( "*.profraw" ) ;
193192
194193 if let Ok ( profile_files) = glob ( pattern. as_str ( ) ) {
195194 for file in profile_files. flatten ( ) {
0 commit comments