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 <adam@westernsemico.com>
Change-Id: Ib8bbafba2ac9ca0e1d3dc5e844167f94890d9fee
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6997
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
This commit is contained in:
Adam Joseph 2022-10-12 23:58:53 -07:00
parent a98cb9d56f
commit a545f21cc0
3 changed files with 16 additions and 0 deletions

View file

@ -100,6 +100,11 @@ fn pure_builtins() -> Vec<Builtin> {
Ok(Value::List(NixList::construct(output.len(), output)))
}),
Builtin::new("baseNameOf", &[true], |args: Vec<Value>, 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<Value>, _: &mut VM| {
Ok(Value::Integer(args[0].as_int()? & args[1].as_int()?))
}),

View file

@ -0,0 +1 @@
[ "bar" "foo" "" "bar" "." "" "" "" ]

View file

@ -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 /.))
]