@@ -5,7 +5,7 @@ use std::env;
5
5
use std:: ffi:: OsStr ;
6
6
use std:: fs;
7
7
use std:: lazy:: SyncLazy ;
8
- use std:: path:: PathBuf ;
8
+ use std:: path:: { Path , PathBuf } ;
9
9
use walkdir:: WalkDir ;
10
10
11
11
use crate :: clippy_project_root;
@@ -16,7 +16,15 @@ pub static CARGO_TARGET_DIR: SyncLazy<PathBuf> = SyncLazy::new(|| match env::var
16
16
None => env:: current_dir ( ) . unwrap ( ) . join ( "target" ) ,
17
17
} ) ;
18
18
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 ) {
20
28
let test_suite_dirs = [
21
29
clippy_project_root ( ) . join ( "tests" ) . join ( "ui" ) ,
22
30
clippy_project_root ( ) . join ( "tests" ) . join ( "ui-internal" ) ,
@@ -30,15 +38,18 @@ pub fn bless() {
30
38
. filter ( |f| f. path ( ) . extension ( ) == Some ( OsStr :: new ( "rs" ) ) )
31
39
. for_each ( |f| {
32
40
let test_name = f. path ( ) . strip_prefix ( test_suite_dir) . unwrap ( ) ;
33
-
34
- update_reference_file ( f. path ( ) . with_extension ( "stdout" ) , test_name. with_extension ( "stdout" ) ) ;
35
- update_reference_file ( f. path ( ) . with_extension ( "stderr" ) , test_name. with_extension ( "stderr" ) ) ;
36
- update_reference_file ( f. path ( ) . with_extension ( "fixed" ) , test_name. with_extension ( "fixed" ) ) ;
41
+ for & ext in & [ "stdout" , "stderr" , "fixed" ] {
42
+ update_reference_file (
43
+ f. path ( ) . with_extension ( ext) ,
44
+ test_name. with_extension ( ext) ,
45
+ ignore_timestamp,
46
+ ) ;
47
+ }
37
48
} ) ;
38
49
}
39
50
}
40
51
41
- fn update_reference_file ( reference_file_path : PathBuf , test_name : PathBuf ) {
52
+ fn update_reference_file ( reference_file_path : PathBuf , test_name : PathBuf , ignore_timestamp : bool ) {
42
53
let test_output_path = build_dir ( ) . join ( test_name) ;
43
54
let relative_reference_file_path = reference_file_path. strip_prefix ( clippy_project_root ( ) ) . unwrap ( ) ;
44
55
@@ -48,6 +59,11 @@ fn update_reference_file(reference_file_path: PathBuf, test_name: PathBuf) {
48
59
return ;
49
60
}
50
61
62
+ // If the test output was not updated since the last clippy build, it may be outdated
63
+ if !ignore_timestamp && !updated_since_clippy_build ( & test_output_path) . unwrap_or ( true ) {
64
+ return ;
65
+ }
66
+
51
67
let test_output_file = fs:: read ( & test_output_path) . expect ( "Unable to read test output file" ) ;
52
68
let reference_file = fs:: read ( & reference_file_path) . unwrap_or_default ( ) ;
53
69
@@ -67,6 +83,12 @@ fn update_reference_file(reference_file_path: PathBuf, test_name: PathBuf) {
67
83
}
68
84
}
69
85
86
+ fn updated_since_clippy_build ( path : & Path ) -> Option < bool > {
87
+ let clippy_build_time = ( * CLIPPY_BUILD_TIME ) ?;
88
+ let modified = fs:: metadata ( path) . ok ( ) ?. modified ( ) . ok ( ) ?;
89
+ Some ( modified >= clippy_build_time)
90
+ }
91
+
70
92
fn build_dir ( ) -> PathBuf {
71
93
let profile = env:: var ( "PROFILE" ) . unwrap_or_else ( |_| "debug" . to_string ( ) ) ;
72
94
let mut path = PathBuf :: new ( ) ;
0 commit comments