1
1
use assert_fs:: { prelude:: * , TempDir } ;
2
2
use predicates:: prelude:: * ;
3
3
use std:: path:: Path ;
4
- use std:: { path:: PathBuf , time :: SystemTime } ;
4
+ use std:: path:: PathBuf ;
5
5
6
6
use crate :: utils:: * ;
7
7
8
- fn file_timestamps ( glob : & str ) -> DynResult < Vec < ( PathBuf , SystemTime ) > > {
8
+ fn file_contents ( glob : & str ) -> DynResult < Vec < ( PathBuf , String ) > > {
9
9
let mut out = vec ! [ ] ;
10
10
for f in glob:: glob ( glob) ? {
11
11
let f = f?;
12
12
let metadata = f. metadata ( ) ?;
13
13
if metadata. is_file ( ) {
14
- out. push ( ( f, metadata. modified ( ) ?) )
14
+ let contents = std:: fs:: read_to_string ( & f) ?;
15
+ out. push ( ( f, contents) )
15
16
}
16
17
}
17
18
@@ -20,34 +21,28 @@ fn file_timestamps(glob: &str) -> DynResult<Vec<(PathBuf, SystemTime)>> {
20
21
Ok ( out)
21
22
}
22
23
23
- fn file_timestamps_in_dir ( dir : & Path ) -> DynResult < Vec < ( PathBuf , SystemTime ) > > {
24
- file_timestamps ( & ( dir. to_string_lossy ( ) + "/**/*" ) )
24
+ fn file_contents_in_dir ( dir : & Path ) -> DynResult < Vec < ( PathBuf , String ) > > {
25
+ file_contents ( & ( dir. to_string_lossy ( ) + "/**/*" ) )
25
26
}
26
27
27
- fn assert_all_timestamps_changed (
28
- glob : & str ,
29
- orig_timestamps : & [ ( PathBuf , SystemTime ) ] ,
30
- ) -> TestResult {
31
- for ( before, after) in orig_timestamps. iter ( ) . zip ( file_timestamps ( glob) ?) {
28
+ fn assert_all_contents_changed ( glob : & str , orig_contents : & [ ( PathBuf , String ) ] ) -> TestResult {
29
+ for ( before, after) in orig_contents. iter ( ) . zip ( file_contents ( glob) ?) {
32
30
assert_eq ! (
33
31
before. 0 , after. 0 ,
34
32
"File paths should be unchanged after formatting"
35
33
) ;
36
34
assert_ne ! (
37
35
before. 1 , after. 1 ,
38
- "File {:?} is expected to be included in formatting, but the last modified time didn't change" ,
36
+ "File {:?} is expected to be included in formatting, but its contents didn't change" ,
39
37
before. 0
40
38
) ;
41
39
}
42
40
43
41
Ok ( ( ) )
44
42
}
45
43
46
- fn assert_no_timestamps_changed (
47
- glob : & str ,
48
- orig_timestamps : & [ ( PathBuf , SystemTime ) ] ,
49
- ) -> TestResult {
50
- assert_eq ! ( orig_timestamps, file_timestamps( glob) ?) ;
44
+ fn assert_no_contents_changed ( glob : & str , orig_contents : & [ ( PathBuf , String ) ] ) -> TestResult {
45
+ assert_eq ! ( orig_contents, file_contents( glob) ?) ;
51
46
52
47
Ok ( ( ) )
53
48
}
@@ -58,30 +53,30 @@ fn recursive_directory_search() -> TestResult {
58
53
59
54
tmp. copy_from ( TESTS_DIR . to_string ( ) + "/data/ext" , & [ "**/*" ] ) ?;
60
55
61
- let included_files_timestamps = file_timestamps_in_dir ( & tmp. child ( "included" ) ) ?;
62
- let excluded_files_timestamps = file_timestamps_in_dir ( & tmp. child ( "excluded" ) ) ?;
56
+ let included_files_contents = file_contents_in_dir ( & tmp. child ( "included" ) ) ?;
57
+ let excluded_files_contents = file_contents_in_dir ( & tmp. child ( "excluded" ) ) ?;
63
58
64
59
assert_eq ! (
65
- included_files_timestamps . len( ) ,
60
+ included_files_contents . len( ) ,
66
61
6 ,
67
62
"Unexpected number of files copied from tests/data/ext/included"
68
63
) ;
69
64
assert_eq ! (
70
- excluded_files_timestamps . len( ) ,
65
+ excluded_files_contents . len( ) ,
71
66
6 ,
72
67
"Unexpected number of files copied from tests/data/ext/excluded"
73
68
) ;
74
69
75
70
fmt ( & * tmp) ?;
76
71
77
- assert_no_timestamps_changed (
72
+ assert_no_contents_changed (
78
73
& ( tmp. to_string_lossy ( ) + "/excluded/**/*" ) ,
79
- & excluded_files_timestamps ,
74
+ & excluded_files_contents ,
80
75
) ?;
81
76
82
- assert_all_timestamps_changed (
77
+ assert_all_contents_changed (
83
78
& ( tmp. to_string_lossy ( ) + "/included/**/*" ) ,
84
- & included_files_timestamps ,
79
+ & included_files_contents ,
85
80
) ?;
86
81
87
82
Ok ( ( ) )
@@ -94,29 +89,29 @@ fn glob() -> TestResult {
94
89
tmp. copy_from ( TESTS_DIR . to_string ( ) + "/data/ext/included" , & [ "**/*" ] ) ?;
95
90
96
91
let pas_glob = tmp. to_string_lossy ( ) + "/**/*.pas" ;
97
- let pas_timestamps = file_timestamps ( & pas_glob) ?;
98
- assert_eq ! ( pas_timestamps . len( ) , 1 ) ;
92
+ let pas_contents = file_contents ( & pas_glob) ?;
93
+ assert_eq ! ( pas_contents . len( ) , 1 ) ;
99
94
100
95
let dpr_glob = tmp. to_string_lossy ( ) + "/**/*.dpr" ;
101
- let dpr_timestamps = file_timestamps ( & dpr_glob) ?;
102
- assert_eq ! ( dpr_timestamps . len( ) , 1 ) ;
96
+ let dpr_contents = file_contents ( & dpr_glob) ?;
97
+ assert_eq ! ( dpr_contents . len( ) , 1 ) ;
103
98
104
99
pasfmt ( ) ?
105
100
. current_dir ( & tmp)
106
101
. arg ( "**/*.pas" )
107
102
. assert ( )
108
103
. success ( ) ;
109
- assert_all_timestamps_changed ( & pas_glob, & pas_timestamps ) ?;
110
- assert_no_timestamps_changed ( & dpr_glob, & dpr_timestamps ) ?;
104
+ assert_all_contents_changed ( & pas_glob, & pas_contents ) ?;
105
+ assert_no_contents_changed ( & dpr_glob, & dpr_contents ) ?;
111
106
112
- let pas_timestamps = file_timestamps ( & pas_glob) ?;
107
+ let pas_contents = file_contents ( & pas_glob) ?;
113
108
pasfmt ( ) ?
114
109
. current_dir ( & tmp)
115
110
. arg ( "**/*.dpr" )
116
111
. assert ( )
117
112
. success ( ) ;
118
- assert_all_timestamps_changed ( & dpr_glob, & dpr_timestamps ) ?;
119
- assert_no_timestamps_changed ( & pas_glob, & pas_timestamps ) ?;
113
+ assert_all_contents_changed ( & dpr_glob, & dpr_contents ) ?;
114
+ assert_no_contents_changed ( & pas_glob, & pas_contents ) ?;
120
115
121
116
Ok ( ( ) )
122
117
}
@@ -128,11 +123,11 @@ fn classic_glob_does_not_recurse() -> TestResult {
128
123
tmp. copy_from ( TESTS_DIR . to_string ( ) + "/data/ext/included" , & [ "**/*" ] ) ?;
129
124
130
125
let pas_glob = tmp. to_string_lossy ( ) + "/**/*.pas" ;
131
- let pas_timestamps = file_timestamps ( & pas_glob) ?;
132
- assert_eq ! ( pas_timestamps . len( ) , 1 ) ;
126
+ let pas_contents = file_contents ( & pas_glob) ?;
127
+ assert_eq ! ( pas_contents . len( ) , 1 ) ;
133
128
134
129
pasfmt ( ) ?. current_dir ( & tmp) . arg ( "*.pas" ) . assert ( ) . success ( ) ;
135
- assert_no_timestamps_changed ( & pas_glob, & pas_timestamps ) ?;
130
+ assert_no_contents_changed ( & pas_glob, & pas_contents ) ?;
136
131
137
132
Ok ( ( ) )
138
133
}
@@ -144,15 +139,15 @@ fn glob_can_include_any_ext() -> TestResult {
144
139
tmp. copy_from ( TESTS_DIR , & [ "data/ext/excluded/*" ] ) ?;
145
140
146
141
let excluded_glob = tmp. to_string_lossy ( ) + "/data/ext/excluded/*" ;
147
- let excluded_files_timestamps = file_timestamps ( & excluded_glob) ?;
148
- assert_eq ! ( excluded_files_timestamps . len( ) , 6 ) ;
142
+ let excluded_files_contents = file_contents ( & excluded_glob) ?;
143
+ assert_eq ! ( excluded_files_contents . len( ) , 6 ) ;
149
144
150
145
pasfmt ( ) ?
151
146
. current_dir ( & tmp)
152
147
. args ( [ "**/*.*pas*" , "**/*.*dpr*" , "**/*.*dpk*" ] )
153
148
. assert ( )
154
149
. success ( ) ;
155
- assert_all_timestamps_changed ( & excluded_glob, & excluded_files_timestamps ) ?;
150
+ assert_all_contents_changed ( & excluded_glob, & excluded_files_contents ) ?;
156
151
157
152
Ok ( ( ) )
158
153
}
0 commit comments