derivations: Determine what's built in 'check' mode.
[jackhill/guix/guix.git] / tests / derivations.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (test-derivations)
20 #:use-module (guix derivations)
21 #:use-module (guix store)
22 #:use-module (guix utils)
23 #:use-module (guix hash)
24 #:use-module (guix base32)
25 #:use-module (guix tests)
26 #:use-module ((guix packages) #:select (package-derivation base32))
27 #:use-module ((guix build utils) #:select (executable-file?))
28 #:use-module ((gnu packages) #:select (search-bootstrap-binary))
29 #:use-module (gnu packages bootstrap)
30 #:use-module ((gnu packages guile) #:select (guile-1.8))
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-11)
33 #:use-module (srfi srfi-26)
34 #:use-module (srfi srfi-34)
35 #:use-module (srfi srfi-64)
36 #:use-module (rnrs io ports)
37 #:use-module (rnrs bytevectors)
38 #:use-module (web uri)
39 #:use-module (ice-9 rdelim)
40 #:use-module (ice-9 regex)
41 #:use-module (ice-9 ftw)
42 #:use-module (ice-9 match))
43
44 (define %store
45 (open-connection-for-tests))
46
47 (define (bootstrap-binary name)
48 (let ((bin (search-bootstrap-binary name (%current-system))))
49 (and %store
50 (add-to-store %store name #t "sha256" bin))))
51
52 (define %bash
53 (bootstrap-binary "bash"))
54 (define %mkdir
55 (bootstrap-binary "mkdir"))
56
57 (define* (directory-contents dir #:optional (slurp get-bytevector-all))
58 "Return an alist representing the contents of DIR."
59 (define prefix-len (string-length dir))
60 (sort (file-system-fold (const #t) ; enter?
61 (lambda (path stat result) ; leaf
62 (alist-cons (string-drop path prefix-len)
63 (call-with-input-file path slurp)
64 result))
65 (lambda (path stat result) result) ; down
66 (lambda (path stat result) result) ; up
67 (lambda (path stat result) result) ; skip
68 (lambda (path stat errno result) result) ; error
69 '()
70 dir)
71 (lambda (e1 e2)
72 (string<? (car e1) (car e2)))))
73
74 (test-begin "derivations")
75
76 (test-assert "parse & export"
77 (let* ((f (search-path %load-path "tests/test.drv"))
78 (b1 (call-with-input-file f get-bytevector-all))
79 (d1 (read-derivation (open-bytevector-input-port b1)))
80 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
81 (d2 (read-derivation (open-bytevector-input-port b2))))
82 (and (equal? b1 b2)
83 (equal? d1 d2))))
84
85 (test-skip (if %store 0 12))
86
87 (test-assert "add-to-store, flat"
88 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
89 (drv (add-to-store %store "flat-test" #f "sha256" file)))
90 (and (eq? 'regular (stat:type (stat drv)))
91 (valid-path? %store drv)
92 (equal? (call-with-input-file file get-bytevector-all)
93 (call-with-input-file drv get-bytevector-all)))))
94
95 (test-assert "add-to-store, recursive"
96 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
97 (drv (add-to-store %store "dir-tree-test" #t "sha256" dir)))
98 (and (eq? 'directory (stat:type (stat drv)))
99 (valid-path? %store drv)
100 (equal? (directory-contents dir)
101 (directory-contents drv)))))
102
103 (test-assert "derivation with no inputs"
104 (let* ((builder (add-text-to-store %store "my-builder.sh"
105 "echo hello, world\n"
106 '()))
107 (drv (derivation %store "foo"
108 %bash `("-e" ,builder)
109 #:env-vars '(("HOME" . "/homeless")))))
110 (and (store-path? (derivation-file-name drv))
111 (valid-path? %store (derivation-file-name drv)))))
112
113 (test-assert "build derivation with 1 source"
114 (let* ((builder (add-text-to-store %store "my-builder.sh"
115 "echo hello, world > \"$out\"\n"
116 '()))
117 (drv (derivation %store "foo"
118 %bash `(,builder)
119 #:env-vars '(("HOME" . "/homeless")
120 ("zzz" . "Z!")
121 ("AAA" . "A!"))
122 #:inputs `((,%bash) (,builder))))
123 (succeeded?
124 (build-derivations %store (list drv))))
125 (and succeeded?
126 (let ((path (derivation->output-path drv)))
127 (and (valid-path? %store path)
128 (string=? (call-with-input-file path read-line)
129 "hello, world"))))))
130
131 (test-assert "derivation with local file as input"
132 (let* ((builder (add-text-to-store
133 %store "my-builder.sh"
134 "(while read line ; do echo \"$line\" ; done) < $in > $out"
135 '()))
136 (input (search-path %load-path "ice-9/boot-9.scm"))
137 (input* (add-to-store %store (basename input)
138 #t "sha256" input))
139 (drv (derivation %store "derivation-with-input-file"
140 %bash `(,builder)
141
142 ;; Cheat to pass the actual file name to the
143 ;; builder.
144 #:env-vars `(("in" . ,input*))
145
146 #:inputs `((,%bash)
147 (,builder)
148 (,input))))) ; ← local file name
149 (and (build-derivations %store (list drv))
150 ;; Note: we can't compare the files because the above trick alters
151 ;; the contents.
152 (valid-path? %store (derivation->output-path drv)))))
153
154 (test-assert "identical files are deduplicated"
155 (let* ((build1 (add-text-to-store %store "one.sh"
156 "echo hello, world > \"$out\"\n"
157 '()))
158 (build2 (add-text-to-store %store "two.sh"
159 "# Hey!\necho hello, world > \"$out\"\n"
160 '()))
161 (drv1 (derivation %store "foo"
162 %bash `(,build1)
163 #:inputs `((,%bash) (,build1))))
164 (drv2 (derivation %store "bar"
165 %bash `(,build2)
166 #:inputs `((,%bash) (,build2)))))
167 (and (build-derivations %store (list drv1 drv2))
168 (let ((file1 (derivation->output-path drv1))
169 (file2 (derivation->output-path drv2)))
170 (and (valid-path? %store file1) (valid-path? %store file2)
171 (string=? (call-with-input-file file1 get-string-all)
172 "hello, world\n")
173 (= (stat:ino (lstat file1))
174 (stat:ino (lstat file2))))))))
175
176 (test-equal "derivation-name"
177 "foo-0.0"
178 (let ((drv (derivation %store "foo-0.0" %bash '())))
179 (derivation-name drv)))
180
181 (test-equal "derivation-output-names"
182 '(("out") ("bar" "chbouib"))
183 (let ((drv1 (derivation %store "foo-0.0" %bash '()))
184 (drv2 (derivation %store "foo-0.0" %bash '()
185 #:outputs '("bar" "chbouib"))))
186 (list (derivation-output-names drv1)
187 (derivation-output-names drv2))))
188
189 (test-assert "offloadable-derivation?"
190 (and (offloadable-derivation? (derivation %store "foo" %bash '()))
191 (offloadable-derivation? ;see <http://bugs.gnu.org/18747>
192 (derivation %store "foo" %bash '()
193 #:substitutable? #f))
194 (not (offloadable-derivation?
195 (derivation %store "foo" %bash '()
196 #:local-build? #t)))))
197
198 (test-assert "substitutable-derivation?"
199 (and (substitutable-derivation? (derivation %store "foo" %bash '()))
200 (substitutable-derivation? ;see <http://bugs.gnu.org/18747>
201 (derivation %store "foo" %bash '()
202 #:local-build? #t))
203 (not (substitutable-derivation?
204 (derivation %store "foo" %bash '()
205 #:substitutable? #f)))))
206
207 (test-assert "fixed-output-derivation?"
208 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
209 "echo -n hello > $out" '()))
210 (hash (sha256 (string->utf8 "hello")))
211 (drv (derivation %store "fixed"
212 %bash `(,builder)
213 #:inputs `((,builder))
214 #:hash hash #:hash-algo 'sha256)))
215 (fixed-output-derivation? drv)))
216
217 (test-assert "fixed-output derivation"
218 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
219 "echo -n hello > $out" '()))
220 (hash (sha256 (string->utf8 "hello")))
221 (drv (derivation %store "fixed"
222 %bash `(,builder)
223 #:inputs `((,builder)) ; optional
224 #:hash hash #:hash-algo 'sha256))
225 (succeeded? (build-derivations %store (list drv))))
226 (and succeeded?
227 (let ((p (derivation->output-path drv)))
228 (and (equal? (string->utf8 "hello")
229 (call-with-input-file p get-bytevector-all))
230 (bytevector? (query-path-hash %store p)))))))
231
232 (test-assert "fixed-output derivation: output paths are equal"
233 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
234 "echo -n hello > $out" '()))
235 (builder2 (add-text-to-store %store "fixed-builder2.sh"
236 "echo hey; echo -n hello > $out" '()))
237 (hash (sha256 (string->utf8 "hello")))
238 (drv1 (derivation %store "fixed"
239 %bash `(,builder1)
240 #:hash hash #:hash-algo 'sha256))
241 (drv2 (derivation %store "fixed"
242 %bash `(,builder2)
243 #:hash hash #:hash-algo 'sha256))
244 (succeeded? (build-derivations %store (list drv1 drv2))))
245 (and succeeded?
246 (equal? (derivation->output-path drv1)
247 (derivation->output-path drv2)))))
248
249 (test-assert "fixed-output derivation, recursive"
250 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
251 "echo -n hello > $out" '()))
252 (hash (sha256 (string->utf8 "hello")))
253 (drv (derivation %store "fixed-rec"
254 %bash `(,builder)
255 #:inputs `((,builder))
256 #:hash (base32 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
257 #:hash-algo 'sha256
258 #:recursive? #t))
259 (succeeded? (build-derivations %store (list drv))))
260 (and succeeded?
261 (let ((p (derivation->output-path drv)))
262 (and (equal? (string->utf8 "hello")
263 (call-with-input-file p get-bytevector-all))
264 (bytevector? (query-path-hash %store p)))))))
265
266 (test-assert "derivation with a fixed-output input"
267 ;; A derivation D using a fixed-output derivation F doesn't has the same
268 ;; output path when passed F or F', as long as F and F' have the same output
269 ;; path.
270 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
271 "echo -n hello > $out" '()))
272 (builder2 (add-text-to-store %store "fixed-builder2.sh"
273 "echo hey; echo -n hello > $out" '()))
274 (hash (sha256 (string->utf8 "hello")))
275 (fixed1 (derivation %store "fixed"
276 %bash `(,builder1)
277 #:hash hash #:hash-algo 'sha256))
278 (fixed2 (derivation %store "fixed"
279 %bash `(,builder2)
280 #:hash hash #:hash-algo 'sha256))
281 (fixed-out (derivation->output-path fixed1))
282 (builder3 (add-text-to-store
283 %store "final-builder.sh"
284 ;; Use Bash hackery to avoid Coreutils.
285 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
286 (final1 (derivation %store "final"
287 %bash `(,builder3)
288 #:env-vars `(("in" . ,fixed-out))
289 #:inputs `((,%bash) (,builder3) (,fixed1))))
290 (final2 (derivation %store "final"
291 %bash `(,builder3)
292 #:env-vars `(("in" . ,fixed-out))
293 #:inputs `((,%bash) (,builder3) (,fixed2))))
294 (succeeded? (build-derivations %store
295 (list final1 final2))))
296 (and succeeded?
297 (equal? (derivation->output-path final1)
298 (derivation->output-path final2)))))
299
300 (test-assert "multiple-output derivation"
301 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
302 "echo one > $out ; echo two > $second"
303 '()))
304 (drv (derivation %store "fixed"
305 %bash `(,builder)
306 #:env-vars '(("HOME" . "/homeless")
307 ("zzz" . "Z!")
308 ("AAA" . "A!"))
309 #:inputs `((,%bash) (,builder))
310 #:outputs '("out" "second")))
311 (succeeded? (build-derivations %store (list drv))))
312 (and succeeded?
313 (let ((one (derivation->output-path drv "out"))
314 (two (derivation->output-path drv "second")))
315 (and (lset= equal?
316 (derivation->output-paths drv)
317 `(("out" . ,one) ("second" . ,two)))
318 (eq? 'one (call-with-input-file one read))
319 (eq? 'two (call-with-input-file two read)))))))
320
321 (test-assert "multiple-output derivation, non-alphabetic order"
322 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
323 ;; path computation must reorder them first.
324 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
325 "echo one > $out ; echo two > $AAA"
326 '()))
327 (drv (derivation %store "fixed"
328 %bash `(,builder)
329 #:inputs `((,%bash) (,builder))
330 #:outputs '("out" "AAA")))
331 (succeeded? (build-derivations %store (list drv))))
332 (and succeeded?
333 (let ((one (derivation->output-path drv "out"))
334 (two (derivation->output-path drv "AAA")))
335 (and (eq? 'one (call-with-input-file one read))
336 (eq? 'two (call-with-input-file two read)))))))
337
338 (test-assert "multiple-output derivation, derivation-path->output-path"
339 (let* ((builder (add-text-to-store %store "builder.sh"
340 "echo one > $out ; echo two > $second"
341 '()))
342 (drv (derivation %store "multiple"
343 %bash `(,builder)
344 #:outputs '("out" "second")))
345 (drv-file (derivation-file-name drv))
346 (one (derivation->output-path drv "out"))
347 (two (derivation->output-path drv "second"))
348 (first (derivation-path->output-path drv-file "out"))
349 (second (derivation-path->output-path drv-file "second")))
350 (and (not (string=? one two))
351 (string-suffix? "-second" two)
352 (string=? first one)
353 (string=? second two))))
354
355 (test-assert "user of multiple-output derivation"
356 ;; Check whether specifying several inputs coming from the same
357 ;; multiple-output derivation works.
358 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
359 "echo one > $out ; echo two > $two"
360 '()))
361 (mdrv (derivation %store "multiple-output"
362 %bash `(,builder1)
363 #:inputs `((,%bash) (,builder1))
364 #:outputs '("out" "two")))
365 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
366 "read x < $one;
367 read y < $two;
368 echo \"($x $y)\" > $out"
369 '()))
370 (udrv (derivation %store "multiple-output-user"
371 %bash `(,builder2)
372 #:env-vars `(("one"
373 . ,(derivation->output-path
374 mdrv "out"))
375 ("two"
376 . ,(derivation->output-path
377 mdrv "two")))
378 #:inputs `((,%bash)
379 (,builder2)
380 ;; two occurrences of MDRV:
381 (,mdrv)
382 (,mdrv "two")))))
383 (and (build-derivations %store (list (pk 'udrv udrv)))
384 (let ((p (derivation->output-path udrv)))
385 (and (valid-path? %store p)
386 (equal? '(one two) (call-with-input-file p read)))))))
387
388 (test-assert "derivation with #:references-graphs"
389 (let* ((input1 (add-text-to-store %store "foo" "hello"
390 (list %bash)))
391 (input2 (add-text-to-store %store "bar"
392 (number->string (random 7777))
393 (list input1)))
394 (builder (add-text-to-store %store "build-graph"
395 (format #f "
396 ~a $out
397 (while read l ; do echo $l ; done) < bash > $out/bash
398 (while read l ; do echo $l ; done) < input1 > $out/input1
399 (while read l ; do echo $l ; done) < input2 > $out/input2"
400 %mkdir)
401 (list %mkdir)))
402 (drv (derivation %store "closure-graphs"
403 %bash `(,builder)
404 #:references-graphs
405 `(("bash" . ,%bash)
406 ("input1" . ,input1)
407 ("input2" . ,input2))
408 #:inputs `((,%bash) (,builder))))
409 (out (derivation->output-path drv)))
410 (define (deps path . deps)
411 (let ((count (length deps)))
412 (string-append path "\n\n" (number->string count) "\n"
413 (string-join (sort deps string<?) "\n")
414 (if (zero? count) "" "\n"))))
415
416 (and (build-derivations %store (list drv))
417 (equal? (directory-contents out get-string-all)
418 `(("/bash" . ,(string-append %bash "\n\n0\n"))
419 ("/input1" . ,(if (string>? input1 %bash)
420 (string-append (deps %bash)
421 (deps input1 %bash))
422 (string-append (deps input1 %bash)
423 (deps %bash))))
424 ("/input2" . ,(string-concatenate
425 (map cdr
426 (sort
427 (map (lambda (p d)
428 (cons p (apply deps p d)))
429 (list %bash input1 input2)
430 (list '() (list %bash) (list input1)))
431 (lambda (x y)
432 (match x
433 ((p1 . _)
434 (match y
435 ((p2 . _)
436 (string<? p1 p2)))))))))))))))
437
438 (test-assert "derivation #:allowed-references, ok"
439 (let ((drv (derivation %store "allowed" %bash
440 '("-c" "echo hello > $out")
441 #:inputs `((,%bash))
442 #:allowed-references '())))
443 (build-derivations %store (list drv))))
444
445 (test-assert "derivation #:allowed-references, not allowed"
446 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
447 (drv (derivation %store "disallowed" %bash
448 `("-c" ,(string-append "echo " txt "> $out"))
449 #:inputs `((,%bash) (,txt))
450 #:allowed-references '())))
451 (guard (c ((nix-protocol-error? c)
452 ;; There's no specific error message to check for.
453 #t))
454 (build-derivations %store (list drv))
455 #f)))
456
457 (test-assert "derivation #:allowed-references, self allowed"
458 (let ((drv (derivation %store "allowed" %bash
459 '("-c" "echo $out > $out")
460 #:inputs `((,%bash))
461 #:allowed-references '("out"))))
462 (build-derivations %store (list drv))))
463
464 (test-assert "derivation #:allowed-references, self not allowed"
465 (let ((drv (derivation %store "disallowed" %bash
466 `("-c" ,"echo $out > $out")
467 #:inputs `((,%bash))
468 #:allowed-references '())))
469 (guard (c ((nix-protocol-error? c)
470 ;; There's no specific error message to check for.
471 #t))
472 (build-derivations %store (list drv))
473 #f)))
474
475 \f
476 (define %coreutils
477 (false-if-exception
478 (and (network-reachable?)
479 (package-derivation %store %bootstrap-coreutils&co))))
480
481 (test-skip (if %coreutils 0 1))
482
483 (test-assert "build derivation with coreutils"
484 (let* ((builder
485 (add-text-to-store %store "build-with-coreutils.sh"
486 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
487 '()))
488 (drv
489 (derivation %store "foo"
490 %bash `(,builder)
491 #:env-vars `(("PATH" .
492 ,(string-append
493 (derivation->output-path %coreutils)
494 "/bin")))
495 #:inputs `((,builder)
496 (,%coreutils))))
497 (succeeded?
498 (build-derivations %store (list drv))))
499 (and succeeded?
500 (let ((p (derivation->output-path drv)))
501 (and (valid-path? %store p)
502 (file-exists? (string-append p "/good")))))))
503
504 (test-skip (if (%guile-for-build) 0 8))
505
506 (test-assert "build-expression->derivation and derivation-prerequisites"
507 (let ((drv (build-expression->derivation %store "fail" #f)))
508 (any (match-lambda
509 (($ <derivation-input> path)
510 (string=? path (derivation-file-name (%guile-for-build)))))
511 (derivation-prerequisites drv))))
512
513 (test-assert "derivation-prerequisites and valid-derivation-input?"
514 (let* ((a (build-expression->derivation %store "a" '(mkdir %output)))
515 (b (build-expression->derivation %store "b" `(list ,(random-text))))
516 (c (build-expression->derivation %store "c" `(mkdir %output)
517 #:inputs `(("a" ,a) ("b" ,b)))))
518 ;; Make sure both A and %BOOTSTRAP-GUILE are built (the latter could have
519 ;; be removed by tests/guix-gc.sh.)
520 (build-derivations %store
521 (list a (package-derivation %store %bootstrap-guile)))
522
523 (match (derivation-prerequisites c
524 (cut valid-derivation-input? %store
525 <>))
526 ((($ <derivation-input> file ("out")))
527 (string=? file (derivation-file-name b)))
528 (x
529 (pk 'fail x #f)))))
530
531 (test-assert "build-expression->derivation without inputs"
532 (let* ((builder '(begin
533 (mkdir %output)
534 (call-with-output-file (string-append %output "/test")
535 (lambda (p)
536 (display '(hello guix) p)))))
537 (drv (build-expression->derivation %store "goo" builder))
538 (succeeded? (build-derivations %store (list drv))))
539 (and succeeded?
540 (let ((p (derivation->output-path drv)))
541 (equal? '(hello guix)
542 (call-with-input-file (string-append p "/test") read))))))
543
544 (test-assert "build-expression->derivation and max-silent-time"
545 (let* ((store (let ((s (open-connection)))
546 (set-build-options s #:max-silent-time 1)
547 s))
548 (builder '(begin (sleep 100) (mkdir %output) #t))
549 (drv (build-expression->derivation store "silent" builder))
550 (out-path (derivation->output-path drv)))
551 (guard (c ((nix-protocol-error? c)
552 (and (string-contains (nix-protocol-error-message c)
553 "failed")
554 (not (valid-path? store out-path)))))
555 (build-derivations store (list drv))
556 #f)))
557
558 (test-assert "build-expression->derivation and timeout"
559 (let* ((store (let ((s (open-connection)))
560 (set-build-options s #:timeout 1)
561 s))
562 (builder '(begin (sleep 100) (mkdir %output) #t))
563 (drv (build-expression->derivation store "slow" builder))
564 (out-path (derivation->output-path drv)))
565 (guard (c ((nix-protocol-error? c)
566 (and (string-contains (nix-protocol-error-message c)
567 "failed")
568 (not (valid-path? store out-path)))))
569 (build-derivations store (list drv))
570 #f)))
571
572 (test-assert "build-expression->derivation and derivation-prerequisites-to-build"
573 (let ((drv (build-expression->derivation %store "fail" #f)))
574 ;; The only direct dependency is (%guile-for-build) and it's already
575 ;; built.
576 (null? (derivation-prerequisites-to-build %store drv))))
577
578 (test-assert "derivation-prerequisites-to-build when outputs already present"
579 (let* ((builder '(begin (mkdir %output) #t))
580 (input-drv (build-expression->derivation %store "input" builder))
581 (input-path (derivation-output-path
582 (assoc-ref (derivation-outputs input-drv)
583 "out")))
584 (drv (build-expression->derivation %store "something" builder
585 #:inputs
586 `(("i" ,input-drv))))
587 (output (derivation->output-path drv)))
588 ;; Make sure these things are not already built.
589 (when (valid-path? %store input-path)
590 (delete-paths %store (list input-path)))
591 (when (valid-path? %store output)
592 (delete-paths %store (list output)))
593
594 (and (equal? (map derivation-input-path
595 (derivation-prerequisites-to-build %store drv))
596 (list (derivation-file-name input-drv)))
597
598 ;; Build DRV and delete its input.
599 (build-derivations %store (list drv))
600 (delete-paths %store (list input-path))
601 (not (valid-path? %store input-path))
602
603 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
604 ;; prerequisite to build because DRV itself is already built.
605 (null? (derivation-prerequisites-to-build %store drv)))))
606
607 (test-assert "derivation-prerequisites-to-build and substitutes"
608 (let* ((store (open-connection))
609 (drv (build-expression->derivation store "prereq-subst"
610 (random 1000)))
611 (output (derivation->output-path drv)))
612
613 ;; Make sure substitutes are usable.
614 (set-build-options store #:use-substitutes? #t
615 #:substitute-urls (%test-substitute-urls))
616
617 (with-derivation-narinfo drv
618 (let-values (((build download)
619 (derivation-prerequisites-to-build store drv))
620 ((build* download*)
621 (derivation-prerequisites-to-build store drv
622 #:substitutable?
623 (const #f))))
624 (and (null? build)
625 (equal? download (list output))
626 (null? download*)
627 (null? build*))))))
628
629 (test-assert "derivation-prerequisites-to-build and substitutes, non-substitutable build"
630 (let* ((store (open-connection))
631 (drv (build-expression->derivation store "prereq-no-subst"
632 (random 1000)
633 #:substitutable? #f))
634 (output (derivation->output-path drv)))
635
636 ;; Make sure substitutes are usable.
637 (set-build-options store #:use-substitutes? #t
638 #:substitute-urls (%test-substitute-urls))
639
640 (with-derivation-narinfo drv
641 (let-values (((build download)
642 (derivation-prerequisites-to-build store drv)))
643 ;; Despite being available as a substitute, DRV will be built locally
644 ;; due to #:substitutable? #f.
645 (and (null? download)
646 (match build
647 (((? derivation-input? input))
648 (string=? (derivation-input-path input)
649 (derivation-file-name drv)))))))))
650
651 (test-assert "derivation-prerequisites-to-build and substitutes, local build"
652 (with-store store
653 (let* ((drv (build-expression->derivation store "prereq-subst-local"
654 (random 1000)
655 #:local-build? #t))
656 (output (derivation->output-path drv)))
657
658 ;; Make sure substitutes are usable.
659 (set-build-options store #:use-substitutes? #t
660 #:substitute-urls (%test-substitute-urls))
661
662 (with-derivation-narinfo drv
663 (let-values (((build download)
664 (derivation-prerequisites-to-build store drv)))
665 ;; #:local-build? is *not* synonymous with #:substitutable?, so we
666 ;; must be able to substitute DRV's output.
667 ;; See <http://bugs.gnu.org/18747>.
668 (and (null? build)
669 (match download
670 (((? string? item))
671 (string=? item (derivation->output-path drv))))))))))
672
673 (test-assert "derivation-prerequisites-to-build in 'check' mode"
674 (with-store store
675 (let* ((dep (build-expression->derivation store "dep"
676 `(begin ,(random-text)
677 (mkdir %output))))
678 (drv (build-expression->derivation store "to-check"
679 '(mkdir %output)
680 #:inputs `(("dep" ,dep)))))
681 (build-derivations store (list drv))
682 (delete-paths store (list (derivation->output-path dep)))
683
684 ;; In 'check' mode, DEP must be rebuilt.
685 (and (null? (derivation-prerequisites-to-build store drv))
686 (match (derivation-prerequisites-to-build store drv
687 #:mode (build-mode
688 check))
689 ((input)
690 (string=? (derivation-input-path input)
691 (derivation-file-name dep))))))))
692
693 (test-assert "build-expression->derivation with expression returning #f"
694 (let* ((builder '(begin
695 (mkdir %output)
696 #f)) ; fail!
697 (drv (build-expression->derivation %store "fail" builder))
698 (out-path (derivation->output-path drv)))
699 (guard (c ((nix-protocol-error? c)
700 ;; Note that the output path may exist at this point, but it
701 ;; is invalid.
702 (and (string-match "build .* failed"
703 (nix-protocol-error-message c))
704 (not (valid-path? %store out-path)))))
705 (build-derivations %store (list drv))
706 #f)))
707
708 (test-assert "build-expression->derivation with two outputs"
709 (let* ((builder '(begin
710 (call-with-output-file (assoc-ref %outputs "out")
711 (lambda (p)
712 (display '(hello) p)))
713 (call-with-output-file (assoc-ref %outputs "second")
714 (lambda (p)
715 (display '(world) p)))))
716 (drv (build-expression->derivation %store "double" builder
717 #:outputs '("out"
718 "second")))
719 (succeeded? (build-derivations %store (list drv))))
720 (and succeeded?
721 (let ((one (derivation->output-path drv))
722 (two (derivation->output-path drv "second")))
723 (and (equal? '(hello) (call-with-input-file one read))
724 (equal? '(world) (call-with-input-file two read)))))))
725
726 (test-skip (if %coreutils 0 1))
727 (test-assert "build-expression->derivation with one input"
728 (let* ((builder '(call-with-output-file %output
729 (lambda (p)
730 (let ((cu (assoc-ref %build-inputs "cu")))
731 (close 1)
732 (dup2 (port->fdes p) 1)
733 (execl (string-append cu "/bin/uname")
734 "uname" "-a")))))
735 (drv (build-expression->derivation %store "uname" builder
736 #:inputs
737 `(("cu" ,%coreutils))))
738 (succeeded? (build-derivations %store (list drv))))
739 (and succeeded?
740 (let ((p (derivation->output-path drv)))
741 (string-contains (call-with-input-file p read-line) "GNU")))))
742
743 (test-assert "build-expression->derivation with modules"
744 (let* ((builder `(begin
745 (use-modules (guix build utils))
746 (let ((out (assoc-ref %outputs "out")))
747 (mkdir-p (string-append out "/guile/guix/nix"))
748 #t)))
749 (drv (build-expression->derivation %store "test-with-modules"
750 builder
751 #:modules
752 '((guix build utils)))))
753 (and (build-derivations %store (list drv))
754 (let* ((p (derivation->output-path drv))
755 (s (stat (string-append p "/guile/guix/nix"))))
756 (eq? (stat:type s) 'directory)))))
757
758 (test-assert "build-expression->derivation: same fixed-output path"
759 (let* ((builder1 '(call-with-output-file %output
760 (lambda (p)
761 (write "hello" p))))
762 (builder2 '(call-with-output-file (pk 'difference-here! %output)
763 (lambda (p)
764 (write "hello" p))))
765 (hash (sha256 (string->utf8 "hello")))
766 (input1 (build-expression->derivation %store "fixed" builder1
767 #:hash hash
768 #:hash-algo 'sha256))
769 (input2 (build-expression->derivation %store "fixed" builder2
770 #:hash hash
771 #:hash-algo 'sha256))
772 (succeeded? (build-derivations %store (list input1 input2))))
773 (and succeeded?
774 (not (string=? (derivation-file-name input1)
775 (derivation-file-name input2)))
776 (string=? (derivation->output-path input1)
777 (derivation->output-path input2)))))
778
779 (test-assert "build-expression->derivation with a fixed-output input"
780 (let* ((builder1 '(call-with-output-file %output
781 (lambda (p)
782 (write "hello" p))))
783 (builder2 '(call-with-output-file (pk 'difference-here! %output)
784 (lambda (p)
785 (write "hello" p))))
786 (hash (sha256 (string->utf8 "hello")))
787 (input1 (build-expression->derivation %store "fixed" builder1
788 #:hash hash
789 #:hash-algo 'sha256))
790 (input2 (build-expression->derivation %store "fixed" builder2
791 #:hash hash
792 #:hash-algo 'sha256))
793 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
794 (call-with-output-file %output
795 (lambda (out)
796 (format #f "My input is ~a.~%" input)))))
797 (final1 (build-expression->derivation %store "final" builder3
798 #:inputs
799 `(("input" ,input1))))
800 (final2 (build-expression->derivation %store "final" builder3
801 #:inputs
802 `(("input" ,input2)))))
803 (and (string=? (derivation->output-path final1)
804 (derivation->output-path final2))
805 (string=? (derivation->output-path final1)
806 (derivation-path->output-path
807 (derivation-file-name final1)))
808 (build-derivations %store (list final1 final2)))))
809
810 (test-assert "build-expression->derivation produces recursive fixed-output"
811 (let* ((builder '(begin
812 (use-modules (srfi srfi-26))
813 (mkdir %output)
814 (chdir %output)
815 (call-with-output-file "exe"
816 (cut display "executable" <>))
817 (chmod "exe" #o777)
818 (symlink "exe" "symlink")
819 (mkdir "subdir")))
820 (drv (build-expression->derivation %store "fixed-rec" builder
821 #:hash-algo 'sha256
822 #:hash (base32
823 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
824 #:recursive? #t)))
825 (and (build-derivations %store (list drv))
826 (let* ((dir (derivation->output-path drv))
827 (exe (string-append dir "/exe"))
828 (link (string-append dir "/symlink"))
829 (subdir (string-append dir "/subdir")))
830 (and (executable-file? exe)
831 (string=? "executable"
832 (call-with-input-file exe get-string-all))
833 (string=? "exe" (readlink link))
834 (file-is-directory? subdir))))))
835
836 (test-assert "build-expression->derivation uses recursive fixed-output"
837 (let* ((builder '(call-with-output-file %output
838 (lambda (port)
839 (display "hello" port))))
840 (fixed (build-expression->derivation %store "small-fixed-rec"
841 builder
842 #:hash-algo 'sha256
843 #:hash (base32
844 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
845 #:recursive? #t))
846 (in (derivation->output-path fixed))
847 (builder `(begin
848 (mkdir %output)
849 (chdir %output)
850 (symlink ,in "symlink")))
851 (drv (build-expression->derivation %store "fixed-rec-user"
852 builder
853 #:inputs `(("fixed" ,fixed)))))
854 (and (build-derivations %store (list drv))
855 (let ((out (derivation->output-path drv)))
856 (string=? (readlink (string-append out "/symlink")) in)))))
857
858 (test-assert "build-expression->derivation with #:references-graphs"
859 (let* ((input (add-text-to-store %store "foo" "hello"
860 (list %bash %mkdir)))
861 (builder '(copy-file "input" %output))
862 (drv (build-expression->derivation %store "references-graphs"
863 builder
864 #:references-graphs
865 `(("input" . ,input))))
866 (out (derivation->output-path drv)))
867 (define (deps path . deps)
868 (let ((count (length deps)))
869 (string-append path "\n\n" (number->string count) "\n"
870 (string-join (sort deps string<?) "\n")
871 (if (zero? count) "" "\n"))))
872
873 (and (build-derivations %store (list drv))
874 (equal? (call-with-input-file out get-string-all)
875 (string-concatenate
876 (map cdr
877 (sort (map (lambda (p d)
878 (cons p (apply deps p d)))
879 (list input %bash %mkdir)
880 (list (list %bash %mkdir)
881 '() '()))
882 (lambda (x y)
883 (match x
884 ((p1 . _)
885 (match y
886 ((p2 . _)
887 (string<? p1 p2)))))))))))))
888
889
890 (test-assert "graft-derivation"
891 (let* ((build `(begin
892 (mkdir %output)
893 (chdir %output)
894 (symlink %output "self")
895 (call-with-output-file "text"
896 (lambda (output)
897 (format output "foo/~a/bar" ,%mkdir)))
898 (symlink ,%bash "sh")))
899 (orig (build-expression->derivation %store "graft" build
900 #:inputs `(("a" ,%bash)
901 ("b" ,%mkdir))))
902 (one (add-text-to-store %store "bash" "fake bash"))
903 (two (build-expression->derivation %store "mkdir"
904 '(call-with-output-file %output
905 (lambda (port)
906 (display "fake mkdir" port)))))
907 (graft (graft-derivation %store "graft" orig
908 (list (graft
909 (origin %bash)
910 (replacement one))
911 (graft
912 (origin %mkdir)
913 (replacement two))))))
914 (and (build-derivations %store (list graft))
915 (let ((two (derivation->output-path two))
916 (graft (derivation->output-path graft)))
917 (and (string=? (format #f "foo/~a/bar" two)
918 (call-with-input-file (string-append graft "/text")
919 get-string-all))
920 (string=? (readlink (string-append graft "/sh")) one)
921 (string=? (readlink (string-append graft "/self")) graft))))))
922
923 (test-equal "map-derivation"
924 "hello"
925 (let* ((joke (package-derivation %store guile-1.8))
926 (good (package-derivation %store %bootstrap-guile))
927 (drv1 (build-expression->derivation %store "original-drv1"
928 #f ; systematically fail
929 #:guile-for-build joke))
930 (drv2 (build-expression->derivation %store "original-drv2"
931 '(call-with-output-file %output
932 (lambda (p)
933 (display "hello" p)))))
934 (drv3 (build-expression->derivation %store "drv-to-remap"
935 '(let ((in (assoc-ref
936 %build-inputs "in")))
937 (copy-file in %output))
938 #:inputs `(("in" ,drv1))
939 #:guile-for-build joke))
940 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
941 (,joke . ,good))))
942 (out (derivation->output-path drv4)))
943 (and (build-derivations %store (list (pk 'remapped drv4)))
944 (call-with-input-file out get-string-all))))
945
946 (test-equal "map-derivation, sources"
947 "hello"
948 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
949 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
950 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
951 (drv1 (derivation %store "drv-to-remap"
952
953 ;; XXX: This wouldn't work in practice, but if
954 ;; we append "/bin/bash" then we can't replace
955 ;; it with the bootstrap bash, which is a
956 ;; single file.
957 (derivation->output-path bash-full)
958
959 `("-e" ,script1)
960 #:inputs `((,bash-full) (,script1))))
961 (drv2 (map-derivation %store drv1
962 `((,bash-full . ,%bash)
963 (,script1 . ,script2))))
964 (out (derivation->output-path drv2)))
965 (and (build-derivations %store (list (pk 'remapped* drv2)))
966 (call-with-input-file out get-string-all))))
967
968 (test-end)
969
970 \f
971 (exit (= (test-runner-fail-count (test-runner-current)) 0))