@@ -5,7 +5,7 @@ use std::env;
55use std:: ffi:: OsStr ;
66use std:: fs;
77use std:: lazy:: SyncLazy ;
8- use std:: path:: PathBuf ;
8+ use std:: path:: { Path , PathBuf } ;
99use walkdir:: WalkDir ;
1010
1111use crate :: clippy_project_root;
@@ -16,7 +16,15 @@ pub static CARGO_TARGET_DIR: SyncLazy<PathBuf> = SyncLazy::new(|| match env::var
1616 None => env:: current_dir ( ) . unwrap ( ) . join ( "target" ) ,
1717} ) ;
1818
19- pub fn bless ( ) {
19+ static CLIPPY_BUILD_TIME : SyncLazy < Option < std:: time:: SystemTime > > = SyncLazy :: new ( || {
20+ let profile = env:: var ( "PROFILE" ) . unwrap_or_else ( |_| "debug" . to_string ( ) ) ;
21+ let mut path = PathBuf :: from ( & * * CARGO_TARGET_DIR ) ;
22+ path. push ( profile) ;
23+ path. push ( "cargo-clippy" ) ;
24+ fs:: metadata ( path) . ok ( ) ?. modified ( ) . ok ( )
25+ } ) ;
26+
27+ pub fn bless ( ignore_timestamp : bool ) {
2028 let test_suite_dirs = [
2129 clippy_project_root ( ) . join ( "tests" ) . join ( "ui" ) ,
2230 clippy_project_root ( ) . join ( "tests" ) . join ( "ui-toml" ) ,
@@ -29,15 +37,18 @@ pub fn bless() {
2937 . filter ( |f| f. path ( ) . extension ( ) == Some ( OsStr :: new ( "rs" ) ) )
3038 . for_each ( |f| {
3139 let test_name = f. path ( ) . strip_prefix ( test_suite_dir) . unwrap ( ) ;
32-
33- update_reference_file ( f. path ( ) . with_extension ( "stdout" ) , test_name. with_extension ( "stdout" ) ) ;
34- update_reference_file ( f. path ( ) . with_extension ( "stderr" ) , test_name. with_extension ( "stderr" ) ) ;
35- update_reference_file ( f. path ( ) . with_extension ( "fixed" ) , test_name. with_extension ( "fixed" ) ) ;
40+ for & ext in & [ "stdout" , "stderr" , "fixed" ] {
41+ update_reference_file (
42+ f. path ( ) . with_extension ( ext) ,
43+ test_name. with_extension ( ext) ,
44+ ignore_timestamp,
45+ ) ;
46+ }
3647 } ) ;
3748 }
3849}
3950
40- fn update_reference_file ( reference_file_path : PathBuf , test_name : PathBuf ) {
51+ fn update_reference_file ( reference_file_path : PathBuf , test_name : PathBuf , ignore_timestamp : bool ) {
4152 let test_output_path = build_dir ( ) . join ( test_name) ;
4253 let relative_reference_file_path = reference_file_path. strip_prefix ( clippy_project_root ( ) ) . unwrap ( ) ;
4354
@@ -47,6 +58,11 @@ fn update_reference_file(reference_file_path: PathBuf, test_name: PathBuf) {
4758 return ;
4859 }
4960
61+ // If the test output was not updated since the last clippy build, it may be outdated
62+ if !ignore_timestamp && !updated_since_clippy_build ( & test_output_path) . unwrap_or ( true ) {
63+ return ;
64+ }
65+
5066 let test_output_file = fs:: read ( & test_output_path) . expect ( "Unable to read test output file" ) ;
5167 let reference_file = fs:: read ( & reference_file_path) . unwrap_or_default ( ) ;
5268
@@ -66,6 +82,12 @@ fn update_reference_file(reference_file_path: PathBuf, test_name: PathBuf) {
6682 }
6783}
6884
85+ fn updated_since_clippy_build ( path : & Path ) -> Option < bool > {
86+ let clippy_build_time = ( * CLIPPY_BUILD_TIME ) ?;
87+ let modified = fs:: metadata ( path) . ok ( ) ?. modified ( ) . ok ( ) ?;
88+ Some ( modified >= clippy_build_time)
89+ }
90+
6991fn build_dir ( ) -> PathBuf {
7092 let profile = env:: var ( "PROFILE" ) . unwrap_or_else ( |_| "debug" . to_string ( ) ) ;
7193 let mut path = PathBuf :: new ( ) ;
0 commit comments