Skip to content

Commit

Permalink
Fix tests for nixVersions.minimum
Browse files Browse the repository at this point in the history
This is important because that's the Nix version that should really be used in CI
  • Loading branch information
infinisil committed Jul 19, 2024
1 parent c208c3e commit 5e3dee7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 1 addition & 0 deletions package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
nixVersionsToTest ? [
nix
nixVersions.stable
nixVersions.minimum
],

nixpkgsLibPath,
Expand Down
34 changes: 27 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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")?;
Expand All @@ -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=======",
Expand Down
18 changes: 1 addition & 17 deletions tests/by-name-failure/expected
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 5e3dee7

Please sign in to comment.