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:
4 # { <string> = <lib.path.subpath.normalise string>; }
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.
9 # The path to the nixpkgs lib to use
11 # A flat directory containing files with randomly-generated
18 # read each file into a string
20 builtins.readFile (dir + "/${name}")
21 ) (builtins.attrNames (builtins.readDir dir));
23 inherit (lib.path.subpath) normalise isValid;
24 inherit (lib.asserts) assertMsg;
26 normaliseAndCheck = str:
28 originalValid = isValid str;
30 tryOnce = builtins.tryEval (normalise str);
31 tryTwice = builtins.tryEval (normalise tryOnce.value);
33 absConcatOrig = /. + ("/" + str);
34 absConcatNormalised = /. + ("/" + tryOnce.value);
36 # Check the lib.path.subpath.normalise property to only error on invalid subpaths
38 (originalValid -> tryOnce.success)
39 "Even though string \"${str}\" is valid as a subpath, the normalisation for it failed";
41 (! originalValid -> ! tryOnce.success)
42 "Even though string \"${str}\" is invalid as a subpath, the normalisation for it succeeded";
44 # Check normalisation idempotency
46 (originalValid -> tryTwice.success)
47 "For valid subpath \"${str}\", the normalisation \"${tryOnce.value}\" was not a valid subpath";
49 (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}\"";
52 # Check that normalisation doesn't change a string when appended to an absolute Nix path value
54 (originalValid -> absConcatOrig == absConcatNormalised)
55 "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}\"";
57 # Return an empty string when failed
58 if tryOnce.success then tryOnce.value else "";
60 in lib.genAttrs strings normaliseAndCheck