at 23.11-beta 7.4 kB view raw
1# Unit tests for lib.path functions. Use `nix-build` in this directory to 2# run these 3{ libpath }: 4let 5 lib = import libpath; 6 inherit (lib.path) hasPrefix removePrefix append splitRoot subpath; 7 8 cases = lib.runTests { 9 # Test examples from the lib.path.append documentation 10 testAppendExample1 = { 11 expr = append /foo "bar/baz"; 12 expected = /foo/bar/baz; 13 }; 14 testAppendExample2 = { 15 expr = append /foo "./bar//baz/./"; 16 expected = /foo/bar/baz; 17 }; 18 testAppendExample3 = { 19 expr = append /. "foo/bar"; 20 expected = /foo/bar; 21 }; 22 testAppendExample4 = { 23 expr = (builtins.tryEval (append "/foo" "bar")).success; 24 expected = false; 25 }; 26 testAppendExample5 = { 27 expr = (builtins.tryEval (append /foo /bar)).success; 28 expected = false; 29 }; 30 testAppendExample6 = { 31 expr = (builtins.tryEval (append /foo "")).success; 32 expected = false; 33 }; 34 testAppendExample7 = { 35 expr = (builtins.tryEval (append /foo "/bar")).success; 36 expected = false; 37 }; 38 testAppendExample8 = { 39 expr = (builtins.tryEval (append /foo "../bar")).success; 40 expected = false; 41 }; 42 43 testHasPrefixExample1 = { 44 expr = hasPrefix /foo /foo/bar; 45 expected = true; 46 }; 47 testHasPrefixExample2 = { 48 expr = hasPrefix /foo /foo; 49 expected = true; 50 }; 51 testHasPrefixExample3 = { 52 expr = hasPrefix /foo/bar /foo; 53 expected = false; 54 }; 55 testHasPrefixExample4 = { 56 expr = hasPrefix /. /foo; 57 expected = true; 58 }; 59 60 testRemovePrefixExample1 = { 61 expr = removePrefix /foo /foo/bar/baz; 62 expected = "./bar/baz"; 63 }; 64 testRemovePrefixExample2 = { 65 expr = removePrefix /foo /foo; 66 expected = "./."; 67 }; 68 testRemovePrefixExample3 = { 69 expr = (builtins.tryEval (removePrefix /foo/bar /foo)).success; 70 expected = false; 71 }; 72 testRemovePrefixExample4 = { 73 expr = removePrefix /. /foo; 74 expected = "./foo"; 75 }; 76 77 testSplitRootExample1 = { 78 expr = splitRoot /foo/bar; 79 expected = { root = /.; subpath = "./foo/bar"; }; 80 }; 81 testSplitRootExample2 = { 82 expr = splitRoot /.; 83 expected = { root = /.; subpath = "./."; }; 84 }; 85 testSplitRootExample3 = { 86 expr = splitRoot /foo/../bar; 87 expected = { root = /.; subpath = "./bar"; }; 88 }; 89 testSplitRootExample4 = { 90 expr = (builtins.tryEval (splitRoot "/foo/bar")).success; 91 expected = false; 92 }; 93 94 # Test examples from the lib.path.subpath.isValid documentation 95 testSubpathIsValidExample1 = { 96 expr = subpath.isValid null; 97 expected = false; 98 }; 99 testSubpathIsValidExample2 = { 100 expr = subpath.isValid ""; 101 expected = false; 102 }; 103 testSubpathIsValidExample3 = { 104 expr = subpath.isValid "/foo"; 105 expected = false; 106 }; 107 testSubpathIsValidExample4 = { 108 expr = subpath.isValid "../foo"; 109 expected = false; 110 }; 111 testSubpathIsValidExample5 = { 112 expr = subpath.isValid "foo/bar"; 113 expected = true; 114 }; 115 testSubpathIsValidExample6 = { 116 expr = subpath.isValid "./foo//bar/"; 117 expected = true; 118 }; 119 # Some extra tests 120 testSubpathIsValidTwoDotsEnd = { 121 expr = subpath.isValid "foo/.."; 122 expected = false; 123 }; 124 testSubpathIsValidTwoDotsMiddle = { 125 expr = subpath.isValid "foo/../bar"; 126 expected = false; 127 }; 128 testSubpathIsValidTwoDotsPrefix = { 129 expr = subpath.isValid "..foo"; 130 expected = true; 131 }; 132 testSubpathIsValidTwoDotsSuffix = { 133 expr = subpath.isValid "foo.."; 134 expected = true; 135 }; 136 testSubpathIsValidTwoDotsPrefixComponent = { 137 expr = subpath.isValid "foo/..bar/baz"; 138 expected = true; 139 }; 140 testSubpathIsValidTwoDotsSuffixComponent = { 141 expr = subpath.isValid "foo/bar../baz"; 142 expected = true; 143 }; 144 testSubpathIsValidThreeDots = { 145 expr = subpath.isValid "..."; 146 expected = true; 147 }; 148 testSubpathIsValidFourDots = { 149 expr = subpath.isValid "...."; 150 expected = true; 151 }; 152 testSubpathIsValidThreeDotsComponent = { 153 expr = subpath.isValid "foo/.../bar"; 154 expected = true; 155 }; 156 testSubpathIsValidFourDotsComponent = { 157 expr = subpath.isValid "foo/..../bar"; 158 expected = true; 159 }; 160 161 # Test examples from the lib.path.subpath.join documentation 162 testSubpathJoinExample1 = { 163 expr = subpath.join [ "foo" "bar/baz" ]; 164 expected = "./foo/bar/baz"; 165 }; 166 testSubpathJoinExample2 = { 167 expr = subpath.join [ "./foo" "." "bar//./baz/" ]; 168 expected = "./foo/bar/baz"; 169 }; 170 testSubpathJoinExample3 = { 171 expr = subpath.join [ ]; 172 expected = "./."; 173 }; 174 testSubpathJoinExample4 = { 175 expr = (builtins.tryEval (subpath.join [ /foo ])).success; 176 expected = false; 177 }; 178 testSubpathJoinExample5 = { 179 expr = (builtins.tryEval (subpath.join [ "" ])).success; 180 expected = false; 181 }; 182 testSubpathJoinExample6 = { 183 expr = (builtins.tryEval (subpath.join [ "/foo" ])).success; 184 expected = false; 185 }; 186 testSubpathJoinExample7 = { 187 expr = (builtins.tryEval (subpath.join [ "../foo" ])).success; 188 expected = false; 189 }; 190 191 # Test examples from the lib.path.subpath.normalise documentation 192 testSubpathNormaliseExample1 = { 193 expr = subpath.normalise "foo//bar"; 194 expected = "./foo/bar"; 195 }; 196 testSubpathNormaliseExample2 = { 197 expr = subpath.normalise "foo/./bar"; 198 expected = "./foo/bar"; 199 }; 200 testSubpathNormaliseExample3 = { 201 expr = subpath.normalise "foo/bar"; 202 expected = "./foo/bar"; 203 }; 204 testSubpathNormaliseExample4 = { 205 expr = subpath.normalise "foo/bar/"; 206 expected = "./foo/bar"; 207 }; 208 testSubpathNormaliseExample5 = { 209 expr = subpath.normalise "foo/bar/."; 210 expected = "./foo/bar"; 211 }; 212 testSubpathNormaliseExample6 = { 213 expr = subpath.normalise "."; 214 expected = "./."; 215 }; 216 testSubpathNormaliseExample7 = { 217 expr = (builtins.tryEval (subpath.normalise "foo/../bar")).success; 218 expected = false; 219 }; 220 testSubpathNormaliseExample8 = { 221 expr = (builtins.tryEval (subpath.normalise "")).success; 222 expected = false; 223 }; 224 testSubpathNormaliseExample9 = { 225 expr = (builtins.tryEval (subpath.normalise "/foo")).success; 226 expected = false; 227 }; 228 # Some extra tests 229 testSubpathNormaliseIsValidDots = { 230 expr = subpath.normalise "./foo/.bar/.../baz...qux"; 231 expected = "./foo/.bar/.../baz...qux"; 232 }; 233 testSubpathNormaliseWrongType = { 234 expr = (builtins.tryEval (subpath.normalise null)).success; 235 expected = false; 236 }; 237 testSubpathNormaliseTwoDots = { 238 expr = (builtins.tryEval (subpath.normalise "..")).success; 239 expected = false; 240 }; 241 242 testSubpathComponentsExample1 = { 243 expr = subpath.components "."; 244 expected = [ ]; 245 }; 246 testSubpathComponentsExample2 = { 247 expr = subpath.components "./foo//bar/./baz/"; 248 expected = [ "foo" "bar" "baz" ]; 249 }; 250 testSubpathComponentsExample3 = { 251 expr = (builtins.tryEval (subpath.components "/foo")).success; 252 expected = false; 253 }; 254 }; 255in 256 if cases == [] then "Unit tests successful" 257 else throw "Path unit tests failed: ${lib.generators.toPretty {} cases}"