gnu: Add parole.
[jackhill/guix/guix.git] / tests / packages.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
bc1ad696 2;;; Copyright © 2012-2022 Ludovic Courtès <ludo@gnu.org>
cfcead2e
MC
3;;; Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
4;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
aaf9aa48 5;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
e3ce5d70 6;;;
233e7676 7;;; This file is part of GNU Guix.
e3ce5d70 8;;;
233e7676 9;;; GNU Guix is free software; you can redistribute it and/or modify it
e3ce5d70
LC
10;;; under the terms of the GNU General Public License as published by
11;;; the Free Software Foundation; either version 3 of the License, or (at
12;;; your option) any later version.
13;;;
233e7676 14;;; GNU Guix is distributed in the hope that it will be useful, but
e3ce5d70
LC
15;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;; GNU General Public License for more details.
18;;;
19;;; You should have received a copy of the GNU General Public License
233e7676 20;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
e3ce5d70 21
cfcead2e 22(define-module (tests packages)
c1bc358f 23 #:use-module (guix tests)
e3ce5d70 24 #:use-module (guix store)
cf81a236 25 #:use-module (guix monads)
fcadd9ff 26 #:use-module (guix grafts)
cfcead2e 27 #:use-module (guix gexp)
a5e2fc73 28 #:use-module (guix utils)
cfcead2e 29 #:use-module ((guix build utils) #:select (tarball?))
a5e2fc73 30 #:use-module ((guix diagnostics)
2e1bafb0
LC
31 ;; Rename the 'location' binding to allow proper syntax
32 ;; matching when setting the 'location' field of a package.
33 #:renamer (lambda (name)
34 (cond ((eq? name 'location) 'make-location)
35 (else name))))
197c07a8 36 #:use-module ((gcrypt hash) #:prefix gcrypt:)
e3ce5d70 37 #:use-module (guix derivations)
cfcead2e 38 #:use-module (guix download)
e3ce5d70 39 #:use-module (guix packages)
7adf9b84 40 #:use-module (guix grafts)
aa8e0515 41 #:use-module (guix search-paths)
a18eda27 42 #:use-module (guix build-system)
be13fbfa 43 #:use-module (guix build-system trivial)
a3d73f59 44 #:use-module (guix build-system gnu)
2bf6f962 45 #:use-module (guix build-system python)
0db65c16 46 #:use-module (guix memoization)
cf81a236
LC
47 #:use-module (guix profiles)
48 #:use-module (guix scripts package)
73744725 49 #:use-module (guix sets)
59a43334 50 #:use-module (gnu packages)
1ffa7090 51 #:use-module (gnu packages base)
05962f29 52 #:use-module (gnu packages guile)
1ffa7090 53 #:use-module (gnu packages bootstrap)
2bf6f962 54 #:use-module (gnu packages python)
fcd75bdb 55 #:use-module (gnu packages version-control)
cf81a236 56 #:use-module (gnu packages xml)
05962f29 57 #:use-module (srfi srfi-1)
cfcead2e 58 #:use-module (srfi srfi-11)
6b1891b0 59 #:use-module (srfi srfi-26)
9b222abe 60 #:use-module (srfi srfi-34)
dbab5150 61 #:use-module (srfi srfi-35)
6b1891b0 62 #:use-module (srfi srfi-64)
73744725 63 #:use-module (srfi srfi-71)
ce0be567 64 #:use-module (rnrs bytevectors)
860a6f1a 65 #:use-module (rnrs io ports)
5239f3d9 66 #:use-module (ice-9 vlist)
2e1bafb0 67 #:use-module (ice-9 regex)
6b1891b0 68 #:use-module (ice-9 match))
e3ce5d70
LC
69
70;; Test the high-level packaging layer.
71
72(define %store
c1bc358f 73 (open-connection-for-tests))
e3ce5d70 74
b4c42a4b
LC
75;; Globally disable grafting to avoid rebuilding the world ('graft-derivation'
76;; can trigger builds early.)
77(%graft? #f)
78
05962f29
LC
79\f
80(test-begin "packages")
81
2e1bafb0 82(test-assert "printer with location"
74e667d1 83 (string-match "^#<package foo@0 foo.scm:42 [[:xdigit:]]+>$"
2e1bafb0
LC
84 (with-output-to-string
85 (lambda ()
86 (write
87 (dummy-package "foo"
88 (location (make-location "foo.scm" 42 7))))))))
89
90(test-assert "printer without location"
74e667d1 91 (string-match "^#<package foo@0 [[:xdigit:]]+>$"
2e1bafb0
LC
92 (with-output-to-string
93 (lambda ()
94 (write
95 (dummy-package "foo" (location #f)))))))
96
6980511b
LC
97(test-assert "hidden-package"
98 (and (hidden-package? (hidden-package (dummy-package "foo")))
99 (not (hidden-package? (dummy-package "foo")))))
100
01afdab8
LC
101(test-assert "package-superseded"
102 (let* ((new (dummy-package "bar"))
103 (old (deprecated-package "foo" new)))
104 (and (eq? (package-superseded old) new)
105 (mock ((gnu packages) find-best-packages-by-name (const (list old)))
106 (specification->package "foo")
107 (and (eq? new (specification->package "foo"))
108 (eq? new (specification->package+output "foo")))))))
109
5239f3d9
LC
110(test-assert "transaction-upgrade-entry, zero upgrades"
111 (let* ((old (dummy-package "foo" (version "1")))
465a0d65
LC
112 (tx (mock ((gnu packages) find-best-packages-by-name
113 (const '()))
84c5da08
LC
114 (transaction-upgrade-entry
115 #f ;no store access needed
5239f3d9
LC
116 (manifest-entry
117 (inherit (package->manifest-entry old))
118 (item (string-append (%store-prefix) "/"
119 (make-string 32 #\e) "-foo-1")))
120 (manifest-transaction)))))
121 (manifest-transaction-null? tx)))
122
b9c79cae
LC
123(test-assert "transaction-upgrade-entry, zero upgrades, equivalent package"
124 (let* ((old (dummy-package "foo" (version "1")))
125 (drv (package-derivation %store old))
126 (tx (mock ((gnu packages) find-best-packages-by-name
127 (const (list old)))
128 (transaction-upgrade-entry
129 %store
130 (manifest-entry
131 (inherit (package->manifest-entry old))
132 (item (derivation->output-path drv)))
133 (manifest-transaction)))))
134 (manifest-transaction-null? tx)))
135
a357849f
LC
136(test-assert "transaction-upgrade-entry, zero upgrades, propagated inputs"
137 ;; Properly detect equivalent packages even when they have propagated
138 ;; inputs. See <https://bugs.gnu.org/35872>.
139 (let* ((dep (dummy-package "dep" (version "2")))
140 (old (dummy-package "foo" (version "1")
8524349f 141 (propagated-inputs (list dep))))
a357849f
LC
142 (drv (package-derivation %store old))
143 (tx (mock ((gnu packages) find-best-packages-by-name
144 (const (list old)))
145 (transaction-upgrade-entry
146 %store
147 (manifest-entry
148 (inherit (package->manifest-entry old))
149 (item (derivation->output-path drv))
150 (dependencies
151 (list (manifest-entry
152 (inherit (package->manifest-entry dep))
153 (item (derivation->output-path
154 (package-derivation %store dep)))))))
155 (manifest-transaction)))))
156 (manifest-transaction-null? tx)))
157
5239f3d9
LC
158(test-assert "transaction-upgrade-entry, one upgrade"
159 (let* ((old (dummy-package "foo" (version "1")))
160 (new (dummy-package "foo" (version "2")))
465a0d65
LC
161 (tx (mock ((gnu packages) find-best-packages-by-name
162 (const (list new)))
84c5da08
LC
163 (transaction-upgrade-entry
164 #f ;no store access needed
5239f3d9
LC
165 (manifest-entry
166 (inherit (package->manifest-entry old))
167 (item (string-append (%store-prefix) "/"
168 (make-string 32 #\e) "-foo-1")))
169 (manifest-transaction)))))
170 (and (match (manifest-transaction-install tx)
171 ((($ <manifest-entry> "foo" "2" "out" item))
172 (eq? item new)))
173 (null? (manifest-transaction-remove tx)))))
174
01afdab8
LC
175(test-assert "transaction-upgrade-entry, superseded package"
176 (let* ((old (dummy-package "foo" (version "1")))
177 (new (dummy-package "bar" (version "2")))
178 (dep (deprecated-package "foo" new))
465a0d65
LC
179 (tx (mock ((gnu packages) find-best-packages-by-name
180 (const (list dep)))
84c5da08
LC
181 (transaction-upgrade-entry
182 #f ;no store access needed
01afdab8
LC
183 (manifest-entry
184 (inherit (package->manifest-entry old))
185 (item (string-append (%store-prefix) "/"
186 (make-string 32 #\e) "-foo-1")))
187 (manifest-transaction)))))
188 (and (match (manifest-transaction-install tx)
189 ((($ <manifest-entry> "bar" "2" "out" item))
190 (eq? item new)))
191 (match (manifest-transaction-remove tx)
192 (((? manifest-pattern? pattern))
193 (and (string=? (manifest-pattern-name pattern) "foo")
194 (string=? (manifest-pattern-version pattern) "1")
195 (string=? (manifest-pattern-output pattern) "out")))))))
196
8e1907a7
LC
197(test-equal "transaction-upgrade-entry, transformation options preserved"
198 (derivation-file-name (package-derivation %store grep))
199
200 (let* ((old (dummy-package "emacs" (version "1")))
201 (props '((transformations . ((with-input . "emacs=grep")))))
202 (tx (transaction-upgrade-entry
203 %store
204 (manifest-entry
205 (inherit (package->manifest-entry old))
206 (properties props)
207 (item (string-append (%store-prefix) "/"
208 (make-string 32 #\e) "-foo-1")))
209 (manifest-transaction))))
210 (match (manifest-transaction-install tx)
211 (((? manifest-entry? entry))
212 (and (string=? (manifest-entry-version entry)
213 (package-version grep))
214 (string=? (manifest-entry-name entry)
215 (package-name grep))
216 (equal? (manifest-entry-properties entry) props)
217 (derivation-file-name
218 (package-derivation %store (manifest-entry-item entry))))))))
219
a187cc56
LC
220(test-assert "transaction-upgrade-entry, grafts"
221 ;; Ensure that, when grafts are enabled, 'transaction-upgrade-entry' doesn't
222 ;; try to build stuff.
223 (with-build-handler (const 'failed!)
224 (parameterize ((%graft? #t))
225 (let* ((old (dummy-package "foo" (version "1")))
226 (bar (dummy-package "bar" (version "0")
227 (replacement old)))
228 (new (dummy-package "foo" (version "1")
8524349f 229 (inputs (list bar))))
a187cc56
LC
230 (tx (mock ((gnu packages) find-best-packages-by-name
231 (const (list new)))
232 (transaction-upgrade-entry
233 %store
234 (manifest-entry
235 (inherit (package->manifest-entry old))
236 (item (string-append (%store-prefix) "/"
237 (make-string 32 #\e) "-foo-1")))
238 (manifest-transaction)))))
239 (and (match (manifest-transaction-install tx)
240 ((($ <manifest-entry> "foo" "1" "out" item))
241 (eq? item new)))
242 (null? (manifest-transaction-remove tx)))))))
243
8531997d
LC
244(test-assert "package-definition-location"
245 (let ((location (package-location hello))
246 (definition (package-definition-location hello)))
247 ;; Check for the usual layout of (define-public hello (package ...)).
248 (and (string=? (location-file location)
249 (location-file definition))
250 (= 0 (location-column definition))
251 (= 2 (location-column location))
252 (= (location-line definition)
253 (- (location-line location) 1)))))
254
d66c7096
LC
255(test-assert "package-field-location"
256 (let ()
257 (define (goto port line column)
258 (unless (and (= (port-column port) (- column 1))
259 (= (port-line port) (- line 1)))
260 (unless (eof-object? (get-char port))
261 (goto port line column))))
262
263 (define read-at
264 (match-lambda
265 (($ <location> file line column)
266 (call-with-input-file (search-path %load-path file)
267 (lambda (port)
268 (goto port line column)
269 (read port))))))
270
ee48b283
LC
271 ;; Until Guile 2.0.6 included, source properties were added only to pairs.
272 ;; Thus, check against both VALUE and (FIELD VALUE).
273 (and (member (read-at (package-field-location %bootstrap-guile 'name))
274 (let ((name (package-name %bootstrap-guile)))
275 (list name `(name ,name))))
276 (member (read-at (package-field-location %bootstrap-guile 'version))
277 (let ((version (package-version %bootstrap-guile)))
278 (list version `(version ,version))))
f903dc05 279 (not (package-field-location %bootstrap-guile 'does-not-exist)))))
d66c7096 280
0b8749b7
LC
281;; Make sure we don't change the file name to an absolute file name.
282(test-equal "package-field-location, relative file name"
283 (location-file (package-location %bootstrap-guile))
284 (with-fluids ((%file-port-name-canonicalization 'absolute))
285 (location-file (package-field-location %bootstrap-guile 'version))))
286
a3d73f59
LC
287(test-assert "package-transitive-inputs"
288 (let* ((a (dummy-package "a"))
289 (b (dummy-package "b"
8524349f 290 (propagated-inputs (list a))))
a3d73f59 291 (c (dummy-package "c"
8524349f 292 (inputs (list a))))
a3d73f59
LC
293 (d (dummy-package "d"
294 (propagated-inputs `(("x" "something.drv")))))
295 (e (dummy-package "e"
8524349f 296 (inputs (list b c d)))))
a3d73f59
LC
297 (and (null? (package-transitive-inputs a))
298 (equal? `(("a" ,a)) (package-transitive-inputs b))
299 (equal? `(("a" ,a)) (package-transitive-inputs c))
300 (equal? (package-propagated-inputs d)
301 (package-transitive-inputs d))
161094c8
LC
302 (equal? `(("b" ,b) ("c" ,c) ("d" ,d)
303 ("a" ,a) ("x" "something.drv"))
a3d73f59
LC
304 (pk 'x (package-transitive-inputs e))))))
305
161094c8
LC
306(test-assert "package-transitive-inputs, no duplicates"
307 (let* ((a (dummy-package "a"))
308 (b (dummy-package "b"
309 (inputs `(("a+" ,a)))
310 (native-inputs `(("a*" ,a)))
311 (propagated-inputs `(("a" ,a)))))
312 (c (dummy-package "c"
313 (propagated-inputs `(("b" ,b)))))
314 (d (dummy-package "d"
315 (inputs `(("a" ,a) ("c" ,c)))))
316 (e (dummy-package "e"
317 (inputs `(("b" ,b) ("c" ,c))))))
318 (and (null? (package-transitive-inputs a))
319 (equal? `(("a*" ,a) ("a+" ,a) ("a" ,a)) ;here duplicates are kept
320 (package-transitive-inputs b))
321 (equal? `(("b" ,b) ("a" ,a))
322 (package-transitive-inputs c))
323 (equal? `(("a" ,a) ("c" ,c) ("b" ,b)) ;duplicate A removed
324 (package-transitive-inputs d))
325 (equal? `(("b" ,b) ("c" ,c) ("a" ,a))
326 (package-transitive-inputs e))))) ;ditto
327
7c3c0374 328(test-equal "package-transitive-supported-systems"
c37a74bd
LC
329 '(("x" "y" "z") ;a
330 ("x" "y") ;b
331 ("y") ;c
332 ("y") ;d
333 ("y")) ;e
9bf3ced0
LC
334 ;; Use TRIVIAL-BUILD-SYSTEM because it doesn't add implicit inputs and thus
335 ;; doesn't restrict the set of supported systems.
336 (let* ((a (dummy-package "a"
337 (build-system trivial-build-system)
338 (supported-systems '("x" "y" "z"))))
339 (b (dummy-package "b"
340 (build-system trivial-build-system)
341 (supported-systems '("x" "y"))
8524349f 342 (inputs (list a))))
9bf3ced0
LC
343 (c (dummy-package "c"
344 (build-system trivial-build-system)
345 (supported-systems '("y" "z"))
8524349f 346 (inputs (list b))))
9bf3ced0
LC
347 (d (dummy-package "d"
348 (build-system trivial-build-system)
349 (supported-systems '("x" "y" "z"))
8524349f 350 (inputs (list b c))))
9bf3ced0
LC
351 (e (dummy-package "e"
352 (build-system trivial-build-system)
353 (supported-systems '("x" "y" "z"))
8524349f 354 (inputs (list d)))))
7c3c0374
LC
355 (list (package-transitive-supported-systems a)
356 (package-transitive-supported-systems b)
c37a74bd
LC
357 (package-transitive-supported-systems c)
358 (package-transitive-supported-systems d)
359 (package-transitive-supported-systems e))))
7c3c0374 360
fb368f4e
LC
361(test-assert "package-development-inputs"
362 ;; Note: Due to propagated inputs, 'package-development-inputs' returns a
363 ;; couple more inputs, such as 'linux-libre-headers'.
364 (lset<= equal?
365 `(("source" ,(package-source hello)) ,@(standard-packages))
366 (package-development-inputs hello)))
367
368(test-assert "package-development-inputs, cross-compilation"
369 (lset<= equal?
370 `(("source" ,(package-source hello))
371 ,@(standard-cross-packages "mips64el-linux-gnu" 'host)
372 ,@(standard-cross-packages "mips64el-linux-gnu" 'target))
373 (package-development-inputs hello #:target "mips64el-linux-gnu")))
374
3e223a22
LC
375(test-assert "package-closure"
376 (let-syntax ((dummy-package/no-implicit
377 (syntax-rules ()
378 ((_ name rest ...)
379 (package
380 (inherit (dummy-package name rest ...))
381 (build-system trivial-build-system))))))
382 (let* ((a (dummy-package/no-implicit "a"))
383 (b (dummy-package/no-implicit "b"
8524349f 384 (propagated-inputs (list a))))
3e223a22 385 (c (dummy-package/no-implicit "c"
8524349f 386 (inputs (list a))))
3e223a22 387 (d (dummy-package/no-implicit "d"
8524349f 388 (native-inputs (list b))))
3e223a22 389 (e (dummy-package/no-implicit "e"
8524349f 390 (inputs (list c d)))))
3e223a22
LC
391 (lset= eq?
392 (list a b c d e)
393 (package-closure (list e))
394 (package-closure (list e d))
395 (package-closure (list e c b))))))
396
3b4d0103
EB
397(test-equal "origin-actual-file-name"
398 "foo-1.tar.gz"
399 (let ((o (dummy-origin (uri "http://www.example.com/foo-1.tar.gz"))))
400 (origin-actual-file-name o)))
401
402(test-equal "origin-actual-file-name, file-name"
403 "foo-1.tar.gz"
404 (let ((o (dummy-origin
405 (uri "http://www.example.com/tarball")
406 (file-name "foo-1.tar.gz"))))
407 (origin-actual-file-name o)))
408
f77bcbc3
EB
409(let* ((o (dummy-origin))
410 (u (dummy-origin))
411 (i (dummy-origin))
412 (a (dummy-package "a"))
8524349f 413 (b (dummy-package "b" (inputs (list a i))))
f77bcbc3
EB
414 (c (package (inherit b) (source o)))
415 (d (dummy-package "d"
416 (build-system trivial-build-system)
8524349f 417 (source u) (inputs (list c)))))
f77bcbc3
EB
418 (test-assert "package-direct-sources, no source"
419 (null? (package-direct-sources a)))
420 (test-equal "package-direct-sources, #f source"
421 (list i)
422 (package-direct-sources b))
423 (test-equal "package-direct-sources, not input source"
424 (list u)
425 (package-direct-sources d))
426 (test-assert "package-direct-sources"
427 (let ((s (package-direct-sources c)))
428 (and (= (length (pk 's-sources s)) 2)
429 (member o s)
430 (member i s))))
431 (test-assert "package-transitive-sources"
432 (let ((s (package-transitive-sources d)))
433 (and (= (length (pk 'd-sources s)) 3)
434 (member o s)
435 (member i s)
436 (member u s)))))
437
a6d0b306
EB
438(test-assert "transitive-input-references"
439 (let* ((a (dummy-package "a"))
440 (b (dummy-package "b"))
441 (c (dummy-package "c"
442 (inputs `(("a" ,a)))
443 (propagated-inputs `(("boo" ,b)))))
444 (d (dummy-package "d"
445 (inputs `(("c*" ,c)))))
446 (keys (map (match-lambda
447 (('assoc-ref 'l key)
448 key))
449 (pk 'refs (transitive-input-references
450 'l (package-inputs d))))))
451 (and (= (length keys) 2)
452 (member "c*" keys)
453 (member "boo" keys))))
454
9bf3ced0
LC
455(test-equal "package-transitive-supported-systems, implicit inputs"
456 %supported-systems
457
0db65c16
JN
458 ;; Here GNU-BUILD-SYSTEM adds implicit inputs that build only on
459 ;; %SUPPORTED-SYSTEMS. Thus the others must be ignored.
460 (let ((p (dummy-package "foo"
461 (build-system gnu-build-system)
462 (supported-systems
463 `("does-not-exist" "foobar" ,@%supported-systems)))))
0db65c16
JN
464 (parameterize ((%current-system "armhf-linux")) ; a traditionally-bootstrapped architecture
465 (package-transitive-supported-systems p))))
466
467(test-equal "package-transitive-supported-systems: reduced binary seed, implicit inputs"
468 '("x86_64-linux" "i686-linux")
469
9bf3ced0
LC
470 ;; Here GNU-BUILD-SYSTEM adds implicit inputs that build only on
471 ;; %SUPPORTED-SYSTEMS. Thus the others must be ignored.
472 (let ((p (dummy-package "foo"
473 (build-system gnu-build-system)
474 (supported-systems
475 `("does-not-exist" "foobar" ,@%supported-systems)))))
0db65c16
JN
476 (parameterize ((%current-system "x86_64-linux"))
477 (package-transitive-supported-systems p))))
9bf3ced0 478
bbceb0ef 479(test-assert "supported-package?"
bc60349b
LC
480 (let* ((d (dummy-package "dep"
481 (build-system trivial-build-system)
482 (supported-systems '("x86_64-linux"))))
483 (p (dummy-package "foo"
484 (build-system gnu-build-system)
8524349f 485 (inputs (list d))
bc60349b
LC
486 (supported-systems '("x86_64-linux" "armhf-linux")))))
487 (and (supported-package? p "x86_64-linux")
488 (not (supported-package? p "i686-linux"))
489 (not (supported-package? p "armhf-linux")))))
490
491(test-assert "supported-package? vs. system-dependent graph"
492 ;; The inputs of a package can depend on (%current-system). Thus,
493 ;; 'supported-package?' must make sure that it binds (%current-system)
494 ;; appropriately before traversing the dependency graph. In the example
495 ;; below, 'supported-package?' must thus return true for both systems.
496 (let* ((p0a (dummy-package "foo-arm"
497 (build-system trivial-build-system)
498 (supported-systems '("armhf-linux"))))
499 (p0b (dummy-package "foo-x86_64"
500 (build-system trivial-build-system)
501 (supported-systems '("x86_64-linux"))))
502 (p (dummy-package "bar"
503 (build-system trivial-build-system)
504 (inputs
505 (if (string=? (%current-system) "armhf-linux")
506 `(("foo" ,p0a))
507 `(("foo" ,p0b)))))))
bbceb0ef 508 (and (supported-package? p "x86_64-linux")
bc60349b 509 (supported-package? p "armhf-linux"))))
bbceb0ef 510
0572737a
LC
511(test-assert "supported-package? vs. %current-target-system"
512 ;; The %CURRENT-TARGET-SYSTEM value should have no influence.
513 (parameterize ((%current-target-system "arm-linux-gnueabihf"))
514 (let ((p (dummy-package "foo"
515 (build-system gnu-build-system)
516 (supported-systems '("x86_64-linux" "armhf-linux")))))
517 (and (supported-package? p "x86_64-linux")
518 (not (supported-package? p "i686-linux"))
519 (supported-package? p "armhf-linux")))))
520
7357138b
LC
521(test-skip (if (not %store) 8 0))
522
523(test-assert "package-source-derivation, file"
524 (let* ((file (search-path %load-path "guix.scm"))
525 (package (package (inherit (dummy-package "p"))
526 (source file)))
527 (source (package-source-derivation %store
528 (package-source package))))
529 (and (store-path? source)
530 (valid-path? %store source)
531 (equal? (call-with-input-file source get-bytevector-all)
532 (call-with-input-file file get-bytevector-all)))))
533
534(test-assert "package-source-derivation, store path"
535 (let* ((file (add-to-store %store "guix.scm" #t "sha256"
536 (search-path %load-path "guix.scm")))
537 (package (package (inherit (dummy-package "p"))
538 (source file)))
539 (source (package-source-derivation %store
540 (package-source package))))
541 (string=? file source)))
e509d152 542
f80594cc
LC
543(test-assert "package-source-derivation, indirect store path"
544 (let* ((dir (add-to-store %store "guix-build" #t "sha256"
545 (dirname (search-path %load-path
546 "guix/build/utils.scm"))))
547 (package (package (inherit (dummy-package "p"))
548 (source (string-append dir "/utils.scm"))))
549 (source (package-source-derivation %store
550 (package-source package))))
551 (and (direct-store-path? source)
552 (string-suffix? "utils.scm" source))))
553
da675305
LC
554(test-assert "package-source-derivation, local-file"
555 (let* ((file (local-file "../guix/base32.scm"))
556 (package (package (inherit (dummy-package "p"))
557 (source file)))
558 (source (package-source-derivation %store
559 (package-source package))))
560 (and (store-path? source)
561 (string-suffix? "base32.scm" source)
562 (valid-path? %store source)
563 (equal? (call-with-input-file source get-bytevector-all)
564 (call-with-input-file
565 (search-path %load-path "guix/base32.scm")
566 get-bytevector-all)))))
567
ce0be567
LC
568(test-equal "package-source-derivation, origin, sha512"
569 "hello"
570 (let* ((bash (search-bootstrap-binary "bash" (%current-system)))
571 (builder (add-text-to-store %store "my-fixed-builder.sh"
572 "echo -n hello > $out" '()))
573 (method (lambda* (url hash-algo hash #:optional name
574 #:rest rest)
575 (and (eq? hash-algo 'sha512)
576 (raw-derivation name bash (list builder)
577 #:sources (list builder)
578 #:hash hash
579 #:hash-algo hash-algo))))
580 (source (origin
581 (method method)
582 (uri "unused://")
583 (file-name "origin-sha512")
584 (hash (content-hash
197c07a8
LC
585 (gcrypt:bytevector-hash (string->utf8 "hello")
586 (gcrypt:lookup-hash-algorithm
587 'sha512))
ce0be567
LC
588 sha512))))
589 (drv (package-source-derivation %store source))
590 (output (derivation->output-path drv)))
591 (build-derivations %store (list drv))
592 (call-with-input-file output get-string-all)))
593
0505eda9
LC
594(test-equal "package-source-derivation, origin, sha3-512"
595 "hello, sha3"
596 (let* ((bash (search-bootstrap-binary "bash" (%current-system)))
597 (builder (add-text-to-store %store "my-fixed-builder.sh"
598 "echo -n hello, sha3 > $out" '()))
599 (method (lambda* (url hash-algo hash #:optional name
600 #:rest rest)
601 (and (eq? hash-algo 'sha3-512)
602 (raw-derivation name bash (list builder)
603 #:sources (list builder)
604 #:hash hash
605 #:hash-algo hash-algo))))
606 (source (origin
607 (method method)
608 (uri "unused://")
609 (file-name "origin-sha3")
610 (hash (content-hash
611 (gcrypt:bytevector-hash (string->utf8 "hello, sha3")
612 (gcrypt:lookup-hash-algorithm
613 'sha3-512))
614 sha3-512))))
615 (drv (package-source-derivation %store source))
616 (output (derivation->output-path drv)))
617 (build-derivations %store (list drv))
618 (call-with-input-file output get-string-all)))
619
cfcead2e
MC
620\f
621;;;
622;;; Source derivation with snippets.
623;;;
624
12d720fd 625(unless (network-reachable?) (test-skip 1))
f9cc8971
LC
626(test-equal "package-source-derivation, snippet"
627 "OK"
f8ec8d96 628 (let* ((source (bootstrap-origin
f9cc8971 629 (origin
f8ec8d96 630 (inherit (bootstrap-guile-origin (%current-system)))
f9cc8971
LC
631 (patch-inputs
632 `(("tar" ,%bootstrap-coreutils&co)
633 ("xz" ,%bootstrap-coreutils&co)
634 ("patch" ,%bootstrap-coreutils&co)))
7db9608d 635 (patch-guile %bootstrap-guile)
f9cc8971 636 (modules '((guix build utils)))
f9cc8971
LC
637 (snippet '(begin
638 ;; We end up in 'bin', because it's the first
639 ;; directory, alphabetically. Not a very good
640 ;; example but hey.
641 (chmod "." #o777)
642 (symlink "guile" "guile-rocks")
643 (copy-recursively "../share/guile/2.0/scripts"
d6445dff
LC
644 "scripts")
645
646 ;; Make sure '.file_list' can be created.
647 (chmod ".." #o777))))))
f9cc8971
LC
648 (package (package (inherit (dummy-package "with-snippet"))
649 (source source)
650 (build-system trivial-build-system)
651 (inputs
652 `(("tar" ,(search-bootstrap-binary "tar"
653 (%current-system)))
654 ("xz" ,(search-bootstrap-binary "xz"
655 (%current-system)))))
656 (arguments
657 `(#:guile ,%bootstrap-guile
1e868858 658 #:modules ((guix build utils))
f9cc8971 659 #:builder
1e868858
MW
660 (begin
661 (use-modules (guix build utils))
662 (let ((tar (assoc-ref %build-inputs "tar"))
663 (xz (assoc-ref %build-inputs "xz"))
664 (source (assoc-ref %build-inputs "source")))
665 (invoke tar "xvf" source
666 "--use-compress-program" xz)
667 (unless (and (string=? "guile" (readlink "bin/guile-rocks"))
668 (file-exists? "bin/scripts/compile.scm"))
669 (error "the snippet apparently failed"))
670 (let ((out (assoc-ref %outputs "out")))
671 (call-with-output-file out
672 (lambda (p)
673 (display "OK" p))))
674 #t))))))
f9cc8971
LC
675 (drv (package-derivation %store package))
676 (out (derivation->output-path drv)))
677 (and (build-derivations %store (list (pk 'snippet-drv drv)))
678 (call-with-input-file out get-string-all))))
679
cfcead2e
MC
680;; Note: lzip is not part of bootstrap-coreutils&co, so is not included to
681;; avoid having to rebuild the world.
682(define compressors '(("gzip" . "gz")
683 ("xz" . "xz")
684 ("bzip2" . "bz2")
685 (#f . #f)))
686
687(for-each
688 (match-lambda
689 ((comp . ext)
690 (unless (network-reachable?) (test-skip 1))
691 (test-equal (string-append "origin->derivation, single file with snippet "
692 "(compression: " (if comp comp "None") ")")
693 "2 + 2 = 4"
694 (let*-values
695 (((name) "maths")
696 ((compressed-name) (if comp
697 (string-append name "." ext)
698 name))
699 ((file hash) (test-file %store compressed-name "2 + 2 = 5"))
700 ;; Create an origin using the above computed file and its hash.
701 ((source) (origin
702 (method url-fetch)
703 (uri (string-append "file://" file))
704 (file-name compressed-name)
705 (patch-inputs `(("tar" ,%bootstrap-coreutils&co)
706 ("xz" ,%bootstrap-coreutils&co)
707 ("bzip2" ,%bootstrap-coreutils&co)
708 ("gzip" ,%bootstrap-coreutils&co)))
709 (patch-guile %bootstrap-guile)
710 (modules '((guix build utils)))
711 (snippet `(substitute* ,name
712 (("5") "4")))
713 (hash (content-hash hash))))
714 ;; Build origin.
715 ((drv) (run-with-store %store (origin->derivation source)))
716 ((out) (derivation->output-path drv)))
717 ;; Decompress the resulting tar.xz and return its content.
718 (and (build-derivations %store (list drv))
719 (if (tarball? out)
720 (let* ((bin #~(string-append #+%bootstrap-coreutils&co
721 "/bin"))
722 (f (computed-file
723 name
724 (with-imported-modules '((guix build utils))
725 #~(begin
726 (use-modules (guix build utils))
727 (setenv "PATH" #+bin)
728 (invoke "tar" "xvf" #+out)
bc1ad696
LC
729 (copy-file #+name #$output)))
730 #:guile %bootstrap-guile))
cfcead2e
MC
731 (drv (run-with-store %store (lower-object f)))
732 (_ (build-derivations %store (list drv))))
733 (call-with-input-file (derivation->output-path drv)
734 get-string-all))
735 (call-with-input-file out get-string-all)))))))
736 compressors)
737
59688fc4
LC
738(test-assert "return value"
739 (let ((drv (package-derivation %store (dummy-package "p"))))
740 (and (derivation? drv)
741 (file-exists? (derivation-file-name drv)))))
be13fbfa 742
6b466336
LC
743(test-assert "package-derivation, inputs deduplicated"
744 (let* ((dep (dummy-package "dep"))
8524349f 745 (p0 (dummy-package "p" (inputs (list dep))))
6b466336
LC
746 (p1 (package (inherit p0)
747 (inputs `(("dep" ,(package (inherit dep)))
748 ,@(package-inputs p0))))))
749 ;; Here P1 ends up with two non-eq? copies of DEP, under the same label.
750 ;; They should be deduplicated so that P0 and P1 lead to the same
751 ;; derivation rather than P1 ending up with duplicate entries in its
752 ;; '%build-inputs' variable.
753 (string=? (derivation-file-name (package-derivation %store p0))
754 (derivation-file-name (package-derivation %store p1)))))
755
98c075c2
LC
756(test-assert "package-derivation, different system"
757 ;; Make sure the 'system' argument of 'package-derivation' is respected.
758 (let* ((system (if (string=? (%current-system) "x86_64-linux")
759 "aarch64-linux"
760 "x86_64-linux"))
761 (drv (package-derivation %store (dummy-package "p")
762 system #:graft? #f)))
763 (define right-system?
764 (mlambdaq (drv)
765 (and (string=? (derivation-system drv) system)
766 (every (compose right-system? derivation-input-derivation)
767 (derivation-inputs drv)))))
768
769 (right-system? drv)))
770
d510ab46
LC
771(test-assert "package-output"
772 (let* ((package (dummy-package "p"))
59688fc4
LC
773 (drv (package-derivation %store package)))
774 (and (derivation? drv)
775 (string=? (derivation->output-path drv)
d510ab46
LC
776 (package-output %store package "out")))))
777
d51bfe24
LC
778(test-equal "patch not found yields a run-time error"
779 '("~a: patch not found\n" "does-not-exist.patch")
780 (guard (c ((formatted-message? c)
781 (cons (formatted-message-string c)
782 (formatted-message-arguments c))))
dbab5150
LC
783 (let ((p (package
784 (inherit (dummy-package "p"))
785 (source (origin
786 (method (const #f))
787 (uri "http://whatever")
788 (patches
789 (list (search-patch "does-not-exist.patch")))
790 (sha256
791 (base32
792 "0amn0bbwqvsvvsh6drfwz20ydc2czk374lzw5kksbh6bf78k4ks4")))))))
793 (package-derivation %store p)
794 #f)))
795
76c48619
LC
796(let ((dummy (dummy-package "foo" (inputs `(("x" ,(current-module)))))))
797 (test-equal "&package-input-error"
e7477dd5 798 (list dummy `("x" ,(current-module)))
76c48619
LC
799 (guard (c ((package-input-error? c)
800 (list (package-error-package c)
801 (package-error-invalid-input c))))
802 (package-derivation %store dummy))))
803
f304c9c2
LC
804(test-assert "reference to non-existent output"
805 ;; See <http://bugs.gnu.org/19630>.
862abf8f
LC
806 (parameterize ((%graft? #f))
807 (let* ((dep (dummy-package "dep"))
808 (p (dummy-package "p"
8524349f 809 (inputs (list `(,dep "non-existent"))))))
862abf8f
LC
810 (guard (c ((derivation-missing-output-error? c)
811 (and (string=? (derivation-missing-output c) "non-existent")
812 (equal? (package-derivation %store dep)
813 (derivation-error-derivation c)))))
814 (package-derivation %store p)))))
f304c9c2 815
be13fbfa
LC
816(test-assert "trivial"
817 (let* ((p (package (inherit (dummy-package "trivial"))
818 (build-system trivial-build-system)
819 (source #f)
820 (arguments
14da91e2
LC
821 `(#:guile ,%bootstrap-guile
822 #:builder
be13fbfa
LC
823 (begin
824 (mkdir %output)
825 (call-with-output-file (string-append %output "/test")
826 (lambda (p)
1e868858
MW
827 (display '(hello guix) p)))
828 #t)))))
be13fbfa
LC
829 (d (package-derivation %store p)))
830 (and (build-derivations %store (list d))
59688fc4 831 (let ((p (pk 'drv d (derivation->output-path d))))
be13fbfa
LC
832 (equal? '(hello guix)
833 (call-with-input-file (string-append p "/test") read))))))
e3ce5d70 834
860a6f1a
LC
835(test-assert "trivial with local file as input"
836 (let* ((i (search-path %load-path "ice-9/boot-9.scm"))
837 (p (package (inherit (dummy-package "trivial-with-input-file"))
838 (build-system trivial-build-system)
839 (source #f)
840 (arguments
841 `(#:guile ,%bootstrap-guile
1e868858
MW
842 #:builder (begin
843 (copy-file (assoc-ref %build-inputs "input")
844 %output)
845 #t)))
860a6f1a
LC
846 (inputs `(("input" ,i)))))
847 (d (package-derivation %store p)))
848 (and (build-derivations %store (list d))
59688fc4 849 (let ((p (pk 'drv d (derivation->output-path d))))
860a6f1a
LC
850 (equal? (call-with-input-file p get-bytevector-all)
851 (call-with-input-file i get-bytevector-all))))))
852
03761a44
LC
853(test-assert "trivial with source"
854 (let* ((i (search-path %load-path "ice-9/boot-9.scm"))
855 (p (package (inherit (dummy-package "trivial-with-source"))
856 (build-system trivial-build-system)
857 (source i)
858 (arguments
859 `(#:guile ,%bootstrap-guile
1e868858
MW
860 #:builder (begin
861 (copy-file (assoc-ref %build-inputs "source")
862 %output)
863 #t)))))
03761a44
LC
864 (d (package-derivation %store p)))
865 (and (build-derivations %store (list d))
866 (let ((p (derivation->output-path d)))
867 (equal? (call-with-input-file p get-bytevector-all)
868 (call-with-input-file i get-bytevector-all))))))
869
592ef6c8
LC
870(test-assert "trivial with system-dependent input"
871 (let* ((p (package (inherit (dummy-package "trivial-system-dependent-input"))
872 (build-system trivial-build-system)
873 (source #f)
874 (arguments
875 `(#:guile ,%bootstrap-guile
1e868858 876 #:modules ((guix build utils))
592ef6c8 877 #:builder
1e868858
MW
878 (begin
879 (use-modules (guix build utils))
880 (let ((out (assoc-ref %outputs "out"))
881 (bash (assoc-ref %build-inputs "bash")))
882 (invoke bash "-c"
883 (format #f "echo hello > ~a" out))))))
dd6b9a37
LC
884 (inputs `(("bash" ,(search-bootstrap-binary "bash"
885 (%current-system)))))))
592ef6c8
LC
886 (d (package-derivation %store p)))
887 (and (build-derivations %store (list d))
59688fc4 888 (let ((p (pk 'drv d (derivation->output-path d))))
592ef6c8
LC
889 (eq? 'hello (call-with-input-file p read))))))
890
29760251
LC
891(test-assert "trivial with #:allowed-references"
892 (let* ((p (package
893 (inherit (dummy-package "trivial"))
894 (build-system trivial-build-system)
895 (arguments
896 `(#:guile ,%bootstrap-guile
897 #:allowed-references (,%bootstrap-guile)
898 #:builder
899 (begin
900 (mkdir %output)
901 ;; The reference to itself isn't allowed so building it
902 ;; should fail.
1e868858
MW
903 (symlink %output (string-append %output "/self"))
904 #t)))))
29760251 905 (d (package-derivation %store p)))
f9e8a123 906 (guard (c ((store-protocol-error? c) #t))
29760251
LC
907 (build-derivations %store (list d))
908 #f)))
909
df46bef4
LC
910(test-assert "trivial with #:allowed-references + grafts"
911 (let* ((g (package
912 (inherit %bootstrap-guile)
913 (replacement (package
914 (inherit %bootstrap-guile)
915 (version "9.9")))))
916 (p (package
917 (inherit (dummy-package "trivial"))
918 (build-system trivial-build-system)
919 (inputs (list g))
920 (arguments
921 `(#:guile ,g
922 #:allowed-references (,g)
923 #:builder (mkdir %output)))))
924 (d0 (package-derivation %store p #:graft? #f))
925 (d1 (parameterize ((%graft? #t))
926 (package-derivation %store p #:graft? #t))))
927 ;; D1 should be equal to D2 because there's nothing to graft. In
928 ;; particular, its #:disallowed-references should be lowered in the same
929 ;; way (ungrafted) whether or not #:graft? is true.
930 (string=? (derivation-file-name d1) (derivation-file-name d0))))
931
a18eda27
LC
932(test-assert "search paths"
933 (let* ((p (make-prompt-tag "return-search-paths"))
89b0c239 934 (t (make-parameter "guile-0"))
a18eda27 935 (s (build-system
89b0c239
LC
936 (name 'raw)
937 (description "Raw build system with direct store access")
938 (lower (lambda* (name #:key source inputs system target
939 #:allow-other-keys)
940 (bag
941 (name name)
942 (system system) (target target)
943 (build-inputs inputs)
944 (build
945 (lambda* (name inputs
946 #:key outputs system search-paths)
947 (if (string=? name (t))
948 (abort-to-prompt p search-paths)
949 (gexp->derivation name
950 #~(mkdir #$output))))))))))
a18eda27
LC
951 (x (list (search-path-specification
952 (variable "GUILE_LOAD_PATH")
af070955 953 (files '("share/guile/site/2.0")))
a18eda27
LC
954 (search-path-specification
955 (variable "GUILE_LOAD_COMPILED_PATH")
af070955 956 (files '("share/guile/site/2.0")))))
a18eda27
LC
957 (a (package (inherit (dummy-package "guile"))
958 (build-system s)
959 (native-search-paths x)))
960 (b (package (inherit (dummy-package "guile-foo"))
961 (build-system s)
962 (inputs `(("guile" ,a)))))
963 (c (package (inherit (dummy-package "guile-bar"))
964 (build-system s)
965 (inputs `(("guile" ,a)
966 ("guile-foo" ,b))))))
967 (let-syntax ((collect (syntax-rules ()
968 ((_ body ...)
969 (call-with-prompt p
970 (lambda ()
971 body ...)
972 (lambda (k search-paths)
973 search-paths))))))
974 (and (null? (collect (package-derivation %store a)))
89b0c239
LC
975 (parameterize ((t "guile-foo-0"))
976 (equal? x (collect (package-derivation %store b))))
977 (parameterize ((t "guile-bar-0"))
978 (equal? x (collect (package-derivation %store c))))))))
a18eda27 979
aa8e0515
LC
980(test-assert "package-transitive-native-search-paths"
981 (let* ((sp (lambda (name)
982 (list (search-path-specification
983 (variable name)
984 (files '("foo/bar"))))))
985 (p0 (dummy-package "p0" (native-search-paths (sp "PATH0"))))
986 (p1 (dummy-package "p1" (native-search-paths (sp "PATH1"))))
987 (p2 (dummy-package "p2"
988 (native-search-paths (sp "PATH2"))
8524349f
LC
989 (inputs (list p0))
990 (propagated-inputs (list p1))))
aa8e0515
LC
991 (p3 (dummy-package "p3"
992 (native-search-paths (sp "PATH3"))
8524349f
LC
993 (native-inputs (list p0))
994 (propagated-inputs (list p2)))))
aa8e0515
LC
995 (lset= string=?
996 '("PATH1" "PATH2" "PATH3")
997 (map search-path-specification-variable
998 (package-transitive-native-search-paths p3)))))
999
9c1edabd 1000(test-assert "package-cross-derivation"
59688fc4
LC
1001 (let ((drv (package-cross-derivation %store (dummy-package "p")
1002 "mips64el-linux-gnu")))
1003 (and (derivation? drv)
1004 (file-exists? (derivation-file-name drv)))))
9c1edabd 1005
5dce8218
LC
1006(test-assert "package-cross-derivation, trivial-build-system"
1007 (let ((p (package (inherit (dummy-package "p"))
1008 (build-system trivial-build-system)
1009 (arguments '(#:builder (exit 1))))))
59688fc4
LC
1010 (let ((drv (package-cross-derivation %store p "mips64el-linux-gnu")))
1011 (derivation? drv))))
5dce8218 1012
9b222abe
LC
1013(test-assert "package-cross-derivation, no cross builder"
1014 (let* ((b (build-system (inherit trivial-build-system)
0d5a559f 1015 (lower (const #f))))
9b222abe
LC
1016 (p (package (inherit (dummy-package "p"))
1017 (build-system b))))
1018 (guard (c ((package-cross-build-system-error? c)
1019 (eq? (package-error-package c) p)))
1020 (package-cross-derivation %store p "mips64el-linux-gnu")
1021 #f)))
1022
b4c42a4b
LC
1023;; XXX: The next two tests can trigger builds when the distro defines
1024;; replacements on core packages, so they're disable for lack of a better
1025;; solution.
1026
1027;; (test-equal "package-derivation, direct graft"
1028;; (package-derivation %store gnu-make #:graft? #f)
1029;; (let ((p (package (inherit coreutils)
1030;; (replacement gnu-make))))
1031;; (package-derivation %store p #:graft? #t)))
05962f29 1032
b4c42a4b
LC
1033;; (test-equal "package-cross-derivation, direct graft"
1034;; (package-cross-derivation %store gnu-make "mips64el-linux-gnu"
1035;; #:graft? #f)
1036;; (let ((p (package (inherit coreutils)
1037;; (replacement gnu-make))))
1038;; (package-cross-derivation %store p "mips64el-linux-gnu"
1039;; #:graft? #t)))
05962f29
LC
1040
1041(test-assert "package-grafts, indirect grafts"
1042 (let* ((new (dummy-package "dep"
1043 (arguments '(#:implicit-inputs? #f))))
1044 (dep (package (inherit new) (version "0.0")))
1045 (dep* (package (inherit dep) (replacement new)))
1046 (dummy (dummy-package "dummy"
1047 (arguments '(#:implicit-inputs? #f))
8524349f 1048 (inputs (list dep*)))))
05962f29
LC
1049 (equal? (package-grafts %store dummy)
1050 (list (graft
1051 (origin (package-derivation %store dep))
1052 (replacement (package-derivation %store new)))))))
1053
d0025d01
LC
1054;; XXX: This test would require building the cross toolchain just to see if it
1055;; needs grafting, which is obviously too expensive, and thus disabled.
1056;;
1057;; (test-assert "package-grafts, indirect grafts, cross"
1058;; (let* ((new (dummy-package "dep"
1059;; (arguments '(#:implicit-inputs? #f))))
1060;; (dep (package (inherit new) (version "0.0")))
1061;; (dep* (package (inherit dep) (replacement new)))
1062;; (dummy (dummy-package "dummy"
1063;; (arguments '(#:implicit-inputs? #f))
1064;; (inputs `(("dep" ,dep*)))))
1065;; (target "mips64el-linux-gnu"))
1066;; ;; XXX: There might be additional grafts, for instance if the distro
1067;; ;; defines replacements for core packages like Perl.
1068;; (member (graft
1069;; (origin (package-cross-derivation %store dep target))
1070;; (replacement
1071;; (package-cross-derivation %store new target)))
1072;; (package-grafts %store dummy #:target target))))
05962f29
LC
1073
1074(test-assert "package-grafts, indirect grafts, propagated inputs"
1075 (let* ((new (dummy-package "dep"
1076 (arguments '(#:implicit-inputs? #f))))
1077 (dep (package (inherit new) (version "0.0")))
1078 (dep* (package (inherit dep) (replacement new)))
1079 (prop (dummy-package "propagated"
8524349f 1080 (propagated-inputs (list dep*))
05962f29
LC
1081 (arguments '(#:implicit-inputs? #f))))
1082 (dummy (dummy-package "dummy"
1083 (arguments '(#:implicit-inputs? #f))
8524349f 1084 (inputs (list prop)))))
05962f29
LC
1085 (equal? (package-grafts %store dummy)
1086 (list (graft
1087 (origin (package-derivation %store dep))
1088 (replacement (package-derivation %store new)))))))
1089
fcadd9ff
LC
1090(test-assert "package-grafts, same replacement twice"
1091 (let* ((new (dummy-package "dep"
1092 (version "1")
1093 (arguments '(#:implicit-inputs? #f))))
1094 (dep (package (inherit new) (version "0") (replacement new)))
1095 (p1 (dummy-package "intermediate1"
1096 (arguments '(#:implicit-inputs? #f))
8524349f 1097 (inputs (list dep))))
fcadd9ff
LC
1098 (p2 (dummy-package "intermediate2"
1099 (arguments '(#:implicit-inputs? #f))
1100 ;; Here we copy DEP to have an equivalent package that is not
1101 ;; 'eq?' to DEP. This is similar to what happens with
1102 ;; 'package-with-explicit-inputs' & co.
8524349f 1103 (inputs (list (package (inherit dep))))))
fcadd9ff
LC
1104 (p3 (dummy-package "final"
1105 (arguments '(#:implicit-inputs? #f))
8524349f 1106 (inputs (list p1 p2)))))
fcadd9ff
LC
1107 (equal? (package-grafts %store p3)
1108 (list (graft
1109 (origin (package-derivation %store
1110 (package (inherit dep)
1111 (replacement #f))))
1112 (replacement (package-derivation %store new)))))))
1113
03a70e4c
LC
1114(test-assert "package-grafts, dependency on several outputs"
1115 ;; Make sure we get one graft per output; see <https://bugs.gnu.org/41796>.
1116 (letrec* ((p0 (dummy-package "p0"
1117 (version "1.0")
1118 (replacement p0*)
1119 (arguments '(#:implicit-inputs? #f))
1120 (outputs '("out" "lib"))))
1121 (p0* (package (inherit p0) (version "1.1")))
1122 (p1 (dummy-package "p1"
1123 (arguments '(#:implicit-inputs? #f))
8524349f 1124 (inputs (list p0 `(,p0 "lib"))))))
03a70e4c
LC
1125 (lset= equal? (pk (package-grafts %store p1))
1126 (list (graft
1127 (origin (package-derivation %store p0))
1128 (origin-output "out")
1129 (replacement (package-derivation %store p0*))
1130 (replacement-output "out"))
1131 (graft
1132 (origin (package-derivation %store p0))
1133 (origin-output "lib")
1134 (replacement (package-derivation %store p0*))
1135 (replacement-output "lib"))))))
1136
d0025d01
LC
1137(test-assert "replacement also grafted"
1138 ;; We build a DAG as below, where dotted arrows represent replacements and
1139 ;; solid arrows represent dependencies:
1140 ;;
1141 ;; P1 ·············> P1R
1142 ;; |\__________________.
1143 ;; v v
1144 ;; P2 ·············> P2R
1145 ;; |
1146 ;; v
1147 ;; P3
1148 ;;
1149 ;; We want to make sure that:
1150 ;; grafts(P3) = (P1,P1R) + (P2, grafted(P2R, (P1,P1R)))
1151 ;; where:
1152 ;; (A,B) is a graft to replace A by B
1153 ;; grafted(DRV,G) denoted DRV with graft G applied
1154 (let* ((p1r (dummy-package "P1"
1155 (build-system trivial-build-system)
1156 (arguments
1157 `(#:guile ,%bootstrap-guile
1158 #:builder (let ((out (assoc-ref %outputs "out")))
1159 (mkdir out)
1160 (call-with-output-file
1161 (string-append out "/replacement")
1162 (const #t)))))))
1163 (p1 (package
1164 (inherit p1r) (name "p1") (replacement p1r)
1165 (arguments
1166 `(#:guile ,%bootstrap-guile
1e868858
MW
1167 #:builder (begin
1168 (mkdir (assoc-ref %outputs "out"))
1169 #t)))))
d0025d01
LC
1170 (p2r (dummy-package "P2"
1171 (build-system trivial-build-system)
8524349f 1172 (inputs (list p1))
d0025d01
LC
1173 (arguments
1174 `(#:guile ,%bootstrap-guile
1175 #:builder (let ((out (assoc-ref %outputs "out")))
1176 (mkdir out)
1177 (chdir out)
1178 (symlink (assoc-ref %build-inputs "p1") "p1")
1179 (call-with-output-file (string-append out "/replacement")
1180 (const #t)))))))
1181 (p2 (package
1182 (inherit p2r) (name "p2") (replacement p2r)
1183 (arguments
1184 `(#:guile ,%bootstrap-guile
1185 #:builder (let ((out (assoc-ref %outputs "out")))
1186 (mkdir out)
1187 (chdir out)
1188 (symlink (assoc-ref %build-inputs "p1")
1e868858
MW
1189 "p1")
1190 #t)))))
d0025d01
LC
1191 (p3 (dummy-package "p3"
1192 (build-system trivial-build-system)
8524349f 1193 (inputs (list p2))
d0025d01
LC
1194 (arguments
1195 `(#:guile ,%bootstrap-guile
1196 #:builder (let ((out (assoc-ref %outputs "out")))
1197 (mkdir out)
1198 (chdir out)
1199 (symlink (assoc-ref %build-inputs "p2")
1e868858
MW
1200 "p2")
1201 #t))))))
d0025d01
LC
1202 (lset= equal?
1203 (package-grafts %store p3)
1204 (list (graft
1205 (origin (package-derivation %store p1 #:graft? #f))
1206 (replacement (package-derivation %store p1r)))
1207 (graft
1208 (origin (package-derivation %store p2 #:graft? #f))
1209 (replacement
1210 (package-derivation %store p2r #:graft? #t)))))))
1211
c22a1324
LC
1212;;; XXX: Nowadays 'graft-derivation' needs to build derivations beforehand to
1213;;; find out about their run-time dependencies, so this test is no longer
1214;;; applicable since it would trigger a full rebuild.
1215;;
1216;; (test-assert "package-derivation, indirect grafts"
1217;; (let* ((new (dummy-package "dep"
1218;; (arguments '(#:implicit-inputs? #f))))
1219;; (dep (package (inherit new) (version "0.0")))
1220;; (dep* (package (inherit dep) (replacement new)))
1221;; (dummy (dummy-package "dummy"
1222;; (arguments '(#:implicit-inputs? #f))
1223;; (inputs `(("dep" ,dep*)))))
1224;; (guile (package-derivation %store (canonical-package guile-2.0)
1225;; #:graft? #f)))
1226;; (equal? (package-derivation %store dummy)
1227;; (graft-derivation %store
1228;; (package-derivation %store dummy #:graft? #f)
1229;; (package-grafts %store dummy)
1230
1231;; ;; Use the same Guile as 'package-derivation'.
1232;; #:guile guile))))
05962f29 1233
d3d337d2
LC
1234(test-equal "package->bag"
1235 `("foo86-hurd" #f (,(package-source gnu-make))
1236 (,(canonical-package glibc)) (,(canonical-package coreutils)))
1237 (let ((bag (package->bag gnu-make "foo86-hurd")))
1238 (list (bag-system bag) (bag-target bag)
1239 (assoc-ref (bag-build-inputs bag) "source")
1240 (assoc-ref (bag-build-inputs bag) "libc")
1241 (assoc-ref (bag-build-inputs bag) "coreutils"))))
1242
b49caaa2
LC
1243(test-assert "package->bag, sensitivity to %current-target-system"
1244 ;; https://bugs.gnu.org/41713
1245 (let* ((lower (lambda* (name #:key system target inputs native-inputs
1246 #:allow-other-keys)
1247 (and (not target)
1248 (bag (name name) (system system) (target target)
1249 (build-inputs native-inputs)
1250 (host-inputs inputs)
7d873f19
LC
1251 (build (lambda* (name inputs
1252 #:key system target
1253 #:allow-other-keys)
1254 (gexp->derivation "foo"
1255 #~(mkdir #$output))))))))
b49caaa2
LC
1256 (bs (build-system
1257 (name 'build-system-without-cross-compilation)
1258 (description "Does not support cross compilation.")
1259 (lower lower)))
1260 (dep (dummy-package "dep" (build-system bs)))
1261 (pkg (dummy-package "example"
8524349f 1262 (native-inputs (list dep))))
b49caaa2
LC
1263 (do-not-build (lambda (continue store lst . _) lst)))
1264 (equal? (with-build-handler do-not-build
1265 (parameterize ((%current-target-system "powerpc64le-linux-gnu")
1266 (%graft? #t))
1267 (package-cross-derivation %store pkg
1268 (%current-target-system)
1269 #:graft? #t)))
1270 (with-build-handler do-not-build
1271 (package-cross-derivation %store
1272 (package (inherit pkg))
1273 "powerpc64le-linux-gnu"
1274 #:graft? #t)))))
1275
d3d337d2
LC
1276(test-equal "package->bag, cross-compilation"
1277 `(,(%current-system) "foo86-hurd"
1278 (,(package-source gnu-make))
1279 (,(canonical-package glibc)) (,(canonical-package coreutils)))
1280 (let ((bag (package->bag gnu-make (%current-system) "foo86-hurd")))
1281 (list (bag-system bag) (bag-target bag)
1282 (assoc-ref (bag-build-inputs bag) "source")
1283 (assoc-ref (bag-build-inputs bag) "libc")
1284 (assoc-ref (bag-build-inputs bag) "coreutils"))))
1285
50373bab
LC
1286(test-assert "package->bag, propagated inputs"
1287 (let* ((dep (dummy-package "dep"))
1288 (prop (dummy-package "prop"
8524349f 1289 (propagated-inputs (list dep))))
50373bab 1290 (dummy (dummy-package "dummy"
8524349f 1291 (inputs (list prop))))
50373bab 1292 (inputs (bag-transitive-inputs (package->bag dummy #:graft? #f))))
161094c8
LC
1293 (match (assoc "dep" inputs)
1294 (("dep" package)
50373bab
LC
1295 (eq? package dep)))))
1296
efb10f17
LC
1297(test-assert "package->bag, sensitivity to %current-system"
1298 (let* ((dep (dummy-package "dep"
1299 (propagated-inputs (if (string=? (%current-system)
1300 "i586-gnu")
1301 `(("libxml2" ,libxml2))
1302 '()))))
1303 (pkg (dummy-package "foo"
8524349f 1304 (native-inputs (list dep))))
efb10f17
LC
1305 (bag (package->bag pkg (%current-system) "i586-gnu")))
1306 (equal? (parameterize ((%current-system "x86_64-linux"))
1307 (bag-transitive-inputs bag))
1308 (parameterize ((%current-system "i586-gnu"))
1309 (bag-transitive-inputs bag)))))
1310
f52fbf70
LC
1311(test-assert "package->bag, sensitivity to %current-target-system"
1312 (let* ((dep (dummy-package "dep"
1313 (propagated-inputs (if (%current-target-system)
1314 `(("libxml2" ,libxml2))
1315 '()))))
1316 (pkg (dummy-package "foo"
8524349f 1317 (native-inputs (list dep))))
f52fbf70
LC
1318 (bag (package->bag pkg (%current-system) "foo86-hurd")))
1319 (equal? (parameterize ((%current-target-system "foo64-gnu"))
1320 (bag-transitive-inputs bag))
1321 (parameterize ((%current-target-system #f))
1322 (bag-transitive-inputs bag)))))
1323
ba41f87e 1324(test-assertm "bag->derivation"
05962f29
LC
1325 (parameterize ((%graft? #f))
1326 (let ((bag (package->bag gnu-make))
1327 (drv (package-derivation %store gnu-make)))
1328 (parameterize ((%current-system "foox86-hurd")) ;should have no effect
ba41f87e
LC
1329 (mlet %store-monad ((bag-drv (bag->derivation bag)))
1330 (return (equal? drv bag-drv)))))))
d3d337d2
LC
1331
1332(test-assert "bag->derivation, cross-compilation"
05962f29
LC
1333 (parameterize ((%graft? #f))
1334 (let* ((target "mips64el-linux-gnu")
1335 (bag (package->bag gnu-make (%current-system) target))
1336 (drv (package-cross-derivation %store gnu-make target)))
1337 (parameterize ((%current-system "foox86-hurd") ;should have no effect
1338 (%current-target-system "foo64-linux-gnu"))
ba41f87e
LC
1339 (mlet %store-monad ((bag-drv (bag->derivation bag)))
1340 (return (equal? drv bag-drv)))))))
d3d337d2 1341
b69c5c2c
LC
1342(when (or (not (network-reachable?)) (shebang-too-long?))
1343 (test-skip 1))
9e782349 1344(test-assert "GNU Make, bootstrap"
03d76577
LC
1345 ;; GNU-MAKE-FOR-TESTS can be built cheaply; we choose it here so that the
1346 ;; test doesn't last for too long.
1347 (let ((gnu-make gnu-make-for-tests))
9e782349
LC
1348 (and (package? gnu-make)
1349 (or (location? (package-location gnu-make))
1350 (not (package-location gnu-make)))
1351 (let* ((drv (package-derivation %store gnu-make))
59688fc4 1352 (out (derivation->output-path drv)))
14da91e2 1353 (and (build-derivations %store (list drv))
9e782349 1354 (file-exists? (string-append out "/bin/make")))))))
e3ce5d70 1355
f37f2b83
LC
1356(test-equal "package-mapping"
1357 42
1358 (let* ((dep (dummy-package "chbouib"
1359 (native-inputs `(("x" ,grep)))))
1360 (p0 (dummy-package "example"
ff39361c 1361 (source 77)
f37f2b83
LC
1362 (inputs `(("foo" ,coreutils)
1363 ("bar" ,grep)
1364 ("baz" ,dep)))))
1365 (transform (lambda (p)
1366 (package (inherit p) (source 42))))
1367 (rewrite (package-mapping transform))
ff39361c
LC
1368 (p1 (rewrite p0))
1369 (bag0 (package->bag p0))
1370 (bag1 (package->bag p1)))
f37f2b83
LC
1371 (and (eq? p1 (rewrite p0))
1372 (eqv? 42 (package-source p1))
ff39361c
LC
1373
1374 ;; Implicit inputs should be left unchanged (skip "source", "foo",
1375 ;; "bar", and "baz" in this comparison).
1376 (equal? (drop (bag-direct-inputs bag0) 4)
1377 (drop (bag-direct-inputs bag1) 4))
1378
f37f2b83
LC
1379 (match (package-inputs p1)
1380 ((("foo" dep1) ("bar" dep2) ("baz" dep3))
1381 (and (eq? dep1 (rewrite coreutils)) ;memoization
1382 (eq? dep2 (rewrite grep))
1383 (eq? dep3 (rewrite dep))
1384 (eqv? 42
1385 (package-source dep1) (package-source dep2)
1386 (package-source dep3))
1387 (match (package-native-inputs dep3)
1388 ((("x" dep))
1389 (and (eq? dep (rewrite grep))
1390 (package-source dep))))))))))
1391
ff39361c
LC
1392(test-equal "package-mapping, deep"
1393 '(42)
1394 (let* ((p0 (dummy-package "example"
1395 (inputs `(("foo" ,coreutils)
1396 ("bar" ,grep)))))
1397 (transform (lambda (p)
1398 (package (inherit p) (source 42))))
1399 (rewrite (package-mapping transform #:deep? #t))
1400 (p1 (rewrite p0))
1401 (bag (package->bag p1)))
1402 (and (eq? p1 (rewrite p0))
1403 (match (bag-direct-inputs bag)
1404 ((("source" 42) ("foo" dep1) ("bar" dep2) rest ..1)
1405 (and (eq? dep1 (rewrite coreutils)) ;memoization
1406 (eq? dep2 (rewrite grep))
1407 (= 42 (package-source dep1))
1408 (= 42 (package-source dep2))
1409
1410 ;; Check that implicit inputs of P0 also got rewritten.
1411 (delete-duplicates
1412 (map (match-lambda
1413 ((_ package . _)
1414 (package-source package)))
1415 rest))))))))
1416
2a75b0b6
LC
1417(test-assert "package-input-rewriting"
1418 (let* ((dep (dummy-package "chbouib"
1419 (native-inputs `(("x" ,grep)))))
1420 (p0 (dummy-package "example"
1421 (inputs `(("foo" ,coreutils)
1422 ("bar" ,grep)
1423 ("baz" ,dep)))))
1424 (rewrite (package-input-rewriting `((,coreutils . ,sed)
1425 (,grep . ,findutils))
8819551c
LC
1426 (cut string-append "r-" <>)
1427 #:deep? #f))
2a75b0b6
LC
1428 (p1 (rewrite p0))
1429 (p2 (rewrite p0)))
1430 (and (not (eq? p1 p0))
1431 (eq? p1 p2) ;memoization
1432 (string=? "r-example" (package-name p1))
1433 (match (package-inputs p1)
1434 ((("foo" dep1) ("bar" dep2) ("baz" dep3))
1435 (and (eq? dep1 sed)
1436 (eq? dep2 findutils)
1437 (string=? (package-name dep3) "r-chbouib")
1438 (eq? dep3 (rewrite dep)) ;memoization
1439 (match (package-native-inputs dep3)
1440 ((("x" dep))
8819551c
LC
1441 (eq? dep findutils))))))
1442
1443 ;; Make sure implicit inputs were left unchanged.
1444 (equal? (drop (bag-direct-inputs (package->bag p1)) 3)
1445 (drop (bag-direct-inputs (package->bag p0)) 3)))))
1446
1447(test-eq "package-input-rewriting, deep"
1448 (derivation-file-name (package-derivation %store sed))
1449 (let* ((p0 (dummy-package "chbouib"
1450 (build-system python-build-system)
1451 (arguments `(#:python ,python))))
1452 (rewrite (package-input-rewriting `((,python . ,sed))))
1453 (p1 (rewrite p0)))
1454 (match (bag-direct-inputs (package->bag p1))
1455 ((("python" python) _ ...)
1456 (derivation-file-name (package-derivation %store python))))))
2a75b0b6 1457
f258d886
LC
1458(test-assert "package-input-rewriting/spec"
1459 (let* ((dep (dummy-package "chbouib"
1460 (native-inputs `(("x" ,grep)))))
1461 (p0 (dummy-package "example"
1462 (inputs `(("foo" ,coreutils)
1463 ("bar" ,grep)
1464 ("baz" ,dep)))))
1465 (rewrite (package-input-rewriting/spec
1466 `(("coreutils" . ,(const sed))
2bf6f962
LC
1467 ("grep" . ,(const findutils)))
1468 #:deep? #f))
f258d886
LC
1469 (p1 (rewrite p0))
1470 (p2 (rewrite p0)))
1471 (and (not (eq? p1 p0))
1472 (eq? p1 p2) ;memoization
1473 (string=? "example" (package-name p1))
1474 (match (package-inputs p1)
1475 ((("foo" dep1) ("bar" dep2) ("baz" dep3))
1476 (and (string=? (package-full-name dep1)
1477 (package-full-name sed))
1478 (string=? (package-full-name dep2)
1479 (package-full-name findutils))
1480 (string=? (package-name dep3) "chbouib")
1481 (eq? dep3 (rewrite dep)) ;memoization
1482 (match (package-native-inputs dep3)
1483 ((("x" dep))
1484 (string=? (package-full-name dep)
2bf6f962
LC
1485 (package-full-name findutils)))))))
1486
1487 ;; Make sure implicit inputs were left unchanged.
1488 (equal? (drop (bag-direct-inputs (package->bag p1)) 3)
1489 (drop (bag-direct-inputs (package->bag p0)) 3)))))
f258d886
LC
1490
1491(test-assert "package-input-rewriting/spec, partial match"
1492 (let* ((dep (dummy-package "chbouib"
1493 (version "1")
1494 (native-inputs `(("x" ,grep)))))
1495 (p0 (dummy-package "example"
1496 (inputs `(("foo" ,coreutils)
1497 ("bar" ,dep)))))
1498 (rewrite (package-input-rewriting/spec
1499 `(("chbouib@123" . ,(const sed)) ;not matched
2bf6f962
LC
1500 ("grep" . ,(const findutils)))
1501 #:deep? #f))
f258d886
LC
1502 (p1 (rewrite p0)))
1503 (and (not (eq? p1 p0))
1504 (string=? "example" (package-name p1))
1505 (match (package-inputs p1)
1506 ((("foo" dep1) ("bar" dep2))
1507 (and (string=? (package-full-name dep1)
1508 (package-full-name coreutils))
1509 (eq? dep2 (rewrite dep)) ;memoization
1510 (match (package-native-inputs dep2)
1511 ((("x" dep))
1512 (string=? (package-full-name dep)
1513 (package-full-name findutils))))))))))
1514
2bf6f962
LC
1515(test-assert "package-input-rewriting/spec, deep"
1516 (let* ((dep (dummy-package "chbouib"))
1517 (p0 (dummy-package "example"
1518 (build-system gnu-build-system)
1519 (inputs `(("dep" ,dep)))))
1520 (rewrite (package-input-rewriting/spec
1521 `(("tar" . ,(const sed))
1522 ("gzip" . ,(const findutils)))))
1523 (p1 (rewrite p0))
1524 (p2 (rewrite p0)))
1525 (and (not (eq? p1 p0))
1526 (eq? p1 p2) ;memoization
1527 (string=? "example" (package-name p1))
1528 (match (package-inputs p1)
1529 ((("dep" dep1))
1530 (and (string=? (package-full-name dep1)
1531 (package-full-name dep))
1532 (eq? dep1 (rewrite dep))))) ;memoization
1533
1534 ;; Make sure implicit inputs were replaced.
1535 (match (bag-direct-inputs (package->bag p1))
1536 ((("dep" dep1) ("tar" tar) ("gzip" gzip) _ ...)
1537 (and (eq? dep1 (rewrite dep))
1538 (string=? (package-full-name tar)
1539 (package-full-name sed))
1540 (string=? (package-full-name gzip)
1541 (package-full-name findutils))))))))
1542
1543(test-assert "package-input-rewriting/spec, no duplicates"
1544 ;; Ensure that deep input rewriting does not forget implicit inputs. Doing
1545 ;; so could lead to duplicates in a package's inputs: in the example below,
1546 ;; P0's transitive inputs would contain one rewritten "python" and one
1547 ;; original "python". These two "python" packages are thus not 'eq?' but
1548 ;; they lower to the same derivation. See <https://bugs.gnu.org/42156>,
1549 ;; which can be reproduced by passing #:deep? #f.
1550 (let* ((dep0 (dummy-package "dep0"
1551 (build-system trivial-build-system)
1552 (propagated-inputs `(("python" ,python)))))
1553 (p0 (dummy-package "chbouib"
1554 (build-system python-build-system)
1555 (arguments `(#:python ,python))
1556 (inputs `(("dep0" ,dep0)))))
1557 (rewrite (package-input-rewriting/spec '() #:deep? #t))
1558 (p1 (rewrite p0))
1559 (bag1 (package->bag p1))
1560 (pythons (filter-map (match-lambda
1561 (("python" python) python)
1562 (_ #f))
1563 (bag-transitive-inputs bag1))))
1564 (match (delete-duplicates pythons eq?)
1565 ((p) (eq? p (rewrite python))))))
1566
b3fc03ee
LC
1567(test-equal "package-input-rewriting/spec, graft"
1568 (derivation-file-name (package-derivation %store sed))
1569
1570 ;; Make sure replacements are rewritten.
1571 (let* ((dep0 (dummy-package "dep"
1572 (version "1")
1573 (build-system trivial-build-system)
1574 (inputs `(("coreutils" ,coreutils)))))
1575 (dep1 (dummy-package "dep"
1576 (version "0")
1577 (build-system trivial-build-system)
1578 (replacement dep0)))
1579 (p0 (dummy-package "p"
1580 (build-system trivial-build-system)
1581 (inputs `(("dep" ,dep1)))))
1582 (rewrite (package-input-rewriting/spec
1583 `(("coreutils" . ,(const sed)))))
1584 (p1 (rewrite p0)))
1585 (match (package-inputs p1)
1586 ((("dep" dep))
1587 (match (package-inputs (package-replacement dep))
1588 ((("coreutils" coreutils))
1589 ;; COREUTILS is not 'eq?' to SED, so the most reliable way to check
1590 ;; for equality is to lower to a derivation.
1591 (derivation-file-name
1592 (package-derivation %store coreutils))))))))
1593
46135ce4
LC
1594(test-assert "package-with-c-toolchain"
1595 (let* ((dep (dummy-package "chbouib"
1596 (build-system gnu-build-system)
1597 (native-inputs `(("x" ,grep)))))
1598 (p0 (dummy-package "thingie"
1599 (build-system gnu-build-system)
1600 (inputs `(("foo" ,grep)
1601 ("bar" ,dep)))))
1602 (tc (dummy-package "my-toolchain"))
1603 (p1 (package-with-c-toolchain p0 `(("toolchain" ,tc)))))
1604 (define toolchain-packages
1605 '("gcc" "binutils" "glibc" "ld-wrapper"))
1606
1607 (match (bag-build-inputs (package->bag p1))
1608 ((("foo" foo) ("bar" bar) (_ (= package-name packages) . _) ...)
1609 (and (not (any (cut member <> packages) toolchain-packages))
1610 (member "my-toolchain" packages)
1611 (eq? foo grep)
1612 (eq? bar dep))))))
1613
8db4ebb0
LC
1614(test-assert "package-input-rewriting/spec, identity"
1615 ;; Make sure that 'package-input-rewriting/spec' doesn't gratuitously
1616 ;; introduce variants. In this case, the LIBFFI propagated input should not
1617 ;; be duplicated when passing GOBJECT through REWRITE.
1618 ;; See <https://issues.guix.gnu.org/43890>.
1619 (let* ((libffi (dummy-package "libffi"
1620 (build-system trivial-build-system)))
1621 (glib (dummy-package "glib"
1622 (build-system trivial-build-system)
8524349f 1623 (propagated-inputs (list libffi))))
8db4ebb0
LC
1624 (gobject (dummy-package "gobject-introspection"
1625 (build-system trivial-build-system)
8524349f
LC
1626 (inputs (list glib))
1627 (propagated-inputs (list libffi))))
8db4ebb0
LC
1628 (rewrite (package-input-rewriting/spec
1629 `(("glib" . ,identity)))))
1630 (and (= (length (package-transitive-inputs gobject))
1631 (length (package-transitive-inputs (rewrite gobject))))
1632 (string=? (derivation-file-name
1633 (package-derivation %store (rewrite gobject)))
1634 (derivation-file-name
1635 (package-derivation %store gobject))))))
1636
1637(test-assert "package-input-rewriting, identity"
1638 ;; Similar to the test above, but with 'package-input-rewriting'.
1639 ;; See <https://issues.guix.gnu.org/43890>.
1640 (let* ((libffi (dummy-package "libffi"
1641 (build-system trivial-build-system)))
1642 (glib (dummy-package "glib"
1643 (build-system trivial-build-system)
8524349f 1644 (propagated-inputs (list libffi))))
8db4ebb0
LC
1645 (gobject (dummy-package "gobject-introspection"
1646 (build-system trivial-build-system)
8524349f
LC
1647 (inputs (list glib))
1648 (propagated-inputs (list libffi))))
8db4ebb0
LC
1649 (rewrite (package-input-rewriting `((,glib . ,glib)))))
1650 (and (= (length (package-transitive-inputs gobject))
1651 (length (package-transitive-inputs (rewrite gobject))))
1652 (string=? (derivation-file-name
1653 (package-derivation %store (rewrite gobject)))
1654 (derivation-file-name
1655 (package-derivation %store gobject))))))
1656
c423ae89
LC
1657(test-equal "package-patched-vulnerabilities"
1658 '(("CVE-2015-1234")
1659 ("CVE-2016-1234" "CVE-2018-4567")
1660 ())
1661 (let ((p1 (dummy-package "pi"
1662 (source (dummy-origin
1663 (patches (list "/a/b/pi-CVE-2015-1234.patch"))))))
1664 (p2 (dummy-package "pi"
1665 (source (dummy-origin
1666 (patches (list
1667 "/a/b/pi-CVE-2016-1234-CVE-2018-4567.patch"))))))
1668 (p3 (dummy-package "pi" (source (dummy-origin)))))
1669 (map package-patched-vulnerabilities
1670 (list p1 p2 p3))))
1671
ba326ce4
LC
1672(test-eq "fold-packages" hello
1673 (fold-packages (lambda (p r)
1674 (if (string=? (package-name p) "hello")
1675 p
1676 r))
1677 #f))
1678
386b71d1
LC
1679(test-assert "fold-packages, hidden package"
1680 ;; There are two public variables providing "guile@2.0" ('guile-final' in
b66d6d52 1681 ;; commencement.scm and 'guile-2.0' in guile.scm), but only the latter
386b71d1
LC
1682 ;; should show up.
1683 (match (fold-packages (lambda (p r)
1684 (if (and (string=? (package-name p) "guile")
1685 (string-prefix? "2.0"
1686 (package-version p)))
1687 (cons p r)
1688 r))
1689 '())
1690 ((one)
b66d6d52 1691 (eq? one guile-2.0))))
386b71d1 1692
0ea939fb
LC
1693(test-assert "fold-available-packages with/without cache"
1694 (let ()
1695 (define no-cache
1696 (fold-available-packages (lambda* (name version result #:rest rest)
1697 (cons (cons* name version rest)
1698 result))
1699 '()))
1700
1701 (define from-cache
1702 (call-with-temporary-directory
1703 (lambda (cache)
1704 (generate-package-cache cache)
1705 (mock ((guix describe) current-profile (const cache))
1706 (mock ((gnu packages) cache-is-authoritative? (const #t))
1707 (fold-available-packages (lambda* (name version result
1708 #:rest rest)
1709 (cons (cons* name version rest)
1710 result))
1711 '()))))))
1712
73744725
LC
1713 (define (list->set* lst)
1714 ;; Return two values: LST represented as a set and the list of
1715 ;; duplicates in LST.
1716 (let loop ((lst lst)
1717 (duplicates '())
1718 (seen (set)))
1719 (match lst
1720 (()
1721 (values seen duplicates))
1722 ((head . tail)
1723 (if (set-contains? seen head)
1724 (loop tail (cons head duplicates) seen)
1725 (loop tail duplicates (set-insert head seen)))))))
1726
1727 ;; Compare FROM-CACHE and NO-CACHE but avoid 'lset=', which exhibits
1728 ;; exponential behavior.
1729 (let ((set1 duplicates1 (list->set* from-cache))
1730 (set2 duplicates2 (list->set* no-cache)))
1731 (and (null? duplicates1) (null? duplicates2)
1732 (every (cut set-contains? set1 <>) no-cache)
1733 (every (cut set-contains? set2 <>) from-cache)))))
0ea939fb 1734
6b1891b0
LC
1735(test-assert "find-packages-by-name"
1736 (match (find-packages-by-name "hello")
1737 (((? (cut eq? hello <>))) #t)
1738 (wrong (pk 'find-packages-by-name wrong #f))))
1739
1740(test-assert "find-packages-by-name with version"
1741 (match (find-packages-by-name "hello" (package-version hello))
1742 (((? (cut eq? hello <>))) #t)
1743 (wrong (pk 'find-packages-by-name wrong #f))))
1744
5fbdc9a5
LC
1745(test-equal "find-packages-by-name with cache"
1746 (find-packages-by-name "guile")
1747 (call-with-temporary-directory
1748 (lambda (cache)
1749 (generate-package-cache cache)
1750 (mock ((guix describe) current-profile (const cache))
1751 (mock ((gnu packages) cache-is-authoritative? (const #t))
1752 (find-packages-by-name "guile"))))))
1753
1754(test-equal "find-packages-by-name + version, with cache"
1755 (find-packages-by-name "guile" "2")
1756 (call-with-temporary-directory
1757 (lambda (cache)
1758 (generate-package-cache cache)
1759 (mock ((guix describe) current-profile (const cache))
1760 (mock ((gnu packages) cache-is-authoritative? (const #t))
1761 (find-packages-by-name "guile" "2"))))))
1762
cf81a236
LC
1763(test-assert "--search-paths with pattern"
1764 ;; Make sure 'guix package --search-paths' correctly reports environment
1765 ;; variables when file patterns are used (in particular, it must follow
1766 ;; symlinks when looking for 'catalog.xml'.) To do that, we rely on the
1767 ;; libxml2 package specification, which contains such a definition.
1768 (let* ((p1 (package
1769 (name "foo") (version "0") (source #f)
1770 (build-system trivial-build-system)
1771 (arguments
1772 `(#:guile ,%bootstrap-guile
1773 #:modules ((guix build utils))
1774 #:builder (begin
1775 (use-modules (guix build utils))
1776 (let ((out (assoc-ref %outputs "out")))
1777 (mkdir-p (string-append out "/xml/bar/baz"))
1778 (call-with-output-file
1779 (string-append out "/xml/bar/baz/catalog.xml")
1780 (lambda (port)
1e868858
MW
1781 (display "xml? wat?!" port)))
1782 #t))))
cf81a236
LC
1783 (synopsis #f) (description #f)
1784 (home-page #f) (license #f)))
1785 (p2 (package
1786 ;; Provide a fake libxml2 to avoid building the real one. This
1787 ;; is OK because 'guix package' gets search path specifications
1788 ;; from the same-named package found in the distro.
1789 (name "libxml2") (version "0.0.0") (source #f)
1790 (build-system trivial-build-system)
1791 (arguments
1792 `(#:guile ,%bootstrap-guile
1e868858
MW
1793 #:builder (begin
1794 (mkdir (assoc-ref %outputs "out"))
1795 #t)))
cf81a236
LC
1796 (native-search-paths (package-native-search-paths libxml2))
1797 (synopsis #f) (description #f)
1798 (home-page #f) (license #f)))
1799 (prof (run-with-store %store
1800 (profile-derivation
1801 (manifest (map package->manifest-entry
1802 (list p1 p2)))
a6562c7e
LC
1803 #:hooks '()
1804 #:locales? #f)
cf81a236
LC
1805 #:guile-for-build (%guile-for-build))))
1806 (build-derivations %store (list prof))
1807 (string-match (format #f "^export XML_CATALOG_FILES=\"~a/xml/+bar/baz/catalog\\.xml\"\n"
77559f23 1808 (regexp-quote (derivation->output-path prof)))
cf81a236
LC
1809 (with-output-to-string
1810 (lambda ()
1811 (guix-package "-p" (derivation->output-path prof)
1812 "--search-paths"))))))
1813
fcd75bdb
LC
1814(test-assert "--search-paths with single-item search path"
1815 ;; Make sure 'guix package --search-paths' correctly reports environment
1816 ;; variables for things like 'GIT_SSL_CAINFO' that have #f as their
1817 ;; separator, meaning that the first match wins.
1818 (let* ((p1 (dummy-package "foo"
1819 (build-system trivial-build-system)
1820 (arguments
1821 `(#:guile ,%bootstrap-guile
1822 #:modules ((guix build utils))
1823 #:builder (begin
1824 (use-modules (guix build utils))
1825 (let ((out (assoc-ref %outputs "out")))
1826 (mkdir-p (string-append out "/etc/ssl/certs"))
1827 (call-with-output-file
1828 (string-append
1829 out "/etc/ssl/certs/ca-certificates.crt")
1830 (const #t))))))))
1831 (p2 (package (inherit p1) (name "bar")))
1832 (p3 (dummy-package "git"
1833 ;; Provide a fake Git to avoid building the real one.
1834 (build-system trivial-build-system)
1835 (arguments
1836 `(#:guile ,%bootstrap-guile
1e868858
MW
1837 #:builder (begin
1838 (mkdir (assoc-ref %outputs "out"))
1839 #t)))
fcd75bdb
LC
1840 (native-search-paths (package-native-search-paths git))))
1841 (prof1 (run-with-store %store
1842 (profile-derivation
1843 (packages->manifest (list p1 p3))
1844 #:hooks '()
1845 #:locales? #f)
1846 #:guile-for-build (%guile-for-build)))
1847 (prof2 (run-with-store %store
1848 (profile-derivation
1849 (packages->manifest (list p2 p3))
1850 #:hooks '()
1851 #:locales? #f)
1852 #:guile-for-build (%guile-for-build))))
1853 (build-derivations %store (list prof1 prof2))
1854 (string-match (format #f "^export GIT_SSL_CAINFO=\"~a/etc/ssl/certs/ca-certificates.crt"
1855 (regexp-quote (derivation->output-path prof1)))
1856 (with-output-to-string
1857 (lambda ()
1858 (guix-package "-p" (derivation->output-path prof1)
1859 "-p" (derivation->output-path prof2)
1860 "--search-paths"))))))
1861
efb107e0
LC
1862(test-equal "specification->package when not found"
1863 'quit
1864 (catch 'quit
1865 (lambda ()
1866 ;; This should call 'leave', producing an error message.
1867 (specification->package "this-package-does-not-exist"))
1868 (lambda (key . args)
1869 key)))
1870
066eeae1
LC
1871(test-equal "specification->package+output"
1872 `((,coreutils "out") (,coreutils "debug"))
1873 (list (call-with-values (lambda ()
1874 (specification->package+output "coreutils"))
1875 list)
1876 (call-with-values (lambda ()
1877 (specification->package+output "coreutils:debug"))
1878 list)))
1879
1880(test-equal "specification->package+output invalid output"
1881 'error
1882 (catch 'quit
1883 (lambda ()
1884 (specification->package+output "coreutils:does-not-exist"))
1885 (lambda _
1886 'error)))
1887
1888(test-equal "specification->package+output no default output"
1889 `(,coreutils #f)
1890 (call-with-values
1891 (lambda ()
1892 (specification->package+output "coreutils" #f))
1893 list))
1894
1895(test-equal "specification->package+output invalid output, no default"
1896 'error
1897 (catch 'quit
1898 (lambda ()
1899 (specification->package+output "coreutils:does-not-exist" #f))
1900 (lambda _
1901 'error)))
1902
ee8099f5
LC
1903(test-equal "find-package-locations"
1904 (map (lambda (package)
1905 (cons (package-version package)
1906 (package-location package)))
1907 (find-packages-by-name "guile"))
1908 (find-package-locations "guile"))
1909
1910(test-equal "find-package-locations with cache"
1911 (map (lambda (package)
1912 (cons (package-version package)
1913 (package-location package)))
1914 (find-packages-by-name "guile"))
1915 (call-with-temporary-directory
1916 (lambda (cache)
1917 (generate-package-cache cache)
1918 (mock ((guix describe) current-profile (const cache))
1919 (mock ((gnu packages) cache-is-authoritative? (const #t))
1920 (find-package-locations "guile"))))))
1921
1922(test-equal "specification->location"
1923 (package-location (specification->package "guile@2"))
1924 (specification->location "guile@2"))
1925
aaf9aa48
MD
1926(test-eq "this-package-input, exists"
1927 hello
1928 (package-arguments
1929 (dummy-package "a"
1930 (inputs `(("hello" ,hello)))
1931 (arguments (this-package-input "hello")))))
1932
1933(test-eq "this-package-input, exists in propagated-inputs"
1934 hello
1935 (package-arguments
1936 (dummy-package "a"
1937 (propagated-inputs `(("hello" ,hello)))
1938 (arguments (this-package-input "hello")))))
1939
1940(test-eq "this-package-input, does not exist"
1941 #f
1942 (package-arguments
1943 (dummy-package "a"
1944 (arguments (this-package-input "hello")))))
1945
1946(test-eq "this-package-native-input, exists"
1947 hello
1948 (package-arguments
1949 (dummy-package "a"
1950 (native-inputs `(("hello" ,hello)))
1951 (arguments (this-package-native-input "hello")))))
1952
1953(test-eq "this-package-native-input, does not exists"
1954 #f
1955 (package-arguments
1956 (dummy-package "a"
1957 (arguments (this-package-native-input "hello")))))
1958
e3801126
LC
1959(test-eq "modify-inputs, replace"
1960 coreutils
1961 ;; Replace an input; notice that the label in unchanged.
1962 (let* ((p1 (dummy-package "p"
1963 (inputs (list hello))))
1964 (p2 (package
1965 (inherit p1)
1966 (version "1")
1967 (inputs (modify-inputs (package-inputs p1)
1968 (replace "hello" coreutils))))))
1969 (lookup-package-input p2 "hello")))
1970
1971(test-eq "modify-inputs, replace, change output"
1972 guile-3.0
1973 ;; Replace an input and choose a different output.
1974 (let* ((p1 (dummy-package "p"
1975 (inputs (list `(,coreutils "debug")))))
1976 (p2 (package
1977 (inherit p1)
1978 (version "1")
1979 (inputs (modify-inputs (package-inputs p1)
1980 (replace "coreutils" `(,guile-3.0 "out")))))))
1981 (match (package-inputs p2)
1982 ((("coreutils" input "out"))
1983 input))))
1984
00dfff84
LC
1985(test-eq "modify-inputs, replace, extra output"
1986 guile-3.0
1987 ;; Replace an input; notice that its output is preserved.
1988 ;; See <https://issues.guix.gnu.org/53915>.
1989 (let* ((p1 (dummy-package "p"
1990 (inputs (list `(,coreutils "debug")))))
1991 (p2 (package
1992 (inherit p1)
1993 (version "1")
1994 (inputs (modify-inputs (package-inputs p1)
1995 (replace "coreutils" guile-3.0))))))
1996 (match (package-inputs p2)
1997 ((("coreutils" input "debug"))
1998 input))))
1999
e3ce5d70
LC
2000(test-end "packages")
2001
e3ce5d70 2002;;; Local Variables:
a3d73f59 2003;;; eval: (put 'dummy-package 'scheme-indent-function 1)
3e223a22 2004;;; eval: (put 'dummy-package/no-implicit 'scheme-indent-function 1)
e3ce5d70 2005;;; End: