Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: more test cleanups #253

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 All @@ -67,15 +44,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 @@ -86,7 +63,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 @@ -96,54 +73,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 @@ -152,7 +129,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 @@ -165,14 +142,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