Commit | Line | Data |
---|---|---|
3e0c0365 DC |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2014 David Thompson <davet@gnu.org> | |
3 | ;;; Copyright © 2016 David Craven <david@craven.ch> | |
72c678af | 4 | ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
3e0c0365 DC |
5 | ;;; |
6 | ;;; This file is part of GNU Guix. | |
7 | ;;; | |
8 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
9 | ;;; under the terms of the GNU General Public License as published by | |
10 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
11 | ;;; your option) any later version. | |
12 | ;;; | |
13 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
14 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;;; GNU General Public License for more details. | |
17 | ;;; | |
18 | ;;; You should have received a copy of the GNU General Public License | |
19 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
20 | ||
21 | (define-module (test-crate) | |
22 | #:use-module (guix import crate) | |
23 | #:use-module (guix base32) | |
24 | #:use-module (guix build-system cargo) | |
ca719424 | 25 | #:use-module (gcrypt hash) |
3e0c0365 DC |
26 | #:use-module (guix tests) |
27 | #:use-module (ice-9 iconv) | |
28 | #:use-module (ice-9 match) | |
29 | #:use-module (srfi srfi-64)) | |
30 | ||
4982de4c | 31 | (define test-foo-crate |
3e0c0365 DC |
32 | "{ |
33 | \"crate\": { | |
34 | \"max_version\": \"1.0.0\", | |
35 | \"name\": \"foo\", | |
3e0c0365 DC |
36 | \"description\": \"summary\", |
37 | \"homepage\": \"http://example.com\", | |
f53a5514 | 38 | \"repository\": \"http://example.com\", |
2791870d LC |
39 | \"keywords\": [\"dummy\" \"test\"], |
40 | \"categories\": [\"test\"] | |
41 | \"actual_versions\": [ | |
42 | { \"id\": \"foo\", | |
43 | \"num\": \"1.0.0\", | |
191668bc | 44 | \"license\": \"MIT OR Apache-2.0\", |
2791870d LC |
45 | \"links\": { |
46 | \"dependencies\": \"/api/v1/crates/foo/1.0.0/dependencies\" | |
47 | } | |
48 | } | |
49 | ] | |
3e0c0365 DC |
50 | } |
51 | }") | |
52 | ||
4982de4c | 53 | (define test-foo-dependencies |
3e0c0365 DC |
54 | "{ |
55 | \"dependencies\": [ | |
56 | { | |
57 | \"crate_id\": \"bar\", | |
58 | \"kind\": \"normal\", | |
59 | } | |
60 | ] | |
61 | }") | |
62 | ||
4982de4c BL |
63 | (define test-root-crate |
64 | "{ | |
65 | \"crate\": { | |
66 | \"max_version\": \"1.0.0\", | |
67 | \"name\": \"root\", | |
68 | \"description\": \"summary\", | |
69 | \"homepage\": \"http://example.com\", | |
70 | \"repository\": \"http://example.com\", | |
71 | \"keywords\": [\"dummy\" \"test\"], | |
72 | \"categories\": [\"test\"] | |
73 | \"actual_versions\": [ | |
74 | { \"id\": \"foo\", | |
75 | \"num\": \"1.0.0\", | |
76 | \"license\": \"MIT OR Apache-2.0\", | |
77 | \"links\": { | |
78 | \"dependencies\": \"/api/v1/crates/root/1.0.0/dependencies\" | |
79 | } | |
80 | } | |
81 | ] | |
82 | } | |
83 | }") | |
84 | ||
85 | (define test-root-dependencies | |
86 | "{ | |
87 | \"dependencies\": [ | |
88 | { | |
89 | \"crate_id\": \"intermediate-1\", | |
90 | \"kind\": \"normal\", | |
91 | }, | |
92 | { | |
93 | \"crate_id\": \"intermediate-2\", | |
94 | \"kind\": \"normal\", | |
95 | } | |
96 | { | |
97 | \"crate_id\": \"leaf-alice\", | |
98 | \"kind\": \"normal\", | |
99 | }, | |
100 | { | |
101 | \"crate_id\": \"leaf-bob\", | |
102 | \"kind\": \"normal\", | |
103 | }, | |
104 | ] | |
105 | }") | |
106 | ||
107 | (define test-intermediate-1-crate | |
108 | "{ | |
109 | \"crate\": { | |
110 | \"max_version\": \"1.0.0\", | |
111 | \"name\": \"intermediate-1\", | |
112 | \"description\": \"summary\", | |
113 | \"homepage\": \"http://example.com\", | |
114 | \"repository\": \"http://example.com\", | |
115 | \"keywords\": [\"dummy\" \"test\"], | |
116 | \"categories\": [\"test\"] | |
117 | \"actual_versions\": [ | |
118 | { \"id\": \"intermediate-1\", | |
119 | \"num\": \"1.0.0\", | |
120 | \"license\": \"MIT OR Apache-2.0\", | |
121 | \"links\": { | |
122 | \"dependencies\": \"/api/v1/crates/intermediate-1/1.0.0/dependencies\" | |
123 | } | |
124 | } | |
125 | ] | |
126 | } | |
127 | }") | |
128 | ||
129 | (define test-intermediate-1-dependencies | |
130 | "{ | |
131 | \"dependencies\": [ | |
132 | { | |
133 | \"crate_id\": \"intermediate-2\", | |
134 | \"kind\": \"normal\", | |
135 | }, | |
136 | { | |
137 | \"crate_id\": \"leaf-alice\", | |
138 | \"kind\": \"normal\", | |
139 | }, | |
140 | { | |
141 | \"crate_id\": \"leaf-bob\", | |
142 | \"kind\": \"normal\", | |
143 | } | |
144 | ] | |
145 | }") | |
146 | ||
147 | (define test-intermediate-2-crate | |
148 | "{ | |
149 | \"crate\": { | |
150 | \"max_version\": \"1.0.0\", | |
151 | \"name\": \"intermediate-2\", | |
152 | \"description\": \"summary\", | |
153 | \"homepage\": \"http://example.com\", | |
154 | \"repository\": \"http://example.com\", | |
155 | \"keywords\": [\"dummy\" \"test\"], | |
156 | \"categories\": [\"test\"] | |
157 | \"actual_versions\": [ | |
158 | { \"id\": \"intermediate-2\", | |
159 | \"num\": \"1.0.0\", | |
160 | \"license\": \"MIT OR Apache-2.0\", | |
161 | \"links\": { | |
162 | \"dependencies\": \"/api/v1/crates/intermediate-2/1.0.0/dependencies\" | |
163 | } | |
164 | } | |
165 | ] | |
166 | } | |
167 | }") | |
168 | ||
169 | (define test-intermediate-2-dependencies | |
170 | "{ | |
171 | \"dependencies\": [ | |
172 | { | |
173 | \"crate_id\": \"leaf-bob\", | |
174 | \"kind\": \"normal\", | |
175 | }, | |
176 | ] | |
177 | }") | |
178 | ||
179 | (define test-leaf-alice-crate | |
180 | "{ | |
181 | \"crate\": { | |
182 | \"max_version\": \"1.0.0\", | |
183 | \"name\": \"leaf-alice\", | |
184 | \"description\": \"summary\", | |
185 | \"homepage\": \"http://example.com\", | |
186 | \"repository\": \"http://example.com\", | |
187 | \"keywords\": [\"dummy\" \"test\"], | |
188 | \"categories\": [\"test\"] | |
189 | \"actual_versions\": [ | |
190 | { \"id\": \"leaf-alice\", | |
191 | \"num\": \"1.0.0\", | |
192 | \"license\": \"MIT OR Apache-2.0\", | |
193 | \"links\": { | |
194 | \"dependencies\": \"/api/v1/crates/leaf-alice/1.0.0/dependencies\" | |
195 | } | |
196 | } | |
197 | ] | |
198 | } | |
199 | }") | |
200 | ||
201 | (define test-leaf-alice-dependencies | |
202 | "{ | |
203 | \"dependencies\": [] | |
204 | }") | |
205 | ||
206 | (define test-leaf-bob-crate | |
207 | "{ | |
208 | \"crate\": { | |
209 | \"max_version\": \"1.0.0\", | |
210 | \"name\": \"leaf-bob\", | |
211 | \"description\": \"summary\", | |
212 | \"homepage\": \"http://example.com\", | |
213 | \"repository\": \"http://example.com\", | |
214 | \"keywords\": [\"dummy\" \"test\"], | |
215 | \"categories\": [\"test\"] | |
216 | \"actual_versions\": [ | |
217 | { \"id\": \"leaf-bob\", | |
218 | \"num\": \"1.0.0\", | |
219 | \"license\": \"MIT OR Apache-2.0\", | |
220 | \"links\": { | |
221 | \"dependencies\": \"/api/v1/crates/leaf-bob/1.0.0/dependencies\" | |
222 | } | |
223 | } | |
224 | ] | |
225 | } | |
226 | }") | |
227 | ||
228 | (define test-leaf-bob-dependencies | |
229 | "{ | |
230 | \"dependencies\": [] | |
231 | }") | |
232 | ||
3e0c0365 DC |
233 | (define test-source-hash |
234 | "") | |
235 | ||
236 | (test-begin "crate") | |
237 | ||
238 | (test-equal "guix-package->crate-name" | |
239 | "rustc-serialize" | |
240 | (guix-package->crate-name | |
241 | (dummy-package | |
242 | "rust-rustc-serialize" | |
243 | (source (dummy-origin | |
244 | (uri (crate-uri "rustc-serialize" "1.0"))))))) | |
245 | ||
246 | (test-assert "crate->guix-package" | |
247 | ;; Replace network resources with sample data. | |
248 | (mock ((guix http-client) http-fetch | |
ce8963c5 | 249 | (lambda (url . rest) |
3e0c0365 DC |
250 | (match url |
251 | ("https://crates.io/api/v1/crates/foo" | |
4982de4c | 252 | (open-input-string test-foo-crate)) |
3e0c0365 DC |
253 | ("https://crates.io/api/v1/crates/foo/1.0.0/download" |
254 | (set! test-source-hash | |
255 | (bytevector->nix-base32-string | |
256 | (sha256 (string->bytevector "empty file\n" "utf-8")))) | |
257 | (open-input-string "empty file\n")) | |
258 | ("https://crates.io/api/v1/crates/foo/1.0.0/dependencies" | |
4982de4c | 259 | (open-input-string test-foo-dependencies)) |
3e0c0365 DC |
260 | (_ (error "Unexpected URL: " url))))) |
261 | (match (crate->guix-package "foo") | |
262 | (('package | |
263 | ('name "rust-foo") | |
264 | ('version "1.0.0") | |
265 | ('source ('origin | |
266 | ('method 'url-fetch) | |
267 | ('uri ('crate-uri "foo" 'version)) | |
268 | ('file-name ('string-append 'name "-" 'version ".tar.gz")) | |
269 | ('sha256 | |
270 | ('base32 | |
271 | (? string? hash))))) | |
272 | ('build-system 'cargo-build-system) | |
5a9ef8a9 | 273 | ('arguments |
3e0c0365 | 274 | ('quasiquote |
022288ba | 275 | ('#:cargo-inputs (("rust-bar" ('unquote rust-bar)))))) |
3e0c0365 DC |
276 | ('home-page "http://example.com") |
277 | ('synopsis "summary") | |
278 | ('description "summary") | |
279 | ('license ('list 'license:expat 'license:asl2.0))) | |
280 | (string=? test-source-hash hash)) | |
281 | (x | |
282 | (pk 'fail x #f))))) | |
283 | ||
4982de4c BL |
284 | (test-assert "cargo-recursive-import" |
285 | ;; Replace network resources with sample data. | |
286 | (mock ((guix http-client) http-fetch | |
287 | (lambda (url . rest) | |
288 | (match url | |
289 | ("https://crates.io/api/v1/crates/root" | |
290 | (open-input-string test-root-crate)) | |
291 | ("https://crates.io/api/v1/crates/root/1.0.0/download" | |
292 | (set! test-source-hash | |
293 | (bytevector->nix-base32-string | |
294 | (sha256 (string->bytevector "empty file\n" "utf-8")))) | |
295 | (open-input-string "empty file\n")) | |
296 | ("https://crates.io/api/v1/crates/root/1.0.0/dependencies" | |
297 | (open-input-string test-root-dependencies)) | |
298 | ("https://crates.io/api/v1/crates/intermediate-1" | |
299 | (open-input-string test-intermediate-1-crate)) | |
300 | ("https://crates.io/api/v1/crates/intermediate-1/1.0.0/download" | |
301 | (set! test-source-hash | |
302 | (bytevector->nix-base32-string | |
303 | (sha256 (string->bytevector "empty file\n" "utf-8")))) | |
304 | (open-input-string "empty file\n")) | |
305 | ("https://crates.io/api/v1/crates/intermediate-1/1.0.0/dependencies" | |
306 | (open-input-string test-intermediate-1-dependencies)) | |
307 | ("https://crates.io/api/v1/crates/intermediate-2" | |
308 | (open-input-string test-intermediate-2-crate)) | |
309 | ("https://crates.io/api/v1/crates/intermediate-2/1.0.0/download" | |
310 | (set! test-source-hash | |
311 | (bytevector->nix-base32-string | |
312 | (sha256 (string->bytevector "empty file\n" "utf-8")))) | |
313 | (open-input-string "empty file\n")) | |
314 | ("https://crates.io/api/v1/crates/intermediate-2/1.0.0/dependencies" | |
315 | (open-input-string test-intermediate-2-dependencies)) | |
316 | ("https://crates.io/api/v1/crates/leaf-alice" | |
317 | (open-input-string test-leaf-alice-crate)) | |
318 | ("https://crates.io/api/v1/crates/leaf-alice/1.0.0/download" | |
319 | (set! test-source-hash | |
320 | (bytevector->nix-base32-string | |
321 | (sha256 (string->bytevector "empty file\n" "utf-8")))) | |
322 | (open-input-string "empty file\n")) | |
323 | ("https://crates.io/api/v1/crates/leaf-alice/1.0.0/dependencies" | |
324 | (open-input-string test-leaf-alice-dependencies)) | |
325 | ("https://crates.io/api/v1/crates/leaf-bob" | |
326 | (open-input-string test-leaf-bob-crate)) | |
327 | ("https://crates.io/api/v1/crates/leaf-bob/1.0.0/download" | |
328 | (set! test-source-hash | |
329 | (bytevector->nix-base32-string | |
330 | (sha256 (string->bytevector "empty file\n" "utf-8")))) | |
331 | (open-input-string "empty file\n")) | |
332 | ("https://crates.io/api/v1/crates/leaf-bob/1.0.0/dependencies" | |
333 | (open-input-string test-leaf-bob-dependencies)) | |
334 | (_ (error "Unexpected URL: " url))))) | |
335 | (match (crate-recursive-import "root") | |
336 | ;; rust-intermediate-2 has no dependency on the rust-leaf-alice package, so this is a valid ordering | |
337 | ((('package | |
338 | ('name "rust-leaf-alice") | |
339 | ('version (? string? ver)) | |
340 | ('source | |
341 | ('origin | |
342 | ('method 'url-fetch) | |
343 | ('uri ('crate-uri "leaf-alice" 'version)) | |
344 | ('file-name | |
345 | ('string-append 'name "-" 'version ".tar.gz")) | |
346 | ('sha256 | |
347 | ('base32 | |
348 | (? string? hash))))) | |
349 | ('build-system 'cargo-build-system) | |
350 | ('home-page "http://example.com") | |
351 | ('synopsis "summary") | |
352 | ('description "summary") | |
353 | ('license ('list 'license:expat 'license:asl2.0))) | |
354 | ('package | |
355 | ('name "rust-leaf-bob") | |
356 | ('version (? string? ver)) | |
357 | ('source | |
358 | ('origin | |
359 | ('method 'url-fetch) | |
360 | ('uri ('crate-uri "leaf-bob" 'version)) | |
361 | ('file-name | |
362 | ('string-append 'name "-" 'version ".tar.gz")) | |
363 | ('sha256 | |
364 | ('base32 | |
365 | (? string? hash))))) | |
366 | ('build-system 'cargo-build-system) | |
367 | ('home-page "http://example.com") | |
368 | ('synopsis "summary") | |
369 | ('description "summary") | |
370 | ('license ('list 'license:expat 'license:asl2.0))) | |
371 | ('package | |
372 | ('name "rust-intermediate-2") | |
373 | ('version (? string? ver)) | |
374 | ('source | |
375 | ('origin | |
376 | ('method 'url-fetch) | |
377 | ('uri ('crate-uri "intermediate-2" 'version)) | |
378 | ('file-name | |
379 | ('string-append 'name "-" 'version ".tar.gz")) | |
380 | ('sha256 | |
381 | ('base32 | |
382 | (? string? hash))))) | |
383 | ('build-system 'cargo-build-system) | |
384 | ('arguments | |
385 | ('quasiquote | |
386 | ('#:cargo-inputs (("rust-leaf-bob" ('unquote rust-leaf-bob)))))) | |
387 | ('home-page "http://example.com") | |
388 | ('synopsis "summary") | |
389 | ('description "summary") | |
390 | ('license ('list 'license:expat 'license:asl2.0))) | |
391 | ('package | |
392 | ('name "rust-intermediate-1") | |
393 | ('version (? string? ver)) | |
394 | ('source | |
395 | ('origin | |
396 | ('method 'url-fetch) | |
397 | ('uri ('crate-uri "intermediate-1" 'version)) | |
398 | ('file-name | |
399 | ('string-append 'name "-" 'version ".tar.gz")) | |
400 | ('sha256 | |
401 | ('base32 | |
402 | (? string? hash))))) | |
403 | ('build-system 'cargo-build-system) | |
404 | ('arguments | |
405 | ('quasiquote | |
406 | ('#:cargo-inputs (("rust-intermediate-2" ('unquote rust-intermediate-2)) | |
407 | ("rust-leaf-alice" ('unquote rust-leaf-alice)) | |
408 | ("rust-leaf-bob" ('unquote rust-leaf-bob)))))) | |
409 | ('home-page "http://example.com") | |
410 | ('synopsis "summary") | |
411 | ('description "summary") | |
412 | ('license ('list 'license:expat 'license:asl2.0))) | |
413 | ('package | |
414 | ('name "rust-root") | |
415 | ('version (? string? ver)) | |
416 | ('source | |
417 | ('origin | |
418 | ('method 'url-fetch) | |
419 | ('uri ('crate-uri "root" 'version)) | |
420 | ('file-name | |
421 | ('string-append 'name "-" 'version ".tar.gz")) | |
422 | ('sha256 | |
423 | ('base32 | |
424 | (? string? hash))))) | |
425 | ('build-system 'cargo-build-system) | |
426 | ('arguments | |
427 | ('quasiquote | |
428 | ('#:cargo-inputs (("rust-intermediate-1" ('unquote rust-intermediate-1)) | |
429 | ("rust-intermediate-2" ('unquote rust-intermediate-2)) | |
430 | ("rust-leaf-alice" ('unquote rust-leaf-alice)) | |
431 | ("rust-leaf-bob" ('unquote rust-leaf-bob)))))) | |
432 | ('home-page "http://example.com") | |
433 | ('synopsis "summary") | |
434 | ('description "summary") | |
435 | ('license ('list 'license:expat 'license:asl2.0)))) | |
436 | #t) | |
437 | (x | |
438 | (pk 'fail x #f))))) | |
439 | ||
263a267b BW |
440 | (test-equal "licenses: MIT OR Apache-2.0" |
441 | '(license:expat license:asl2.0) | |
442 | (string->license "MIT OR Apache-2.0")) | |
443 | ||
444 | (test-equal "licenses: Apache-2.0 / MIT" | |
445 | '(license:asl2.0 license:expat) | |
446 | (string->license "Apache-2.0 / MIT")) | |
447 | ||
448 | (test-equal "licenses: Apache-2.0 WITH LLVM-exception" | |
449 | '(license:asl2.0 unknown-license!) | |
450 | (string->license "Apache-2.0 WITH LLVM-exception")) | |
451 | ||
452 | (test-equal "licenses: MIT/Apache-2.0 AND BSD-2-Clause" | |
453 | '(license:expat license:asl2.0 unknown-license!) | |
454 | (string->license "MIT/Apache-2.0 AND BSD-2-Clause")) | |
455 | ||
456 | (test-equal "licenses: MIT/Apache-2.0" | |
457 | '(license:expat license:asl2.0) | |
458 | (string->license "MIT/Apache-2.0")) | |
459 | ||
3e0c0365 | 460 | (test-end "crate") |