From 69dd3b5456dee9788861d8eba891472df08190b6 Mon Sep 17 00:00:00 2001 From: "mula.liu" Date: Thu, 15 Oct 2026 16:49:49 +0800 Subject: [PATCH] Stop matching path separators in suffixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- crates/squid-core/src/fs.rs | 18 +++++++++---- crates/squid-engine/src/purge.rs | 39 ++++++++++++++++++---------- crates/squid-engine/src/uninstall.rs | 19 +++++++++++--- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/crates/squid-core/src/fs.rs b/crates/squid-core/src/fs.rs index a78fd85..1031fc7 100644 --- a/crates/squid-core/src/fs.rs +++ b/crates/squid-core/src/fs.rs @@ -646,7 +646,6 @@ pub type SharedCancel = Arc; 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); diff --git a/crates/squid-engine/src/purge.rs b/crates/squid-engine/src/purge.rs index 641bb2c..dee3170 100644 --- a/crates/squid-engine/src/purge.rs +++ b/crates/squid-engine/src/purge.rs @@ -657,26 +657,37 @@ mod tests { &cancel, ) .expect("scan"); - let names: Vec = 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::>() + }; + 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); diff --git a/crates/squid-engine/src/uninstall.rs b/crates/squid-engine/src/uninstall.rs index 77c60fb..8361d98 100644 --- a/crates/squid-engine/src/uninstall.rs +++ b/crates/squid-engine/src/uninstall.rs @@ -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