From a545f21cc0ec279be5e128bcbb2ab822787579dc Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Wed, 12 Oct 2022 23:58:53 -0700 Subject: feat(tvix/eval): implement builtins.baseNameOf This commit implements builtins.baseNameOf and adds a test case eval-okay-basenameof.nix to the test suite. Signed-off-by: Adam Joseph Change-Id: Ib8bbafba2ac9ca0e1d3dc5e844167f94890d9fee Reviewed-on: https://cl.tvl.fyi/c/depot/+/6997 Tested-by: BuildkiteCI Reviewed-by: grfn --- tvix/eval/src/builtins/mod.rs | 5 +++++ tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.exp | 1 + tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.nix | 10 ++++++++++ 3 files changed, 16 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.nix (limited to 'tvix') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 0b19a89aa316..cabdaf0070c7 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -100,6 +100,11 @@ fn pure_builtins() -> Vec { Ok(Value::List(NixList::construct(output.len(), output))) }), + Builtin::new("baseNameOf", &[true], |args: Vec, vm: &mut VM| { + let s = args[0].coerce_to_string(CoercionKind::Weak, vm)?; + let result: String = s.rsplit_once('/').map(|(_, x)| x).unwrap_or(&s).into(); + Ok(result.into()) + }), Builtin::new("bitAnd", &[true, true], |args: Vec, _: &mut VM| { Ok(Value::Integer(args[0].as_int()? & args[1].as_int()?)) }), diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.exp new file mode 100644 index 000000000000..60a773f4af69 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.exp @@ -0,0 +1 @@ +[ "bar" "foo" "" "bar" "." "" "" "" ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.nix new file mode 100644 index 000000000000..bc59613f541d --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.nix @@ -0,0 +1,10 @@ +[ + (builtins.baseNameOf /foo/bar) + (builtins.baseNameOf "foo") + (builtins.baseNameOf "foo///") + (builtins.baseNameOf "foo/bar") + (builtins.baseNameOf "./.") + (builtins.baseNameOf "") + (builtins.baseNameOf /.) + (builtins.toString (builtins.baseNameOf /.)) +] -- cgit 1.4.1