lib.path.subpath.components: init

Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>

+44
+31
lib/path/default.nix
··· 336 336 ${subpathInvalidReason path}'' 337 337 ) 0 subpaths; 338 338 339 + /* 340 + Split [a subpath](#function-library-lib.path.subpath.isValid) into its path component strings. 341 + Throw an error if the subpath isn't valid. 342 + Note that the returned path components are also valid subpath strings, though they are intentionally not [normalised](#function-library-lib.path.subpath.normalise). 343 + 344 + Laws: 345 + 346 + - Splitting a subpath into components and [joining](#function-library-lib.path.subpath.join) the components gives the same subpath but [normalised](#function-library-lib.path.subpath.normalise): 347 + 348 + subpath.join (subpath.components s) == subpath.normalise s 349 + 350 + Type: 351 + subpath.components :: String -> [ String ] 352 + 353 + Example: 354 + subpath.components "." 355 + => [ ] 356 + 357 + subpath.components "./foo//bar/./baz/" 358 + => [ "foo" "bar" "baz" ] 359 + 360 + subpath.components "/foo" 361 + => <error> 362 + */ 363 + subpath.components = 364 + subpath: 365 + assert assertMsg (isValid subpath) '' 366 + lib.path.subpath.components: Argument is not a valid subpath string: 367 + ${subpathInvalidReason subpath}''; 368 + splitRelPath subpath; 369 + 339 370 /* Normalise a subpath. Throw an error if the subpath isn't valid, see 340 371 `lib.path.subpath.isValid` 341 372
+13
lib/path/tests/unit.nix
··· 204 204 expr = (builtins.tryEval (subpath.normalise "..")).success; 205 205 expected = false; 206 206 }; 207 + 208 + testSubpathComponentsExample1 = { 209 + expr = subpath.components "."; 210 + expected = [ ]; 211 + }; 212 + testSubpathComponentsExample2 = { 213 + expr = subpath.components "./foo//bar/./baz/"; 214 + expected = [ "foo" "bar" "baz" ]; 215 + }; 216 + testSubpathComponentsExample3 = { 217 + expr = (builtins.tryEval (subpath.components "/foo")).success; 218 + expected = false; 219 + }; 207 220 }; 208 221 in 209 222 if cases == [] then "Unit tests successful"