Skip to content

Commit 05e0749

Browse files
committed
feat: Added assert_file_layout() to CargoPathExt
1 parent 1c714ca commit 05e0749

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cargo-credential-macos-keychain = { version = "0.4.18", path = "credential/cargo
3232
cargo-credential-wincred = { version = "0.4.18", path = "credential/cargo-credential-wincred" }
3333
cargo-platform = { path = "crates/cargo-platform", version = "0.3.0" }
3434
cargo-test-macro = { version = "0.4.7", path = "crates/cargo-test-macro" }
35-
cargo-test-support = { version = "0.8.2", path = "crates/cargo-test-support" }
35+
cargo-test-support = { version = "0.9.0", path = "crates/cargo-test-support" }
3636
cargo-util = { version = "0.2.25", path = "crates/cargo-util" }
3737
cargo-util-schemas = { version = "0.10.1", path = "crates/cargo-util-schemas" }
3838
cargo_metadata = "0.21.0"

crates/cargo-test-support/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cargo-test-support"
3-
version = "0.8.2"
3+
version = "0.9.0"
44
edition.workspace = true
55
rust-version = "1.90" # MSRV:1
66
license.workspace = true

crates/cargo-test-support/src/compare.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ fn add_regex_redactions(subs: &mut snapbox::Redactions) {
229229
.unwrap();
230230
subs.insert("[HASH]", regex!(r"/[a-z0-9\-_]+-(?<redacted>[0-9a-f]{16})"))
231231
.unwrap();
232+
// Match multi-part hashes like `06/b451d0d6f88b1d` used in directory paths
233+
subs.insert("[HASH]", regex!(r"/(?<redacted>[a-f0-9]{2}\/[0-9a-f]{14})"))
234+
.unwrap();
235+
// Match file name hashes like `foo-06b451d0d6f88b1d`
236+
subs.insert("[HASH]", regex!(r"[a-z0-9]+-(?<redacted>[a-f0-9]{16})"))
237+
.unwrap();
232238
subs.insert(
233239
"[AVG_ELAPSED]",
234240
regex!(r"(?<redacted>[0-9]+(\.[0-9]+)?) ns/iter"),

crates/cargo-test-support/src/paths.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Access common paths and manipulate the filesystem
22
33
use filetime::FileTime;
4+
use itertools::Itertools;
5+
use walkdir::WalkDir;
46

57
use std::cell::RefCell;
68
use std::env;
@@ -12,6 +14,9 @@ use std::sync::Mutex;
1214
use std::sync::OnceLock;
1315
use std::sync::atomic::{AtomicUsize, Ordering};
1416

17+
use crate::compare::assert_e2e;
18+
use crate::compare::match_contains;
19+
1520
static CARGO_INTEGRATION_TEST_DIR: &str = "cit";
1621

1722
static GLOBAL_ROOT: OnceLock<Mutex<Option<PathBuf>>> = OnceLock::new();
@@ -152,6 +157,10 @@ pub trait CargoPathExt {
152157
fn move_in_time<F>(&self, travel_amount: F)
153158
where
154159
F: Fn(i64, u32) -> (i64, u32);
160+
161+
fn assert_build_dir_layout(&self, expected: impl snapbox::IntoData);
162+
163+
fn assert_dir_layout(&self, expected: impl snapbox::IntoData, ignored_path_patterns: &[String]);
155164
}
156165

157166
impl CargoPathExt for Path {
@@ -236,6 +245,37 @@ impl CargoPathExt for Path {
236245
});
237246
}
238247
}
248+
249+
#[track_caller]
250+
fn assert_build_dir_layout(&self, expected: impl snapbox::IntoData) {
251+
self.assert_dir_layout(expected.unordered(), &build_dir_ignored_path_patterns());
252+
}
253+
254+
#[track_caller]
255+
fn assert_dir_layout(
256+
&self,
257+
expected: impl snapbox::IntoData,
258+
ignored_path_patterns: &[String],
259+
) {
260+
let assert = assert_e2e();
261+
let actual = WalkDir::new(self)
262+
.sort_by_file_name()
263+
.into_iter()
264+
.filter_map(|e| e.ok())
265+
.filter(|e| e.file_type().is_file())
266+
.map(|e| e.path().to_string_lossy().into_owned())
267+
.filter(|file| {
268+
for ignored in ignored_path_patterns {
269+
if match_contains(&ignored, file, &assert.redactions()).is_ok() {
270+
return false;
271+
}
272+
}
273+
return true;
274+
})
275+
.join("\n");
276+
277+
assert.eq(format!("{actual}\n"), expected);
278+
}
239279
}
240280

241281
impl CargoPathExt for PathBuf {
@@ -260,6 +300,21 @@ impl CargoPathExt for PathBuf {
260300
{
261301
self.as_path().move_in_time(travel_amount)
262302
}
303+
304+
#[track_caller]
305+
fn assert_build_dir_layout(&self, expected: impl snapbox::IntoData) {
306+
self.as_path().assert_build_dir_layout(expected);
307+
}
308+
309+
#[track_caller]
310+
fn assert_dir_layout(
311+
&self,
312+
expected: impl snapbox::IntoData,
313+
ignored_path_patterns: &[String],
314+
) {
315+
self.as_path()
316+
.assert_dir_layout(expected, ignored_path_patterns);
317+
}
263318
}
264319

265320
fn do_op<F>(path: &Path, desc: &str, mut f: F)
@@ -290,6 +345,20 @@ where
290345
}
291346
}
292347

348+
/// The paths to ignore when [`CargoPathExt::assert_build_dir_layout`] is called
349+
fn build_dir_ignored_path_patterns() -> Vec<String> {
350+
vec![
351+
// Ignore MacOS debug symbols as there are many files/directories that would clutter up
352+
// tests few not a lot of benefit.
353+
"[..].dSYM/[..]",
354+
// Ignore Windows debub symbols files (.pdb)
355+
"[..].pdb",
356+
]
357+
.into_iter()
358+
.map(ToString::to_string)
359+
.collect()
360+
}
361+
293362
/// Get the filename for a library.
294363
///
295364
/// `kind` should be one of:

0 commit comments

Comments
 (0)