summary refs log tree commit diff
path: root/pkgs/test/nixpkgs-check-by-name/src/main.rs
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2023-09-27 18:40:33 +0000
committerAlyssa Ross <hi@alyssa.is>2023-09-27 18:40:41 +0000
commit030c5028b07afcedce7c5956015c629486cc79d9 (patch)
tree4c3cb9c6cff0e30919a97fc0c1d3203446696f4e /pkgs/test/nixpkgs-check-by-name/src/main.rs
parent4b852f7ef3cb92277f212ba7dc168da1073e65cc (diff)
parent04c0744afbab2369baf4f134c544db3f24164d80 (diff)
downloadnixpkgs-030c5028b07afcedce7c5956015c629486cc79d9.tar
nixpkgs-030c5028b07afcedce7c5956015c629486cc79d9.tar.gz
nixpkgs-030c5028b07afcedce7c5956015c629486cc79d9.tar.bz2
nixpkgs-030c5028b07afcedce7c5956015c629486cc79d9.tar.lz
nixpkgs-030c5028b07afcedce7c5956015c629486cc79d9.tar.xz
nixpkgs-030c5028b07afcedce7c5956015c629486cc79d9.tar.zst
nixpkgs-030c5028b07afcedce7c5956015c629486cc79d9.zip
Rebase onto c1a53897ad4290a1cbfa02fbe6f3869577b93744
Signed-off-by: Alyssa Ross <hi@alyssa.is>
Diffstat (limited to 'pkgs/test/nixpkgs-check-by-name/src/main.rs')
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/main.rs41
1 files changed, 17 insertions, 24 deletions
diff --git a/pkgs/test/nixpkgs-check-by-name/src/main.rs b/pkgs/test/nixpkgs-check-by-name/src/main.rs
index 751b5dbd024..8c663061bb0 100644
--- a/pkgs/test/nixpkgs-check-by-name/src/main.rs
+++ b/pkgs/test/nixpkgs-check-by-name/src/main.rs
@@ -87,10 +87,9 @@ mod tests {
     use crate::check_nixpkgs;
     use crate::structure;
     use anyhow::Context;
-    use std::env;
     use std::fs;
     use std::path::Path;
-    use tempfile::{tempdir, tempdir_in};
+    use tempfile::{tempdir_in, TempDir};
 
     #[test]
     fn tests_dir() -> anyhow::Result<()> {
@@ -111,6 +110,13 @@ mod tests {
         Ok(())
     }
 
+    // tempfile::tempdir needs to be wrapped in temp_env lock
+    // because it accesses TMPDIR environment variable.
+    fn tempdir() -> anyhow::Result<TempDir> {
+        let empty_list: [(&str, Option<&str>); 0] = [];
+        Ok(temp_env::with_vars(empty_list, tempfile::tempdir)?)
+    }
+
     // We cannot check case-conflicting files into Nixpkgs (the channel would fail to
     // build), so we generate the case-conflicting file instead.
     #[test]
@@ -157,34 +163,21 @@ mod tests {
         std::os::unix::fs::symlink("actual", temp_root.path().join("symlinked"))?;
         let tmpdir = temp_root.path().join("symlinked");
 
-        // Then set TMPDIR to the symlinked directory
-        // Make sure to persist the old value so we can undo this later
-        let old_tmpdir = env::var("TMPDIR").ok();
-        env::set_var("TMPDIR", &tmpdir);
-
-        // Then run a simple test with this symlinked temporary directory
-        // This should be successful
-        test_nixpkgs("symlinked_tmpdir", Path::new("tests/success"), "")?;
-
-        // Undo the env variable change
-        if let Some(old) = old_tmpdir {
-            env::set_var("TMPDIR", old);
-        } else {
-            env::remove_var("TMPDIR");
-        }
-
-        Ok(())
+        temp_env::with_var("TMPDIR", Some(&tmpdir), || {
+            test_nixpkgs("symlinked_tmpdir", Path::new("tests/success"), "")
+        })
     }
 
     fn test_nixpkgs(name: &str, path: &Path, expected_errors: &str) -> anyhow::Result<()> {
         let extra_nix_path = Path::new("tests/mock-nixpkgs.nix");
 
         // We don't want coloring to mess up the tests
-        env::set_var("NO_COLOR", "1");
-
-        let mut writer = vec![];
-        check_nixpkgs(&path, vec![&extra_nix_path], &mut writer)
-            .context(format!("Failed test case {name}"))?;
+        let writer = temp_env::with_var("NO_COLOR", Some("1"), || -> anyhow::Result<_> {
+            let mut writer = vec![];
+            check_nixpkgs(&path, vec![&extra_nix_path], &mut writer)
+                .context(format!("Failed test case {name}"))?;
+            Ok(writer)
+        })?;
 
         let actual_errors = String::from_utf8_lossy(&writer);