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 ++ cygwin
53 ++ embedded
54 ++ mmix
55 ++ js
56 ++ genode
57 ++ redox
58 );
59
60 testarm = mseteq arm [
61 "armv5tel-linux"
62 "armv6l-linux"
63 "armv6l-netbsd"
64 "armv6l-none"
65 "armv7a-linux"
66 "armv7a-netbsd"
67 "armv7l-linux"
68 "armv7l-netbsd"
69 "arm-none"
70 ];
71 testarmv7 = mseteq armv7 [
72 "armv7a-linux"
73 "armv7l-linux"
74 "armv7a-netbsd"
75 "armv7l-netbsd"
76 ];
77 testi686 = mseteq i686 [
78 "i686-linux"
79 "i686-freebsd"
80 "i686-genode"
81 "i686-netbsd"
82 "i686-openbsd"
83 "i686-cygwin"
84 "i686-windows"
85 "i686-none"
86 ];
87 testmips = mseteq mips [
88 "mips-none"
89 "mips64-none"
90 "mips-linux"
91 "mips64-linux"
92 "mips64el-linux"
93 "mipsel-linux"
94 "mipsel-netbsd"
95 ];
96 testmmix = mseteq mmix [ "mmix-mmixware" ];
97 testpower = mseteq power [
98 "powerpc-linux"
99 "powerpc-netbsd"
100 "powerpc-none"
101 "powerpc64-linux"
102 "powerpc64le-linux"
103 "powerpcle-none"
104 ];
105 testriscv = mseteq riscv [
106 "riscv32-linux"
107 "riscv64-linux"
108 "riscv32-netbsd"
109 "riscv64-netbsd"
110 "riscv32-none"
111 "riscv64-none"
112 ];
113 testriscv32 = mseteq riscv32 [
114 "riscv32-linux"
115 "riscv32-netbsd"
116 "riscv32-none"
117 ];
118 testriscv64 = mseteq riscv64 [
119 "riscv64-linux"
120 "riscv64-netbsd"
121 "riscv64-none"
122 ];
123 tests390x = mseteq s390x [
124 "s390x-linux"
125 "s390x-none"
126 ];
127 testx86_64 = mseteq x86_64 [
128 "x86_64-linux"
129 "x86_64-darwin"
130 "x86_64-freebsd"
131 "x86_64-genode"
132 "x86_64-redox"
133 "x86_64-openbsd"
134 "x86_64-netbsd"
135 "x86_64-cygwin"
136 "x86_64-solaris"
137 "x86_64-windows"
138 "x86_64-none"
139 ];
140
141 testcygwin = mseteq cygwin [
142 "i686-cygwin"
143 "x86_64-cygwin"
144 ];
145 testdarwin = mseteq darwin [
146 "x86_64-darwin"
147 "aarch64-darwin"
148 ];
149 testfreebsd = mseteq freebsd [
150 "aarch64-freebsd"
151 "i686-freebsd"
152 "x86_64-freebsd"
153 ];
154 testgenode = mseteq genode [
155 "aarch64-genode"
156 "i686-genode"
157 "x86_64-genode"
158 ];
159 testredox = mseteq redox [ "x86_64-redox" ];
160 testgnu = mseteq gnu linux; # ++ kfreebsd ++ ...
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 "powerpc-linux"
178 "powerpc64-linux"
179 "powerpc64le-linux"
180 "riscv32-linux"
181 "riscv64-linux"
182 "s390-linux"
183 "s390x-linux"
184 "x86_64-linux"
185 ];
186 testnetbsd = mseteq netbsd [
187 "aarch64-netbsd"
188 "armv6l-netbsd"
189 "armv7a-netbsd"
190 "armv7l-netbsd"
191 "i686-netbsd"
192 "m68k-netbsd"
193 "mipsel-netbsd"
194 "powerpc-netbsd"
195 "riscv32-netbsd"
196 "riscv64-netbsd"
197 "x86_64-netbsd"
198 ];
199 testopenbsd = mseteq openbsd [
200 "i686-openbsd"
201 "x86_64-openbsd"
202 ];
203 testwindows = mseteq windows [
204 "aarch64-windows"
205 "i686-windows"
206 "x86_64-windows"
207 ];
208 testunix = mseteq unix (
209 linux ++ darwin ++ freebsd ++ openbsd ++ netbsd ++ illumos ++ cygwin ++ redox
210 );
211 })
212
213 // {
214 test_equals_example_x86_64-linux = {
215 expr = lib.systems.equals (lib.systems.elaborate "x86_64-linux") (
216 lib.systems.elaborate "x86_64-linux"
217 );
218 expected = true;
219 };
220
221 test_toLosslessStringMaybe_example_x86_64-linux = {
222 expr = toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux");
223 expected = "x86_64-linux";
224 };
225 test_toLosslessStringMaybe_fail = {
226 expr = toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux" // { something = "extra"; });
227 expected = null;
228 };
229 test_elaborate_config_over_system = {
230 expr =
231 (lib.systems.elaborate {
232 config = "i686-unknown-linux-gnu";
233 system = "x86_64-linux";
234 }).system;
235 expected = "i686-linux";
236 };
237 test_elaborate_config_over_parsed = {
238 expr =
239 (lib.systems.elaborate {
240 config = "i686-unknown-linux-gnu";
241 parsed = (lib.systems.elaborate "x86_64-linux").parsed;
242 }).parsed.cpu.arch;
243 expected = "i686";
244 };
245 test_elaborate_system_over_parsed = {
246 expr =
247 (lib.systems.elaborate {
248 system = "i686-linux";
249 parsed = (lib.systems.elaborate "x86_64-linux").parsed;
250 }).parsed.cpu.arch;
251 expected = "i686";
252 };
253 }
254
255 # Generate test cases to assert that a change in any non-function attribute makes a platform unequal
256 //
257 lib.concatMapAttrs
258 (platformAttrName: origValue: {
259
260 ${"test_equals_unequal_${platformAttrName}"} =
261 let
262 modified =
263 assert origValue != arbitraryValue;
264 lib.systems.elaborate "x86_64-linux" // { ${platformAttrName} = arbitraryValue; };
265 arbitraryValue = x: "<<modified>>";
266 in
267 {
268 expr = lib.systems.equals (lib.systems.elaborate "x86_64-linux") modified;
269 expected =
270 {
271 # Changes in these attrs are not detectable because they're function.
272 # The functions should be derived from the data, so this is not a problem.
273 canExecute = null;
274 emulator = null;
275 emulatorAvailable = null;
276 staticEmulatorAvailable = null;
277 isCompatible = null;
278 } ? ${platformAttrName};
279 };
280
281 })
282 (
283 lib.systems.elaborate "x86_64-linux" # arbitrary choice, just to get all the elaborated attrNames
284 )
285
286)