From 5e3dee70a76c77e270c3d173203a4b43377940c0 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 19 Jul 2024 22:46:43 +0200 Subject: [PATCH] Fix tests for nixVersions.minimum This is important because that's the Nix version that should really be used in CI --- CONTRIBUTING.md | 4 ++++ package.nix | 1 + src/main.rs | 34 +++++++++++++++++++++++++++------- tests/by-name-failure/expected | 18 +----------------- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e67fdb8..2b47d54 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -61,6 +61,10 @@ Integration tests are declared in [`./tests`](./tests) as subdirectories imitati A file containing the expected standard output. The default is expecting an empty standard output. + This file is matched against the error almost literally, + with the only exception being that the `@REDACTED@` string can match anything, + which is useful for non-deterministic errors. + ## Automation Pinned dependencies are [regularly updated automatically](./.github/workflows/update.yml). diff --git a/package.nix b/package.nix index 37ff64c..b37d2f2 100644 --- a/package.nix +++ b/package.nix @@ -9,6 +9,7 @@ nixVersionsToTest ? [ nix nixVersions.stable + nixVersions.minimum ], nixpkgsLibPath, diff --git a/src/main.rs b/src/main.rs index 870e7dc..33111f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -181,6 +181,7 @@ mod tests { use crate::process; use crate::utils; use anyhow::Context; + use std::ffi::OsStr; use std::fs; use std::path::Path; use tempfile::{tempdir_in, TempDir}; @@ -277,14 +278,24 @@ mod tests { } else { Path::new("tests/empty-base") }; + // Empty dir, needed so that no warnings are printed when testing older Nix versions + // that don't recognise certain newer keys in nix.conf + let nix_conf_dir = tempdir()?; // We don't want coloring to mess up the tests - let writer = temp_env::with_var("NO_COLOR", Some("1"), || -> anyhow::Result<_> { - let mut writer = vec![]; - process(base_nixpkgs.to_owned(), path.to_owned(), true, &mut writer) - .with_context(|| format!("Failed test case {name}"))?; - Ok(writer) - })?; + let writer = temp_env::with_vars( + [ + ("NO_COLOR", Some(OsStr::new("1"))), + // See above comment on nix_conf_dir + ("NIX_CONF_DIR", Some(nix_conf_dir.path().as_os_str())), + ], + || -> anyhow::Result<_> { + let mut writer = vec![]; + process(base_nixpkgs.to_owned(), path.to_owned(), true, &mut writer) + .with_context(|| format!("Failed test case {name}"))?; + Ok(writer) + }, + )?; let expr_path = std::env::var("NIX_CHECK_BY_NAME_EXPR_PATH") .with_context(|| "Could not get environment variable NIX_CHECK_BY_NAME_EXPR_PATH")?; @@ -293,7 +304,16 @@ mod tests { // on the output, which we need to relativise for the tests to succeed everywhere let actual_errors = String::from_utf8_lossy(&writer).replace(&expr_path, "src/eval.nix"); - if actual_errors != expected_errors { + let pattern = format!( + "^{}$", + regex::escape(expected_errors).replace("@REDACTED@", ".*") + ); + + let expected_errors_regex = regex::RegexBuilder::new(&pattern) + .dot_matches_new_line(true) + .build()?; + + if !expected_errors_regex.is_match(&actual_errors) { panic!( "Failed test case {name}, expected these errors:\n=======\n{}\n=======\n\ but got these:\n=======\n{}\n=======", diff --git a/tests/by-name-failure/expected b/tests/by-name-failure/expected index 97c5e98..e76e502 100644 --- a/tests/by-name-failure/expected +++ b/tests/by-name-failure/expected @@ -1,20 +1,4 @@ trace: This should be on stderr! -error: - … while evaluating list element at index 3 - - … while evaluating list element at index 1 - - … while evaluating attribute 'ByName' - - at src/eval.nix:76:7: - - 75| inherit name; - 76| value.ByName = - | ^ - 77| if !pkgs ? ${name} then - - (stack trace truncated; use '--show-trace' to show the full trace) - - error: This is an error! +@REDACTED@error: This is an error!@REDACTED@ - Nix evaluation failed for some package in `pkgs/by-name`, see error above This PR introduces the problems listed above. Please fix them before merging, otherwise the base branch would break.