at 24.11-pre 8.5 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 hasStorePathPrefix subpath; 7 8 # This is not allowed generally, but we're in the tests here, so we'll allow ourselves. 9 storeDirPath = /. + builtins.storeDir; 10 11 cases = lib.runTests { 12 # Test examples from the lib.path.append documentation 13 testAppendExample1 = { 14 expr = append /foo "bar/baz"; 15 expected = /foo/bar/baz; 16 }; 17 testAppendExample2 = { 18 expr = append /foo "./bar//baz/./"; 19 expected = /foo/bar/baz; 20 }; 21 testAppendExample3 = { 22 expr = append /. "foo/bar"; 23 expected = /foo/bar; 24 }; 25 testAppendExample4 = { 26 expr = (builtins.tryEval (append "/foo" "bar")).success; 27 expected = false; 28 }; 29 testAppendExample5 = { 30 expr = (builtins.tryEval (append /foo /bar)).success; 31 expected = false; 32 }; 33 testAppendExample6 = { 34 expr = (builtins.tryEval (append /foo "")).success; 35 expected = false; 36 }; 37 testAppendExample7 = { 38 expr = (builtins.tryEval (append /foo "/bar")).success; 39 expected = false; 40 }; 41 testAppendExample8 = { 42 expr = (builtins.tryEval (append /foo "../bar")).success; 43 expected = false; 44 }; 45 46 testHasPrefixExample1 = { 47 expr = hasPrefix /foo /foo/bar; 48 expected = true; 49 }; 50 testHasPrefixExample2 = { 51 expr = hasPrefix /foo /foo; 52 expected = true; 53 }; 54 testHasPrefixExample3 = { 55 expr = hasPrefix /foo/bar /foo; 56 expected = false; 57 }; 58 testHasPrefixExample4 = { 59 expr = hasPrefix /. /foo; 60 expected = true; 61 }; 62 63 testRemovePrefixExample1 = { 64 expr = removePrefix /foo /foo/bar/baz; 65 expected = "./bar/baz"; 66 }; 67 testRemovePrefixExample2 = { 68 expr = removePrefix /foo /foo; 69 expected = "./."; 70 }; 71 testRemovePrefixExample3 = { 72 expr = (builtins.tryEval (removePrefix /foo/bar /foo)).success; 73 expected = false; 74 }; 75 testRemovePrefixExample4 = { 76 expr = removePrefix /. /foo; 77 expected = "./foo"; 78 }; 79 80 testSplitRootExample1 = { 81 expr = splitRoot /foo/bar; 82 expected = { root = /.; subpath = "./foo/bar"; }; 83 }; 84 testSplitRootExample2 = { 85 expr = splitRoot /.; 86 expected = { root = /.; subpath = "./."; }; 87 }; 88 testSplitRootExample3 = { 89 expr = splitRoot /foo/../bar; 90 expected = { root = /.; subpath = "./bar"; }; 91 }; 92 testSplitRootExample4 = { 93 expr = (builtins.tryEval (splitRoot "/foo/bar")).success; 94 expected = false; 95 }; 96 97 testHasStorePathPrefixExample1 = { 98 expr = hasStorePathPrefix (storeDirPath + "/nvl9ic0pj1fpyln3zaqrf4cclbqdfn1j-foo/bar/baz"); 99 expected = true; 100 }; 101 testHasStorePathPrefixExample2 = { 102 expr = hasStorePathPrefix storeDirPath; 103 expected = false; 104 }; 105 testHasStorePathPrefixExample3 = { 106 expr = hasStorePathPrefix (storeDirPath + "/nvl9ic0pj1fpyln3zaqrf4cclbqdfn1j-foo"); 107 expected = true; 108 }; 109 testHasStorePathPrefixExample4 = { 110 expr = hasStorePathPrefix /home/user; 111 expected = false; 112 }; 113 testHasStorePathPrefixExample5 = { 114 expr = hasStorePathPrefix (storeDirPath + "/.links/10gg8k3rmbw8p7gszarbk7qyd9jwxhcfq9i6s5i0qikx8alkk4hq"); 115 expected = false; 116 }; 117 testHasStorePathPrefixExample6 = { 118 expr = hasStorePathPrefix (storeDirPath + "/nvl9ic0pj1fpyln3zaqrf4cclbqdfn1j-foo.drv"); 119 expected = true; 120 }; 121 122 # Test examples from the lib.path.subpath.isValid documentation 123 testSubpathIsValidExample1 = { 124 expr = subpath.isValid null; 125 expected = false; 126 }; 127 testSubpathIsValidExample2 = { 128 expr = subpath.isValid ""; 129 expected = false; 130 }; 131 testSubpathIsValidExample3 = { 132 expr = subpath.isValid "/foo"; 133 expected = false; 134 }; 135 testSubpathIsValidExample4 = { 136 expr = subpath.isValid "../foo"; 137 expected = false; 138 }; 139 testSubpathIsValidExample5 = { 140 expr = subpath.isValid "foo/bar"; 141 expected = true; 142 }; 143 testSubpathIsValidExample6 = { 144 expr = subpath.isValid "./foo//bar/"; 145 expected = true; 146 }; 147 # Some extra tests 148 testSubpathIsValidTwoDotsEnd = { 149 expr = subpath.isValid "foo/.."; 150 expected = false; 151 }; 152 testSubpathIsValidTwoDotsMiddle = { 153 expr = subpath.isValid "foo/../bar"; 154 expected = false; 155 }; 156 testSubpathIsValidTwoDotsPrefix = { 157 expr = subpath.isValid "..foo"; 158 expected = true; 159 }; 160 testSubpathIsValidTwoDotsSuffix = { 161 expr = subpath.isValid "foo.."; 162 expected = true; 163 }; 164 testSubpathIsValidTwoDotsPrefixComponent = { 165 expr = subpath.isValid "foo/..bar/baz"; 166 expected = true; 167 }; 168 testSubpathIsValidTwoDotsSuffixComponent = { 169 expr = subpath.isValid "foo/bar../baz"; 170 expected = true; 171 }; 172 testSubpathIsValidThreeDots = { 173 expr = subpath.isValid "..."; 174 expected = true; 175 }; 176 testSubpathIsValidFourDots = { 177 expr = subpath.isValid "...."; 178 expected = true; 179 }; 180 testSubpathIsValidThreeDotsComponent = { 181 expr = subpath.isValid "foo/.../bar"; 182 expected = true; 183 }; 184 testSubpathIsValidFourDotsComponent = { 185 expr = subpath.isValid "foo/..../bar"; 186 expected = true; 187 }; 188 189 # Test examples from the lib.path.subpath.join documentation 190 testSubpathJoinExample1 = { 191 expr = subpath.join [ "foo" "bar/baz" ]; 192 expected = "./foo/bar/baz"; 193 }; 194 testSubpathJoinExample2 = { 195 expr = subpath.join [ "./foo" "." "bar//./baz/" ]; 196 expected = "./foo/bar/baz"; 197 }; 198 testSubpathJoinExample3 = { 199 expr = subpath.join [ ]; 200 expected = "./."; 201 }; 202 testSubpathJoinExample4 = { 203 expr = (builtins.tryEval (subpath.join [ /foo ])).success; 204 expected = false; 205 }; 206 testSubpathJoinExample5 = { 207 expr = (builtins.tryEval (subpath.join [ "" ])).success; 208 expected = false; 209 }; 210 testSubpathJoinExample6 = { 211 expr = (builtins.tryEval (subpath.join [ "/foo" ])).success; 212 expected = false; 213 }; 214 testSubpathJoinExample7 = { 215 expr = (builtins.tryEval (subpath.join [ "../foo" ])).success; 216 expected = false; 217 }; 218 219 # Test examples from the lib.path.subpath.normalise documentation 220 testSubpathNormaliseExample1 = { 221 expr = subpath.normalise "foo//bar"; 222 expected = "./foo/bar"; 223 }; 224 testSubpathNormaliseExample2 = { 225 expr = subpath.normalise "foo/./bar"; 226 expected = "./foo/bar"; 227 }; 228 testSubpathNormaliseExample3 = { 229 expr = subpath.normalise "foo/bar"; 230 expected = "./foo/bar"; 231 }; 232 testSubpathNormaliseExample4 = { 233 expr = subpath.normalise "foo/bar/"; 234 expected = "./foo/bar"; 235 }; 236 testSubpathNormaliseExample5 = { 237 expr = subpath.normalise "foo/bar/."; 238 expected = "./foo/bar"; 239 }; 240 testSubpathNormaliseExample6 = { 241 expr = subpath.normalise "."; 242 expected = "./."; 243 }; 244 testSubpathNormaliseExample7 = { 245 expr = (builtins.tryEval (subpath.normalise "foo/../bar")).success; 246 expected = false; 247 }; 248 testSubpathNormaliseExample8 = { 249 expr = (builtins.tryEval (subpath.normalise "")).success; 250 expected = false; 251 }; 252 testSubpathNormaliseExample9 = { 253 expr = (builtins.tryEval (subpath.normalise "/foo")).success; 254 expected = false; 255 }; 256 # Some extra tests 257 testSubpathNormaliseIsValidDots = { 258 expr = subpath.normalise "./foo/.bar/.../baz...qux"; 259 expected = "./foo/.bar/.../baz...qux"; 260 }; 261 testSubpathNormaliseWrongType = { 262 expr = (builtins.tryEval (subpath.normalise null)).success; 263 expected = false; 264 }; 265 testSubpathNormaliseTwoDots = { 266 expr = (builtins.tryEval (subpath.normalise "..")).success; 267 expected = false; 268 }; 269 270 testSubpathComponentsExample1 = { 271 expr = subpath.components "."; 272 expected = [ ]; 273 }; 274 testSubpathComponentsExample2 = { 275 expr = subpath.components "./foo//bar/./baz/"; 276 expected = [ "foo" "bar" "baz" ]; 277 }; 278 testSubpathComponentsExample3 = { 279 expr = (builtins.tryEval (subpath.components "/foo")).success; 280 expected = false; 281 }; 282 }; 283in 284 if cases == [] then "Unit tests successful" 285 else throw "Path unit tests failed: ${lib.generators.toPretty {} cases}"