Przeglądaj źródła

Don't use env::tempdir

This can cause test pollution. Create a new temp directory on each run.
Tamir Duberstein 1 rok temu
rodzic
commit
5407d4a9a1
2 zmienionych plików z 10 dodań i 7 usunięć
  1. 1 0
      aya/Cargo.toml
  2. 9 7
      aya/src/programs/links.rs

+ 1 - 0
aya/Cargo.toml

@@ -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 = []

+ 9 - 7
aya/src/programs/links.rs

@@ -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};
 
@@ -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
@@ -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());
     }
 }