Browse Source

Use tempfile crate

david 2 weeks ago
parent
commit
a0fefec934
3 changed files with 110 additions and 5 deletions
  1. 98 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 11 5
      src/lib.rs

+ 98 - 0
Cargo.lock

@@ -58,6 +58,18 @@ version = "1.0.96"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "6b964d184e89d9b6b67dd2715bc8e74cf3107fb2b529990c90cf517326150bf4"
 
+[[package]]
+name = "bitflags"
+version = "2.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
 [[package]]
 name = "clap"
 version = "4.5.31"
@@ -104,6 +116,34 @@ version = "1.0.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
 
+[[package]]
+name = "errno"
+version = "0.3.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d"
+dependencies = [
+ "libc",
+ "windows-sys",
+]
+
+[[package]]
+name = "fastrand"
+version = "2.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
+
+[[package]]
+name = "getrandom"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+ "windows-targets",
+]
+
 [[package]]
 name = "heck"
 version = "0.5.0"
@@ -116,6 +156,18 @@ version = "1.70.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
 
+[[package]]
+name = "libc"
+version = "0.2.171"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6"
+
+[[package]]
+name = "linux-raw-sys"
+version = "0.9.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6db9c683daf087dc577b7506e9695b3d556a9f3849903fa28186283afd6809e9"
+
 [[package]]
 name = "once_cell"
 version = "1.20.3"
@@ -140,12 +192,26 @@ dependencies = [
  "proc-macro2",
 ]
 
+[[package]]
+name = "rustix"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7178faa4b75a30e269c71e61c353ce2748cf3d76f0c44c393f4e60abf49b825"
+dependencies = [
+ "bitflags",
+ "errno",
+ "libc",
+ "linux-raw-sys",
+ "windows-sys",
+]
+
 [[package]]
 name = "rustpan"
 version = "0.2.1"
 dependencies = [
  "anyhow",
  "clap",
+ "tempfile",
 ]
 
 [[package]]
@@ -165,6 +231,20 @@ dependencies = [
  "unicode-ident",
 ]
 
+[[package]]
+name = "tempfile"
+version = "3.18.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2c317e0a526ee6120d8dabad239c8dadca62b24b6f168914bbbc8e2fb1f0e567"
+dependencies = [
+ "cfg-if",
+ "fastrand",
+ "getrandom",
+ "once_cell",
+ "rustix",
+ "windows-sys",
+]
+
 [[package]]
 name = "unicode-ident"
 version = "1.0.17"
@@ -177,6 +257,15 @@ version = "0.2.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
 
+[[package]]
+name = "wasi"
+version = "0.13.3+wasi-0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2"
+dependencies = [
+ "wit-bindgen-rt",
+]
+
 [[package]]
 name = "windows-sys"
 version = "0.59.0"
@@ -249,3 +338,12 @@ name = "windows_x86_64_msvc"
 version = "0.52.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
+
+[[package]]
+name = "wit-bindgen-rt"
+version = "0.33.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c"
+dependencies = [
+ "bitflags",
+]

+ 1 - 0
Cargo.toml

@@ -6,3 +6,4 @@ edition = "2021"
 [dependencies]
 anyhow = "1.0.96"
 clap = { version = "4.5.31", features = ["derive"] }
+tempfile = "3.18.0"

+ 11 - 5
src/lib.rs

@@ -99,17 +99,23 @@ pub fn clean_in_dir(path: PathBuf, forced: bool, days: u64) -> anyResult<u64> {
 #[cfg(test)]
 mod tests {
     use std::{fs, path::PathBuf};
+    use tempfile::tempdir;
     use anyhow::Result as anyResult;
     use crate::{find_target_dir, find_newest_file, execute_clean, clean_in_dir};
 
     #[test]
     fn locate_target_dir() -> anyResult<()> {
-        fs::create_dir(PathBuf::from("test"))?;
-        fs::create_dir(PathBuf::from("test/target"))?;
-        fs::write(PathBuf::from("test/Cargo.toml"), "")?;
-        let found = find_target_dir(PathBuf::from("test"))?;
-        fs::remove_dir_all(PathBuf::from("test"))?;
+        let temp = tempdir()?;
+        let p = temp.path();
+        let target_cargo = p.join("Cargo.toml");
+        fs::write(target_cargo, "")?;
+        let target_dir = p.join("target");
+        fs::create_dir(&target_dir)?;
+        let found = find_target_dir(PathBuf::from(&p))?;
         assert!(found, "Should have found the target dir");
+        fs::remove_dir(target_dir)?;
+        let found = find_target_dir(PathBuf::from(p))?;
+        assert!(!found, "Should NOT have found the target dir (should be missing target/)");
         Ok(())
     }
 }