1 # Unit tests for lib.path functions. Use `nix-build` in this directory to
6 inherit (lib.path) hasPrefix removePrefix append splitRoot hasStorePathPrefix subpath;
8 # This is not allowed generally, but we're in the tests here, so we'll allow ourselves.
9 storeDirPath = /. + builtins.storeDir;
11 cases = lib.runTests {
12 # Test examples from the lib.path.append documentation
13 testAppendExample1 = {
14 expr = append /foo "bar/baz";
15 expected = /foo/bar/baz;
17 testAppendExample2 = {
18 expr = append /foo "./bar//baz/./";
19 expected = /foo/bar/baz;
21 testAppendExample3 = {
22 expr = append /. "foo/bar";
25 testAppendExample4 = {
26 expr = (builtins.tryEval (append "/foo" "bar")).success;
29 testAppendExample5 = {
30 expr = (builtins.tryEval (append /foo /bar)).success;
33 testAppendExample6 = {
34 expr = (builtins.tryEval (append /foo "")).success;
37 testAppendExample7 = {
38 expr = (builtins.tryEval (append /foo "/bar")).success;
41 testAppendExample8 = {
42 expr = (builtins.tryEval (append /foo "../bar")).success;
46 testHasPrefixExample1 = {
47 expr = hasPrefix /foo /foo/bar;
50 testHasPrefixExample2 = {
51 expr = hasPrefix /foo /foo;
54 testHasPrefixExample3 = {
55 expr = hasPrefix /foo/bar /foo;
58 testHasPrefixExample4 = {
59 expr = hasPrefix /. /foo;
63 testRemovePrefixExample1 = {
64 expr = removePrefix /foo /foo/bar/baz;
65 expected = "./bar/baz";
67 testRemovePrefixExample2 = {
68 expr = removePrefix /foo /foo;
71 testRemovePrefixExample3 = {
72 expr = (builtins.tryEval (removePrefix /foo/bar /foo)).success;
75 testRemovePrefixExample4 = {
76 expr = removePrefix /. /foo;
80 testSplitRootExample1 = {
81 expr = splitRoot /foo/bar;
82 expected = { root = /.; subpath = "./foo/bar"; };
84 testSplitRootExample2 = {
86 expected = { root = /.; subpath = "./."; };
88 testSplitRootExample3 = {
89 expr = splitRoot /foo/../bar;
90 expected = { root = /.; subpath = "./bar"; };
92 testSplitRootExample4 = {
93 expr = (builtins.tryEval (splitRoot "/foo/bar")).success;
97 testHasStorePathPrefixExample1 = {
98 expr = hasStorePathPrefix (storeDirPath + "/nvl9ic0pj1fpyln3zaqrf4cclbqdfn1j-foo/bar/baz");
101 testHasStorePathPrefixExample2 = {
102 expr = hasStorePathPrefix storeDirPath;
105 testHasStorePathPrefixExample3 = {
106 expr = hasStorePathPrefix (storeDirPath + "/nvl9ic0pj1fpyln3zaqrf4cclbqdfn1j-foo");
109 testHasStorePathPrefixExample4 = {
110 expr = hasStorePathPrefix /home/user;
113 testHasStorePathPrefixExample5 = {
114 expr = hasStorePathPrefix (storeDirPath + "/.links/10gg8k3rmbw8p7gszarbk7qyd9jwxhcfq9i6s5i0qikx8alkk4hq");
117 testHasStorePathPrefixExample6 = {
118 expr = hasStorePathPrefix (storeDirPath + "/nvl9ic0pj1fpyln3zaqrf4cclbqdfn1j-foo.drv");
122 # Test examples from the lib.path.subpath.isValid documentation
123 testSubpathIsValidExample1 = {
124 expr = subpath.isValid null;
127 testSubpathIsValidExample2 = {
128 expr = subpath.isValid "";
131 testSubpathIsValidExample3 = {
132 expr = subpath.isValid "/foo";
135 testSubpathIsValidExample4 = {
136 expr = subpath.isValid "../foo";
139 testSubpathIsValidExample5 = {
140 expr = subpath.isValid "foo/bar";
143 testSubpathIsValidExample6 = {
144 expr = subpath.isValid "./foo//bar/";
148 testSubpathIsValidTwoDotsEnd = {
149 expr = subpath.isValid "foo/..";
152 testSubpathIsValidTwoDotsMiddle = {
153 expr = subpath.isValid "foo/../bar";
156 testSubpathIsValidTwoDotsPrefix = {
157 expr = subpath.isValid "..foo";
160 testSubpathIsValidTwoDotsSuffix = {
161 expr = subpath.isValid "foo..";
164 testSubpathIsValidTwoDotsPrefixComponent = {
165 expr = subpath.isValid "foo/..bar/baz";
168 testSubpathIsValidTwoDotsSuffixComponent = {
169 expr = subpath.isValid "foo/bar../baz";
172 testSubpathIsValidThreeDots = {
173 expr = subpath.isValid "...";
176 testSubpathIsValidFourDots = {
177 expr = subpath.isValid "....";
180 testSubpathIsValidThreeDotsComponent = {
181 expr = subpath.isValid "foo/.../bar";
184 testSubpathIsValidFourDotsComponent = {
185 expr = subpath.isValid "foo/..../bar";
189 # Test examples from the lib.path.subpath.join documentation
190 testSubpathJoinExample1 = {
191 expr = subpath.join [ "foo" "bar/baz" ];
192 expected = "./foo/bar/baz";
194 testSubpathJoinExample2 = {
195 expr = subpath.join [ "./foo" "." "bar//./baz/" ];
196 expected = "./foo/bar/baz";
198 testSubpathJoinExample3 = {
199 expr = subpath.join [ ];
202 testSubpathJoinExample4 = {
203 expr = (builtins.tryEval (subpath.join [ /foo ])).success;
206 testSubpathJoinExample5 = {
207 expr = (builtins.tryEval (subpath.join [ "" ])).success;
210 testSubpathJoinExample6 = {
211 expr = (builtins.tryEval (subpath.join [ "/foo" ])).success;
214 testSubpathJoinExample7 = {
215 expr = (builtins.tryEval (subpath.join [ "../foo" ])).success;
219 # Test examples from the lib.path.subpath.normalise documentation
220 testSubpathNormaliseExample1 = {
221 expr = subpath.normalise "foo//bar";
222 expected = "./foo/bar";
224 testSubpathNormaliseExample2 = {
225 expr = subpath.normalise "foo/./bar";
226 expected = "./foo/bar";
228 testSubpathNormaliseExample3 = {
229 expr = subpath.normalise "foo/bar";
230 expected = "./foo/bar";
232 testSubpathNormaliseExample4 = {
233 expr = subpath.normalise "foo/bar/";
234 expected = "./foo/bar";
236 testSubpathNormaliseExample5 = {
237 expr = subpath.normalise "foo/bar/.";
238 expected = "./foo/bar";
240 testSubpathNormaliseExample6 = {
241 expr = subpath.normalise ".";
244 testSubpathNormaliseExample7 = {
245 expr = (builtins.tryEval (subpath.normalise "foo/../bar")).success;
248 testSubpathNormaliseExample8 = {
249 expr = (builtins.tryEval (subpath.normalise "")).success;
252 testSubpathNormaliseExample9 = {
253 expr = (builtins.tryEval (subpath.normalise "/foo")).success;
257 testSubpathNormaliseIsValidDots = {
258 expr = subpath.normalise "./foo/.bar/.../baz...qux";
259 expected = "./foo/.bar/.../baz...qux";
261 testSubpathNormaliseWrongType = {
262 expr = (builtins.tryEval (subpath.normalise null)).success;
265 testSubpathNormaliseTwoDots = {
266 expr = (builtins.tryEval (subpath.normalise "..")).success;
270 testSubpathComponentsExample1 = {
271 expr = subpath.components ".";
274 testSubpathComponentsExample2 = {
275 expr = subpath.components "./foo//bar/./baz/";
276 expected = [ "foo" "bar" "baz" ];
278 testSubpathComponentsExample3 = {
279 expr = (builtins.tryEval (subpath.components "/foo")).success;
284 if cases == [] then "Unit tests successful"
285 else throw "Path unit tests failed: ${lib.generators.toPretty {} cases}"