···30 # We'd run into https://github.com/NixOS/nix/issues/2706 unless the store is initialised first
31 nix-store --init
32 '';
33- # The tests use the shared environment variables,
34- # so we cannot run them in parallel
35- dontUseCargoParallelTests = true;
36 postCheck = ''
37 cargo fmt --check
38 cargo clippy -- -D warnings
···30 # We'd run into https://github.com/NixOS/nix/issues/2706 unless the store is initialised first
31 nix-store --init
32 '';
00033 postCheck = ''
34 cargo fmt --check
35 cargo clippy -- -D warnings
+17-24
pkgs/test/nixpkgs-check-by-name/src/main.rs
···87 use crate::check_nixpkgs;
88 use crate::structure;
89 use anyhow::Context;
90- use std::env;
91 use std::fs;
92 use std::path::Path;
93- use tempfile::{tempdir, tempdir_in};
9495 #[test]
96 fn tests_dir() -> anyhow::Result<()> {
···109 test_nixpkgs(&name, &path, &expected_errors)?;
110 }
111 Ok(())
0000000112 }
113114 // We cannot check case-conflicting files into Nixpkgs (the channel would fail to
···157 std::os::unix::fs::symlink("actual", temp_root.path().join("symlinked"))?;
158 let tmpdir = temp_root.path().join("symlinked");
159160- // Then set TMPDIR to the symlinked directory
161- // Make sure to persist the old value so we can undo this later
162- let old_tmpdir = env::var("TMPDIR").ok();
163- env::set_var("TMPDIR", &tmpdir);
164-165- // Then run a simple test with this symlinked temporary directory
166- // This should be successful
167- test_nixpkgs("symlinked_tmpdir", Path::new("tests/success"), "")?;
168-169- // Undo the env variable change
170- if let Some(old) = old_tmpdir {
171- env::set_var("TMPDIR", old);
172- } else {
173- env::remove_var("TMPDIR");
174- }
175-176- Ok(())
177 }
178179 fn test_nixpkgs(name: &str, path: &Path, expected_errors: &str) -> anyhow::Result<()> {
180 let extra_nix_path = Path::new("tests/mock-nixpkgs.nix");
181182 // We don't want coloring to mess up the tests
183- env::set_var("NO_COLOR", "1");
184-185- let mut writer = vec![];
186- check_nixpkgs(&path, vec![&extra_nix_path], &mut writer)
187- .context(format!("Failed test case {name}"))?;
0188189 let actual_errors = String::from_utf8_lossy(&writer);
190
···87 use crate::check_nixpkgs;
88 use crate::structure;
89 use anyhow::Context;
090 use std::fs;
91 use std::path::Path;
92+ use tempfile::{tempdir_in, TempDir};
9394 #[test]
95 fn tests_dir() -> anyhow::Result<()> {
···108 test_nixpkgs(&name, &path, &expected_errors)?;
109 }
110 Ok(())
111+ }
112+113+ // tempfile::tempdir needs to be wrapped in temp_env lock
114+ // because it accesses TMPDIR environment variable.
115+ fn tempdir() -> anyhow::Result<TempDir> {
116+ let empty_list: [(&str, Option<&str>); 0] = [];
117+ Ok(temp_env::with_vars(empty_list, tempfile::tempdir)?)
118 }
119120 // We cannot check case-conflicting files into Nixpkgs (the channel would fail to
···163 std::os::unix::fs::symlink("actual", temp_root.path().join("symlinked"))?;
164 let tmpdir = temp_root.path().join("symlinked");
165166+ temp_env::with_var("TMPDIR", Some(&tmpdir), || {
167+ test_nixpkgs("symlinked_tmpdir", Path::new("tests/success"), "")
168+ })
00000000000000169 }
170171 fn test_nixpkgs(name: &str, path: &Path, expected_errors: &str) -> anyhow::Result<()> {
172 let extra_nix_path = Path::new("tests/mock-nixpkgs.nix");
173174 // We don't want coloring to mess up the tests
175+ let writer = temp_env::with_var("NO_COLOR", Some("1"), || -> anyhow::Result<_> {
176+ let mut writer = vec![];
177+ check_nixpkgs(&path, vec![&extra_nix_path], &mut writer)
178+ .context(format!("Failed test case {name}"))?;
179+ Ok(writer)
180+ })?;
181182 let actual_errors = String::from_utf8_lossy(&writer);
183