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

tests.nixpkgs-check-by-name: Make tests thread safe
This commit is contained in:
Silvan Mosberger 2023-09-23 23:31:49 +02:00 committed by GitHub
commit 85d77bc124
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 27 deletions

View file

@ -242,6 +242,16 @@ version = "0.4.5"
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"
@ -269,6 +279,7 @@ dependencies = [
"rnix",
"serde",
"serde_json",
"temp-env",
"tempfile",
]
@ -278,6 +289,29 @@ version = "1.18.0"
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"
@ -381,6 +415,12 @@ version = "1.0.15"
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"
@ -412,6 +452,12 @@ dependencies = [
"serde",
]
[[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"
@ -429,6 +475,15 @@ dependencies = [
"unicode-ident",
]
[[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"

View file

@ -13,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"

View file

@ -30,9 +30,6 @@ let
# We'd run into https://github.com/NixOS/nix/issues/2706 unless the store is initialised first
nix-store --init
'';
# The tests use the shared environment variables,
# so we cannot run them in parallel
dontUseCargoParallelTests = true;
postCheck = ''
cargo fmt --check
cargo clippy -- -D warnings

View file

@ -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);