mirror of
https://github.com/NixOS/nix.git
synced 2024-11-25 08:12:29 +00:00
Test and document builtins.baseNameOf
This commit is contained in:
parent
38ba96d7b0
commit
175afc7106
@ -1580,11 +1580,18 @@ static void prim_baseNameOf(EvalState & state, const PosIdx pos, Value * * args,
|
|||||||
|
|
||||||
static RegisterPrimOp primop_baseNameOf({
|
static RegisterPrimOp primop_baseNameOf({
|
||||||
.name = "baseNameOf",
|
.name = "baseNameOf",
|
||||||
.args = {"s"},
|
.args = {"x"},
|
||||||
.doc = R"(
|
.doc = R"(
|
||||||
Return the *base name* of the string *s*, that is, everything
|
Return the *base name* of either a [path value](@docroot@/language/values.md#type-path) *x* or a string *x*, depending on which type is passed, and according to the following rules.
|
||||||
following the final slash in the string. This is similar to the GNU
|
|
||||||
`basename` command.
|
For a path value, the *base name* is considered to be the part of the path after the last directory separator, including any file extensions.
|
||||||
|
This is the simple case, as path values don't have trailing slashes.
|
||||||
|
|
||||||
|
When the argument is a string, a more involved logic applies. If the string ends with a `/`, only this one final slash is removed.
|
||||||
|
|
||||||
|
After this, the *base name* is returned as previously described, assuming `/` as the directory separator. (Note that evaluation must be platform independent.)
|
||||||
|
|
||||||
|
This is somewhat similar to the [GNU `basename`](https://www.gnu.org/software/coreutils/manual/html_node/basename-invocation.html) command, but GNU `basename` will strip any number of trailing slashes.
|
||||||
)",
|
)",
|
||||||
.fun = prim_baseNameOf,
|
.fun = prim_baseNameOf,
|
||||||
});
|
});
|
||||||
|
1
tests/functional/lang/eval-okay-baseNameOf.exp
Normal file
1
tests/functional/lang/eval-okay-baseNameOf.exp
Normal file
@ -0,0 +1 @@
|
|||||||
|
"ok"
|
32
tests/functional/lang/eval-okay-baseNameOf.nix
Normal file
32
tests/functional/lang/eval-okay-baseNameOf.nix
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
assert baseNameOf "" == "";
|
||||||
|
assert baseNameOf "." == ".";
|
||||||
|
assert baseNameOf ".." == "..";
|
||||||
|
assert baseNameOf "a" == "a";
|
||||||
|
assert baseNameOf "a." == "a.";
|
||||||
|
assert baseNameOf "a.." == "a..";
|
||||||
|
assert baseNameOf "a.b" == "a.b";
|
||||||
|
assert baseNameOf "a.b." == "a.b.";
|
||||||
|
assert baseNameOf "a.b.." == "a.b..";
|
||||||
|
assert baseNameOf "a/" == "a";
|
||||||
|
assert baseNameOf "a/." == ".";
|
||||||
|
assert baseNameOf "a/.." == "..";
|
||||||
|
assert baseNameOf "a/b" == "b";
|
||||||
|
assert baseNameOf "a/b." == "b.";
|
||||||
|
assert baseNameOf "a/b.." == "b..";
|
||||||
|
assert baseNameOf "a/b/c" == "c";
|
||||||
|
assert baseNameOf "a/b/c." == "c.";
|
||||||
|
assert baseNameOf "a/b/c.." == "c..";
|
||||||
|
assert baseNameOf "a/b/c/d" == "d";
|
||||||
|
assert baseNameOf "a/b/c/d." == "d.";
|
||||||
|
assert baseNameOf "a\\b" == "a\\b";
|
||||||
|
assert baseNameOf "C:a" == "C:a";
|
||||||
|
assert baseNameOf "a//b" == "b";
|
||||||
|
|
||||||
|
# It's been like this for close to a decade. We ought to commit to it.
|
||||||
|
# https://github.com/NixOS/nix/pull/582#issuecomment-121014450
|
||||||
|
assert baseNameOf "a//" == "";
|
||||||
|
|
||||||
|
assert baseNameOf ./foo == "foo";
|
||||||
|
assert baseNameOf ./foo/bar == "bar";
|
||||||
|
|
||||||
|
"ok"
|
Loading…
Reference in New Issue
Block a user