at 25.11-pre 7.4 kB view raw
1# Run: 2# [nixpkgs]$ nix-instantiate --eval --strict lib/tests/systems.nix 3# Expected output: [], or the failed cases 4# 5# OfBorg runs (approximately) nix-build lib/tests/release.nix 6let 7 lib = import ../default.nix; 8 mseteq = x: y: { 9 expr = lib.sort lib.lessThan x; 10 expected = lib.sort lib.lessThan y; 11 }; 12 13 /* 14 Try to convert an elaborated system back to a simple string. If not possible, 15 return null. So we have the property: 16 17 sys: _valid_ sys -> 18 sys == elaborate (toLosslessStringMaybe sys) 19 20 NOTE: This property is not guaranteed when `sys` was elaborated by a different 21 version of Nixpkgs. 22 */ 23 toLosslessStringMaybe = 24 sys: 25 if lib.isString sys then 26 sys 27 else if lib.systems.equals sys (lib.systems.elaborate sys.system) then 28 sys.system 29 else 30 null; 31 32in 33lib.runTests ( 34 # We assert that the new algorithmic way of generating these lists matches the 35 # way they were hard-coded before. 36 # 37 # One might think "if we exhaustively test, what's the point of procedurally 38 # calculating the lists anyway?". The answer is one can mindlessly update these 39 # tests as new platforms become supported, and then just give the diff a quick 40 # sanity check before committing :). 41 42 (with lib.systems.doubles; { 43 testall = mseteq all ( 44 linux 45 ++ darwin 46 ++ freebsd 47 ++ openbsd 48 ++ netbsd 49 ++ illumos 50 ++ wasi 51 ++ windows 52 ++ embedded 53 ++ mmix 54 ++ js 55 ++ genode 56 ++ redox 57 ); 58 59 testarm = mseteq arm [ 60 "armv5tel-linux" 61 "armv6l-linux" 62 "armv6l-netbsd" 63 "armv6l-none" 64 "armv7a-linux" 65 "armv7a-netbsd" 66 "armv7l-linux" 67 "armv7l-netbsd" 68 "arm-none" 69 ]; 70 testarmv7 = mseteq armv7 [ 71 "armv7a-linux" 72 "armv7l-linux" 73 "armv7a-netbsd" 74 "armv7l-netbsd" 75 ]; 76 testi686 = mseteq i686 [ 77 "i686-linux" 78 "i686-freebsd" 79 "i686-genode" 80 "i686-netbsd" 81 "i686-openbsd" 82 "i686-cygwin" 83 "i686-windows" 84 "i686-none" 85 ]; 86 testmips = mseteq mips [ 87 "mips-none" 88 "mips64-none" 89 "mips-linux" 90 "mips64-linux" 91 "mips64el-linux" 92 "mipsel-linux" 93 "mipsel-netbsd" 94 ]; 95 testmmix = mseteq mmix [ "mmix-mmixware" ]; 96 testpower = mseteq power [ 97 "powerpc-netbsd" 98 "powerpc-none" 99 "powerpc64-linux" 100 "powerpc64le-linux" 101 "powerpcle-none" 102 ]; 103 testriscv = mseteq riscv [ 104 "riscv32-linux" 105 "riscv64-linux" 106 "riscv32-netbsd" 107 "riscv64-netbsd" 108 "riscv32-none" 109 "riscv64-none" 110 ]; 111 testriscv32 = mseteq riscv32 [ 112 "riscv32-linux" 113 "riscv32-netbsd" 114 "riscv32-none" 115 ]; 116 testriscv64 = mseteq riscv64 [ 117 "riscv64-linux" 118 "riscv64-netbsd" 119 "riscv64-none" 120 ]; 121 tests390x = mseteq s390x [ 122 "s390x-linux" 123 "s390x-none" 124 ]; 125 testx86_64 = mseteq x86_64 [ 126 "x86_64-linux" 127 "x86_64-darwin" 128 "x86_64-freebsd" 129 "x86_64-genode" 130 "x86_64-redox" 131 "x86_64-openbsd" 132 "x86_64-netbsd" 133 "x86_64-cygwin" 134 "x86_64-solaris" 135 "x86_64-windows" 136 "x86_64-none" 137 ]; 138 139 testcygwin = mseteq cygwin [ 140 "i686-cygwin" 141 "x86_64-cygwin" 142 ]; 143 testdarwin = mseteq darwin [ 144 "x86_64-darwin" 145 "aarch64-darwin" 146 ]; 147 testfreebsd = mseteq freebsd [ 148 "aarch64-freebsd" 149 "i686-freebsd" 150 "x86_64-freebsd" 151 ]; 152 testgenode = mseteq genode [ 153 "aarch64-genode" 154 "i686-genode" 155 "x86_64-genode" 156 ]; 157 testredox = mseteq redox [ "x86_64-redox" ]; 158 testgnu = mseteq gnu ( 159 linux # ++ kfreebsd ++ ... 160 ); 161 testillumos = mseteq illumos [ "x86_64-solaris" ]; 162 testlinux = mseteq linux [ 163 "aarch64-linux" 164 "armv5tel-linux" 165 "armv6l-linux" 166 "armv7a-linux" 167 "armv7l-linux" 168 "i686-linux" 169 "loongarch64-linux" 170 "m68k-linux" 171 "microblaze-linux" 172 "microblazeel-linux" 173 "mips-linux" 174 "mips64-linux" 175 "mips64el-linux" 176 "mipsel-linux" 177 "powerpc64-linux" 178 "powerpc64le-linux" 179 "riscv32-linux" 180 "riscv64-linux" 181 "s390-linux" 182 "s390x-linux" 183 "x86_64-linux" 184 ]; 185 testnetbsd = mseteq netbsd [ 186 "aarch64-netbsd" 187 "armv6l-netbsd" 188 "armv7a-netbsd" 189 "armv7l-netbsd" 190 "i686-netbsd" 191 "m68k-netbsd" 192 "mipsel-netbsd" 193 "powerpc-netbsd" 194 "riscv32-netbsd" 195 "riscv64-netbsd" 196 "x86_64-netbsd" 197 ]; 198 testopenbsd = mseteq openbsd [ 199 "i686-openbsd" 200 "x86_64-openbsd" 201 ]; 202 testwindows = mseteq windows [ 203 "i686-cygwin" 204 "x86_64-cygwin" 205 "aarch64-windows" 206 "i686-windows" 207 "x86_64-windows" 208 ]; 209 testunix = mseteq unix ( 210 linux ++ darwin ++ freebsd ++ openbsd ++ netbsd ++ illumos ++ cygwin ++ redox 211 ); 212 }) 213 214 // { 215 test_equals_example_x86_64-linux = { 216 expr = lib.systems.equals (lib.systems.elaborate "x86_64-linux") ( 217 lib.systems.elaborate "x86_64-linux" 218 ); 219 expected = true; 220 }; 221 222 test_toLosslessStringMaybe_example_x86_64-linux = { 223 expr = toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux"); 224 expected = "x86_64-linux"; 225 }; 226 test_toLosslessStringMaybe_fail = { 227 expr = toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux" // { something = "extra"; }); 228 expected = null; 229 }; 230 test_elaborate_config_over_system = { 231 expr = 232 (lib.systems.elaborate { 233 config = "i686-unknown-linux-gnu"; 234 system = "x86_64-linux"; 235 }).system; 236 expected = "i686-linux"; 237 }; 238 test_elaborate_config_over_parsed = { 239 expr = 240 (lib.systems.elaborate { 241 config = "i686-unknown-linux-gnu"; 242 parsed = (lib.systems.elaborate "x86_64-linux").parsed; 243 }).parsed.cpu.arch; 244 expected = "i686"; 245 }; 246 test_elaborate_system_over_parsed = { 247 expr = 248 (lib.systems.elaborate { 249 system = "i686-linux"; 250 parsed = (lib.systems.elaborate "x86_64-linux").parsed; 251 }).parsed.cpu.arch; 252 expected = "i686"; 253 }; 254 } 255 256 # Generate test cases to assert that a change in any non-function attribute makes a platform unequal 257 // 258 lib.concatMapAttrs 259 (platformAttrName: origValue: { 260 261 ${"test_equals_unequal_${platformAttrName}"} = 262 let 263 modified = 264 assert origValue != arbitraryValue; 265 lib.systems.elaborate "x86_64-linux" // { ${platformAttrName} = arbitraryValue; }; 266 arbitraryValue = x: "<<modified>>"; 267 in 268 { 269 expr = lib.systems.equals (lib.systems.elaborate "x86_64-linux") modified; 270 expected = 271 { 272 # Changes in these attrs are not detectable because they're function. 273 # The functions should be derived from the data, so this is not a problem. 274 canExecute = null; 275 emulator = null; 276 emulatorAvailable = null; 277 staticEmulatorAvailable = null; 278 isCompatible = null; 279 } ? ${platformAttrName}; 280 }; 281 282 }) 283 ( 284 lib.systems.elaborate "x86_64-linux" # arbitrary choice, just to get all the elaborated attrNames 285 ) 286 287)