1# Given a list of path-like strings, check some properties of the path library
2# using those paths and return a list of attribute sets of the following form:
3#
4# { <string> = <lib.path.subpath.normalise string>; }
5#
6# If `normalise` fails to evaluate, the attribute value is set to `""`.
7# If not, the resulting value is normalised again and an appropriate attribute set added to the output list.
8{
9 # The path to the nixpkgs lib to use
10 libpath,
11 # A flat directory containing files with randomly-generated
12 # path-like values
13 dir,
14}:
15let
16 lib = import libpath;
17
18 # read each file into a string
19 strings = map (name: builtins.readFile (dir + "/${name}")) (
20 builtins.attrNames (builtins.readDir dir)
21 );
22
23 inherit (lib.path.subpath) normalise isValid;
24 inherit (lib.asserts) assertMsg;
25
26 normaliseAndCheck =
27 str:
28 let
29 originalValid = isValid str;
30
31 tryOnce = builtins.tryEval (normalise str);
32 tryTwice = builtins.tryEval (normalise tryOnce.value);
33
34 absConcatOrig = /. + ("/" + str);
35 absConcatNormalised = /. + ("/" + tryOnce.value);
36 in
37 # Check the lib.path.subpath.normalise property to only error on invalid subpaths
38 assert assertMsg (
39 originalValid -> tryOnce.success
40 ) "Even though string \"${str}\" is valid as a subpath, the normalisation for it failed";
41 assert assertMsg (
42 !originalValid -> !tryOnce.success
43 ) "Even though string \"${str}\" is invalid as a subpath, the normalisation for it succeeded";
44
45 # Check normalisation idempotency
46 assert assertMsg (
47 originalValid -> tryTwice.success
48 ) "For valid subpath \"${str}\", the normalisation \"${tryOnce.value}\" was not a valid subpath";
49 assert assertMsg (originalValid -> tryOnce.value == tryTwice.value)
50 "For valid subpath \"${str}\", normalising it once gives \"${tryOnce.value}\" but normalising it twice gives a different result: \"${tryTwice.value}\"";
51
52 # Check that normalisation doesn't change a string when appended to an absolute Nix path value
53 assert assertMsg (originalValid -> absConcatOrig == absConcatNormalised)
54 "For valid subpath \"${str}\", appending to an absolute Nix path value gives \"${absConcatOrig}\", but appending the normalised result \"${tryOnce.value}\" gives a different value \"${absConcatNormalised}\"";
55
56 # Return an empty string when failed
57 if tryOnce.success then tryOnce.value else "";
58
59in
60lib.genAttrs strings normaliseAndCheck