diagnostics: Add '&formatted-message'.
[jackhill/guix/guix.git] / tests / packages.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
84c5da08 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
0db65c16 3;;; Copyright © Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
e3ce5d70 4;;;
233e7676 5;;; This file is part of GNU Guix.
e3ce5d70 6;;;
233e7676 7;;; GNU Guix is free software; you can redistribute it and/or modify it
e3ce5d70
LC
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
233e7676 12;;; GNU Guix is distributed in the hope that it will be useful, but
e3ce5d70
LC
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
233e7676 18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
e3ce5d70 19
e3ce5d70 20(define-module (test-packages)
c1bc358f 21 #:use-module (guix tests)
e3ce5d70 22 #:use-module (guix store)
cf81a236 23 #:use-module (guix monads)
fcadd9ff 24 #:use-module (guix grafts)
da675305 25 #:use-module ((guix gexp) #:select (local-file local-file-file))
a5e2fc73
LC
26 #:use-module (guix utils)
27 #:use-module ((guix diagnostics)
2e1bafb0
LC
28 ;; Rename the 'location' binding to allow proper syntax
29 ;; matching when setting the 'location' field of a package.
30 #:renamer (lambda (name)
31 (cond ((eq? name 'location) 'make-location)
32 (else name))))
197c07a8 33 #:use-module ((gcrypt hash) #:prefix gcrypt:)
e3ce5d70
LC
34 #:use-module (guix derivations)
35 #:use-module (guix packages)
7adf9b84 36 #:use-module (guix grafts)
aa8e0515 37 #:use-module (guix search-paths)
a18eda27 38 #:use-module (guix build-system)
be13fbfa 39 #:use-module (guix build-system trivial)
a3d73f59 40 #:use-module (guix build-system gnu)
0db65c16 41 #:use-module (guix memoization)
cf81a236
LC
42 #:use-module (guix profiles)
43 #:use-module (guix scripts package)
59a43334 44 #:use-module (gnu packages)
1ffa7090 45 #:use-module (gnu packages base)
05962f29 46 #:use-module (gnu packages guile)
1ffa7090 47 #:use-module (gnu packages bootstrap)
fcd75bdb 48 #:use-module (gnu packages version-control)
cf81a236 49 #:use-module (gnu packages xml)
05962f29 50 #:use-module (srfi srfi-1)
6b1891b0 51 #:use-module (srfi srfi-26)
9b222abe 52 #:use-module (srfi srfi-34)
dbab5150 53 #:use-module (srfi srfi-35)
6b1891b0 54 #:use-module (srfi srfi-64)
ce0be567 55 #:use-module (rnrs bytevectors)
860a6f1a 56 #:use-module (rnrs io ports)
5239f3d9 57 #:use-module (ice-9 vlist)
2e1bafb0 58 #:use-module (ice-9 regex)
6b1891b0 59 #:use-module (ice-9 match))
e3ce5d70
LC
60
61;; Test the high-level packaging layer.
62
63(define %store
c1bc358f 64 (open-connection-for-tests))
e3ce5d70 65
b4c42a4b
LC
66;; Globally disable grafting to avoid rebuilding the world ('graft-derivation'
67;; can trigger builds early.)
68(%graft? #f)
69
05962f29
LC
70\f
71(test-begin "packages")
72
2e1bafb0 73(test-assert "printer with location"
74e667d1 74 (string-match "^#<package foo@0 foo.scm:42 [[:xdigit:]]+>$"
2e1bafb0
LC
75 (with-output-to-string
76 (lambda ()
77 (write
78 (dummy-package "foo"
79 (location (make-location "foo.scm" 42 7))))))))
80
81(test-assert "printer without location"
74e667d1 82 (string-match "^#<package foo@0 [[:xdigit:]]+>$"
2e1bafb0
LC
83 (with-output-to-string
84 (lambda ()
85 (write
86 (dummy-package "foo" (location #f)))))))
87
6980511b
LC
88(test-assert "hidden-package"
89 (and (hidden-package? (hidden-package (dummy-package "foo")))
90 (not (hidden-package? (dummy-package "foo")))))
91
01afdab8
LC
92(test-assert "package-superseded"
93 (let* ((new (dummy-package "bar"))
94 (old (deprecated-package "foo" new)))
95 (and (eq? (package-superseded old) new)
96 (mock ((gnu packages) find-best-packages-by-name (const (list old)))
97 (specification->package "foo")
98 (and (eq? new (specification->package "foo"))
99 (eq? new (specification->package+output "foo")))))))
100
5239f3d9
LC
101(test-assert "transaction-upgrade-entry, zero upgrades"
102 (let* ((old (dummy-package "foo" (version "1")))
465a0d65
LC
103 (tx (mock ((gnu packages) find-best-packages-by-name
104 (const '()))
84c5da08
LC
105 (transaction-upgrade-entry
106 #f ;no store access needed
5239f3d9
LC
107 (manifest-entry
108 (inherit (package->manifest-entry old))
109 (item (string-append (%store-prefix) "/"
110 (make-string 32 #\e) "-foo-1")))
111 (manifest-transaction)))))
112 (manifest-transaction-null? tx)))
113
b9c79cae
LC
114(test-assert "transaction-upgrade-entry, zero upgrades, equivalent package"
115 (let* ((old (dummy-package "foo" (version "1")))
116 (drv (package-derivation %store old))
117 (tx (mock ((gnu packages) find-best-packages-by-name
118 (const (list old)))
119 (transaction-upgrade-entry
120 %store
121 (manifest-entry
122 (inherit (package->manifest-entry old))
123 (item (derivation->output-path drv)))
124 (manifest-transaction)))))
125 (manifest-transaction-null? tx)))
126
a357849f
LC
127(test-assert "transaction-upgrade-entry, zero upgrades, propagated inputs"
128 ;; Properly detect equivalent packages even when they have propagated
129 ;; inputs. See <https://bugs.gnu.org/35872>.
130 (let* ((dep (dummy-package "dep" (version "2")))
131 (old (dummy-package "foo" (version "1")
132 (propagated-inputs `(("dep" ,dep)))))
133 (drv (package-derivation %store old))
134 (tx (mock ((gnu packages) find-best-packages-by-name
135 (const (list old)))
136 (transaction-upgrade-entry
137 %store
138 (manifest-entry
139 (inherit (package->manifest-entry old))
140 (item (derivation->output-path drv))
141 (dependencies
142 (list (manifest-entry
143 (inherit (package->manifest-entry dep))
144 (item (derivation->output-path
145 (package-derivation %store dep)))))))
146 (manifest-transaction)))))
147 (manifest-transaction-null? tx)))
148
5239f3d9
LC
149(test-assert "transaction-upgrade-entry, one upgrade"
150 (let* ((old (dummy-package "foo" (version "1")))
151 (new (dummy-package "foo" (version "2")))
465a0d65
LC
152 (tx (mock ((gnu packages) find-best-packages-by-name
153 (const (list new)))
84c5da08
LC
154 (transaction-upgrade-entry
155 #f ;no store access needed
5239f3d9
LC
156 (manifest-entry
157 (inherit (package->manifest-entry old))
158 (item (string-append (%store-prefix) "/"
159 (make-string 32 #\e) "-foo-1")))
160 (manifest-transaction)))))
161 (and (match (manifest-transaction-install tx)
162 ((($ <manifest-entry> "foo" "2" "out" item))
163 (eq? item new)))
164 (null? (manifest-transaction-remove tx)))))
165
01afdab8
LC
166(test-assert "transaction-upgrade-entry, superseded package"
167 (let* ((old (dummy-package "foo" (version "1")))
168 (new (dummy-package "bar" (version "2")))
169 (dep (deprecated-package "foo" new))
465a0d65
LC
170 (tx (mock ((gnu packages) find-best-packages-by-name
171 (const (list dep)))
84c5da08
LC
172 (transaction-upgrade-entry
173 #f ;no store access needed
01afdab8
LC
174 (manifest-entry
175 (inherit (package->manifest-entry old))
176 (item (string-append (%store-prefix) "/"
177 (make-string 32 #\e) "-foo-1")))
178 (manifest-transaction)))))
179 (and (match (manifest-transaction-install tx)
180 ((($ <manifest-entry> "bar" "2" "out" item))
181 (eq? item new)))
182 (match (manifest-transaction-remove tx)
183 (((? manifest-pattern? pattern))
184 (and (string=? (manifest-pattern-name pattern) "foo")
185 (string=? (manifest-pattern-version pattern) "1")
186 (string=? (manifest-pattern-output pattern) "out")))))))
187
a187cc56
LC
188(test-assert "transaction-upgrade-entry, grafts"
189 ;; Ensure that, when grafts are enabled, 'transaction-upgrade-entry' doesn't
190 ;; try to build stuff.
191 (with-build-handler (const 'failed!)
192 (parameterize ((%graft? #t))
193 (let* ((old (dummy-package "foo" (version "1")))
194 (bar (dummy-package "bar" (version "0")
195 (replacement old)))
196 (new (dummy-package "foo" (version "1")
197 (inputs `(("bar" ,bar)))))
198 (tx (mock ((gnu packages) find-best-packages-by-name
199 (const (list new)))
200 (transaction-upgrade-entry
201 %store
202 (manifest-entry
203 (inherit (package->manifest-entry old))
204 (item (string-append (%store-prefix) "/"
205 (make-string 32 #\e) "-foo-1")))
206 (manifest-transaction)))))
207 (and (match (manifest-transaction-install tx)
208 ((($ <manifest-entry> "foo" "1" "out" item))
209 (eq? item new)))
210 (null? (manifest-transaction-remove tx)))))))
211
d66c7096
LC
212(test-assert "package-field-location"
213 (let ()
214 (define (goto port line column)
215 (unless (and (= (port-column port) (- column 1))
216 (= (port-line port) (- line 1)))
217 (unless (eof-object? (get-char port))
218 (goto port line column))))
219
220 (define read-at
221 (match-lambda
222 (($ <location> file line column)
223 (call-with-input-file (search-path %load-path file)
224 (lambda (port)
225 (goto port line column)
226 (read port))))))
227
ee48b283
LC
228 ;; Until Guile 2.0.6 included, source properties were added only to pairs.
229 ;; Thus, check against both VALUE and (FIELD VALUE).
230 (and (member (read-at (package-field-location %bootstrap-guile 'name))
231 (let ((name (package-name %bootstrap-guile)))
232 (list name `(name ,name))))
233 (member (read-at (package-field-location %bootstrap-guile 'version))
234 (let ((version (package-version %bootstrap-guile)))
235 (list version `(version ,version))))
f903dc05 236 (not (package-field-location %bootstrap-guile 'does-not-exist)))))
d66c7096 237
0b8749b7
LC
238;; Make sure we don't change the file name to an absolute file name.
239(test-equal "package-field-location, relative file name"
240 (location-file (package-location %bootstrap-guile))
241 (with-fluids ((%file-port-name-canonicalization 'absolute))
242 (location-file (package-field-location %bootstrap-guile 'version))))
243
a3d73f59
LC
244(test-assert "package-transitive-inputs"
245 (let* ((a (dummy-package "a"))
246 (b (dummy-package "b"
247 (propagated-inputs `(("a" ,a)))))
248 (c (dummy-package "c"
249 (inputs `(("a" ,a)))))
250 (d (dummy-package "d"
251 (propagated-inputs `(("x" "something.drv")))))
252 (e (dummy-package "e"
253 (inputs `(("b" ,b) ("c" ,c) ("d" ,d))))))
254 (and (null? (package-transitive-inputs a))
255 (equal? `(("a" ,a)) (package-transitive-inputs b))
256 (equal? `(("a" ,a)) (package-transitive-inputs c))
257 (equal? (package-propagated-inputs d)
258 (package-transitive-inputs d))
161094c8
LC
259 (equal? `(("b" ,b) ("c" ,c) ("d" ,d)
260 ("a" ,a) ("x" "something.drv"))
a3d73f59
LC
261 (pk 'x (package-transitive-inputs e))))))
262
161094c8
LC
263(test-assert "package-transitive-inputs, no duplicates"
264 (let* ((a (dummy-package "a"))
265 (b (dummy-package "b"
266 (inputs `(("a+" ,a)))
267 (native-inputs `(("a*" ,a)))
268 (propagated-inputs `(("a" ,a)))))
269 (c (dummy-package "c"
270 (propagated-inputs `(("b" ,b)))))
271 (d (dummy-package "d"
272 (inputs `(("a" ,a) ("c" ,c)))))
273 (e (dummy-package "e"
274 (inputs `(("b" ,b) ("c" ,c))))))
275 (and (null? (package-transitive-inputs a))
276 (equal? `(("a*" ,a) ("a+" ,a) ("a" ,a)) ;here duplicates are kept
277 (package-transitive-inputs b))
278 (equal? `(("b" ,b) ("a" ,a))
279 (package-transitive-inputs c))
280 (equal? `(("a" ,a) ("c" ,c) ("b" ,b)) ;duplicate A removed
281 (package-transitive-inputs d))
282 (equal? `(("b" ,b) ("c" ,c) ("a" ,a))
283 (package-transitive-inputs e))))) ;ditto
284
7c3c0374 285(test-equal "package-transitive-supported-systems"
c37a74bd
LC
286 '(("x" "y" "z") ;a
287 ("x" "y") ;b
288 ("y") ;c
289 ("y") ;d
290 ("y")) ;e
9bf3ced0
LC
291 ;; Use TRIVIAL-BUILD-SYSTEM because it doesn't add implicit inputs and thus
292 ;; doesn't restrict the set of supported systems.
293 (let* ((a (dummy-package "a"
294 (build-system trivial-build-system)
295 (supported-systems '("x" "y" "z"))))
296 (b (dummy-package "b"
297 (build-system trivial-build-system)
298 (supported-systems '("x" "y"))
299 (inputs `(("a" ,a)))))
300 (c (dummy-package "c"
301 (build-system trivial-build-system)
302 (supported-systems '("y" "z"))
303 (inputs `(("b" ,b)))))
304 (d (dummy-package "d"
305 (build-system trivial-build-system)
306 (supported-systems '("x" "y" "z"))
307 (inputs `(("b" ,b) ("c" ,c)))))
308 (e (dummy-package "e"
309 (build-system trivial-build-system)
310 (supported-systems '("x" "y" "z"))
311 (inputs `(("d" ,d))))))
7c3c0374
LC
312 (list (package-transitive-supported-systems a)
313 (package-transitive-supported-systems b)
c37a74bd
LC
314 (package-transitive-supported-systems c)
315 (package-transitive-supported-systems d)
316 (package-transitive-supported-systems e))))
7c3c0374 317
3e223a22
LC
318(test-assert "package-closure"
319 (let-syntax ((dummy-package/no-implicit
320 (syntax-rules ()
321 ((_ name rest ...)
322 (package
323 (inherit (dummy-package name rest ...))
324 (build-system trivial-build-system))))))
325 (let* ((a (dummy-package/no-implicit "a"))
326 (b (dummy-package/no-implicit "b"
327 (propagated-inputs `(("a" ,a)))))
328 (c (dummy-package/no-implicit "c"
329 (inputs `(("a" ,a)))))
330 (d (dummy-package/no-implicit "d"
331 (native-inputs `(("b" ,b)))))
332 (e (dummy-package/no-implicit "e"
333 (inputs `(("c" ,c) ("d" ,d))))))
334 (lset= eq?
335 (list a b c d e)
336 (package-closure (list e))
337 (package-closure (list e d))
338 (package-closure (list e c b))))))
339
3b4d0103
EB
340(test-equal "origin-actual-file-name"
341 "foo-1.tar.gz"
342 (let ((o (dummy-origin (uri "http://www.example.com/foo-1.tar.gz"))))
343 (origin-actual-file-name o)))
344
345(test-equal "origin-actual-file-name, file-name"
346 "foo-1.tar.gz"
347 (let ((o (dummy-origin
348 (uri "http://www.example.com/tarball")
349 (file-name "foo-1.tar.gz"))))
350 (origin-actual-file-name o)))
351
f77bcbc3
EB
352(let* ((o (dummy-origin))
353 (u (dummy-origin))
354 (i (dummy-origin))
355 (a (dummy-package "a"))
356 (b (dummy-package "b"
357 (inputs `(("a" ,a) ("i" ,i)))))
358 (c (package (inherit b) (source o)))
359 (d (dummy-package "d"
360 (build-system trivial-build-system)
361 (source u) (inputs `(("c" ,c))))))
362 (test-assert "package-direct-sources, no source"
363 (null? (package-direct-sources a)))
364 (test-equal "package-direct-sources, #f source"
365 (list i)
366 (package-direct-sources b))
367 (test-equal "package-direct-sources, not input source"
368 (list u)
369 (package-direct-sources d))
370 (test-assert "package-direct-sources"
371 (let ((s (package-direct-sources c)))
372 (and (= (length (pk 's-sources s)) 2)
373 (member o s)
374 (member i s))))
375 (test-assert "package-transitive-sources"
376 (let ((s (package-transitive-sources d)))
377 (and (= (length (pk 'd-sources s)) 3)
378 (member o s)
379 (member i s)
380 (member u s)))))
381
a6d0b306
EB
382(test-assert "transitive-input-references"
383 (let* ((a (dummy-package "a"))
384 (b (dummy-package "b"))
385 (c (dummy-package "c"
386 (inputs `(("a" ,a)))
387 (propagated-inputs `(("boo" ,b)))))
388 (d (dummy-package "d"
389 (inputs `(("c*" ,c)))))
390 (keys (map (match-lambda
391 (('assoc-ref 'l key)
392 key))
393 (pk 'refs (transitive-input-references
394 'l (package-inputs d))))))
395 (and (= (length keys) 2)
396 (member "c*" keys)
397 (member "boo" keys))))
398
9bf3ced0
LC
399(test-equal "package-transitive-supported-systems, implicit inputs"
400 %supported-systems
401
0db65c16
JN
402 ;; Here GNU-BUILD-SYSTEM adds implicit inputs that build only on
403 ;; %SUPPORTED-SYSTEMS. Thus the others must be ignored.
404 (let ((p (dummy-package "foo"
405 (build-system gnu-build-system)
406 (supported-systems
407 `("does-not-exist" "foobar" ,@%supported-systems)))))
0db65c16
JN
408 (parameterize ((%current-system "armhf-linux")) ; a traditionally-bootstrapped architecture
409 (package-transitive-supported-systems p))))
410
411(test-equal "package-transitive-supported-systems: reduced binary seed, implicit inputs"
412 '("x86_64-linux" "i686-linux")
413
9bf3ced0
LC
414 ;; Here GNU-BUILD-SYSTEM adds implicit inputs that build only on
415 ;; %SUPPORTED-SYSTEMS. Thus the others must be ignored.
416 (let ((p (dummy-package "foo"
417 (build-system gnu-build-system)
418 (supported-systems
419 `("does-not-exist" "foobar" ,@%supported-systems)))))
0db65c16
JN
420 (parameterize ((%current-system "x86_64-linux"))
421 (package-transitive-supported-systems p))))
9bf3ced0 422
bbceb0ef 423(test-assert "supported-package?"
bc60349b
LC
424 (let* ((d (dummy-package "dep"
425 (build-system trivial-build-system)
426 (supported-systems '("x86_64-linux"))))
427 (p (dummy-package "foo"
428 (build-system gnu-build-system)
429 (inputs `(("d" ,d)))
430 (supported-systems '("x86_64-linux" "armhf-linux")))))
431 (and (supported-package? p "x86_64-linux")
432 (not (supported-package? p "i686-linux"))
433 (not (supported-package? p "armhf-linux")))))
434
435(test-assert "supported-package? vs. system-dependent graph"
436 ;; The inputs of a package can depend on (%current-system). Thus,
437 ;; 'supported-package?' must make sure that it binds (%current-system)
438 ;; appropriately before traversing the dependency graph. In the example
439 ;; below, 'supported-package?' must thus return true for both systems.
440 (let* ((p0a (dummy-package "foo-arm"
441 (build-system trivial-build-system)
442 (supported-systems '("armhf-linux"))))
443 (p0b (dummy-package "foo-x86_64"
444 (build-system trivial-build-system)
445 (supported-systems '("x86_64-linux"))))
446 (p (dummy-package "bar"
447 (build-system trivial-build-system)
448 (inputs
449 (if (string=? (%current-system) "armhf-linux")
450 `(("foo" ,p0a))
451 `(("foo" ,p0b)))))))
bbceb0ef 452 (and (supported-package? p "x86_64-linux")
bc60349b 453 (supported-package? p "armhf-linux"))))
bbceb0ef 454
7357138b
LC
455(test-skip (if (not %store) 8 0))
456
457(test-assert "package-source-derivation, file"
458 (let* ((file (search-path %load-path "guix.scm"))
459 (package (package (inherit (dummy-package "p"))
460 (source file)))
461 (source (package-source-derivation %store
462 (package-source package))))
463 (and (store-path? source)
464 (valid-path? %store source)
465 (equal? (call-with-input-file source get-bytevector-all)
466 (call-with-input-file file get-bytevector-all)))))
467
468(test-assert "package-source-derivation, store path"
469 (let* ((file (add-to-store %store "guix.scm" #t "sha256"
470 (search-path %load-path "guix.scm")))
471 (package (package (inherit (dummy-package "p"))
472 (source file)))
473 (source (package-source-derivation %store
474 (package-source package))))
475 (string=? file source)))
e509d152 476
f80594cc
LC
477(test-assert "package-source-derivation, indirect store path"
478 (let* ((dir (add-to-store %store "guix-build" #t "sha256"
479 (dirname (search-path %load-path
480 "guix/build/utils.scm"))))
481 (package (package (inherit (dummy-package "p"))
482 (source (string-append dir "/utils.scm"))))
483 (source (package-source-derivation %store
484 (package-source package))))
485 (and (direct-store-path? source)
486 (string-suffix? "utils.scm" source))))
487
da675305
LC
488(test-assert "package-source-derivation, local-file"
489 (let* ((file (local-file "../guix/base32.scm"))
490 (package (package (inherit (dummy-package "p"))
491 (source file)))
492 (source (package-source-derivation %store
493 (package-source package))))
494 (and (store-path? source)
495 (string-suffix? "base32.scm" source)
496 (valid-path? %store source)
497 (equal? (call-with-input-file source get-bytevector-all)
498 (call-with-input-file
499 (search-path %load-path "guix/base32.scm")
500 get-bytevector-all)))))
501
ce0be567
LC
502(test-equal "package-source-derivation, origin, sha512"
503 "hello"
504 (let* ((bash (search-bootstrap-binary "bash" (%current-system)))
505 (builder (add-text-to-store %store "my-fixed-builder.sh"
506 "echo -n hello > $out" '()))
507 (method (lambda* (url hash-algo hash #:optional name
508 #:rest rest)
509 (and (eq? hash-algo 'sha512)
510 (raw-derivation name bash (list builder)
511 #:sources (list builder)
512 #:hash hash
513 #:hash-algo hash-algo))))
514 (source (origin
515 (method method)
516 (uri "unused://")
517 (file-name "origin-sha512")
518 (hash (content-hash
197c07a8
LC
519 (gcrypt:bytevector-hash (string->utf8 "hello")
520 (gcrypt:lookup-hash-algorithm
521 'sha512))
ce0be567
LC
522 sha512))))
523 (drv (package-source-derivation %store source))
524 (output (derivation->output-path drv)))
525 (build-derivations %store (list drv))
526 (call-with-input-file output get-string-all)))
527
0505eda9
LC
528(test-equal "package-source-derivation, origin, sha3-512"
529 "hello, sha3"
530 (let* ((bash (search-bootstrap-binary "bash" (%current-system)))
531 (builder (add-text-to-store %store "my-fixed-builder.sh"
532 "echo -n hello, sha3 > $out" '()))
533 (method (lambda* (url hash-algo hash #:optional name
534 #:rest rest)
535 (and (eq? hash-algo 'sha3-512)
536 (raw-derivation name bash (list builder)
537 #:sources (list builder)
538 #:hash hash
539 #:hash-algo hash-algo))))
540 (source (origin
541 (method method)
542 (uri "unused://")
543 (file-name "origin-sha3")
544 (hash (content-hash
545 (gcrypt:bytevector-hash (string->utf8 "hello, sha3")
546 (gcrypt:lookup-hash-algorithm
547 'sha3-512))
548 sha3-512))))
549 (drv (package-source-derivation %store source))
550 (output (derivation->output-path drv)))
551 (build-derivations %store (list drv))
552 (call-with-input-file output get-string-all)))
553
12d720fd 554(unless (network-reachable?) (test-skip 1))
f9cc8971
LC
555(test-equal "package-source-derivation, snippet"
556 "OK"
f8ec8d96 557 (let* ((source (bootstrap-origin
f9cc8971 558 (origin
f8ec8d96 559 (inherit (bootstrap-guile-origin (%current-system)))
f9cc8971
LC
560 (patch-inputs
561 `(("tar" ,%bootstrap-coreutils&co)
562 ("xz" ,%bootstrap-coreutils&co)
563 ("patch" ,%bootstrap-coreutils&co)))
7db9608d 564 (patch-guile %bootstrap-guile)
f9cc8971 565 (modules '((guix build utils)))
f9cc8971
LC
566 (snippet '(begin
567 ;; We end up in 'bin', because it's the first
568 ;; directory, alphabetically. Not a very good
569 ;; example but hey.
570 (chmod "." #o777)
571 (symlink "guile" "guile-rocks")
572 (copy-recursively "../share/guile/2.0/scripts"
d6445dff
LC
573 "scripts")
574
575 ;; Make sure '.file_list' can be created.
576 (chmod ".." #o777))))))
f9cc8971
LC
577 (package (package (inherit (dummy-package "with-snippet"))
578 (source source)
579 (build-system trivial-build-system)
580 (inputs
581 `(("tar" ,(search-bootstrap-binary "tar"
582 (%current-system)))
583 ("xz" ,(search-bootstrap-binary "xz"
584 (%current-system)))))
585 (arguments
586 `(#:guile ,%bootstrap-guile
1e868858 587 #:modules ((guix build utils))
f9cc8971 588 #:builder
1e868858
MW
589 (begin
590 (use-modules (guix build utils))
591 (let ((tar (assoc-ref %build-inputs "tar"))
592 (xz (assoc-ref %build-inputs "xz"))
593 (source (assoc-ref %build-inputs "source")))
594 (invoke tar "xvf" source
595 "--use-compress-program" xz)
596 (unless (and (string=? "guile" (readlink "bin/guile-rocks"))
597 (file-exists? "bin/scripts/compile.scm"))
598 (error "the snippet apparently failed"))
599 (let ((out (assoc-ref %outputs "out")))
600 (call-with-output-file out
601 (lambda (p)
602 (display "OK" p))))
603 #t))))))
f9cc8971
LC
604 (drv (package-derivation %store package))
605 (out (derivation->output-path drv)))
606 (and (build-derivations %store (list (pk 'snippet-drv drv)))
607 (call-with-input-file out get-string-all))))
608
59688fc4
LC
609(test-assert "return value"
610 (let ((drv (package-derivation %store (dummy-package "p"))))
611 (and (derivation? drv)
612 (file-exists? (derivation-file-name drv)))))
be13fbfa 613
d510ab46
LC
614(test-assert "package-output"
615 (let* ((package (dummy-package "p"))
59688fc4
LC
616 (drv (package-derivation %store package)))
617 (and (derivation? drv)
618 (string=? (derivation->output-path drv)
d510ab46
LC
619 (package-output %store package "out")))))
620
dbab5150
LC
621(test-assert "patch not found yields a run-time error"
622 (guard (c ((condition-has-type? c &message)
623 (and (string-contains (condition-message c)
624 "does-not-exist.patch")
625 (string-contains (condition-message c)
626 "not found"))))
627 (let ((p (package
628 (inherit (dummy-package "p"))
629 (source (origin
630 (method (const #f))
631 (uri "http://whatever")
632 (patches
633 (list (search-patch "does-not-exist.patch")))
634 (sha256
635 (base32
636 "0amn0bbwqvsvvsh6drfwz20ydc2czk374lzw5kksbh6bf78k4ks4")))))))
637 (package-derivation %store p)
638 #f)))
639
76c48619
LC
640(let ((dummy (dummy-package "foo" (inputs `(("x" ,(current-module)))))))
641 (test-equal "&package-input-error"
642 (list dummy (current-module))
643 (guard (c ((package-input-error? c)
644 (list (package-error-package c)
645 (package-error-invalid-input c))))
646 (package-derivation %store dummy))))
647
f304c9c2
LC
648(test-assert "reference to non-existent output"
649 ;; See <http://bugs.gnu.org/19630>.
862abf8f
LC
650 (parameterize ((%graft? #f))
651 (let* ((dep (dummy-package "dep"))
652 (p (dummy-package "p"
653 (inputs `(("dep" ,dep "non-existent"))))))
654 (guard (c ((derivation-missing-output-error? c)
655 (and (string=? (derivation-missing-output c) "non-existent")
656 (equal? (package-derivation %store dep)
657 (derivation-error-derivation c)))))
658 (package-derivation %store p)))))
f304c9c2 659
be13fbfa
LC
660(test-assert "trivial"
661 (let* ((p (package (inherit (dummy-package "trivial"))
662 (build-system trivial-build-system)
663 (source #f)
664 (arguments
14da91e2
LC
665 `(#:guile ,%bootstrap-guile
666 #:builder
be13fbfa
LC
667 (begin
668 (mkdir %output)
669 (call-with-output-file (string-append %output "/test")
670 (lambda (p)
1e868858
MW
671 (display '(hello guix) p)))
672 #t)))))
be13fbfa
LC
673 (d (package-derivation %store p)))
674 (and (build-derivations %store (list d))
59688fc4 675 (let ((p (pk 'drv d (derivation->output-path d))))
be13fbfa
LC
676 (equal? '(hello guix)
677 (call-with-input-file (string-append p "/test") read))))))
e3ce5d70 678
860a6f1a
LC
679(test-assert "trivial with local file as input"
680 (let* ((i (search-path %load-path "ice-9/boot-9.scm"))
681 (p (package (inherit (dummy-package "trivial-with-input-file"))
682 (build-system trivial-build-system)
683 (source #f)
684 (arguments
685 `(#:guile ,%bootstrap-guile
1e868858
MW
686 #:builder (begin
687 (copy-file (assoc-ref %build-inputs "input")
688 %output)
689 #t)))
860a6f1a
LC
690 (inputs `(("input" ,i)))))
691 (d (package-derivation %store p)))
692 (and (build-derivations %store (list d))
59688fc4 693 (let ((p (pk 'drv d (derivation->output-path d))))
860a6f1a
LC
694 (equal? (call-with-input-file p get-bytevector-all)
695 (call-with-input-file i get-bytevector-all))))))
696
03761a44
LC
697(test-assert "trivial with source"
698 (let* ((i (search-path %load-path "ice-9/boot-9.scm"))
699 (p (package (inherit (dummy-package "trivial-with-source"))
700 (build-system trivial-build-system)
701 (source i)
702 (arguments
703 `(#:guile ,%bootstrap-guile
1e868858
MW
704 #:builder (begin
705 (copy-file (assoc-ref %build-inputs "source")
706 %output)
707 #t)))))
03761a44
LC
708 (d (package-derivation %store p)))
709 (and (build-derivations %store (list d))
710 (let ((p (derivation->output-path d)))
711 (equal? (call-with-input-file p get-bytevector-all)
712 (call-with-input-file i get-bytevector-all))))))
713
592ef6c8
LC
714(test-assert "trivial with system-dependent input"
715 (let* ((p (package (inherit (dummy-package "trivial-system-dependent-input"))
716 (build-system trivial-build-system)
717 (source #f)
718 (arguments
719 `(#:guile ,%bootstrap-guile
1e868858 720 #:modules ((guix build utils))
592ef6c8 721 #:builder
1e868858
MW
722 (begin
723 (use-modules (guix build utils))
724 (let ((out (assoc-ref %outputs "out"))
725 (bash (assoc-ref %build-inputs "bash")))
726 (invoke bash "-c"
727 (format #f "echo hello > ~a" out))))))
dd6b9a37
LC
728 (inputs `(("bash" ,(search-bootstrap-binary "bash"
729 (%current-system)))))))
592ef6c8
LC
730 (d (package-derivation %store p)))
731 (and (build-derivations %store (list d))
59688fc4 732 (let ((p (pk 'drv d (derivation->output-path d))))
592ef6c8
LC
733 (eq? 'hello (call-with-input-file p read))))))
734
29760251
LC
735(test-assert "trivial with #:allowed-references"
736 (let* ((p (package
737 (inherit (dummy-package "trivial"))
738 (build-system trivial-build-system)
739 (arguments
740 `(#:guile ,%bootstrap-guile
741 #:allowed-references (,%bootstrap-guile)
742 #:builder
743 (begin
744 (mkdir %output)
745 ;; The reference to itself isn't allowed so building it
746 ;; should fail.
1e868858
MW
747 (symlink %output (string-append %output "/self"))
748 #t)))))
29760251 749 (d (package-derivation %store p)))
f9e8a123 750 (guard (c ((store-protocol-error? c) #t))
29760251
LC
751 (build-derivations %store (list d))
752 #f)))
753
a18eda27
LC
754(test-assert "search paths"
755 (let* ((p (make-prompt-tag "return-search-paths"))
756 (s (build-system
0d5a559f 757 (name 'raw)
a18eda27 758 (description "Raw build system with direct store access")
d3d337d2
LC
759 (lower (lambda* (name #:key source inputs system target
760 #:allow-other-keys)
0d5a559f
LC
761 (bag
762 (name name)
d3d337d2 763 (system system) (target target)
0d5a559f
LC
764 (build-inputs inputs)
765 (build
766 (lambda* (store name inputs
767 #:key outputs system search-paths)
768 search-paths)))))))
a18eda27
LC
769 (x (list (search-path-specification
770 (variable "GUILE_LOAD_PATH")
af070955 771 (files '("share/guile/site/2.0")))
a18eda27
LC
772 (search-path-specification
773 (variable "GUILE_LOAD_COMPILED_PATH")
af070955 774 (files '("share/guile/site/2.0")))))
a18eda27
LC
775 (a (package (inherit (dummy-package "guile"))
776 (build-system s)
777 (native-search-paths x)))
778 (b (package (inherit (dummy-package "guile-foo"))
779 (build-system s)
780 (inputs `(("guile" ,a)))))
781 (c (package (inherit (dummy-package "guile-bar"))
782 (build-system s)
783 (inputs `(("guile" ,a)
784 ("guile-foo" ,b))))))
785 (let-syntax ((collect (syntax-rules ()
786 ((_ body ...)
787 (call-with-prompt p
788 (lambda ()
789 body ...)
790 (lambda (k search-paths)
791 search-paths))))))
792 (and (null? (collect (package-derivation %store a)))
793 (equal? x (collect (package-derivation %store b)))
794 (equal? x (collect (package-derivation %store c)))))))
795
aa8e0515
LC
796(test-assert "package-transitive-native-search-paths"
797 (let* ((sp (lambda (name)
798 (list (search-path-specification
799 (variable name)
800 (files '("foo/bar"))))))
801 (p0 (dummy-package "p0" (native-search-paths (sp "PATH0"))))
802 (p1 (dummy-package "p1" (native-search-paths (sp "PATH1"))))
803 (p2 (dummy-package "p2"
804 (native-search-paths (sp "PATH2"))
805 (inputs `(("p0" ,p0)))
806 (propagated-inputs `(("p1" ,p1)))))
807 (p3 (dummy-package "p3"
808 (native-search-paths (sp "PATH3"))
809 (native-inputs `(("p0" ,p0)))
810 (propagated-inputs `(("p2" ,p2))))))
811 (lset= string=?
812 '("PATH1" "PATH2" "PATH3")
813 (map search-path-specification-variable
814 (package-transitive-native-search-paths p3)))))
815
9c1edabd 816(test-assert "package-cross-derivation"
59688fc4
LC
817 (let ((drv (package-cross-derivation %store (dummy-package "p")
818 "mips64el-linux-gnu")))
819 (and (derivation? drv)
820 (file-exists? (derivation-file-name drv)))))
9c1edabd 821
5dce8218
LC
822(test-assert "package-cross-derivation, trivial-build-system"
823 (let ((p (package (inherit (dummy-package "p"))
824 (build-system trivial-build-system)
825 (arguments '(#:builder (exit 1))))))
59688fc4
LC
826 (let ((drv (package-cross-derivation %store p "mips64el-linux-gnu")))
827 (derivation? drv))))
5dce8218 828
9b222abe
LC
829(test-assert "package-cross-derivation, no cross builder"
830 (let* ((b (build-system (inherit trivial-build-system)
0d5a559f 831 (lower (const #f))))
9b222abe
LC
832 (p (package (inherit (dummy-package "p"))
833 (build-system b))))
834 (guard (c ((package-cross-build-system-error? c)
835 (eq? (package-error-package c) p)))
836 (package-cross-derivation %store p "mips64el-linux-gnu")
837 #f)))
838
b4c42a4b
LC
839;; XXX: The next two tests can trigger builds when the distro defines
840;; replacements on core packages, so they're disable for lack of a better
841;; solution.
842
843;; (test-equal "package-derivation, direct graft"
844;; (package-derivation %store gnu-make #:graft? #f)
845;; (let ((p (package (inherit coreutils)
846;; (replacement gnu-make))))
847;; (package-derivation %store p #:graft? #t)))
05962f29 848
b4c42a4b
LC
849;; (test-equal "package-cross-derivation, direct graft"
850;; (package-cross-derivation %store gnu-make "mips64el-linux-gnu"
851;; #:graft? #f)
852;; (let ((p (package (inherit coreutils)
853;; (replacement gnu-make))))
854;; (package-cross-derivation %store p "mips64el-linux-gnu"
855;; #:graft? #t)))
05962f29
LC
856
857(test-assert "package-grafts, indirect grafts"
858 (let* ((new (dummy-package "dep"
859 (arguments '(#:implicit-inputs? #f))))
860 (dep (package (inherit new) (version "0.0")))
861 (dep* (package (inherit dep) (replacement new)))
862 (dummy (dummy-package "dummy"
863 (arguments '(#:implicit-inputs? #f))
864 (inputs `(("dep" ,dep*))))))
865 (equal? (package-grafts %store dummy)
866 (list (graft
867 (origin (package-derivation %store dep))
868 (replacement (package-derivation %store new)))))))
869
d0025d01
LC
870;; XXX: This test would require building the cross toolchain just to see if it
871;; needs grafting, which is obviously too expensive, and thus disabled.
872;;
873;; (test-assert "package-grafts, indirect grafts, cross"
874;; (let* ((new (dummy-package "dep"
875;; (arguments '(#:implicit-inputs? #f))))
876;; (dep (package (inherit new) (version "0.0")))
877;; (dep* (package (inherit dep) (replacement new)))
878;; (dummy (dummy-package "dummy"
879;; (arguments '(#:implicit-inputs? #f))
880;; (inputs `(("dep" ,dep*)))))
881;; (target "mips64el-linux-gnu"))
882;; ;; XXX: There might be additional grafts, for instance if the distro
883;; ;; defines replacements for core packages like Perl.
884;; (member (graft
885;; (origin (package-cross-derivation %store dep target))
886;; (replacement
887;; (package-cross-derivation %store new target)))
888;; (package-grafts %store dummy #:target target))))
05962f29
LC
889
890(test-assert "package-grafts, indirect grafts, propagated inputs"
891 (let* ((new (dummy-package "dep"
892 (arguments '(#:implicit-inputs? #f))))
893 (dep (package (inherit new) (version "0.0")))
894 (dep* (package (inherit dep) (replacement new)))
895 (prop (dummy-package "propagated"
896 (propagated-inputs `(("dep" ,dep*)))
897 (arguments '(#:implicit-inputs? #f))))
898 (dummy (dummy-package "dummy"
899 (arguments '(#:implicit-inputs? #f))
900 (inputs `(("prop" ,prop))))))
901 (equal? (package-grafts %store dummy)
902 (list (graft
903 (origin (package-derivation %store dep))
904 (replacement (package-derivation %store new)))))))
905
fcadd9ff
LC
906(test-assert "package-grafts, same replacement twice"
907 (let* ((new (dummy-package "dep"
908 (version "1")
909 (arguments '(#:implicit-inputs? #f))))
910 (dep (package (inherit new) (version "0") (replacement new)))
911 (p1 (dummy-package "intermediate1"
912 (arguments '(#:implicit-inputs? #f))
913 (inputs `(("dep" ,dep)))))
914 (p2 (dummy-package "intermediate2"
915 (arguments '(#:implicit-inputs? #f))
916 ;; Here we copy DEP to have an equivalent package that is not
917 ;; 'eq?' to DEP. This is similar to what happens with
918 ;; 'package-with-explicit-inputs' & co.
919 (inputs `(("dep" ,(package (inherit dep)))))))
920 (p3 (dummy-package "final"
921 (arguments '(#:implicit-inputs? #f))
922 (inputs `(("p1" ,p1) ("p2" ,p2))))))
923 (equal? (package-grafts %store p3)
924 (list (graft
925 (origin (package-derivation %store
926 (package (inherit dep)
927 (replacement #f))))
928 (replacement (package-derivation %store new)))))))
929
03a70e4c
LC
930(test-assert "package-grafts, dependency on several outputs"
931 ;; Make sure we get one graft per output; see <https://bugs.gnu.org/41796>.
932 (letrec* ((p0 (dummy-package "p0"
933 (version "1.0")
934 (replacement p0*)
935 (arguments '(#:implicit-inputs? #f))
936 (outputs '("out" "lib"))))
937 (p0* (package (inherit p0) (version "1.1")))
938 (p1 (dummy-package "p1"
939 (arguments '(#:implicit-inputs? #f))
940 (inputs `(("p0" ,p0)
941 ("p0:lib" ,p0 "lib"))))))
942 (lset= equal? (pk (package-grafts %store p1))
943 (list (graft
944 (origin (package-derivation %store p0))
945 (origin-output "out")
946 (replacement (package-derivation %store p0*))
947 (replacement-output "out"))
948 (graft
949 (origin (package-derivation %store p0))
950 (origin-output "lib")
951 (replacement (package-derivation %store p0*))
952 (replacement-output "lib"))))))
953
d0025d01
LC
954(test-assert "replacement also grafted"
955 ;; We build a DAG as below, where dotted arrows represent replacements and
956 ;; solid arrows represent dependencies:
957 ;;
958 ;; P1 ·············> P1R
959 ;; |\__________________.
960 ;; v v
961 ;; P2 ·············> P2R
962 ;; |
963 ;; v
964 ;; P3
965 ;;
966 ;; We want to make sure that:
967 ;; grafts(P3) = (P1,P1R) + (P2, grafted(P2R, (P1,P1R)))
968 ;; where:
969 ;; (A,B) is a graft to replace A by B
970 ;; grafted(DRV,G) denoted DRV with graft G applied
971 (let* ((p1r (dummy-package "P1"
972 (build-system trivial-build-system)
973 (arguments
974 `(#:guile ,%bootstrap-guile
975 #:builder (let ((out (assoc-ref %outputs "out")))
976 (mkdir out)
977 (call-with-output-file
978 (string-append out "/replacement")
979 (const #t)))))))
980 (p1 (package
981 (inherit p1r) (name "p1") (replacement p1r)
982 (arguments
983 `(#:guile ,%bootstrap-guile
1e868858
MW
984 #:builder (begin
985 (mkdir (assoc-ref %outputs "out"))
986 #t)))))
d0025d01
LC
987 (p2r (dummy-package "P2"
988 (build-system trivial-build-system)
989 (inputs `(("p1" ,p1)))
990 (arguments
991 `(#:guile ,%bootstrap-guile
992 #:builder (let ((out (assoc-ref %outputs "out")))
993 (mkdir out)
994 (chdir out)
995 (symlink (assoc-ref %build-inputs "p1") "p1")
996 (call-with-output-file (string-append out "/replacement")
997 (const #t)))))))
998 (p2 (package
999 (inherit p2r) (name "p2") (replacement p2r)
1000 (arguments
1001 `(#:guile ,%bootstrap-guile
1002 #:builder (let ((out (assoc-ref %outputs "out")))
1003 (mkdir out)
1004 (chdir out)
1005 (symlink (assoc-ref %build-inputs "p1")
1e868858
MW
1006 "p1")
1007 #t)))))
d0025d01
LC
1008 (p3 (dummy-package "p3"
1009 (build-system trivial-build-system)
1010 (inputs `(("p2" ,p2)))
1011 (arguments
1012 `(#:guile ,%bootstrap-guile
1013 #:builder (let ((out (assoc-ref %outputs "out")))
1014 (mkdir out)
1015 (chdir out)
1016 (symlink (assoc-ref %build-inputs "p2")
1e868858
MW
1017 "p2")
1018 #t))))))
d0025d01
LC
1019 (lset= equal?
1020 (package-grafts %store p3)
1021 (list (graft
1022 (origin (package-derivation %store p1 #:graft? #f))
1023 (replacement (package-derivation %store p1r)))
1024 (graft
1025 (origin (package-derivation %store p2 #:graft? #f))
1026 (replacement
1027 (package-derivation %store p2r #:graft? #t)))))))
1028
c22a1324
LC
1029;;; XXX: Nowadays 'graft-derivation' needs to build derivations beforehand to
1030;;; find out about their run-time dependencies, so this test is no longer
1031;;; applicable since it would trigger a full rebuild.
1032;;
1033;; (test-assert "package-derivation, indirect grafts"
1034;; (let* ((new (dummy-package "dep"
1035;; (arguments '(#:implicit-inputs? #f))))
1036;; (dep (package (inherit new) (version "0.0")))
1037;; (dep* (package (inherit dep) (replacement new)))
1038;; (dummy (dummy-package "dummy"
1039;; (arguments '(#:implicit-inputs? #f))
1040;; (inputs `(("dep" ,dep*)))))
1041;; (guile (package-derivation %store (canonical-package guile-2.0)
1042;; #:graft? #f)))
1043;; (equal? (package-derivation %store dummy)
1044;; (graft-derivation %store
1045;; (package-derivation %store dummy #:graft? #f)
1046;; (package-grafts %store dummy)
1047
1048;; ;; Use the same Guile as 'package-derivation'.
1049;; #:guile guile))))
05962f29 1050
d3d337d2
LC
1051(test-equal "package->bag"
1052 `("foo86-hurd" #f (,(package-source gnu-make))
1053 (,(canonical-package glibc)) (,(canonical-package coreutils)))
1054 (let ((bag (package->bag gnu-make "foo86-hurd")))
1055 (list (bag-system bag) (bag-target bag)
1056 (assoc-ref (bag-build-inputs bag) "source")
1057 (assoc-ref (bag-build-inputs bag) "libc")
1058 (assoc-ref (bag-build-inputs bag) "coreutils"))))
1059
b49caaa2
LC
1060(test-assert "package->bag, sensitivity to %current-target-system"
1061 ;; https://bugs.gnu.org/41713
1062 (let* ((lower (lambda* (name #:key system target inputs native-inputs
1063 #:allow-other-keys)
1064 (and (not target)
1065 (bag (name name) (system system) (target target)
1066 (build-inputs native-inputs)
1067 (host-inputs inputs)
1068 (build (lambda* (store name inputs
1069 #:key system target
1070 #:allow-other-keys)
1071 (build-expression->derivation
1072 store "foo" '(mkdir %output))))))))
1073 (bs (build-system
1074 (name 'build-system-without-cross-compilation)
1075 (description "Does not support cross compilation.")
1076 (lower lower)))
1077 (dep (dummy-package "dep" (build-system bs)))
1078 (pkg (dummy-package "example"
1079 (native-inputs `(("dep" ,dep)))))
1080 (do-not-build (lambda (continue store lst . _) lst)))
1081 (equal? (with-build-handler do-not-build
1082 (parameterize ((%current-target-system "powerpc64le-linux-gnu")
1083 (%graft? #t))
1084 (package-cross-derivation %store pkg
1085 (%current-target-system)
1086 #:graft? #t)))
1087 (with-build-handler do-not-build
1088 (package-cross-derivation %store
1089 (package (inherit pkg))
1090 "powerpc64le-linux-gnu"
1091 #:graft? #t)))))
1092
d3d337d2
LC
1093(test-equal "package->bag, cross-compilation"
1094 `(,(%current-system) "foo86-hurd"
1095 (,(package-source gnu-make))
1096 (,(canonical-package glibc)) (,(canonical-package coreutils)))
1097 (let ((bag (package->bag gnu-make (%current-system) "foo86-hurd")))
1098 (list (bag-system bag) (bag-target bag)
1099 (assoc-ref (bag-build-inputs bag) "source")
1100 (assoc-ref (bag-build-inputs bag) "libc")
1101 (assoc-ref (bag-build-inputs bag) "coreutils"))))
1102
50373bab
LC
1103(test-assert "package->bag, propagated inputs"
1104 (let* ((dep (dummy-package "dep"))
1105 (prop (dummy-package "prop"
1106 (propagated-inputs `(("dep" ,dep)))))
1107 (dummy (dummy-package "dummy"
1108 (inputs `(("prop" ,prop)))))
1109 (inputs (bag-transitive-inputs (package->bag dummy #:graft? #f))))
161094c8
LC
1110 (match (assoc "dep" inputs)
1111 (("dep" package)
50373bab
LC
1112 (eq? package dep)))))
1113
efb10f17
LC
1114(test-assert "package->bag, sensitivity to %current-system"
1115 (let* ((dep (dummy-package "dep"
1116 (propagated-inputs (if (string=? (%current-system)
1117 "i586-gnu")
1118 `(("libxml2" ,libxml2))
1119 '()))))
1120 (pkg (dummy-package "foo"
1121 (native-inputs `(("dep" ,dep)))))
1122 (bag (package->bag pkg (%current-system) "i586-gnu")))
1123 (equal? (parameterize ((%current-system "x86_64-linux"))
1124 (bag-transitive-inputs bag))
1125 (parameterize ((%current-system "i586-gnu"))
1126 (bag-transitive-inputs bag)))))
1127
f52fbf70
LC
1128(test-assert "package->bag, sensitivity to %current-target-system"
1129 (let* ((dep (dummy-package "dep"
1130 (propagated-inputs (if (%current-target-system)
1131 `(("libxml2" ,libxml2))
1132 '()))))
1133 (pkg (dummy-package "foo"
1134 (native-inputs `(("dep" ,dep)))))
1135 (bag (package->bag pkg (%current-system) "foo86-hurd")))
1136 (equal? (parameterize ((%current-target-system "foo64-gnu"))
1137 (bag-transitive-inputs bag))
1138 (parameterize ((%current-target-system #f))
1139 (bag-transitive-inputs bag)))))
1140
d3d337d2 1141(test-assert "bag->derivation"
05962f29
LC
1142 (parameterize ((%graft? #f))
1143 (let ((bag (package->bag gnu-make))
1144 (drv (package-derivation %store gnu-make)))
1145 (parameterize ((%current-system "foox86-hurd")) ;should have no effect
1146 (equal? drv (bag->derivation %store bag))))))
d3d337d2
LC
1147
1148(test-assert "bag->derivation, cross-compilation"
05962f29
LC
1149 (parameterize ((%graft? #f))
1150 (let* ((target "mips64el-linux-gnu")
1151 (bag (package->bag gnu-make (%current-system) target))
1152 (drv (package-cross-derivation %store gnu-make target)))
1153 (parameterize ((%current-system "foox86-hurd") ;should have no effect
1154 (%current-target-system "foo64-linux-gnu"))
1155 (equal? drv (bag->derivation %store bag))))))
d3d337d2 1156
b69c5c2c
LC
1157(when (or (not (network-reachable?)) (shebang-too-long?))
1158 (test-skip 1))
9e782349 1159(test-assert "GNU Make, bootstrap"
03d76577
LC
1160 ;; GNU-MAKE-FOR-TESTS can be built cheaply; we choose it here so that the
1161 ;; test doesn't last for too long.
1162 (let ((gnu-make gnu-make-for-tests))
9e782349
LC
1163 (and (package? gnu-make)
1164 (or (location? (package-location gnu-make))
1165 (not (package-location gnu-make)))
1166 (let* ((drv (package-derivation %store gnu-make))
59688fc4 1167 (out (derivation->output-path drv)))
14da91e2 1168 (and (build-derivations %store (list drv))
9e782349 1169 (file-exists? (string-append out "/bin/make")))))))
e3ce5d70 1170
f37f2b83
LC
1171(test-equal "package-mapping"
1172 42
1173 (let* ((dep (dummy-package "chbouib"
1174 (native-inputs `(("x" ,grep)))))
1175 (p0 (dummy-package "example"
1176 (inputs `(("foo" ,coreutils)
1177 ("bar" ,grep)
1178 ("baz" ,dep)))))
1179 (transform (lambda (p)
1180 (package (inherit p) (source 42))))
1181 (rewrite (package-mapping transform))
1182 (p1 (rewrite p0)))
1183 (and (eq? p1 (rewrite p0))
1184 (eqv? 42 (package-source p1))
1185 (match (package-inputs p1)
1186 ((("foo" dep1) ("bar" dep2) ("baz" dep3))
1187 (and (eq? dep1 (rewrite coreutils)) ;memoization
1188 (eq? dep2 (rewrite grep))
1189 (eq? dep3 (rewrite dep))
1190 (eqv? 42
1191 (package-source dep1) (package-source dep2)
1192 (package-source dep3))
1193 (match (package-native-inputs dep3)
1194 ((("x" dep))
1195 (and (eq? dep (rewrite grep))
1196 (package-source dep))))))))))
1197
2a75b0b6
LC
1198(test-assert "package-input-rewriting"
1199 (let* ((dep (dummy-package "chbouib"
1200 (native-inputs `(("x" ,grep)))))
1201 (p0 (dummy-package "example"
1202 (inputs `(("foo" ,coreutils)
1203 ("bar" ,grep)
1204 ("baz" ,dep)))))
1205 (rewrite (package-input-rewriting `((,coreutils . ,sed)
1206 (,grep . ,findutils))
1207 (cut string-append "r-" <>)))
1208 (p1 (rewrite p0))
1209 (p2 (rewrite p0)))
1210 (and (not (eq? p1 p0))
1211 (eq? p1 p2) ;memoization
1212 (string=? "r-example" (package-name p1))
1213 (match (package-inputs p1)
1214 ((("foo" dep1) ("bar" dep2) ("baz" dep3))
1215 (and (eq? dep1 sed)
1216 (eq? dep2 findutils)
1217 (string=? (package-name dep3) "r-chbouib")
1218 (eq? dep3 (rewrite dep)) ;memoization
1219 (match (package-native-inputs dep3)
1220 ((("x" dep))
1221 (eq? dep findutils)))))))))
1222
f258d886
LC
1223(test-assert "package-input-rewriting/spec"
1224 (let* ((dep (dummy-package "chbouib"
1225 (native-inputs `(("x" ,grep)))))
1226 (p0 (dummy-package "example"
1227 (inputs `(("foo" ,coreutils)
1228 ("bar" ,grep)
1229 ("baz" ,dep)))))
1230 (rewrite (package-input-rewriting/spec
1231 `(("coreutils" . ,(const sed))
1232 ("grep" . ,(const findutils)))))
1233 (p1 (rewrite p0))
1234 (p2 (rewrite p0)))
1235 (and (not (eq? p1 p0))
1236 (eq? p1 p2) ;memoization
1237 (string=? "example" (package-name p1))
1238 (match (package-inputs p1)
1239 ((("foo" dep1) ("bar" dep2) ("baz" dep3))
1240 (and (string=? (package-full-name dep1)
1241 (package-full-name sed))
1242 (string=? (package-full-name dep2)
1243 (package-full-name findutils))
1244 (string=? (package-name dep3) "chbouib")
1245 (eq? dep3 (rewrite dep)) ;memoization
1246 (match (package-native-inputs dep3)
1247 ((("x" dep))
1248 (string=? (package-full-name dep)
1249 (package-full-name findutils))))))))))
1250
1251(test-assert "package-input-rewriting/spec, partial match"
1252 (let* ((dep (dummy-package "chbouib"
1253 (version "1")
1254 (native-inputs `(("x" ,grep)))))
1255 (p0 (dummy-package "example"
1256 (inputs `(("foo" ,coreutils)
1257 ("bar" ,dep)))))
1258 (rewrite (package-input-rewriting/spec
1259 `(("chbouib@123" . ,(const sed)) ;not matched
1260 ("grep" . ,(const findutils)))))
1261 (p1 (rewrite p0)))
1262 (and (not (eq? p1 p0))
1263 (string=? "example" (package-name p1))
1264 (match (package-inputs p1)
1265 ((("foo" dep1) ("bar" dep2))
1266 (and (string=? (package-full-name dep1)
1267 (package-full-name coreutils))
1268 (eq? dep2 (rewrite dep)) ;memoization
1269 (match (package-native-inputs dep2)
1270 ((("x" dep))
1271 (string=? (package-full-name dep)
1272 (package-full-name findutils))))))))))
1273
c423ae89
LC
1274(test-equal "package-patched-vulnerabilities"
1275 '(("CVE-2015-1234")
1276 ("CVE-2016-1234" "CVE-2018-4567")
1277 ())
1278 (let ((p1 (dummy-package "pi"
1279 (source (dummy-origin
1280 (patches (list "/a/b/pi-CVE-2015-1234.patch"))))))
1281 (p2 (dummy-package "pi"
1282 (source (dummy-origin
1283 (patches (list
1284 "/a/b/pi-CVE-2016-1234-CVE-2018-4567.patch"))))))
1285 (p3 (dummy-package "pi" (source (dummy-origin)))))
1286 (map package-patched-vulnerabilities
1287 (list p1 p2 p3))))
1288
ba326ce4
LC
1289(test-eq "fold-packages" hello
1290 (fold-packages (lambda (p r)
1291 (if (string=? (package-name p) "hello")
1292 p
1293 r))
1294 #f))
1295
386b71d1
LC
1296(test-assert "fold-packages, hidden package"
1297 ;; There are two public variables providing "guile@2.0" ('guile-final' in
b66d6d52 1298 ;; commencement.scm and 'guile-2.0' in guile.scm), but only the latter
386b71d1
LC
1299 ;; should show up.
1300 (match (fold-packages (lambda (p r)
1301 (if (and (string=? (package-name p) "guile")
1302 (string-prefix? "2.0"
1303 (package-version p)))
1304 (cons p r)
1305 r))
1306 '())
1307 ((one)
b66d6d52 1308 (eq? one guile-2.0))))
386b71d1 1309
0ea939fb
LC
1310(test-assert "fold-available-packages with/without cache"
1311 (let ()
1312 (define no-cache
1313 (fold-available-packages (lambda* (name version result #:rest rest)
1314 (cons (cons* name version rest)
1315 result))
1316 '()))
1317
1318 (define from-cache
1319 (call-with-temporary-directory
1320 (lambda (cache)
1321 (generate-package-cache cache)
1322 (mock ((guix describe) current-profile (const cache))
1323 (mock ((gnu packages) cache-is-authoritative? (const #t))
1324 (fold-available-packages (lambda* (name version result
1325 #:rest rest)
1326 (cons (cons* name version rest)
1327 result))
1328 '()))))))
1329
36754eee
LC
1330 (and (equal? (delete-duplicates from-cache) from-cache)
1331 (lset= equal? no-cache from-cache))))
0ea939fb 1332
6b1891b0
LC
1333(test-assert "find-packages-by-name"
1334 (match (find-packages-by-name "hello")
1335 (((? (cut eq? hello <>))) #t)
1336 (wrong (pk 'find-packages-by-name wrong #f))))
1337
1338(test-assert "find-packages-by-name with version"
1339 (match (find-packages-by-name "hello" (package-version hello))
1340 (((? (cut eq? hello <>))) #t)
1341 (wrong (pk 'find-packages-by-name wrong #f))))
1342
5fbdc9a5
LC
1343(test-equal "find-packages-by-name with cache"
1344 (find-packages-by-name "guile")
1345 (call-with-temporary-directory
1346 (lambda (cache)
1347 (generate-package-cache cache)
1348 (mock ((guix describe) current-profile (const cache))
1349 (mock ((gnu packages) cache-is-authoritative? (const #t))
1350 (find-packages-by-name "guile"))))))
1351
1352(test-equal "find-packages-by-name + version, with cache"
1353 (find-packages-by-name "guile" "2")
1354 (call-with-temporary-directory
1355 (lambda (cache)
1356 (generate-package-cache cache)
1357 (mock ((guix describe) current-profile (const cache))
1358 (mock ((gnu packages) cache-is-authoritative? (const #t))
1359 (find-packages-by-name "guile" "2"))))))
1360
cf81a236
LC
1361(test-assert "--search-paths with pattern"
1362 ;; Make sure 'guix package --search-paths' correctly reports environment
1363 ;; variables when file patterns are used (in particular, it must follow
1364 ;; symlinks when looking for 'catalog.xml'.) To do that, we rely on the
1365 ;; libxml2 package specification, which contains such a definition.
1366 (let* ((p1 (package
1367 (name "foo") (version "0") (source #f)
1368 (build-system trivial-build-system)
1369 (arguments
1370 `(#:guile ,%bootstrap-guile
1371 #:modules ((guix build utils))
1372 #:builder (begin
1373 (use-modules (guix build utils))
1374 (let ((out (assoc-ref %outputs "out")))
1375 (mkdir-p (string-append out "/xml/bar/baz"))
1376 (call-with-output-file
1377 (string-append out "/xml/bar/baz/catalog.xml")
1378 (lambda (port)
1e868858
MW
1379 (display "xml? wat?!" port)))
1380 #t))))
cf81a236
LC
1381 (synopsis #f) (description #f)
1382 (home-page #f) (license #f)))
1383 (p2 (package
1384 ;; Provide a fake libxml2 to avoid building the real one. This
1385 ;; is OK because 'guix package' gets search path specifications
1386 ;; from the same-named package found in the distro.
1387 (name "libxml2") (version "0.0.0") (source #f)
1388 (build-system trivial-build-system)
1389 (arguments
1390 `(#:guile ,%bootstrap-guile
1e868858
MW
1391 #:builder (begin
1392 (mkdir (assoc-ref %outputs "out"))
1393 #t)))
cf81a236
LC
1394 (native-search-paths (package-native-search-paths libxml2))
1395 (synopsis #f) (description #f)
1396 (home-page #f) (license #f)))
1397 (prof (run-with-store %store
1398 (profile-derivation
1399 (manifest (map package->manifest-entry
1400 (list p1 p2)))
a6562c7e
LC
1401 #:hooks '()
1402 #:locales? #f)
cf81a236
LC
1403 #:guile-for-build (%guile-for-build))))
1404 (build-derivations %store (list prof))
1405 (string-match (format #f "^export XML_CATALOG_FILES=\"~a/xml/+bar/baz/catalog\\.xml\"\n"
77559f23 1406 (regexp-quote (derivation->output-path prof)))
cf81a236
LC
1407 (with-output-to-string
1408 (lambda ()
1409 (guix-package "-p" (derivation->output-path prof)
1410 "--search-paths"))))))
1411
fcd75bdb
LC
1412(test-assert "--search-paths with single-item search path"
1413 ;; Make sure 'guix package --search-paths' correctly reports environment
1414 ;; variables for things like 'GIT_SSL_CAINFO' that have #f as their
1415 ;; separator, meaning that the first match wins.
1416 (let* ((p1 (dummy-package "foo"
1417 (build-system trivial-build-system)
1418 (arguments
1419 `(#:guile ,%bootstrap-guile
1420 #:modules ((guix build utils))
1421 #:builder (begin
1422 (use-modules (guix build utils))
1423 (let ((out (assoc-ref %outputs "out")))
1424 (mkdir-p (string-append out "/etc/ssl/certs"))
1425 (call-with-output-file
1426 (string-append
1427 out "/etc/ssl/certs/ca-certificates.crt")
1428 (const #t))))))))
1429 (p2 (package (inherit p1) (name "bar")))
1430 (p3 (dummy-package "git"
1431 ;; Provide a fake Git to avoid building the real one.
1432 (build-system trivial-build-system)
1433 (arguments
1434 `(#:guile ,%bootstrap-guile
1e868858
MW
1435 #:builder (begin
1436 (mkdir (assoc-ref %outputs "out"))
1437 #t)))
fcd75bdb
LC
1438 (native-search-paths (package-native-search-paths git))))
1439 (prof1 (run-with-store %store
1440 (profile-derivation
1441 (packages->manifest (list p1 p3))
1442 #:hooks '()
1443 #:locales? #f)
1444 #:guile-for-build (%guile-for-build)))
1445 (prof2 (run-with-store %store
1446 (profile-derivation
1447 (packages->manifest (list p2 p3))
1448 #:hooks '()
1449 #:locales? #f)
1450 #:guile-for-build (%guile-for-build))))
1451 (build-derivations %store (list prof1 prof2))
1452 (string-match (format #f "^export GIT_SSL_CAINFO=\"~a/etc/ssl/certs/ca-certificates.crt"
1453 (regexp-quote (derivation->output-path prof1)))
1454 (with-output-to-string
1455 (lambda ()
1456 (guix-package "-p" (derivation->output-path prof1)
1457 "-p" (derivation->output-path prof2)
1458 "--search-paths"))))))
1459
efb107e0
LC
1460(test-equal "specification->package when not found"
1461 'quit
1462 (catch 'quit
1463 (lambda ()
1464 ;; This should call 'leave', producing an error message.
1465 (specification->package "this-package-does-not-exist"))
1466 (lambda (key . args)
1467 key)))
1468
066eeae1
LC
1469(test-equal "specification->package+output"
1470 `((,coreutils "out") (,coreutils "debug"))
1471 (list (call-with-values (lambda ()
1472 (specification->package+output "coreutils"))
1473 list)
1474 (call-with-values (lambda ()
1475 (specification->package+output "coreutils:debug"))
1476 list)))
1477
1478(test-equal "specification->package+output invalid output"
1479 'error
1480 (catch 'quit
1481 (lambda ()
1482 (specification->package+output "coreutils:does-not-exist"))
1483 (lambda _
1484 'error)))
1485
1486(test-equal "specification->package+output no default output"
1487 `(,coreutils #f)
1488 (call-with-values
1489 (lambda ()
1490 (specification->package+output "coreutils" #f))
1491 list))
1492
1493(test-equal "specification->package+output invalid output, no default"
1494 'error
1495 (catch 'quit
1496 (lambda ()
1497 (specification->package+output "coreutils:does-not-exist" #f))
1498 (lambda _
1499 'error)))
1500
ee8099f5
LC
1501(test-equal "find-package-locations"
1502 (map (lambda (package)
1503 (cons (package-version package)
1504 (package-location package)))
1505 (find-packages-by-name "guile"))
1506 (find-package-locations "guile"))
1507
1508(test-equal "find-package-locations with cache"
1509 (map (lambda (package)
1510 (cons (package-version package)
1511 (package-location package)))
1512 (find-packages-by-name "guile"))
1513 (call-with-temporary-directory
1514 (lambda (cache)
1515 (generate-package-cache cache)
1516 (mock ((guix describe) current-profile (const cache))
1517 (mock ((gnu packages) cache-is-authoritative? (const #t))
1518 (find-package-locations "guile"))))))
1519
1520(test-equal "specification->location"
1521 (package-location (specification->package "guile@2"))
1522 (specification->location "guile@2"))
1523
e3ce5d70
LC
1524(test-end "packages")
1525
e3ce5d70 1526;;; Local Variables:
a3d73f59 1527;;; eval: (put 'dummy-package 'scheme-indent-function 1)
3e223a22 1528;;; eval: (put 'dummy-package/no-implicit 'scheme-indent-function 1)
e3ce5d70 1529;;; End: