diff options
author | Andrew Gallant <jamslam@gmail.com> | 2019-08-06 09:46:05 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2019-08-06 09:46:05 -0400 |
commit | 31807f805a793c254587105ca8ee0d41dfe3004b (patch) | |
tree | 10e28947b264ff1a116a9caad0cd97651b0fc980 /ignore | |
parent | 4de227fd9a1bbc0396c1f1e6d5209a1171fad88a (diff) |
deps: drop tempfile
We were only using it to create temporary directories for `ignore`
tests, but it pulls in a bunch of dependencies and we don't really need
randomness. So just use our own simple wrapper instead.
Diffstat (limited to 'ignore')
-rw-r--r-- | ignore/Cargo.toml | 3 | ||||
-rw-r--r-- | ignore/src/dir.rs | 5 | ||||
-rw-r--r-- | ignore/src/lib.rs | 65 | ||||
-rw-r--r-- | ignore/src/walk.rs | 5 |
4 files changed, 67 insertions, 11 deletions
diff --git a/ignore/Cargo.toml b/ignore/Cargo.toml index 984d722b..7cf64416 100644 --- a/ignore/Cargo.toml +++ b/ignore/Cargo.toml @@ -31,8 +31,5 @@ walkdir = "2.2.7" [target.'cfg(windows)'.dependencies.winapi-util] version = "0.1.2" -[dev-dependencies] -tempfile = "3.0.5" - [features] simd-accel = ["globset/simd-accel"] diff --git a/ignore/src/dir.rs b/ignore/src/dir.rs index 479e0c24..12201f22 100644 --- a/ignore/src/dir.rs +++ b/ignore/src/dir.rs @@ -721,10 +721,9 @@ mod tests { use std::io::Write; use std::path::Path; - use tempfile::{self, TempDir}; - use dir::IgnoreBuilder; use gitignore::Gitignore; + use tests::TempDir; use Error; fn wfile<P: AsRef<Path>>(path: P, contents: &str) { @@ -744,7 +743,7 @@ mod tests { } fn tmpdir(prefix: &str) -> TempDir { - tempfile::Builder::new().prefix(prefix).tempdir().unwrap() + TempDir::new().unwrap() } #[test] diff --git a/ignore/src/lib.rs b/ignore/src/lib.rs index d0c8d2fc..61768abd 100644 --- a/ignore/src/lib.rs +++ b/ignore/src/lib.rs @@ -55,8 +55,6 @@ extern crate log; extern crate memchr; extern crate regex; extern crate same_file; -#[cfg(test)] -extern crate tempfile; extern crate thread_local; extern crate walkdir; #[cfg(windows)] @@ -442,3 +440,66 @@ impl<T> Match<T> { } } } + +#[cfg(test)] +mod tests { + use std::env; + use std::error; + use std::fs; + use std::path::{Path, PathBuf}; + use std::result; + + /// A convenient result type alias. + pub type Result<T> = + result::Result<T, Box<dyn error::Error + Send + Sync>>; + + macro_rules! err { + ($($tt:tt)*) => { + Box::<dyn error::Error + Send + Sync>::from(format!($($tt)*)) + } + } + + /// A simple wrapper for creating a temporary directory that is + /// automatically deleted when it's dropped. + /// + /// We use this in lieu of tempfile because tempfile brings in too many + /// dependencies. + #[derive(Debug)] + pub struct TempDir(PathBuf); + + impl Drop for TempDir { + fn drop(&mut self) { + fs::remove_dir_all(&self.0).unwrap(); + } + } + + impl TempDir { + /// Create a new empty temporary directory under the system's configured + /// temporary directory. + pub fn new() -> Result<TempDir> { + use std::sync::atomic::{AtomicUsize, Ordering}; + + static TRIES: usize = 100; + static COUNTER: AtomicUsize = AtomicUsize::new(0); + + let tmpdir = env::temp_dir(); + for _ in 0..TRIES { + let count = COUNTER.fetch_add(1, Ordering::SeqCst); + let path = tmpdir.join("rust-ignore").join(count.to_string()); + if path.is_dir() { + continue; + } + fs::create_dir_all(&path).map_err(|e| { + err!("failed to create {}: {}", path.display(), e) + })?; + return Ok(TempDir(path)); + } + Err(err!("failed to create temp dir after {} tries", TRIES)) + } + + /// Return the underlying path to this temporary directory. + pub fn path(&self) -> &Path { + &self.0 + } + } +} diff --git a/ignore/src/walk.rs b/ignore/src/walk.rs index df796d41..b3e05f08 100644 --- a/ignore/src/walk.rs +++ b/ignore/src/walk.rs @@ -1730,8 +1730,7 @@ mod tests { use std::path::Path; use std::sync::{Arc, Mutex}; - use tempfile::{self, TempDir}; - + use tests::TempDir; use super::{DirEntry, WalkBuilder, WalkState}; fn wfile<P: AsRef<Path>>(path: P, contents: &str) { @@ -1818,7 +1817,7 @@ mod tests { } fn tmpdir(prefix: &str) -> TempDir { - tempfile::Builder::new().prefix(prefix).tempdir().unwrap() + TempDir::new().unwrap() } fn assert_paths( |