projects/guix·blob python-team · refs

337 lines · 11.3 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2014 David Thompson <[email protected]>
3;;; Copyright © 2016 David Craven <[email protected]>
4;;; Copyright © 2019, 2020, 2022 Ludovic Courtès <[email protected]>
5;;; Copyright © 2020 Martin Becze <[email protected]>
6;;; Copyright © 2023, 2025 Efraim Flashner <[email protected]>
7;;; Copyright © 2023 David Elsing <[email protected]>
8;;; Copyright © 2025 Herman Rimm <[email protected]>
9;;;
10;;; This file is part of GNU Guix.
11;;;
12;;; GNU Guix is free software; you can redistribute it and/or modify it
13;;; under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 3 of the License, or (at
15;;; your option) any later version.
16;;;
17;;; GNU Guix is distributed in the hope that it will be useful, but
18;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
23;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24
25(define-module (test-crate)
26 #:use-module (guix import crate)
27 #:use-module (guix base32)
28 #:use-module (guix build-system cargo)
29 #:use-module ((gcrypt hash)
30 #:select ((sha256 . gcrypt-sha256)))
31 #:use-module (guix packages)
32 #:use-module (guix read-print)
33 #:use-module (guix tests)
34 #:use-module (gnu packages)
35 #:use-module (ice-9 iconv)
36 #:use-module (ice-9 match)
37 #:use-module (srfi srfi-11)
38 #:use-module (srfi srfi-64))
39
40(define test-foo-crate
41 "{
42 \"crate\": {
43 \"max_version\": \"1.0.0\",
44 \"name\": \"foo\",
45 \"description\": \"summary\",
46 \"homepage\": \"http://example.com\",
47 \"repository\": \"http://example.com\",
48 \"keywords\": [\"dummy\", \"test\"],
49 \"categories\": [\"test\"],
50 \"actual_versions\": [
51 { \"id\": 234210,
52 \"num\": \"0.8.1\",
53 \"license\": \"MIT OR Apache-2.0\",
54 \"links\": {
55 \"dependencies\": \"/api/v1/crates/foo/0.8.1/dependencies\"
56 },
57 \"yanked\": false
58 },
59 { \"id\": 234212,
60 \"num\": \"1.0.0\",
61 \"license\": \"MIT OR Apache-2.0\",
62 \"links\": {
63 \"dependencies\": \"/api/v1/crates/foo/1.0.0/dependencies\"
64 },
65 \"yanked\": false
66 },
67 { \"id\": 234214,
68 \"num\": \"1.0.3\",
69 \"license\": \"MIT OR Apache-2.0\",
70 \"links\": {
71 \"dependencies\": \"/api/v1/crates/foo/1.0.3/dependencies\"
72 },
73 \"yanked\": true
74 }
75 ]
76 }
77}")
78
79(define test-bar-crate
80 "{
81 \"crate\": {
82 \"max_version\": \"1.0.0\",
83 \"name\": \"bar\",
84 \"description\": \"summary\",
85 \"homepage\": \"http://example.com\",
86 \"repository\": \"http://example.com\",
87 \"keywords\": [\"dummy\", \"test\"],
88 \"categories\": [\"test\"],
89 \"actual_versions\": [
90 { \"id\": 234100,
91 \"num\": \"1.0.0\",
92 \"license\": \"MIT OR Apache-2.0\",
93 \"links\": {
94 \"dependencies\": \"/api/v1/crates/bar/1.0.0/dependencies\"
95 },
96 \"yanked\": true
97 }
98 ]
99 }
100}")
101
102(define test-source-hash
103 "")
104
105(define have-guile-semver?
106 (false-if-exception (resolve-interface '(semver))))
107
108(define temp-file
109 (string-append "t-crate-" (number->string (getpid))))
110
111
112(test-begin "crate")
113
114(test-equal "guix-package->crate-name"
115 "rustc-serialize"
116 (guix-package->crate-name
117 (dummy-package
118 "rust-rustc-serialize"
119 (source (dummy-origin
120 (uri (crate-uri "rustc-serialize" "1.0")))))))
121
122(unless have-guile-semver? (test-skip 3))
123(test-assert "crate->guix-package"
124 ;; Replace network resources with sample data.
125 (mock ((guix http-client) http-fetch
126 (lambda (url . rest)
127 (match url
128 ("https://crates.io/api/v1/crates/foo"
129 (open-input-string test-foo-crate))
130 ("https://crates.io/api/v1/crates/foo/1.0.0/download"
131 (set! test-source-hash
132 (bytevector->nix-base32-string
133 (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
134 (open-input-string "empty file\n"))
135 (_ (error "Unexpected URL: " url)))))
136
137 (match (crate->guix-package "foo")
138 (`(define-public foo
139 (package (name "foo")
140 (version "1.0.0")
141 (source
142 (origin
143 (method url-fetch)
144 (uri (crate-uri "foo" version))
145 (file-name (string-append name "-" version ".tar.gz"))
146 (sha256
147 (base32
148 ,(? string? hash)))))
149 (build-system cargo-build-system)
150 (inputs (cargo-inputs 'foo))
151 (home-page "http://example.com")
152 (synopsis "summary")
153 (description "This package provides summary.")
154 (license (list license:expat license:asl2.0))))
155
156 (string=? test-source-hash hash))
157 (x
158 (pk 'fail x #f)))))
159
160(test-assert "crate->guix-package yanked"
161 (mock
162 ((guix http-client) http-fetch
163 (lambda (url . rest)
164 (match url
165 ("https://crates.io/api/v1/crates/foo"
166 (open-input-string test-foo-crate))
167 ("https://crates.io/api/v1/crates/foo/1.0.3/download"
168 (set! test-source-hash
169 (bytevector->nix-base32-string
170 (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
171 (open-input-string "empty file\n"))
172 (_ (error "Unexpected URL: " url)))))
173
174 (match (crate->guix-package "foo" #:version "1.0.3" #:allow-yanked? #t)
175 (`(define-public foo
176 (package (name "foo")
177 (version "1.0.3")
178 ,(? comment?)
179 (source
180 (origin
181 (method url-fetch)
182 (uri (crate-uri "foo" version))
183 (file-name (string-append name "-" version "-yanked.tar.gz"))
184 (sha256
185 (base32
186 ,(? string? hash)))))
187 (properties '((crate-version-yanked? . #t)))
188 (build-system cargo-build-system)
189 (inputs (cargo-inputs 'foo))
190 (home-page "http://example.com")
191 (synopsis "summary")
192 (description "This package provides summary.")
193 (license (list license:expat license:asl2.0))))
194 (string=? test-source-hash hash))
195 (x
196 (pk 'fail x #f)))))
197
198(test-assert "crate->guix-package only yanked available"
199 (mock
200 ((guix http-client) http-fetch
201 (lambda (url . rest)
202 (match url
203 ("https://crates.io/api/v1/crates/bar"
204 (open-input-string test-foo-crate))
205 ("https://crates.io/api/v1/crates/bar/1.0.0/download"
206 (set! test-source-hash
207 (bytevector->nix-base32-string
208 (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
209 (open-input-string "empty file\n"))
210 (_ (error "Unexpected URL: " url)))))
211
212 (match (crate->guix-package "bar")
213 (`(define-public bar
214 (package (name "bar")
215 (version "1.0.0")
216 (source
217 (origin
218 (method url-fetch)
219 (uri (crate-uri "bar" version))
220 (file-name
221 (string-append name "-" version ".tar.gz"))
222 (sha256
223 (base32
224 ,(? string? hash)))))
225 (build-system cargo-build-system)
226 (inputs (cargo-inputs 'bar))
227 (home-page "http://example.com")
228 (synopsis "summary")
229 (description "This package provides summary.")
230 (license (list license:expat license:asl2.0))))
231 (string=? test-source-hash hash))
232 (x
233 (pk 'fail x #f)))))
234
235(test-equal "licenses: MIT OR Apache-2.0"
236 '(license:expat license:asl2.0)
237 (string->license "MIT OR Apache-2.0"))
238
239(test-equal "licenses: Apache-2.0 / MIT"
240 '(license:asl2.0 license:expat)
241 (string->license "Apache-2.0 / MIT"))
242
243(test-equal "licenses: Apache-2.0 WITH LLVM-exception"
244 '(license:asl2.0 unknown-license!)
245 (string->license "Apache-2.0 WITH LLVM-exception"))
246
247(test-equal "licenses: MIT/Apache-2.0 AND BSD-2-Clause"
248 '(license:expat license:asl2.0 license:bsd-2)
249 (string->license "MIT/Apache-2.0 AND BSD-2-Clause"))
250
251(test-equal "licenses: MIT/Apache-2.0"
252 '(license:expat license:asl2.0)
253 (string->license "MIT/Apache-2.0"))
254
255
256(test-assert "crate-lockfile-import"
257 (begin
258 (call-with-output-file temp-file
259 (lambda (port)
260 (display "\
261# This file is automatically @generated by Cargo.
262# It is not intended for manual editing.
263version = 3
264
265[[package]]
266name = \"adler2\"
267version = \"2.0.0\"
268source = \"registry+https://github.com/rust-lang/crates.io-index\"
269checksum = \"512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627\"
270
271[[package]]
272name = \"aho-corasick\"
273version = \"1.1.3\"
274source = \"registry+https://github.com/rust-lang/crates.io-index\"
275checksum = \"8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916\"
276dependencies = [
277 \"memchr\",
278]
279
280[[package]]
281name = \"smithay\"
282version = \"0.4.0\"
283source = \"git+https://github.com/Smithay/smithay.git?rev=\
2840cd3345c59f7cb139521f267956a1a4e33248393#\
2850cd3345c59f7cb139521f267956a1a4e33248393\"
286dependencies = [
287 \"appendlist\",
288]
289
290[[package]]
291name = \"test\"
292version = \"25.2.0\"\n" port)))
293 (mock
294 ((guix scripts download) guix-download
295 (lambda _
296 (format #t "~a/~a~%~a~%"
297 "/gnu/store"
298 "in056fyrz6nvy3jpxrxglgj30g0lwniv-smithay-0cd3345"
299 "191h87bpzg0l1ihfb4hmx00b86pfb5mwwc6s8i49al0vigc14l37")))
300 (let-values
301 (((source-expressions cargo-inputs-entry)
302 (cargo-lock->expressions temp-file "test")))
303 (and
304 (match source-expressions
305 (`((define rust-adler2-2.0.0
306 (crate-source
307 "adler2" "2.0.0"
308 "09r6drylvgy8vv8k20lnbvwq8gp09h7smfn6h1rxsy15pgh629si"))
309 (define rust-aho-corasick-1.1.3
310 (crate-source
311 "aho-corasick" "1.1.3"
312 "05mrpkvdgp5d20y2p989f187ry9diliijgwrs254fs9s1m1x6q4f"))
313 (define rust-smithay-0.4.0.0cd3345
314 ,(? comment?)
315 (origin
316 (method git-fetch)
317 (uri (git-reference
318 (url "https://github.com/Smithay/smithay.git")
319 (commit "0cd3345c59f7cb139521f267956a1a4e33248393")))
320 (file-name (git-file-name "rust-smithay" "0.4.0.0cd3345"))
321 (sha256
322 (base32
323 "191h87bpzg0l1ihfb4hmx00b86pfb5mwwc6s8i49al0vigc14l37")))))
324 #t)
325 (x
326 (pk 'fail (pretty-print-with-comments (current-output-port) x) #f)))
327 (match cargo-inputs-entry
328 (`(test => (list rust-adler2-2.0.0
329 rust-aho-corasick-1.1.3
330 rust-smithay-0.4.0.0cd3345))
331 #t)
332 (x
333 (pk 'fail x #f))))))))
334
335(test-end "crate")
336
337(false-if-exception (delete-file temp-file))