···3030 # We'd run into https://github.com/NixOS/nix/issues/2706 unless the store is initialised first
3131 nix-store --init
3232 '';
3333- # The tests use the shared environment variables,
3434- # so we cannot run them in parallel
3535- dontUseCargoParallelTests = true;
3633 postCheck = ''
3734 cargo fmt --check
3835 cargo clippy -- -D warnings
+17-24
pkgs/test/nixpkgs-check-by-name/src/main.rs
···8787 use crate::check_nixpkgs;
8888 use crate::structure;
8989 use anyhow::Context;
9090- use std::env;
9190 use std::fs;
9291 use std::path::Path;
9393- use tempfile::{tempdir, tempdir_in};
9292+ use tempfile::{tempdir_in, TempDir};
94939594 #[test]
9695 fn tests_dir() -> anyhow::Result<()> {
···109108 test_nixpkgs(&name, &path, &expected_errors)?;
110109 }
111110 Ok(())
111111+ }
112112+113113+ // tempfile::tempdir needs to be wrapped in temp_env lock
114114+ // because it accesses TMPDIR environment variable.
115115+ fn tempdir() -> anyhow::Result<TempDir> {
116116+ let empty_list: [(&str, Option<&str>); 0] = [];
117117+ Ok(temp_env::with_vars(empty_list, tempfile::tempdir)?)
112118 }
113119114120 // We cannot check case-conflicting files into Nixpkgs (the channel would fail to
···157163 std::os::unix::fs::symlink("actual", temp_root.path().join("symlinked"))?;
158164 let tmpdir = temp_root.path().join("symlinked");
159165160160- // Then set TMPDIR to the symlinked directory
161161- // Make sure to persist the old value so we can undo this later
162162- let old_tmpdir = env::var("TMPDIR").ok();
163163- env::set_var("TMPDIR", &tmpdir);
164164-165165- // Then run a simple test with this symlinked temporary directory
166166- // This should be successful
167167- test_nixpkgs("symlinked_tmpdir", Path::new("tests/success"), "")?;
168168-169169- // Undo the env variable change
170170- if let Some(old) = old_tmpdir {
171171- env::set_var("TMPDIR", old);
172172- } else {
173173- env::remove_var("TMPDIR");
174174- }
175175-176176- Ok(())
166166+ temp_env::with_var("TMPDIR", Some(&tmpdir), || {
167167+ test_nixpkgs("symlinked_tmpdir", Path::new("tests/success"), "")
168168+ })
177169 }
178170179171 fn test_nixpkgs(name: &str, path: &Path, expected_errors: &str) -> anyhow::Result<()> {
180172 let extra_nix_path = Path::new("tests/mock-nixpkgs.nix");
181173182174 // We don't want coloring to mess up the tests
183183- env::set_var("NO_COLOR", "1");
184184-185185- let mut writer = vec![];
186186- check_nixpkgs(&path, vec![&extra_nix_path], &mut writer)
187187- .context(format!("Failed test case {name}"))?;
175175+ let writer = temp_env::with_var("NO_COLOR", Some("1"), || -> anyhow::Result<_> {
176176+ let mut writer = vec![];
177177+ check_nixpkgs(&path, vec![&extra_nix_path], &mut writer)
178178+ .context(format!("Failed test case {name}"))?;
179179+ Ok(writer)
180180+ })?;
188181189182 let actual_errors = String::from_utf8_lossy(&writer);
190183