@@ -172,7 +172,7 @@ impl Command {
172
172
Command :: Install { flags } => Self :: install ( flags) ,
173
173
Command :: Build { flags } => Self :: build ( flags) ,
174
174
Command :: Check { flags } => Self :: check ( flags) ,
175
- Command :: Test { bless, flags, target } => Self :: test ( bless, flags, target) ,
175
+ Command :: Test { bless, flags, target, coverage } => Self :: test ( bless, flags, target, coverage ) ,
176
176
Command :: Run { dep, verbose, many_seeds, target, edition, flags } =>
177
177
Self :: run ( dep, verbose, many_seeds, target, edition, flags) ,
178
178
Command :: Doc { flags } => Self :: doc ( flags) ,
@@ -458,7 +458,7 @@ impl Command {
458
458
Ok ( ( ) )
459
459
}
460
460
461
- fn test ( bless : bool , mut flags : Vec < String > , target : Option < String > ) -> Result < ( ) > {
461
+ fn test ( bless : bool , mut flags : Vec < String > , target : Option < String > , coverage : bool ) -> Result < ( ) > {
462
462
let mut e = MiriEnv :: new ( ) ?;
463
463
464
464
// Prepare a sysroot. (Also builds cargo-miri, which we need.)
@@ -468,6 +468,9 @@ impl Command {
468
468
if bless {
469
469
e. sh . set_var ( "RUSTC_BLESS" , "Gesundheit" ) ;
470
470
}
471
+ if coverage {
472
+ e. sh . set_var ( "RUSTFLAGS" , "-C instrument-coverage" ) ;
473
+ }
471
474
if let Some ( target) = target {
472
475
// Tell the harness which target to test.
473
476
e. sh . set_var ( "MIRI_TEST_TARGET" , target) ;
@@ -479,6 +482,41 @@ impl Command {
479
482
// Then test, and let caller control flags.
480
483
// Only in root project as `cargo-miri` has no tests.
481
484
e. test ( "." , & flags) ?;
485
+
486
+ if coverage {
487
+ Self :: show_coverage_report ( & e) ?;
488
+ }
489
+ Ok ( ( ) )
490
+ }
491
+
492
+ fn show_coverage_report ( e : & MiriEnv ) -> Result < ( ) > {
493
+ let profraw_files: Vec < _ > = std:: fs:: read_dir ( "." ) ?
494
+ . filter_map ( |r| r. ok ( ) )
495
+ . map ( |e| e. path ( ) )
496
+ . filter ( |p| p. extension ( ) . map ( |e| e == "profraw" ) . unwrap_or ( false ) )
497
+ . map ( |p| p. as_os_str ( ) . to_os_string ( ) )
498
+ . collect ( ) ;
499
+
500
+ // Merge the profraw files
501
+ let profraw_files_cloned= profraw_files. iter ( ) ;
502
+ cmd ! (
503
+ e. sh,
504
+ "cargo-profdata -- merge -sparse {profraw_files_cloned...} -o merged.profdata"
505
+ ) . quiet ( ) . run ( ) ?;
506
+
507
+ // Create the coverage report.
508
+ let home = std:: env:: var ( "HOME" ) ?;
509
+ let ignored = format ! ( "{home}/*|miri/target/*|target/debug/*|rust/deps/*" ) ;
510
+ cmd ! (
511
+ e. sh,
512
+ "cargo-cov -- report --instr-profile=merged.profdata --object target/debug/miri -ignore-filename-regex={ignored}"
513
+ ) . run ( ) ?;
514
+
515
+ // Delete artifacts.
516
+ cmd ! (
517
+ e. sh,
518
+ "rm {profraw_files...} merged.profdata"
519
+ ) . quiet ( ) . run ( ) ?;
482
520
Ok ( ( ) )
483
521
}
484
522
0 commit comments