at 24.11-pre 2.5 kB view raw
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: 20 builtins.readFile (dir + "/${name}") 21 ) (builtins.attrNames (builtins.readDir dir)); 22 23 inherit (lib.path.subpath) normalise isValid; 24 inherit (lib.asserts) assertMsg; 25 26 normaliseAndCheck = str: 27 let 28 originalValid = isValid str; 29 30 tryOnce = builtins.tryEval (normalise str); 31 tryTwice = builtins.tryEval (normalise tryOnce.value); 32 33 absConcatOrig = /. + ("/" + str); 34 absConcatNormalised = /. + ("/" + tryOnce.value); 35 in 36 # Check the lib.path.subpath.normalise property to only error on invalid subpaths 37 assert assertMsg 38 (originalValid -> tryOnce.success) 39 "Even though string \"${str}\" is valid as a subpath, the normalisation for it failed"; 40 assert assertMsg 41 (! originalValid -> ! tryOnce.success) 42 "Even though string \"${str}\" is invalid as a subpath, the normalisation for it succeeded"; 43 44 # Check normalisation idempotency 45 assert assertMsg 46 (originalValid -> tryTwice.success) 47 "For valid subpath \"${str}\", the normalisation \"${tryOnce.value}\" was not a valid subpath"; 48 assert assertMsg 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}\""; 51 52 # Check that normalisation doesn't change a string when appended to an absolute Nix path value 53 assert assertMsg 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}\""; 56 57 # Return an empty string when failed 58 if tryOnce.success then tryOnce.value else ""; 59 60in lib.genAttrs strings normaliseAndCheck