@@ -8,6 +8,7 @@ mod macros;
8
8
9
9
pub mod ar;
10
10
pub mod artifact_names;
11
+ pub mod assertion_helpers;
11
12
pub mod diff;
12
13
pub mod env_checked;
13
14
pub mod external_deps;
@@ -18,7 +19,6 @@ pub mod run;
18
19
pub mod scoped_run;
19
20
pub mod targets;
20
21
21
- use std:: panic;
22
22
use std:: path:: { Path , PathBuf } ;
23
23
24
24
// Re-exports of third-party library crates.
@@ -77,68 +77,14 @@ pub use fs_helpers::{copy_dir_all, create_symlink, read_dir};
77
77
/// Helpers for scoped test execution where certain properties are attempted to be maintained.
78
78
pub use scoped_run:: { run_in_tmpdir, test_while_readonly} ;
79
79
80
- use command:: { Command , CompletedProcess } ;
81
-
82
- /// Browse the directory `path` non-recursively and return all files which respect the parameters
83
- /// outlined by `closure`.
84
- #[ track_caller]
85
- pub fn shallow_find_files < P : AsRef < Path > , F : Fn ( & PathBuf ) -> bool > (
86
- path : P ,
87
- filter : F ,
88
- ) -> Vec < PathBuf > {
89
- let mut matching_files = Vec :: new ( ) ;
90
- for entry in fs_wrapper:: read_dir ( path) {
91
- let entry = entry. expect ( "failed to read directory entry." ) ;
92
- let path = entry. path ( ) ;
93
-
94
- if path. is_file ( ) && filter ( & path) {
95
- matching_files. push ( path) ;
96
- }
97
- }
98
- matching_files
99
- }
100
-
101
- /// Returns true if the filename at `path` starts with `prefix`.
102
- pub fn has_prefix < P : AsRef < Path > > ( path : P , prefix : & str ) -> bool {
103
- path. as_ref ( ) . file_name ( ) . is_some_and ( |name| name. to_str ( ) . unwrap ( ) . starts_with ( prefix) )
104
- }
105
-
106
- /// Returns true if the filename at `path` has the extension `extension`.
107
- pub fn has_extension < P : AsRef < Path > > ( path : P , extension : & str ) -> bool {
108
- path. as_ref ( ) . extension ( ) . is_some_and ( |ext| ext == extension)
109
- }
110
-
111
- /// Returns true if the filename at `path` does not contain `expected`.
112
- pub fn not_contains < P : AsRef < Path > > ( path : P , expected : & str ) -> bool {
113
- !path. as_ref ( ) . file_name ( ) . is_some_and ( |name| name. to_str ( ) . unwrap ( ) . contains ( expected) )
114
- }
115
-
116
- /// Returns true if the filename at `path` is not in `expected`.
117
- pub fn filename_not_in_denylist < P : AsRef < Path > , V : AsRef < [ String ] > > ( path : P , expected : V ) -> bool {
118
- let expected = expected. as_ref ( ) ;
119
- path. as_ref ( )
120
- . file_name ( )
121
- . is_some_and ( |name| !expected. contains ( & name. to_str ( ) . unwrap ( ) . to_owned ( ) ) )
122
- }
123
-
124
- /// Returns true if the filename at `path` ends with `suffix`.
125
- pub fn has_suffix < P : AsRef < Path > > ( path : P , suffix : & str ) -> bool {
126
- path. as_ref ( ) . file_name ( ) . is_some_and ( |name| name. to_str ( ) . unwrap ( ) . ends_with ( suffix) )
127
- }
128
-
129
- /// Gathers all files in the current working directory that have the extension `ext`, and counts
130
- /// the number of lines within that contain a match with the regex pattern `re`.
131
- pub fn count_regex_matches_in_files_with_extension ( re : & regex:: Regex , ext : & str ) -> usize {
132
- let fetched_files = shallow_find_files ( cwd ( ) , |path| has_extension ( path, ext) ) ;
133
-
134
- let mut count = 0 ;
135
- for file in fetched_files {
136
- let content = fs_wrapper:: read_to_string ( file) ;
137
- count += content. lines ( ) . filter ( |line| re. is_match ( & line) ) . count ( ) ;
138
- }
80
+ pub use assertion_helpers:: {
81
+ assert_contains, assert_equals, assert_not_contains,
82
+ count_regex_matches_in_files_with_extension, filename_not_in_denylist, has_extension,
83
+ has_prefix, has_suffix, invalid_utf8_contains, invalid_utf8_not_contains, not_contains,
84
+ shallow_find_files,
85
+ } ;
139
86
140
- count
141
- }
87
+ use command:: { Command , CompletedProcess } ;
142
88
143
89
pub ( crate ) fn handle_failed_output (
144
90
cmd : & Command ,
@@ -171,36 +117,6 @@ pub fn set_host_rpath(cmd: &mut Command) {
171
117
} ) ;
172
118
}
173
119
174
- /// Read the contents of a file that cannot simply be read by
175
- /// read_to_string, due to invalid utf8 data, then assert that it contains `expected`.
176
- #[ track_caller]
177
- pub fn invalid_utf8_contains < P : AsRef < Path > , S : AsRef < str > > ( path : P , expected : S ) {
178
- let buffer = fs_wrapper:: read ( path. as_ref ( ) ) ;
179
- let expected = expected. as_ref ( ) ;
180
- if !String :: from_utf8_lossy ( & buffer) . contains ( expected) {
181
- eprintln ! ( "=== FILE CONTENTS (LOSSY) ===" ) ;
182
- eprintln ! ( "{}" , String :: from_utf8_lossy( & buffer) ) ;
183
- eprintln ! ( "=== SPECIFIED TEXT ===" ) ;
184
- eprintln ! ( "{}" , expected) ;
185
- panic ! ( "specified text was not found in file" ) ;
186
- }
187
- }
188
-
189
- /// Read the contents of a file that cannot simply be read by
190
- /// read_to_string, due to invalid utf8 data, then assert that it does not contain `expected`.
191
- #[ track_caller]
192
- pub fn invalid_utf8_not_contains < P : AsRef < Path > , S : AsRef < str > > ( path : P , expected : S ) {
193
- let buffer = fs_wrapper:: read ( path. as_ref ( ) ) ;
194
- let expected = expected. as_ref ( ) ;
195
- if String :: from_utf8_lossy ( & buffer) . contains ( expected) {
196
- eprintln ! ( "=== FILE CONTENTS (LOSSY) ===" ) ;
197
- eprintln ! ( "{}" , String :: from_utf8_lossy( & buffer) ) ;
198
- eprintln ! ( "=== SPECIFIED TEXT ===" ) ;
199
- eprintln ! ( "{}" , expected) ;
200
- panic ! ( "specified text was unexpectedly found in file" ) ;
201
- }
202
- }
203
-
204
120
/// Check that all files in `dir1` exist and have the same content in `dir2`. Panic otherwise.
205
121
pub fn recursive_diff ( dir1 : impl AsRef < Path > , dir2 : impl AsRef < Path > ) {
206
122
let dir2 = dir2. as_ref ( ) ;
@@ -225,45 +141,3 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
225
141
}
226
142
} ) ;
227
143
}
228
-
229
- /// Check that `actual` is equal to `expected`. Panic otherwise.
230
- #[ track_caller]
231
- pub fn assert_equals < S1 : AsRef < str > , S2 : AsRef < str > > ( actual : S1 , expected : S2 ) {
232
- let actual = actual. as_ref ( ) ;
233
- let expected = expected. as_ref ( ) ;
234
- if actual != expected {
235
- eprintln ! ( "=== ACTUAL TEXT ===" ) ;
236
- eprintln ! ( "{}" , actual) ;
237
- eprintln ! ( "=== EXPECTED ===" ) ;
238
- eprintln ! ( "{}" , expected) ;
239
- panic ! ( "expected text was not found in actual text" ) ;
240
- }
241
- }
242
-
243
- /// Check that `haystack` contains `needle`. Panic otherwise.
244
- #[ track_caller]
245
- pub fn assert_contains < S1 : AsRef < str > , S2 : AsRef < str > > ( haystack : S1 , needle : S2 ) {
246
- let haystack = haystack. as_ref ( ) ;
247
- let needle = needle. as_ref ( ) ;
248
- if !haystack. contains ( needle) {
249
- eprintln ! ( "=== HAYSTACK ===" ) ;
250
- eprintln ! ( "{}" , haystack) ;
251
- eprintln ! ( "=== NEEDLE ===" ) ;
252
- eprintln ! ( "{}" , needle) ;
253
- panic ! ( "needle was not found in haystack" ) ;
254
- }
255
- }
256
-
257
- /// Check that `haystack` does not contain `needle`. Panic otherwise.
258
- #[ track_caller]
259
- pub fn assert_not_contains < S1 : AsRef < str > , S2 : AsRef < str > > ( haystack : S1 , needle : S2 ) {
260
- let haystack = haystack. as_ref ( ) ;
261
- let needle = needle. as_ref ( ) ;
262
- if haystack. contains ( needle) {
263
- eprintln ! ( "=== HAYSTACK ===" ) ;
264
- eprintln ! ( "{}" , haystack) ;
265
- eprintln ! ( "=== NEEDLE ===" ) ;
266
- eprintln ! ( "{}" , needle) ;
267
- panic ! ( "needle was unexpectedly found in haystack" ) ;
268
- }
269
- }
0 commit comments