Don't use env::tempdir

This can cause test pollution. Create a new temp directory on each run.
pull/650/head
Tamir Duberstein 1 year ago
parent fa91fb4f59
commit 5407d4a9a1
No known key found for this signature in database

@ -31,6 +31,7 @@ tokio = { version = "1.24.0", features = ["rt"], optional = true }
[dev-dependencies] [dev-dependencies]
futures = { version = "0.3.12", default-features = false, features = ["std"] } futures = { version = "0.3.12", default-features = false, features = ["std"] }
matches = "0.1.8" matches = "0.1.8"
tempfile = "3"
[features] [features]
default = [] default = []

@ -354,7 +354,8 @@ pub enum LinkError {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use matches::assert_matches; use matches::assert_matches;
use std::{cell::RefCell, env, fs::File, mem, os::unix::io::AsRawFd, rc::Rc}; use std::{cell::RefCell, fs::File, mem, os::unix::io::AsRawFd, rc::Rc};
use tempfile::tempdir;
use crate::{programs::ProgramError, sys::override_syscall}; use crate::{programs::ProgramError, sys::override_syscall};
@ -489,8 +490,8 @@ mod tests {
#[test] #[test]
#[cfg_attr(miri, ignore)] #[cfg_attr(miri, ignore)]
fn test_pin() { fn test_pin() {
let dir = env::temp_dir(); let dir = tempdir().unwrap();
let f1 = File::create(dir.join("f1")).expect("unable to create file in tmpdir"); let f1 = File::create(dir.path().join("f1")).expect("unable to create file in tmpdir");
let fd_link = FdLink::new(f1.as_raw_fd()); let fd_link = FdLink::new(f1.as_raw_fd());
// leak the fd, it will get closed when our pinned link is dropped // leak the fd, it will get closed when our pinned link is dropped
@ -499,11 +500,12 @@ mod tests {
// override syscall to allow for pin to happen in our tmpdir // override syscall to allow for pin to happen in our tmpdir
override_syscall(|_| Ok(0)); override_syscall(|_| Ok(0));
// create the file that would have happened as a side-effect of a real pin operation // create the file that would have happened as a side-effect of a real pin operation
File::create(dir.join("f1-pin")).expect("unable to create file in tmpdir"); let pin = dir.path().join("f1-pin");
assert!(dir.join("f1-pin").exists()); File::create(&pin).expect("unable to create file in tmpdir");
assert!(pin.exists());
let pinned_link = fd_link.pin(dir.join("f1-pin")).expect("pin failed"); let pinned_link = fd_link.pin(&pin).expect("pin failed");
pinned_link.unpin().expect("unpin failed"); pinned_link.unpin().expect("unpin failed");
assert!(!dir.join("f1-pin").exists()); assert!(!pin.exists());
} }
} }

Loading…
Cancel
Save