From 077bf06c1d429584422437d920c91a102b6bdc59 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sat, 21 Oct 2023 17:51:39 +0100 Subject: refactor(tvix/cli): move evaluator instantiation to helper Have a private `eval` function in the test module that returns an EvaluationResult, and migrate the existing tests over to use it, rather than repeating itself. Change-Id: I879987700c8507248c644ef03b62a8cb8e308139 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9816 Tested-by: BuildkiteCI Reviewed-by: raitobezarius --- tvix/cli/src/derivation.rs | 59 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/tvix/cli/src/derivation.rs b/tvix/cli/src/derivation.rs index 15df6ed76102..f1747a52ca60 100644 --- a/tvix/cli/src/derivation.rs +++ b/tvix/cli/src/derivation.rs @@ -470,13 +470,13 @@ mod tests { use crate::known_paths::KnownPaths; use nix_compat::store_path::hash_placeholder; use std::{cell::RefCell, rc::Rc}; + use tvix_eval::EvaluationResult; - #[test] - fn derivation() { - let mut eval = tvix_eval::Evaluation::new_impure( - r#"(derivation { name = "foo"; builder = "/bin/sh"; system = "x86_64-linux";}).outPath"#, - None, - ); + /// evaluates a given nix expression and returns the result. + /// Takes care of setting up the evaluator so it knows about the + // `derivation` builtin. + fn eval(str: &str) -> EvaluationResult { + let mut eval = tvix_eval::Evaluation::new_impure(str, None); let known_paths: Rc> = Default::default(); @@ -484,42 +484,41 @@ mod tests { .extend(crate::derivation::derivation_builtins(known_paths)); // Add the actual `builtins.derivation` from compiled Nix code - // TODO: properly compose this eval.src_builtins .push(("derivation", include_str!("derivation.nix"))); - let result = eval.evaluate(); + // run the evaluation itself. + eval.evaluate() + } + + #[test] + fn derivation() { + let result = eval( + r#"(derivation { name = "foo"; builder = "/bin/sh"; system = "x86_64-linux";}).outPath"#, + ); assert!(result.errors.is_empty(), "expect evaluation to succeed"); let value = result.value.expect("must be some"); - // TODO: test this more reliably, derive Eq? - assert_eq!( - "\"/nix/store/xpcvxsx5sw4rbq666blz6sxqlmsqphmr-foo\"", - value.to_string() - ); + + match value { + tvix_eval::Value::String(s) => { + assert_eq!( + "/nix/store/xpcvxsx5sw4rbq666blz6sxqlmsqphmr-foo", + s.as_str() + ); + } + _ => panic!("unexpected value type: {:?}", value), + } } + /// a derivation with an empty name is an error. #[test] - fn derivation_empty_name() { - let mut eval = tvix_eval::Evaluation::new_impure( + fn derivation_empty_name_fail() { + let result = eval( r#"(derivation { name = ""; builder = "/bin/sh"; system = "x86_64-linux";}).outPath"#, - None, ); - let known_paths: Rc> = Default::default(); - - eval.builtins - .extend(crate::derivation::derivation_builtins(known_paths)); - - // Add the actual `builtins.derivation` from compiled Nix code - // TODO: properly compose this - eval.src_builtins - .push(("derivation", include_str!("derivation.nix"))); - - assert!( - !eval.evaluate().errors.is_empty(), - "expect evaluation to fail" - ); + assert!(!result.errors.is_empty(), "expect evaluation to fail"); } // TODO: These tests are commented out because we do not have -- cgit 1.4.1