Skip to content

Commit

Permalink
chore: more test cleanups (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Aug 18, 2023
1 parent b4a2f64 commit 32ef2ac
Showing 1 changed file with 18 additions and 41 deletions.
59 changes: 18 additions & 41 deletions tests/tempdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,9 @@ use std::thread;

use tempfile::{Builder, TempDir};

macro_rules! t {
($e:expr) => {
match $e {
Ok(n) => n,
Err(e) => panic!("error: {}", e),
}
};
}

trait PathExt {
fn exists(&self) -> bool;
fn is_dir(&self) -> bool;
}

impl PathExt for Path {
fn exists(&self) -> bool {
fs::metadata(self).is_ok()
}
fn is_dir(&self) -> bool {
fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
}
}

fn test_tempdir() {
let path = {
let p = t!(Builder::new().prefix("foobar").tempdir_in(Path::new(".")));
let p = Builder::new().prefix("foobar").tempdir_in(".").unwrap();
let p = p.path();
assert!(p.to_str().unwrap().contains("foobar"));
p.to_path_buf()
Expand Down Expand Up @@ -73,15 +50,15 @@ fn test_customnamed() {
fn test_rm_tempdir() {
let (tx, rx) = channel();
let f = move || {
let tmp = t!(TempDir::new());
let tmp = TempDir::new().unwrap();
tx.send(tmp.path().to_path_buf()).unwrap();
panic!("panic to unwind past `tmp`");
};
let _ = thread::spawn(f).join();
let path = rx.recv().unwrap();
assert!(!path.exists());

let tmp = t!(TempDir::new());
let tmp = TempDir::new().unwrap();
let path = tmp.path().to_path_buf();
let f = move || {
let _tmp = tmp;
Expand All @@ -92,7 +69,7 @@ fn test_rm_tempdir() {

let path;
{
let f = move || t!(TempDir::new());
let f = move || TempDir::new().unwrap();

let tmp = thread::spawn(f).join().unwrap();
path = tmp.path().to_path_buf();
Expand All @@ -102,54 +79,54 @@ fn test_rm_tempdir() {

let path;
{
let tmp = t!(TempDir::new());
let tmp = TempDir::new().unwrap();
path = tmp.into_path();
}
assert!(path.exists());
t!(fs::remove_dir_all(&path));
fs::remove_dir_all(&path).unwrap();
assert!(!path.exists());
}

fn test_rm_tempdir_close() {
let (tx, rx) = channel();
let f = move || {
let tmp = t!(TempDir::new());
let tmp = TempDir::new().unwrap();
tx.send(tmp.path().to_path_buf()).unwrap();
t!(tmp.close());
tmp.close().unwrap();
panic!("panic when unwinding past `tmp`");
};
let _ = thread::spawn(f).join();
let path = rx.recv().unwrap();
assert!(!path.exists());

let tmp = t!(TempDir::new());
let tmp = TempDir::new().unwrap();
let path = tmp.path().to_path_buf();
let f = move || {
let tmp = tmp;
t!(tmp.close());
tmp.close().unwrap();
panic!("panic when unwinding past `tmp`");
};
let _ = thread::spawn(f).join();
assert!(!path.exists());

let path;
{
let f = move || t!(TempDir::new());
let f = move || TempDir::new().unwrap();

let tmp = thread::spawn(f).join().unwrap();
path = tmp.path().to_path_buf();
assert!(path.exists());
t!(tmp.close());
tmp.close().unwrap();
}
assert!(!path.exists());

let path;
{
let tmp = t!(TempDir::new());
let tmp = TempDir::new().unwrap();
path = tmp.into_path();
}
assert!(path.exists());
t!(fs::remove_dir_all(&path));
fs::remove_dir_all(&path).unwrap();
assert!(!path.exists());
}

Expand All @@ -158,7 +135,7 @@ fn dont_double_panic() {
let tmpdir = TempDir::new().unwrap();
// Remove the temporary directory so that TempDir sees
// an error on drop
t!(fs::remove_dir(tmpdir.path()));
fs::remove_dir(tmpdir.path()).unwrap();
// Panic. If TempDir panics *again* due to the rmdir
// error then the process will abort.
panic!();
Expand All @@ -171,14 +148,14 @@ fn in_tmpdir<F>(f: F)
where
F: FnOnce(),
{
let tmpdir = t!(TempDir::new());
let tmpdir = TempDir::new().unwrap();
assert!(env::set_current_dir(tmpdir.path()).is_ok());

f();
}

pub fn pass_as_asref_path() {
let tempdir = t!(TempDir::new());
fn pass_as_asref_path() {
let tempdir = TempDir::new().unwrap();
takes_asref_path(&tempdir);

fn takes_asref_path<T: AsRef<Path>>(path: T) {
Expand Down

0 comments on commit 32ef2ac

Please sign in to comment.