summary refs log tree commit diff
path: root/pkgs/test
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/test')
-rw-r--r--pkgs/test/nixpkgs-check-by-name/Cargo.lock56
-rw-r--r--pkgs/test/nixpkgs-check-by-name/Cargo.toml4
-rw-r--r--pkgs/test/nixpkgs-check-by-name/default.nix4
-rw-r--r--pkgs/test/nixpkgs-check-by-name/src/main.rs41
4 files changed, 77 insertions, 28 deletions
diff --git a/pkgs/test/nixpkgs-check-by-name/Cargo.lock b/pkgs/test/nixpkgs-check-by-name/Cargo.lock
index 3859d2b6e97..aa4459c7cff 100644
--- a/pkgs/test/nixpkgs-check-by-name/Cargo.lock
+++ b/pkgs/test/nixpkgs-check-by-name/Cargo.lock
@@ -243,6 +243,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503"
 
 [[package]]
+name = "lock_api"
+version = "0.4.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16"
+dependencies = [
+ "autocfg",
+ "scopeguard",
+]
+
+[[package]]
 name = "memchr"
 version = "2.5.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -267,9 +277,9 @@ dependencies = [
  "lazy_static",
  "regex",
  "rnix",
- "rowan",
  "serde",
  "serde_json",
+ "temp-env",
  "tempfile",
 ]
 
@@ -280,6 +290,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
 
 [[package]]
+name = "parking_lot"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
+dependencies = [
+ "lock_api",
+ "parking_lot_core",
+]
+
+[[package]]
+name = "parking_lot_core"
+version = "0.9.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "redox_syscall",
+ "smallvec",
+ "windows-targets",
+]
+
+[[package]]
 name = "proc-macro2"
 version = "1.0.66"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -383,6 +416,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
 
 [[package]]
+name = "scopeguard"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
+
+[[package]]
 name = "serde"
 version = "1.0.186"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -414,6 +453,12 @@ dependencies = [
 ]
 
 [[package]]
+name = "smallvec"
+version = "1.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a"
+
+[[package]]
 name = "strsim"
 version = "0.10.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -431,6 +476,15 @@ dependencies = [
 ]
 
 [[package]]
+name = "temp-env"
+version = "0.3.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e010429b1f3ea1311190c658c7570100f03c1dab05c16cfab774181c648d656a"
+dependencies = [
+ "parking_lot",
+]
+
+[[package]]
 name = "tempfile"
 version = "3.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/pkgs/test/nixpkgs-check-by-name/Cargo.toml b/pkgs/test/nixpkgs-check-by-name/Cargo.toml
index 003aab6d584..70b44d04820 100644
--- a/pkgs/test/nixpkgs-check-by-name/Cargo.toml
+++ b/pkgs/test/nixpkgs-check-by-name/Cargo.toml
@@ -5,7 +5,6 @@ edition = "2021"
 
 [dependencies]
 rnix = "0.11.0"
-rowan = "0.15.0"
 regex = "1.9.3"
 clap = { version = "4.3.23", features = ["derive"] }
 serde_json = "1.0.105"
@@ -14,3 +13,6 @@ serde = { version = "1.0.185", features = ["derive"] }
 anyhow = "1.0"
 lazy_static = "1.4.0"
 colored = "2.0.4"
+
+[dev-dependencies]
+temp-env = "0.3.5"
diff --git a/pkgs/test/nixpkgs-check-by-name/default.nix b/pkgs/test/nixpkgs-check-by-name/default.nix
index cb8a672bc99..0d4e2fcc48a 100644
--- a/pkgs/test/nixpkgs-check-by-name/default.nix
+++ b/pkgs/test/nixpkgs-check-by-name/default.nix
@@ -26,8 +26,8 @@ let
         export NIX_STATE_DIR=$TEST_ROOT/var/nix
         export NIX_STORE_DIR=$TEST_ROOT/store
 
-        # cargo tests run in parallel by default, which would then run into
-        # https://github.com/NixOS/nix/issues/2706 unless the store is initialised first
+        # Ensure that even if tests run in parallel, we don't get an error
+        # We'd run into https://github.com/NixOS/nix/issues/2706 unless the store is initialised first
         nix-store --init
       '';
       postCheck = ''
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);