diff options
author | Ryan Lahfa <tvl@lahfa.xyz> | 2024-01-14T15·31+0100 |
---|---|---|
committer | raitobezarius <tvl@lahfa.xyz> | 2024-01-17T07·34+0000 |
commit | bc8fb825c7e1e933e2b1ace21c305433e0c57522 (patch) | |
tree | 40eae4a30b2d818f609d6aafa0d9e123ef39fda0 /tvix/eval | |
parent | 850a4bfc7b93a802518128755bdbb4aa7a5eed9c (diff) |
feat(tvix/eval): move away from `test_generator` to `rstest` r/7398
`test-generator` has not been updated in the past 2 years. `rstest` has not been updated in the past 5 months. This is an improvement in the maintenance state… I guess? We get also new features, it changes the name of the tests with numbers too. Change-Id: I5376104c7704f525dba7524da78daa09867cc669 Reviewed-on: https://cl.tvl.fyi/c/depot/+/10623 Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/eval')
-rw-r--r-- | tvix/eval/Cargo.toml | 7 | ||||
-rw-r--r-- | tvix/eval/src/tests/mod.rs | 71 |
2 files changed, 44 insertions, 34 deletions
diff --git a/tvix/eval/Cargo.toml b/tvix/eval/Cargo.toml index 2c8bb206acdd..d1bb48a7c512 100644 --- a/tvix/eval/Cargo.toml +++ b/tvix/eval/Cargo.toml @@ -35,14 +35,9 @@ xml-rs = "0.8.4" criterion = "0.5" itertools = "0.12.0" pretty_assertions = "1.2.1" +rstest = "0.18.2" tempfile = "3.3.0" -[dev-dependencies.test-generator] -# This fork of test-generator adds support for cargo workspaces, see -# also https://github.com/frehberg/test-generator/pull/14 -git = "https://github.com/JamesGuthrie/test-generator.git" -rev = "82e799979980962aec1aa324ec6e0e4cad781f41" - [features] default = ["impure", "arbitrary", "nix_tests"] diff --git a/tvix/eval/src/tests/mod.rs b/tvix/eval/src/tests/mod.rs index af0a160f8917..2dceeadc7ae9 100644 --- a/tvix/eval/src/tests/mod.rs +++ b/tvix/eval/src/tests/mod.rs @@ -1,7 +1,8 @@ use crate::value::Value; use builtin_macros::builtins; use pretty_assertions::assert_eq; -use test_generator::test_resources; +use rstest::rstest; +use std::path::PathBuf; /// Module for one-off tests which do not follow the rest of the /// test layout. @@ -38,14 +39,17 @@ mod mock_builtins { } } -fn eval_test(code_path: &str, expect_success: bool) { - let base = code_path - .strip_suffix("nix") - .expect("test files always end in .nix"); - let exp_path = format!("{}exp", base); - let exp_xml_path = std::path::PathBuf::from(format!("{}exp.xml", base)); +fn eval_test(code_path: PathBuf, expect_success: bool) { + eprintln!("path: {}", code_path.display()); + assert_eq!( + code_path.extension().unwrap(), + "nix", + "test files always end in .nix" + ); + let exp_path = code_path.with_extension("exp"); + let exp_xml_path = code_path.with_extension("exp.xml"); - let code = std::fs::read_to_string(code_path).expect("should be able to read test code"); + let code = std::fs::read_to_string(&code_path).expect("should be able to read test code"); if exp_xml_path.exists() { // We can't test them at the moment because we don't have XML output yet. @@ -57,14 +61,15 @@ fn eval_test(code_path: &str, expect_success: bool) { eval.strict = true; eval.builtins.extend(mock_builtins::builtins()); - let result = eval.evaluate(code, Some(code_path.into())); + let result = eval.evaluate(code, Some(code_path.clone())); let failed = match result.value { Some(Value::Catchable(_)) => true, _ => !result.errors.is_empty(), }; if expect_success && failed { panic!( - "{code_path}: evaluation of eval-okay test should succeed, but failed with {:?}", + "{}: evaluation of eval-okay test should succeed, but failed with {:?}", + code_path.display(), result.errors, ); } @@ -81,20 +86,26 @@ fn eval_test(code_path: &str, expect_success: bool) { assert_eq!( result_str, exp.trim(), - "{code_path}: result value representation (left) must match expectation (right)" + "{}: result value representation (left) must match expectation (right)", + code_path.display() ); } else { assert_ne!( result_str, exp.trim(), - "{code_path}: test passed unexpectedly! consider moving it out of notyetpassing" + "{}: test passed unexpectedly! consider moving it out of notyetpassing", + code_path.display() ); } } else if expect_success { - panic!("{code_path}: should be able to read test expectation"); + panic!( + "{}: should be able to read test expectation", + code_path.display() + ); } else { panic!( - "{code_path}: test should have failed, but succeeded with output {}", + "{}: test should have failed, but succeeded with output {}", + code_path.display(), result_str ); } @@ -102,8 +113,8 @@ fn eval_test(code_path: &str, expect_success: bool) { // identity-* tests contain Nix code snippets which should evaluate to // themselves exactly (i.e. literals). -#[test_resources("src/tests/tvix_tests/identity-*.nix")] -fn identity(code_path: &str) { +#[rstest] +fn identity(#[files("src/tests/tvix_tests/identity-*.nix")] code_path: PathBuf) { let code = std::fs::read_to_string(code_path).expect("should be able to read test code"); let eval = crate::Evaluation { @@ -133,15 +144,15 @@ fn identity(code_path: &str) { // // These evaluations are always supposed to succeed, i.e. all snippets // are guaranteed to be valid Nix code. -#[test_resources("src/tests/tvix_tests/eval-okay-*.nix")] -fn eval_okay(code_path: &str) { +#[rstest] +fn eval_okay(#[files("src/tests/tvix_tests/eval-okay-*.nix")] code_path: PathBuf) { eval_test(code_path, true) } // eval-okay-* tests from the original Nix test suite. #[cfg(feature = "nix_tests")] -#[test_resources("src/tests/nix_tests/eval-okay-*.nix")] -fn nix_eval_okay(code_path: &str) { +#[rstest] +fn nix_eval_okay(#[files("src/tests/nix_tests/eval-okay-*.nix")] code_path: PathBuf) { eval_test(code_path, true) } @@ -163,27 +174,31 @@ fn nix_eval_okay(code_path: &str) { // // https://github.com/frehberg/test-generator/pull/10 // https://github.com/frehberg/test-generator/pull/8 -#[test_resources("src/tests/nix_tests/notyetpassing/eval-okay-*.nix")] -fn nix_eval_okay_currently_failing(code_path: &str) { +#[rstest] +fn nix_eval_okay_currently_failing( + #[files("src/tests/nix_tests/notyetpassing/eval-okay-*.nix")] code_path: PathBuf, +) { eval_test(code_path, false) } -#[test_resources("src/tests/tvix_tests/notyetpassing/eval-okay-*.nix")] -fn eval_okay_currently_failing(code_path: &str) { +#[rstest] +fn eval_okay_currently_failing( + #[files("src/tests/tvix_tests/notyetpassing/eval-okay-*.nix")] code_path: PathBuf, +) { eval_test(code_path, false) } // eval-fail-* tests contain a snippet of Nix code, which is // expected to fail evaluation. The exact type of failure // (assertion, parse error, etc) is not currently checked. -#[test_resources("src/tests/tvix_tests/eval-fail-*.nix")] -fn eval_fail(code_path: &str) { +#[rstest] +fn eval_fail(#[files("src/tests/tvix_tests/eval-fail-*.nix")] code_path: PathBuf) { eval_test(code_path, false) } // eval-fail-* tests from the original Nix test suite. #[cfg(feature = "nix_tests")] -#[test_resources("src/tests/nix_tests/eval-fail-*.nix")] -fn nix_eval_fail(code_path: &str) { +#[rstest] +fn nix_eval_fail(#[files("src/tests/nix_tests/eval-fail-*.nix")] code_path: PathBuf) { eval_test(code_path, false) } |