From bd63e2997c6cd40c5f1e66d600f6d4fec2bbf1e6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 09:42:35 +0000 Subject: [PATCH] xtask stats: test the directory walk (links, identity, size limit) --- xtask/src/stats.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/xtask/src/stats.rs b/xtask/src/stats.rs index 3c9c2dc8b..7163ac45c 100644 --- a/xtask/src/stats.rs +++ b/xtask/src/stats.rs @@ -143,7 +143,7 @@ pub fn run(_root: &Path, exact: bool) -> Result<(), String> { #[cfg(test)] mod tests { - use super::{MAX_SOURCE_BYTES, count_tests, read_source}; + use super::{Counts, MAX_SOURCE_BYTES, count_tests, read_source, walk}; #[test] fn counts_attributes_and_proptest_blocks() { @@ -164,4 +164,23 @@ mod tests { assert_eq!(read_source(&big), None); let _ = std::fs::remove_dir_all(dir); } + + #[cfg(unix)] + #[test] + fn walk_counts_regular_sources_once_and_skips_links_and_big_files() { + let dir = std::env::temp_dir().join(format!("xtask-stats-walk-{}", std::process::id())); + let sub = dir.join("sub"); + std::fs::create_dir_all(&sub).unwrap(); + std::fs::write(dir.join("a.rs"), "#[test]\nfn a() {}\n").unwrap(); + std::fs::write(sub.join("b.rs"), "fn b() {}\n").unwrap(); + // A directory link back to the root is one identity, a file link is not a regular file, + // and a source over the size limit is not read. + std::os::unix::fs::symlink(&dir, sub.join("loop")).unwrap(); + std::os::unix::fs::symlink(dir.join("a.rs"), sub.join("link.rs")).unwrap(); + std::fs::File::create(dir.join("big.rs")).unwrap().set_len(MAX_SOURCE_BYTES + 1).unwrap(); + let mut c = Counts::default(); + walk(&dir, &mut c, &mut std::collections::HashSet::new(), &mut 100); + assert_eq!((c.files, c.tests), (2, 1)); + let _ = std::fs::remove_dir_all(dir); + } }