Stop matching path separators in suffixes

The purge test compared rendered path strings against `demo/node_modules` and
friends, so on Windows every positive assertion failed and the negative one —
`loose/dist` must not be offered — passed for the wrong reason. Assertions now
compare trailing components, which is what they meant all along and holds on
both separators.

`category_index` made the same assumption in production: it lowercased the path
and looked for `/library/caches`, so a leftover root was only ever reclassified
on the OS whose separator it was written for.
main
mula.liu 2026-10-15 16:49:49 +08:00
parent 7584811e91
commit 69dd3b5456
3 changed files with 54 additions and 22 deletions

View File

@ -646,7 +646,6 @@ pub type SharedCancel = Arc<CancelToken>;
mod tests {
use super::*;
use std::fs;
use std::io::Write as _;
#[test]
fn measures_contents_and_path() {
@ -675,9 +674,18 @@ mod tests {
let root = std::env::temp_dir().join(format!("squid-sparse-{}", std::process::id()));
let _ = fs::remove_dir_all(&root);
fs::create_dir_all(&root).unwrap();
// One gibibyte is plenty to show the property and small enough that a
// nearly full CI disk can still extend to it.
const SPARSE_LEN: u64 = 1 << 30;
let raw = root.join("Docker.raw");
let file = fs::File::create(&raw).unwrap();
file.set_len(8 << 30).unwrap();
if file.set_len(SPARSE_LEN).is_err() {
// Volumes with a space-reservation filter answer `SetEndOfFile`
// with ERROR_DISK_FULL instead of handing out a hole.
eprintln!("skipped: this volume will not extend a file without the space");
let _ = fs::remove_dir_all(&root);
return;
}
drop(file);
if !mark_sparse(&raw) {
eprintln!("skipped: this volume refuses to make a file sparse");
@ -687,11 +695,11 @@ mod tests {
// One block of real data, so the file has both a hole and a tail the
// volume had to hand over — the shape of an actual thin disk image.
let mut hole = fs::OpenOptions::new().write(true).open(&raw).unwrap();
hole.write_all(&[3u8; 4096]).unwrap();
std::io::Write::write_all(&mut hole, &[3u8; 4096]).unwrap();
drop(hole);
let info = measure(&root, ItemKind::Path);
assert!(info.logical.as_u64() >= 8 << 30, "logical length is 8 GiB");
assert_eq!(info.logical, Bytes(SPARSE_LEN), "logical length is 1 GiB");
assert!(
info.on_disk().as_u64() * 100 < info.logical.as_u64(),
"the walk reported {}/{}; a sparse file must count as the blocks it wrote",
@ -736,7 +744,7 @@ mod tests {
fs::create_dir_all(&root).unwrap();
let mut file = fs::File::create(root.join("solid.bin")).unwrap();
file.set_len(ALLOCATION_PROBE_MIN).unwrap();
file.write_all(&[9u8; 4096]).unwrap();
std::io::Write::write_all(&mut file, &[9u8; 4096]).unwrap();
drop(file);
let info = measure(&root, ItemKind::Path);

View File

@ -657,26 +657,37 @@ mod tests {
&cancel,
)
.expect("scan");
let names: Vec<String> = report
let found: Vec<&std::path::Path> = report
.items
.iter()
.map(|item| item.path.to_string_lossy().into_owned())
.map(|item| item.path.as_path())
.collect();
// Compare trailing *components*, never the rendered string: on Windows
// `...\demo\node_modules` never ends with "demo/node_modules", which
// failed the positive asserts and — far worse — made the negative one
// below pass for the wrong reason on every Windows run.
let has = |suffix: &str| {
found
.iter()
.any(|path| path.ends_with(std::path::Path::new(suffix)))
};
let listed = || {
found
.iter()
.map(|path| path.display().to_string())
.collect::<Vec<_>>()
};
assert!(has("demo/node_modules"), "{:?}", listed());
assert!(has("rust/checkme/target"), "{:?}", listed());
assert!(
names.iter().any(|p| p.ends_with("demo/node_modules")),
"{names:?}"
has("demo/dist"),
"dist inside a project is an artefact: {:?}",
listed()
);
assert!(
names.iter().any(|p| p.ends_with("rust/checkme/target")),
"{names:?}"
);
assert!(
names.iter().any(|p| p.ends_with("demo/dist")),
"dist inside a project is an artefact: {names:?}"
);
assert!(
!names.iter().any(|p| p.ends_with("loose/dist")),
"dist outside a project must not be offered: {names:?}"
!has("loose/dist"),
"dist outside a project must not be offered: {:?}",
listed()
);
for item in &report.items {
assert_eq!(item.kind, ItemKind::Path);

View File

@ -369,10 +369,23 @@ fn category_index(roots: &[squid_platform::LeftoverRoot]) -> Vec<(PathBuf, Categ
roots
.iter()
.map(|root| {
let text = root.dir.to_string_lossy().to_lowercase();
let category = if text.ends_with("/library/caches") || text.ends_with("/.cache") {
// Trailing components, case-insensitively: `ends_with` on the
// rendered path string only holds on whichever OS wrote the
// separator, and quietly stopped classifying on the other one.
let name = root
.dir
.file_name()
.map(|name| name.to_string_lossy().to_lowercase())
.unwrap_or_default();
let parent_is_library = root
.dir
.parent()
.and_then(|parent| parent.file_name())
.map(|name| name.to_string_lossy().eq_ignore_ascii_case("library"))
.unwrap_or(false);
let category = if name == ".cache" || (name == "caches" && parent_is_library) {
Category::AppCaches
} else if text.ends_with("/library/logs") {
} else if name == "logs" && parent_is_library {
Category::Logs
} else {
root.category