Merge pull request #256913 from xfix/make-nixpkgs-check-by-name-tests-thread-safe

tests.nixpkgs-check-by-name: Make tests thread safe

authored by Silvan Mosberger and committed by GitHub 85d77bc1 71f9fd4e

+75 -27
+55
pkgs/test/nixpkgs-check-by-name/Cargo.lock
··· 243 243 checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503" 244 244 245 245 [[package]] 246 + name = "lock_api" 247 + version = "0.4.10" 248 + source = "registry+https://github.com/rust-lang/crates.io-index" 249 + checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16" 250 + dependencies = [ 251 + "autocfg", 252 + "scopeguard", 253 + ] 254 + 255 + [[package]] 246 256 name = "memchr" 247 257 version = "2.5.0" 248 258 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 269 279 "rnix", 270 280 "serde", 271 281 "serde_json", 282 + "temp-env", 272 283 "tempfile", 273 284 ] 274 285 ··· 279 290 checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" 280 291 281 292 [[package]] 293 + name = "parking_lot" 294 + version = "0.12.1" 295 + source = "registry+https://github.com/rust-lang/crates.io-index" 296 + checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" 297 + dependencies = [ 298 + "lock_api", 299 + "parking_lot_core", 300 + ] 301 + 302 + [[package]] 303 + name = "parking_lot_core" 304 + version = "0.9.8" 305 + source = "registry+https://github.com/rust-lang/crates.io-index" 306 + checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447" 307 + dependencies = [ 308 + "cfg-if", 309 + "libc", 310 + "redox_syscall", 311 + "smallvec", 312 + "windows-targets", 313 + ] 314 + 315 + [[package]] 282 316 name = "proc-macro2" 283 317 version = "1.0.66" 284 318 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 382 416 checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" 383 417 384 418 [[package]] 419 + name = "scopeguard" 420 + version = "1.2.0" 421 + source = "registry+https://github.com/rust-lang/crates.io-index" 422 + checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" 423 + 424 + [[package]] 385 425 name = "serde" 386 426 version = "1.0.186" 387 427 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 413 453 ] 414 454 415 455 [[package]] 456 + name = "smallvec" 457 + version = "1.11.1" 458 + source = "registry+https://github.com/rust-lang/crates.io-index" 459 + checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" 460 + 461 + [[package]] 416 462 name = "strsim" 417 463 version = "0.10.0" 418 464 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 427 473 "proc-macro2", 428 474 "quote", 429 475 "unicode-ident", 476 + ] 477 + 478 + [[package]] 479 + name = "temp-env" 480 + version = "0.3.5" 481 + source = "registry+https://github.com/rust-lang/crates.io-index" 482 + checksum = "e010429b1f3ea1311190c658c7570100f03c1dab05c16cfab774181c648d656a" 483 + dependencies = [ 484 + "parking_lot", 430 485 ] 431 486 432 487 [[package]]
+3
pkgs/test/nixpkgs-check-by-name/Cargo.toml
··· 13 13 anyhow = "1.0" 14 14 lazy_static = "1.4.0" 15 15 colored = "2.0.4" 16 + 17 + [dev-dependencies] 18 + temp-env = "0.3.5"
-3
pkgs/test/nixpkgs-check-by-name/default.nix
··· 30 30 # We'd run into https://github.com/NixOS/nix/issues/2706 unless the store is initialised first 31 31 nix-store --init 32 32 ''; 33 - # The tests use the shared environment variables, 34 - # so we cannot run them in parallel 35 - dontUseCargoParallelTests = true; 36 33 postCheck = '' 37 34 cargo fmt --check 38 35 cargo clippy -- -D warnings
+17 -24
pkgs/test/nixpkgs-check-by-name/src/main.rs
··· 87 87 use crate::check_nixpkgs; 88 88 use crate::structure; 89 89 use anyhow::Context; 90 - use std::env; 91 90 use std::fs; 92 91 use std::path::Path; 93 - use tempfile::{tempdir, tempdir_in}; 92 + use tempfile::{tempdir_in, TempDir}; 94 93 95 94 #[test] 96 95 fn tests_dir() -> anyhow::Result<()> { ··· 109 108 test_nixpkgs(&name, &path, &expected_errors)?; 110 109 } 111 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)?) 112 118 } 113 119 114 120 // We cannot check case-conflicting files into Nixpkgs (the channel would fail to ··· 157 163 std::os::unix::fs::symlink("actual", temp_root.path().join("symlinked"))?; 158 164 let tmpdir = temp_root.path().join("symlinked"); 159 165 160 - // 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(()) 166 + temp_env::with_var("TMPDIR", Some(&tmpdir), || { 167 + test_nixpkgs("symlinked_tmpdir", Path::new("tests/success"), "") 168 + }) 177 169 } 178 170 179 171 fn test_nixpkgs(name: &str, path: &Path, expected_errors: &str) -> anyhow::Result<()> { 180 172 let extra_nix_path = Path::new("tests/mock-nixpkgs.nix"); 181 173 182 174 // 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}"))?; 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 + })?; 188 181 189 182 let actual_errors = String::from_utf8_lossy(&writer); 190 183