about summary refs log tree commit diff
path: root/tvix
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-10-21T16·51+0100
committerflokli <flokli@flokli.de>2023-10-23T14·57+0000
commit077bf06c1d429584422437d920c91a102b6bdc59 (patch)
tree7f46e9a5a19753a14d4dc481d4330c9f72d2616a /tvix
parent6fe76848323d99129da415d63dd9880d6d580f15 (diff)
refactor(tvix/cli): move evaluator instantiation to helper r/6873
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>
Diffstat (limited to 'tvix')
-rw-r--r--tvix/cli/src/derivation.rs59
1 files 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<RefCell<KnownPaths>> = 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<RefCell<KnownPaths>> = 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