summaryrefslogtreecommitdiffstats
path: root/ignore/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ignore/src/lib.rs')
-rw-r--r--ignore/src/lib.rs65
1 files changed, 63 insertions, 2 deletions
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
+ }
+ }
+}