Skip to content

Commit

Permalink
deps: drop tempfile
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BurntSushi committed Aug 6, 2019
1 parent 4de227f commit 31807f8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 108 deletions.
97 changes: 0 additions & 97 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions ignore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
5 changes: 2 additions & 3 deletions ignore/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -744,7 +743,7 @@ mod tests {
}

fn tmpdir(prefix: &str) -> TempDir {
tempfile::Builder::new().prefix(prefix).tempdir().unwrap()
TempDir::new().unwrap()
}

#[test]
Expand Down
65 changes: 63 additions & 2 deletions ignore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
}
}
}
5 changes: 2 additions & 3 deletions ignore/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1818,7 +1817,7 @@ mod tests {
}

fn tmpdir(prefix: &str) -> TempDir {
tempfile::Builder::new().prefix(prefix).tempdir().unwrap()
TempDir::new().unwrap()
}

fn assert_paths(
Expand Down

0 comments on commit 31807f8

Please sign in to comment.