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 <tvl@lahfa.xyz>
This commit is contained in:
parent
6fe7684832
commit
077bf06c1d
1 changed files with 32 additions and 33 deletions
|
@ -470,13 +470,13 @@ mod tests {
|
||||||
use crate::known_paths::KnownPaths;
|
use crate::known_paths::KnownPaths;
|
||||||
use nix_compat::store_path::hash_placeholder;
|
use nix_compat::store_path::hash_placeholder;
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
use tvix_eval::EvaluationResult;
|
||||||
|
|
||||||
#[test]
|
/// evaluates a given nix expression and returns the result.
|
||||||
fn derivation() {
|
/// Takes care of setting up the evaluator so it knows about the
|
||||||
let mut eval = tvix_eval::Evaluation::new_impure(
|
// `derivation` builtin.
|
||||||
r#"(derivation { name = "foo"; builder = "/bin/sh"; system = "x86_64-linux";}).outPath"#,
|
fn eval(str: &str) -> EvaluationResult {
|
||||||
None,
|
let mut eval = tvix_eval::Evaluation::new_impure(str, None);
|
||||||
);
|
|
||||||
|
|
||||||
let known_paths: Rc<RefCell<KnownPaths>> = Default::default();
|
let known_paths: Rc<RefCell<KnownPaths>> = Default::default();
|
||||||
|
|
||||||
|
@ -484,42 +484,41 @@ mod tests {
|
||||||
.extend(crate::derivation::derivation_builtins(known_paths));
|
.extend(crate::derivation::derivation_builtins(known_paths));
|
||||||
|
|
||||||
// Add the actual `builtins.derivation` from compiled Nix code
|
// Add the actual `builtins.derivation` from compiled Nix code
|
||||||
// TODO: properly compose this
|
|
||||||
eval.src_builtins
|
eval.src_builtins
|
||||||
.push(("derivation", include_str!("derivation.nix")));
|
.push(("derivation", include_str!("derivation.nix")));
|
||||||
|
|
||||||
let result = eval.evaluate();
|
// run the evaluation itself.
|
||||||
|
eval.evaluate()
|
||||||
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()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn derivation_empty_name() {
|
fn derivation() {
|
||||||
let mut eval = tvix_eval::Evaluation::new_impure(
|
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");
|
||||||
|
|
||||||
|
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_fail() {
|
||||||
|
let result = eval(
|
||||||
r#"(derivation { name = ""; builder = "/bin/sh"; system = "x86_64-linux";}).outPath"#,
|
r#"(derivation { name = ""; builder = "/bin/sh"; system = "x86_64-linux";}).outPath"#,
|
||||||
None,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let known_paths: Rc<RefCell<KnownPaths>> = Default::default();
|
assert!(!result.errors.is_empty(), "expect evaluation to fail");
|
||||||
|
|
||||||
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"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: These tests are commented out because we do not have
|
// TODO: These tests are commented out because we do not have
|
||||||
|
|
Loading…
Add table
Reference in a new issue