From caf9cbf71147054c01c6fdf49773d646cbfb46eb Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Wed, 12 Oct 2022 02:46:20 -0700 Subject: feat(tvix/eval): implement builtins.dirOf This commit causes the test eval-okay-builtins.nix to pass. It also adds tests/tvix_tests/eval-okay-dirof.nix which has better coverage than the nix tests for this builtin. Signed-off-by: Adam Joseph Change-Id: I71d96b48680696fd6e4fea3a9861742b35cfaa66 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6987 Reviewed-by: tazjin Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 15 +++++++++++++++ tvix/eval/src/tests/tvix_tests/eval-okay-dirof.exp | 1 + tvix/eval/src/tests/tvix_tests/eval-okay-dirof.nix | 10 ++++++++++ tvix/eval/src/value/mod.rs | 1 + 4 files changed, 27 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-dirof.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-dirof.nix (limited to 'tvix') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 050481eb7b..d767d09b99 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -194,6 +194,21 @@ fn pure_builtins() -> Vec { &[false, false], |args: Vec, vm: &mut VM| arithmetic_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, /), ), + Builtin::new("dirOf", &[true], |args: Vec, vm: &mut VM| { + let s = args[0].coerce_to_string(CoercionKind::Weak, vm)?; + let result = s + .rsplit_once('/') + .map(|(x, _)| match x { + "" => "/", + _ => x, + }) + .unwrap_or("."); + if args[0].is_path() { + Ok(Value::Path(result.into())) + } else { + Ok(result.into()) + } + }), Builtin::new("elem", &[true, true], |args: Vec, vm: &mut VM| { for val in args[1].to_list()? { if val.nix_eq(&args[0], vm)? { diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-dirof.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-dirof.exp new file mode 100644 index 0000000000..ff464e4c30 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-dirof.exp @@ -0,0 +1 @@ +[ /foo "." "foo//" "foo" "." "." / "/" ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-dirof.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-dirof.nix new file mode 100644 index 0000000000..13cf473205 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-dirof.nix @@ -0,0 +1,10 @@ +[ + (builtins.dirOf /foo/bar) + (builtins.dirOf "foo") + (builtins.dirOf "foo///") + (builtins.dirOf "foo/bar") + (builtins.dirOf "./.") + (builtins.dirOf "") + (builtins.dirOf /.) + (builtins.toString (builtins.dirOf /.)) +] diff --git a/tvix/eval/src/value/mod.rs b/tvix/eval/src/value/mod.rs index c8c8a54a40..8672ffc1bb 100644 --- a/tvix/eval/src/value/mod.rs +++ b/tvix/eval/src/value/mod.rs @@ -276,6 +276,7 @@ impl Value { gen_cast!(to_list, NixList, "list", Value::List(l), l.clone()); gen_cast!(to_closure, Closure, "lambda", Value::Closure(c), c.clone()); + gen_is!(is_path, Value::Path(_)); gen_is!(is_number, Value::Integer(_) | Value::Float(_)); gen_is!(is_bool, Value::Bool(_)); -- cgit 1.4.1