# Unit tests for lib.path functions. Use `nix-build` in this directory to
# run these
{ libpath }:
let
lib = import libpath;
inherit (lib.path)
hasPrefix
removePrefix
append
splitRoot
hasStorePathPrefix
subpath
;
# This is not allowed generally, but we're in the tests here, so we'll allow ourselves.
storeDirPath = /. + builtins.storeDir;
cases = lib.runTests {
# Test examples from the lib.path.append documentation
testAppendExample1 = {
expr = append /foo "bar/baz";
expected = /foo/bar/baz;
};
testAppendExample2 = {
expr = append /foo "./bar//baz/./";
testAppendExample3 = {
expr = append /. "foo/bar";
expected = /foo/bar;
testAppendExample4 = {
expr = (builtins.tryEval (append "/foo" "bar")).success;
expected = false;
testAppendExample5 = {
expr = (builtins.tryEval (append /foo /bar)).success;
testAppendExample6 = {
expr = (builtins.tryEval (append /foo "")).success;
testAppendExample7 = {
expr = (builtins.tryEval (append /foo "/bar")).success;
testAppendExample8 = {
expr = (builtins.tryEval (append /foo "../bar")).success;
testHasPrefixExample1 = {
expr = hasPrefix /foo /foo/bar;
expected = true;
testHasPrefixExample2 = {
expr = hasPrefix /foo /foo;
testHasPrefixExample3 = {
expr = hasPrefix /foo/bar /foo;
testHasPrefixExample4 = {
expr = hasPrefix /. /foo;
testRemovePrefixExample1 = {
expr = removePrefix /foo /foo/bar/baz;
expected = "./bar/baz";
testRemovePrefixExample2 = {
expr = removePrefix /foo /foo;
expected = "./.";
testRemovePrefixExample3 = {
expr = (builtins.tryEval (removePrefix /foo/bar /foo)).success;
testRemovePrefixExample4 = {
expr = removePrefix /. /foo;
expected = "./foo";
testSplitRootExample1 = {
expr = splitRoot /foo/bar;
expected = {
root = /.;
subpath = "./foo/bar";
testSplitRootExample2 = {
expr = splitRoot /.;
subpath = "./.";
testSplitRootExample3 = {
expr = splitRoot /foo/../bar;
subpath = "./bar";
testSplitRootExample4 = {
expr = (builtins.tryEval (splitRoot "/foo/bar")).success;
testHasStorePathPrefixExample1 = {
expr = hasStorePathPrefix (storeDirPath + "/nvl9ic0pj1fpyln3zaqrf4cclbqdfn1j-foo/bar/baz");
testHasStorePathPrefixExample2 = {
expr = hasStorePathPrefix storeDirPath;
testHasStorePathPrefixExample3 = {
expr = hasStorePathPrefix (storeDirPath + "/nvl9ic0pj1fpyln3zaqrf4cclbqdfn1j-foo");
testHasStorePathPrefixExample4 = {
expr = hasStorePathPrefix /home/user;
testHasStorePathPrefixExample5 = {
expr = hasStorePathPrefix (
storeDirPath + "/.links/10gg8k3rmbw8p7gszarbk7qyd9jwxhcfq9i6s5i0qikx8alkk4hq"
);
testHasStorePathPrefixExample6 = {
expr = hasStorePathPrefix (storeDirPath + "/nvl9ic0pj1fpyln3zaqrf4cclbqdfn1j-foo.drv");
# Test examples from the lib.path.subpath.isValid documentation
testSubpathIsValidExample1 = {
expr = subpath.isValid null;
testSubpathIsValidExample2 = {
expr = subpath.isValid "";
testSubpathIsValidExample3 = {
expr = subpath.isValid "/foo";
testSubpathIsValidExample4 = {
expr = subpath.isValid "../foo";
testSubpathIsValidExample5 = {
expr = subpath.isValid "foo/bar";
testSubpathIsValidExample6 = {
expr = subpath.isValid "./foo//bar/";
# Some extra tests
testSubpathIsValidTwoDotsEnd = {
expr = subpath.isValid "foo/..";
testSubpathIsValidTwoDotsMiddle = {
expr = subpath.isValid "foo/../bar";
testSubpathIsValidTwoDotsPrefix = {
expr = subpath.isValid "..foo";
testSubpathIsValidTwoDotsSuffix = {
expr = subpath.isValid "foo..";
testSubpathIsValidTwoDotsPrefixComponent = {
expr = subpath.isValid "foo/..bar/baz";
testSubpathIsValidTwoDotsSuffixComponent = {
expr = subpath.isValid "foo/bar../baz";
testSubpathIsValidThreeDots = {
expr = subpath.isValid "...";
testSubpathIsValidFourDots = {
expr = subpath.isValid "....";
testSubpathIsValidThreeDotsComponent = {
expr = subpath.isValid "foo/.../bar";
testSubpathIsValidFourDotsComponent = {
expr = subpath.isValid "foo/..../bar";
# Test examples from the lib.path.subpath.join documentation
testSubpathJoinExample1 = {
expr = subpath.join [
"foo"
"bar/baz"
];
expected = "./foo/bar/baz";
testSubpathJoinExample2 = {
"./foo"
"."
"bar//./baz/"
testSubpathJoinExample3 = {
expr = subpath.join [ ];
testSubpathJoinExample4 = {
expr = (builtins.tryEval (subpath.join [ /foo ])).success;
testSubpathJoinExample5 = {
expr = (builtins.tryEval (subpath.join [ "" ])).success;
testSubpathJoinExample6 = {
expr = (builtins.tryEval (subpath.join [ "/foo" ])).success;
testSubpathJoinExample7 = {
expr = (builtins.tryEval (subpath.join [ "../foo" ])).success;
# Test examples from the lib.path.subpath.normalise documentation
testSubpathNormaliseExample1 = {
expr = subpath.normalise "foo//bar";
expected = "./foo/bar";
testSubpathNormaliseExample2 = {
expr = subpath.normalise "foo/./bar";
testSubpathNormaliseExample3 = {
expr = subpath.normalise "foo/bar";
testSubpathNormaliseExample4 = {
expr = subpath.normalise "foo/bar/";
testSubpathNormaliseExample5 = {
expr = subpath.normalise "foo/bar/.";
testSubpathNormaliseExample6 = {
expr = subpath.normalise ".";
testSubpathNormaliseExample7 = {
expr = (builtins.tryEval (subpath.normalise "foo/../bar")).success;
testSubpathNormaliseExample8 = {
expr = (builtins.tryEval (subpath.normalise "")).success;
testSubpathNormaliseExample9 = {
expr = (builtins.tryEval (subpath.normalise "/foo")).success;
testSubpathNormaliseIsValidDots = {
expr = subpath.normalise "./foo/.bar/.../baz...qux";
expected = "./foo/.bar/.../baz...qux";
testSubpathNormaliseWrongType = {
expr = (builtins.tryEval (subpath.normalise null)).success;
testSubpathNormaliseTwoDots = {
expr = (builtins.tryEval (subpath.normalise "..")).success;
testSubpathComponentsExample1 = {
expr = subpath.components ".";
expected = [ ];
testSubpathComponentsExample2 = {
expr = subpath.components "./foo//bar/./baz/";
expected = [
"bar"
"baz"
testSubpathComponentsExample3 = {
expr = (builtins.tryEval (subpath.components "/foo")).success;
in
if cases == [ ] then
"Unit tests successful"
else
throw "Path unit tests failed: ${lib.generators.toPretty { } cases}"