Skip to content

Commit

Permalink
Don't use env::tempdir
Browse files Browse the repository at this point in the history
This can cause test pollution. Create a new temp directory on each run.
  • Loading branch information
tamird committed Jul 14, 2023
1 parent fa91fb4 commit 5407d4a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions aya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ tokio = { version = "1.24.0", features = ["rt"], optional = true }
[dev-dependencies]
futures = { version = "0.3.12", default-features = false, features = ["std"] }
matches = "0.1.8"
tempfile = "3"

[features]
default = []
Expand Down
16 changes: 9 additions & 7 deletions aya/src/programs/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ pub enum LinkError {
#[cfg(test)]
mod tests {
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};

Expand Down Expand Up @@ -489,8 +490,8 @@ mod tests {
#[test]
#[cfg_attr(miri, ignore)]
fn test_pin() {
let dir = env::temp_dir();
let f1 = File::create(dir.join("f1")).expect("unable to create file in tmpdir");
let dir = tempdir().unwrap();
let f1 = File::create(dir.path().join("f1")).expect("unable to create file in tmpdir");
let fd_link = FdLink::new(f1.as_raw_fd());

// leak the fd, it will get closed when our pinned link is dropped
Expand All @@ -499,11 +500,12 @@ mod tests {
// override syscall to allow for pin to happen in our tmpdir
override_syscall(|_| Ok(0));
// 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");
assert!(dir.join("f1-pin").exists());
let pin = dir.path().join("f1-pin");
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");
assert!(!dir.join("f1-pin").exists());
assert!(!pin.exists());
}
}

0 comments on commit 5407d4a

Please sign in to comment.