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