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:
parent
a98cb9d56f
commit
a545f21cc0
3 changed files with 16 additions and 0 deletions
|
@ -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()?))
|
||||
}),
|
||||
|
|
1
tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.exp
Normal file
1
tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.exp
Normal file
|
@ -0,0 +1 @@
|
|||
[ "bar" "foo" "" "bar" "." "" "" "" ]
|
10
tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.nix
Normal file
10
tvix/eval/src/tests/tvix_tests/eval-okay-basenameof.nix
Normal 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 /.))
|
||||
]
|
Loading…
Reference in a new issue