profiles: Compute transaction effects in a functional way.
[jackhill/guix/guix.git] / tests / derivations.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
36bbbbd1 2;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
341c6fdd 3;;;
233e7676 4;;; This file is part of GNU Guix.
341c6fdd 5;;;
233e7676 6;;; GNU Guix is free software; you can redistribute it and/or modify it
341c6fdd
LC
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;;;
233e7676 11;;; GNU Guix is distributed in the hope that it will be useful, but
341c6fdd
LC
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
233e7676 17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
341c6fdd 18
341c6fdd
LC
19(define-module (test-derivations)
20 #:use-module (guix derivations)
26bbbb95 21 #:use-module (guix store)
de4c3f26 22 #:use-module (guix utils)
72626a71 23 #:use-module (guix hash)
ddc29a78 24 #:use-module (guix base32)
c1bc358f 25 #:use-module (guix tests)
36bbbbd1
LC
26 #:use-module ((guix packages) #:select (package-derivation base32))
27 #:use-module ((guix build utils) #:select (executable-file?))
59a43334 28 #:use-module ((gnu packages) #:select (search-bootstrap-binary))
1ffa7090 29 #:use-module (gnu packages bootstrap)
e387ab7c 30 #:use-module ((gnu packages guile) #:select (guile-1.8))
b37eb5ed 31 #:use-module (srfi srfi-1)
fb3eec83 32 #:use-module (srfi srfi-11)
341c6fdd 33 #:use-module (srfi srfi-26)
99634e3f 34 #:use-module (srfi srfi-34)
341c6fdd 35 #:use-module (srfi srfi-64)
fb3eec83 36 #:use-module (rnrs io ports)
749c6567 37 #:use-module (rnrs bytevectors)
dd36b51b 38 #:use-module (web uri)
b37eb5ed 39 #:use-module (ice-9 rdelim)
db393b33 40 #:use-module (ice-9 regex)
99634e3f
LC
41 #:use-module (ice-9 ftw)
42 #:use-module (ice-9 match))
341c6fdd 43
26bbbb95 44(define %store
c1bc358f 45 (open-connection-for-tests))
b272c474 46
5b0c9d16
LC
47(define (bootstrap-binary name)
48 (let ((bin (search-bootstrap-binary name (%current-system))))
97d3998e 49 (and %store
5b0c9d16
LC
50 (add-to-store %store name #t "sha256" bin))))
51
52(define %bash
53 (bootstrap-binary "bash"))
54(define %mkdir
55 (bootstrap-binary "mkdir"))
97d3998e 56
5b0c9d16 57(define* (directory-contents dir #:optional (slurp get-bytevector-all))
b37eb5ed
LC
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)
5b0c9d16 63 (call-with-input-file path slurp)
b37eb5ed
LC
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
341c6fdd
LC
74(test-begin "derivations")
75
76(test-assert "parse & export"
33594aa4
LC
77 (let* ((f (search-path %load-path "tests/test.drv"))
78 (b1 (call-with-input-file f get-bytevector-all))
341c6fdd
LC
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
5b0c9d16 85(test-skip (if %store 0 12))
b37eb5ed 86
d1b1c424
LC
87(test-assert "add-to-store, flat"
88 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
a9ebd9ef 89 (drv (add-to-store %store "flat-test" #f "sha256" file)))
d1b1c424 90 (and (eq? 'regular (stat:type (stat drv)))
31ef99a8 91 (valid-path? %store drv)
d1b1c424
LC
92 (equal? (call-with-input-file file get-bytevector-all)
93 (call-with-input-file drv get-bytevector-all)))))
94
b37eb5ed
LC
95(test-assert "add-to-store, recursive"
96 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
a9ebd9ef 97 (drv (add-to-store %store "dir-tree-test" #t "sha256" dir)))
b37eb5ed 98 (and (eq? 'directory (stat:type (stat drv)))
31ef99a8 99 (valid-path? %store drv)
b37eb5ed
LC
100 (equal? (directory-contents dir)
101 (directory-contents drv)))))
26bbbb95
LC
102
103(test-assert "derivation with no inputs"
31ef99a8 104 (let* ((builder (add-text-to-store %store "my-builder.sh"
97d3998e 105 "echo hello, world\n"
31ef99a8 106 '()))
59688fc4 107 (drv (derivation %store "foo"
97d3998e 108 %bash `("-e" ,builder)
a987d2c0 109 #:env-vars '(("HOME" . "/homeless")))))
59688fc4
LC
110 (and (store-path? (derivation-file-name drv))
111 (valid-path? %store (derivation-file-name drv)))))
26bbbb95 112
fb3eec83 113(test-assert "build derivation with 1 source"
59688fc4
LC
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!"))
bde2d9cf 122 #:inputs `((,%bash) (,builder))))
59688fc4
LC
123 (succeeded?
124 (build-derivations %store (list drv))))
fb3eec83 125 (and succeeded?
59688fc4 126 (let ((path (derivation->output-path drv)))
31ef99a8
LC
127 (and (valid-path? %store path)
128 (string=? (call-with-input-file path read-line)
129 "hello, world"))))))
fb3eec83 130
860a6f1a
LC
131(test-assert "derivation with local file as input"
132 (let* ((builder (add-text-to-store
133 %store "my-builder.sh"
bbb76f6f 134 "(while read line ; do echo \"$line\" ; done) < $in > $out"
860a6f1a
LC
135 '()))
136 (input (search-path %load-path "ice-9/boot-9.scm"))
a987d2c0
LC
137 (input* (add-to-store %store (basename input)
138 #t "sha256" input))
59688fc4 139 (drv (derivation %store "derivation-with-input-file"
97d3998e 140 %bash `(,builder)
a987d2c0
LC
141
142 ;; Cheat to pass the actual file name to the
143 ;; builder.
144 #:env-vars `(("in" . ,input*))
145
bde2d9cf
LC
146 #:inputs `((,%bash)
147 (,builder)
a987d2c0 148 (,input))))) ; ← local file name
59688fc4 149 (and (build-derivations %store (list drv))
bbb76f6f
LC
150 ;; Note: we can't compare the files because the above trick alters
151 ;; the contents.
59688fc4 152 (valid-path? %store (derivation->output-path drv)))))
860a6f1a 153
99e17dc9
LC
154(test-assert "fixed-output-derivation?"
155 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
156 "echo -n hello > $out" '()))
157 (hash (sha256 (string->utf8 "hello")))
158 (drv (derivation %store "fixed"
159 %bash `(,builder)
160 #:inputs `((,builder))
161 #:hash hash #:hash-algo 'sha256)))
162 (fixed-output-derivation? drv)))
163
749c6567
LC
164(test-assert "fixed-output derivation"
165 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
166 "echo -n hello > $out" '()))
167 (hash (sha256 (string->utf8 "hello")))
59688fc4 168 (drv (derivation %store "fixed"
97d3998e 169 %bash `(,builder)
a987d2c0 170 #:inputs `((,builder)) ; optional
749c6567 171 #:hash hash #:hash-algo 'sha256))
59688fc4 172 (succeeded? (build-derivations %store (list drv))))
749c6567 173 (and succeeded?
59688fc4 174 (let ((p (derivation->output-path drv)))
82058eff
LC
175 (and (equal? (string->utf8 "hello")
176 (call-with-input-file p get-bytevector-all))
177 (bytevector? (query-path-hash %store p)))))))
749c6567 178
813986ac
LC
179(test-assert "fixed-output derivation: output paths are equal"
180 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
181 "echo -n hello > $out" '()))
182 (builder2 (add-text-to-store %store "fixed-builder2.sh"
183 "echo hey; echo -n hello > $out" '()))
184 (hash (sha256 (string->utf8 "hello")))
59688fc4 185 (drv1 (derivation %store "fixed"
97d3998e 186 %bash `(,builder1)
813986ac 187 #:hash hash #:hash-algo 'sha256))
59688fc4 188 (drv2 (derivation %store "fixed"
97d3998e 189 %bash `(,builder2)
813986ac 190 #:hash hash #:hash-algo 'sha256))
59688fc4 191 (succeeded? (build-derivations %store (list drv1 drv2))))
813986ac 192 (and succeeded?
59688fc4
LC
193 (equal? (derivation->output-path drv1)
194 (derivation->output-path drv2)))))
813986ac 195
36bbbbd1
LC
196(test-assert "fixed-output derivation, recursive"
197 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
198 "echo -n hello > $out" '()))
199 (hash (sha256 (string->utf8 "hello")))
200 (drv (derivation %store "fixed-rec"
201 %bash `(,builder)
202 #:inputs `((,builder))
203 #:hash (base32 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
204 #:hash-algo 'sha256
205 #:recursive? #t))
206 (succeeded? (build-derivations %store (list drv))))
207 (and succeeded?
208 (let ((p (derivation->output-path drv)))
209 (and (equal? (string->utf8 "hello")
210 (call-with-input-file p get-bytevector-all))
211 (bytevector? (query-path-hash %store p)))))))
212
813986ac
LC
213(test-assert "derivation with a fixed-output input"
214 ;; A derivation D using a fixed-output derivation F doesn't has the same
215 ;; output path when passed F or F', as long as F and F' have the same output
216 ;; path.
217 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
218 "echo -n hello > $out" '()))
219 (builder2 (add-text-to-store %store "fixed-builder2.sh"
220 "echo hey; echo -n hello > $out" '()))
221 (hash (sha256 (string->utf8 "hello")))
a987d2c0 222 (fixed1 (derivation %store "fixed"
97d3998e 223 %bash `(,builder1)
813986ac 224 #:hash hash #:hash-algo 'sha256))
a987d2c0 225 (fixed2 (derivation %store "fixed"
97d3998e 226 %bash `(,builder2)
813986ac 227 #:hash hash #:hash-algo 'sha256))
59688fc4 228 (fixed-out (derivation->output-path fixed1))
813986ac
LC
229 (builder3 (add-text-to-store
230 %store "final-builder.sh"
231 ;; Use Bash hackery to avoid Coreutils.
232 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
a987d2c0 233 (final1 (derivation %store "final"
97d3998e 234 %bash `(,builder3)
a987d2c0 235 #:env-vars `(("in" . ,fixed-out))
bde2d9cf 236 #:inputs `((,%bash) (,builder3) (,fixed1))))
a987d2c0 237 (final2 (derivation %store "final"
97d3998e 238 %bash `(,builder3)
a987d2c0 239 #:env-vars `(("in" . ,fixed-out))
bde2d9cf 240 #:inputs `((,%bash) (,builder3) (,fixed2))))
813986ac
LC
241 (succeeded? (build-derivations %store
242 (list final1 final2))))
243 (and succeeded?
59688fc4
LC
244 (equal? (derivation->output-path final1)
245 (derivation->output-path final2)))))
813986ac 246
7946c4e7
LC
247(test-assert "multiple-output derivation"
248 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
249 "echo one > $out ; echo two > $second"
250 '()))
59688fc4 251 (drv (derivation %store "fixed"
97d3998e 252 %bash `(,builder)
a987d2c0
LC
253 #:env-vars '(("HOME" . "/homeless")
254 ("zzz" . "Z!")
255 ("AAA" . "A!"))
bde2d9cf 256 #:inputs `((,%bash) (,builder))
7946c4e7 257 #:outputs '("out" "second")))
59688fc4 258 (succeeded? (build-derivations %store (list drv))))
7946c4e7 259 (and succeeded?
59688fc4
LC
260 (let ((one (derivation->output-path drv "out"))
261 (two (derivation->output-path drv "second")))
7244a5f7 262 (and (lset= equal?
59688fc4 263 (derivation->output-paths drv)
7244a5f7
LC
264 `(("out" . ,one) ("second" . ,two)))
265 (eq? 'one (call-with-input-file one read))
7946c4e7
LC
266 (eq? 'two (call-with-input-file two read)))))))
267
4b1786aa
LC
268(test-assert "multiple-output derivation, non-alphabetic order"
269 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
270 ;; path computation must reorder them first.
271 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
272 "echo one > $out ; echo two > $AAA"
273 '()))
59688fc4 274 (drv (derivation %store "fixed"
97d3998e 275 %bash `(,builder)
bde2d9cf 276 #:inputs `((,%bash) (,builder))
4b1786aa 277 #:outputs '("out" "AAA")))
59688fc4 278 (succeeded? (build-derivations %store (list drv))))
4b1786aa 279 (and succeeded?
59688fc4
LC
280 (let ((one (derivation->output-path drv "out"))
281 (two (derivation->output-path drv "AAA")))
4b1786aa
LC
282 (and (eq? 'one (call-with-input-file one read))
283 (eq? 'two (call-with-input-file two read)))))))
284
d0dc4907
LC
285(test-assert "multiple-output derivation, derivation-path->output-path"
286 (let* ((builder (add-text-to-store %store "builder.sh"
287 "echo one > $out ; echo two > $second"
288 '()))
289 (drv (derivation %store "multiple"
290 %bash `(,builder)
291 #:outputs '("out" "second")))
292 (drv-file (derivation-file-name drv))
293 (one (derivation->output-path drv "out"))
294 (two (derivation->output-path drv "second"))
295 (first (derivation-path->output-path drv-file "out"))
296 (second (derivation-path->output-path drv-file "second")))
297 (and (not (string=? one two))
298 (string-suffix? "-second" two)
299 (string=? first one)
300 (string=? second two))))
301
d66ac374
LC
302(test-assert "user of multiple-output derivation"
303 ;; Check whether specifying several inputs coming from the same
304 ;; multiple-output derivation works.
305 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
306 "echo one > $out ; echo two > $two"
307 '()))
a987d2c0 308 (mdrv (derivation %store "multiple-output"
97d3998e 309 %bash `(,builder1)
bde2d9cf 310 #:inputs `((,%bash) (,builder1))
d66ac374
LC
311 #:outputs '("out" "two")))
312 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
313 "read x < $one;
314 read y < $two;
315 echo \"($x $y)\" > $out"
316 '()))
317 (udrv (derivation %store "multiple-output-user"
97d3998e 318 %bash `(,builder2)
a987d2c0 319 #:env-vars `(("one"
59688fc4 320 . ,(derivation->output-path
a987d2c0
LC
321 mdrv "out"))
322 ("two"
59688fc4 323 . ,(derivation->output-path
a987d2c0 324 mdrv "two")))
bde2d9cf
LC
325 #:inputs `((,%bash)
326 (,builder2)
a987d2c0
LC
327 ;; two occurrences of MDRV:
328 (,mdrv)
329 (,mdrv "two")))))
d66ac374 330 (and (build-derivations %store (list (pk 'udrv udrv)))
59688fc4 331 (let ((p (derivation->output-path udrv)))
d66ac374
LC
332 (and (valid-path? %store p)
333 (equal? '(one two) (call-with-input-file p read)))))))
334
858e9282 335(test-assert "derivation with #:references-graphs"
5b0c9d16
LC
336 (let* ((input1 (add-text-to-store %store "foo" "hello"
337 (list %bash)))
338 (input2 (add-text-to-store %store "bar"
339 (number->string (random 7777))
340 (list input1)))
341 (builder (add-text-to-store %store "build-graph"
342 (format #f "
343~a $out
344 (while read l ; do echo $l ; done) < bash > $out/bash
345 (while read l ; do echo $l ; done) < input1 > $out/input1
346 (while read l ; do echo $l ; done) < input2 > $out/input2"
347 %mkdir)
348 (list %mkdir)))
349 (drv (derivation %store "closure-graphs"
350 %bash `(,builder)
858e9282 351 #:references-graphs
5b0c9d16
LC
352 `(("bash" . ,%bash)
353 ("input1" . ,input1)
354 ("input2" . ,input2))
355 #:inputs `((,%bash) (,builder))))
59688fc4 356 (out (derivation->output-path drv)))
5b0c9d16
LC
357 (define (deps path . deps)
358 (let ((count (length deps)))
359 (string-append path "\n\n" (number->string count) "\n"
360 (string-join (sort deps string<?) "\n")
361 (if (zero? count) "" "\n"))))
362
363 (and (build-derivations %store (list drv))
364 (equal? (directory-contents out get-string-all)
365 `(("/bash" . ,(string-append %bash "\n\n0\n"))
366 ("/input1" . ,(if (string>? input1 %bash)
367 (string-append (deps %bash)
368 (deps input1 %bash))
369 (string-append (deps input1 %bash)
370 (deps %bash))))
371 ("/input2" . ,(string-concatenate
372 (map cdr
373 (sort
374 (map (lambda (p d)
375 (cons p (apply deps p d)))
376 (list %bash input1 input2)
377 (list '() (list %bash) (list input1)))
378 (lambda (x y)
379 (match x
380 ((p1 . _)
381 (match y
382 ((p2 . _)
383 (string<? p1 p2)))))))))))))))
384
b53be755
LC
385(test-assert "derivation #:allowed-references, ok"
386 (let ((drv (derivation %store "allowed" %bash
387 '("-c" "echo hello > $out")
388 #:inputs `((,%bash))
389 #:allowed-references '())))
390 (build-derivations %store (list drv))))
391
392(test-assert "derivation #:allowed-references, not allowed"
393 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
394 (drv (derivation %store "disallowed" %bash
395 `("-c" ,(string-append "echo " txt "> $out"))
396 #:inputs `((,%bash) (,txt))
397 #:allowed-references '())))
398 (guard (c ((nix-protocol-error? c)
399 ;; There's no specific error message to check for.
400 #t))
401 (build-derivations %store (list drv))
402 #f)))
403
404(test-assert "derivation #:allowed-references, self allowed"
405 (let ((drv (derivation %store "allowed" %bash
406 '("-c" "echo $out > $out")
407 #:inputs `((,%bash))
408 #:allowed-references '("out"))))
409 (build-derivations %store (list drv))))
410
411(test-assert "derivation #:allowed-references, self not allowed"
412 (let ((drv (derivation %store "disallowed" %bash
413 `("-c" ,"echo $out > $out")
414 #:inputs `((,%bash))
415 #:allowed-references '())))
416 (guard (c ((nix-protocol-error? c)
417 ;; There's no specific error message to check for.
418 #t))
419 (build-derivations %store (list drv))
420 #f)))
421
de4c3f26
LC
422\f
423(define %coreutils
8f3ecbd7 424 (false-if-exception
ad1ebab3
LC
425 (and (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)
426 (or (package-derivation %store %bootstrap-coreutils&co)
427 (nixpkgs-derivation "coreutils")))))
de4c3f26
LC
428
429(test-skip (if %coreutils 0 1))
430
431(test-assert "build derivation with coreutils"
432 (let* ((builder
433 (add-text-to-store %store "build-with-coreutils.sh"
434 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
435 '()))
59688fc4 436 (drv
a987d2c0 437 (derivation %store "foo"
97d3998e 438 %bash `(,builder)
a987d2c0
LC
439 #:env-vars `(("PATH" .
440 ,(string-append
59688fc4 441 (derivation->output-path %coreutils)
a987d2c0
LC
442 "/bin")))
443 #:inputs `((,builder)
444 (,%coreutils))))
de4c3f26 445 (succeeded?
59688fc4 446 (build-derivations %store (list drv))))
de4c3f26 447 (and succeeded?
59688fc4 448 (let ((p (derivation->output-path drv)))
31ef99a8
LC
449 (and (valid-path? %store p)
450 (file-exists? (string-append p "/good")))))))
de4c3f26 451
9c629a27 452(test-skip (if (%guile-for-build) 0 8))
9a20830e
LC
453
454(test-assert "build-expression->derivation and derivation-prerequisites"
dd1a5a15 455 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e
LC
456 (any (match-lambda
457 (($ <derivation-input> path)
59688fc4 458 (string=? path (derivation-file-name (%guile-for-build)))))
9a20830e 459 (derivation-prerequisites drv))))
d9085c23
LC
460
461(test-assert "build-expression->derivation without inputs"
462 (let* ((builder '(begin
463 (mkdir %output)
464 (call-with-output-file (string-append %output "/test")
465 (lambda (p)
466 (display '(hello guix) p)))))
dd1a5a15 467 (drv (build-expression->derivation %store "goo" builder))
59688fc4 468 (succeeded? (build-derivations %store (list drv))))
d9085c23 469 (and succeeded?
59688fc4 470 (let ((p (derivation->output-path drv)))
d9085c23
LC
471 (equal? '(hello guix)
472 (call-with-input-file (string-append p "/test") read))))))
473
969e678e
LC
474(test-assert "build-expression->derivation and max-silent-time"
475 (let* ((store (let ((s (open-connection)))
476 (set-build-options s #:max-silent-time 1)
477 s))
01e82af5 478 (builder '(begin (sleep 100) (mkdir %output) #t))
dd1a5a15 479 (drv (build-expression->derivation store "silent" builder))
59688fc4 480 (out-path (derivation->output-path drv)))
6c20d1d0
LC
481 (guard (c ((nix-protocol-error? c)
482 (and (string-contains (nix-protocol-error-message c)
483 "failed")
484 (not (valid-path? store out-path)))))
485 (build-derivations store (list drv))
486 #f)))
487
488(test-assert "build-expression->derivation and timeout"
489 (let* ((store (let ((s (open-connection)))
490 (set-build-options s #:timeout 1)
491 s))
492 (builder '(begin (sleep 100) (mkdir %output) #t))
493 (drv (build-expression->derivation store "slow" builder))
494 (out-path (derivation->output-path drv)))
969e678e
LC
495 (guard (c ((nix-protocol-error? c)
496 (and (string-contains (nix-protocol-error-message c)
497 "failed")
498 (not (valid-path? store out-path)))))
01e82af5
LC
499 (build-derivations store (list drv))
500 #f)))
969e678e 501
9a20830e 502(test-assert "build-expression->derivation and derivation-prerequisites-to-build"
dd1a5a15 503 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e
LC
504 ;; The only direct dependency is (%guile-for-build) and it's already
505 ;; built.
506 (null? (derivation-prerequisites-to-build %store drv))))
507
784bb1f3 508(test-assert "derivation-prerequisites-to-build when outputs already present"
59688fc4 509 (let* ((builder '(begin (mkdir %output) #t))
dd1a5a15 510 (input-drv (build-expression->derivation %store "input" builder))
59688fc4
LC
511 (input-path (derivation-output-path
512 (assoc-ref (derivation-outputs input-drv)
513 "out")))
dd1a5a15
LC
514 (drv (build-expression->derivation %store "something" builder
515 #:inputs
59688fc4
LC
516 `(("i" ,input-drv))))
517 (output (derivation->output-path drv)))
784bb1f3
LC
518 ;; Make sure these things are not already built.
519 (when (valid-path? %store input-path)
520 (delete-paths %store (list input-path)))
521 (when (valid-path? %store output)
522 (delete-paths %store (list output)))
523
524 (and (equal? (map derivation-input-path
525 (derivation-prerequisites-to-build %store drv))
59688fc4 526 (list (derivation-file-name input-drv)))
784bb1f3
LC
527
528 ;; Build DRV and delete its input.
59688fc4 529 (build-derivations %store (list drv))
784bb1f3
LC
530 (delete-paths %store (list input-path))
531 (not (valid-path? %store input-path))
532
533 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
534 ;; prerequisite to build because DRV itself is already built.
535 (null? (derivation-prerequisites-to-build %store drv)))))
536
dd36b51b
LC
537(test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
538(test-assert "derivation-prerequisites-to-build and substitutes"
59688fc4
LC
539 (let* ((store (open-connection))
540 (drv (build-expression->derivation store "prereq-subst"
dd1a5a15 541 (random 1000)))
59688fc4
LC
542 (output (derivation->output-path drv))
543 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
dd36b51b
LC
544 (compose uri-path string->uri))))
545 ;; Create fake substituter data, to be read by `substitute-binary'.
546 (call-with-output-file (string-append dir "/nix-cache-info")
547 (lambda (p)
548 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
549 (%store-prefix))))
550 (call-with-output-file (string-append dir "/" (store-path-hash-part output)
551 ".narinfo")
552 (lambda (p)
553 (format p "StorePath: ~a
554URL: ~a
555Compression: none
556NarSize: 1234
557References:
558System: ~a
559Deriver: ~a~%"
560 output ; StorePath
561 (string-append dir "/example.nar") ; URL
562 (%current-system) ; System
59688fc4
LC
563 (basename
564 (derivation-file-name drv))))) ; Deriver
dd36b51b 565
1950bf56
LC
566 ;; Make sure substitutes are usable.
567 (set-build-options store #:use-substitutes? #t)
568
dd36b51b
LC
569 (let-values (((build download)
570 (derivation-prerequisites-to-build store drv))
571 ((build* download*)
572 (derivation-prerequisites-to-build store drv
573 #:use-substitutes? #f)))
574 (pk build download build* download*)
575 (and (null? build)
576 (equal? download (list output))
577 (null? download*)
578 (null? build*)))))
579
db393b33
LC
580(test-assert "build-expression->derivation with expression returning #f"
581 (let* ((builder '(begin
582 (mkdir %output)
583 #f)) ; fail!
dd1a5a15 584 (drv (build-expression->derivation %store "fail" builder))
59688fc4 585 (out-path (derivation->output-path drv)))
db393b33
LC
586 (guard (c ((nix-protocol-error? c)
587 ;; Note that the output path may exist at this point, but it
588 ;; is invalid.
31ef99a8
LC
589 (and (string-match "build .* failed"
590 (nix-protocol-error-message c))
591 (not (valid-path? %store out-path)))))
59688fc4 592 (build-derivations %store (list drv))
db393b33
LC
593 #f)))
594
9bc07f4d
LC
595(test-assert "build-expression->derivation with two outputs"
596 (let* ((builder '(begin
597 (call-with-output-file (assoc-ref %outputs "out")
598 (lambda (p)
599 (display '(hello) p)))
600 (call-with-output-file (assoc-ref %outputs "second")
601 (lambda (p)
602 (display '(world) p)))))
dd1a5a15 603 (drv (build-expression->derivation %store "double" builder
9bc07f4d
LC
604 #:outputs '("out"
605 "second")))
59688fc4 606 (succeeded? (build-derivations %store (list drv))))
9bc07f4d 607 (and succeeded?
59688fc4
LC
608 (let ((one (derivation->output-path drv))
609 (two (derivation->output-path drv "second")))
9bc07f4d
LC
610 (and (equal? '(hello) (call-with-input-file one read))
611 (equal? '(world) (call-with-input-file two read)))))))
612
ad1ebab3 613(test-skip (if %coreutils 0 1))
d9085c23
LC
614(test-assert "build-expression->derivation with one input"
615 (let* ((builder '(call-with-output-file %output
616 (lambda (p)
617 (let ((cu (assoc-ref %build-inputs "cu")))
618 (close 1)
619 (dup2 (port->fdes p) 1)
620 (execl (string-append cu "/bin/uname")
621 "uname" "-a")))))
dd1a5a15
LC
622 (drv (build-expression->derivation %store "uname" builder
623 #:inputs
2acb2cb6 624 `(("cu" ,%coreutils))))
59688fc4 625 (succeeded? (build-derivations %store (list drv))))
d9085c23 626 (and succeeded?
59688fc4 627 (let ((p (derivation->output-path drv)))
d9085c23
LC
628 (string-contains (call-with-input-file p read-line) "GNU")))))
629
99634e3f
LC
630(test-assert "imported-files"
631 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
632 ("a/b/c" . ,(search-path %load-path
633 "guix/derivations.scm"))
224f7ad6
LC
634 ("p/q" . ,(search-path %load-path "guix.scm"))
635 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
59688fc4
LC
636 (drv (imported-files %store files)))
637 (and (build-derivations %store (list drv))
638 (let ((dir (derivation->output-path drv)))
99634e3f
LC
639 (every (match-lambda
640 ((path . source)
641 (equal? (call-with-input-file (string-append dir "/" path)
642 get-bytevector-all)
643 (call-with-input-file source
644 get-bytevector-all))))
645 files)))))
646
d9024884
LC
647(test-assert "build-expression->derivation with modules"
648 (let* ((builder `(begin
649 (use-modules (guix build utils))
650 (let ((out (assoc-ref %outputs "out")))
651 (mkdir-p (string-append out "/guile/guix/nix"))
652 #t)))
59688fc4 653 (drv (build-expression->derivation %store "test-with-modules"
dd1a5a15 654 builder
d9024884
LC
655 #:modules
656 '((guix build utils)))))
59688fc4
LC
657 (and (build-derivations %store (list drv))
658 (let* ((p (derivation->output-path drv))
d9024884
LC
659 (s (stat (string-append p "/guile/guix/nix"))))
660 (eq? (stat:type s) 'directory)))))
661
813986ac
LC
662(test-assert "build-expression->derivation: same fixed-output path"
663 (let* ((builder1 '(call-with-output-file %output
664 (lambda (p)
665 (write "hello" p))))
666 (builder2 '(call-with-output-file (pk 'difference-here! %output)
667 (lambda (p)
668 (write "hello" p))))
669 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 670 (input1 (build-expression->derivation %store "fixed" builder1
813986ac
LC
671 #:hash hash
672 #:hash-algo 'sha256))
dd1a5a15 673 (input2 (build-expression->derivation %store "fixed" builder2
813986ac
LC
674 #:hash hash
675 #:hash-algo 'sha256))
676 (succeeded? (build-derivations %store (list input1 input2))))
677 (and succeeded?
59688fc4
LC
678 (not (string=? (derivation-file-name input1)
679 (derivation-file-name input2)))
680 (string=? (derivation->output-path input1)
681 (derivation->output-path input2)))))
813986ac 682
7bdd1f0e
LC
683(test-assert "build-expression->derivation with a fixed-output input"
684 (let* ((builder1 '(call-with-output-file %output
685 (lambda (p)
686 (write "hello" p))))
687 (builder2 '(call-with-output-file (pk 'difference-here! %output)
688 (lambda (p)
689 (write "hello" p))))
690 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 691 (input1 (build-expression->derivation %store "fixed" builder1
7bdd1f0e
LC
692 #:hash hash
693 #:hash-algo 'sha256))
dd1a5a15 694 (input2 (build-expression->derivation %store "fixed" builder2
7bdd1f0e
LC
695 #:hash hash
696 #:hash-algo 'sha256))
697 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
698 (call-with-output-file %output
699 (lambda (out)
700 (format #f "My input is ~a.~%" input)))))
dd1a5a15
LC
701 (final1 (build-expression->derivation %store "final" builder3
702 #:inputs
7bdd1f0e 703 `(("input" ,input1))))
dd1a5a15
LC
704 (final2 (build-expression->derivation %store "final" builder3
705 #:inputs
7bdd1f0e 706 `(("input" ,input2)))))
59688fc4
LC
707 (and (string=? (derivation->output-path final1)
708 (derivation->output-path final2))
709 (string=? (derivation->output-path final1)
710 (derivation-path->output-path
711 (derivation-file-name final1)))
7bdd1f0e
LC
712 (build-derivations %store (list final1 final2)))))
713
36bbbbd1
LC
714(test-assert "build-expression->derivation produces recursive fixed-output"
715 (let* ((builder '(begin
716 (use-modules (srfi srfi-26))
717 (mkdir %output)
718 (chdir %output)
719 (call-with-output-file "exe"
720 (cut display "executable" <>))
721 (chmod "exe" #o777)
722 (symlink "exe" "symlink")
723 (mkdir "subdir")))
724 (drv (build-expression->derivation %store "fixed-rec" builder
725 #:hash-algo 'sha256
726 #:hash (base32
727 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
728 #:recursive? #t)))
729 (and (build-derivations %store (list drv))
730 (let* ((dir (derivation->output-path drv))
731 (exe (string-append dir "/exe"))
732 (link (string-append dir "/symlink"))
733 (subdir (string-append dir "/subdir")))
734 (and (executable-file? exe)
735 (string=? "executable"
736 (call-with-input-file exe get-string-all))
737 (string=? "exe" (readlink link))
738 (file-is-directory? subdir))))))
739
740(test-assert "build-expression->derivation uses recursive fixed-output"
741 (let* ((builder '(call-with-output-file %output
742 (lambda (port)
743 (display "hello" port))))
744 (fixed (build-expression->derivation %store "small-fixed-rec"
745 builder
746 #:hash-algo 'sha256
747 #:hash (base32
748 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
749 #:recursive? #t))
750 (in (derivation->output-path fixed))
751 (builder `(begin
752 (mkdir %output)
753 (chdir %output)
754 (symlink ,in "symlink")))
755 (drv (build-expression->derivation %store "fixed-rec-user"
756 builder
757 #:inputs `(("fixed" ,fixed)))))
758 (and (build-derivations %store (list drv))
759 (let ((out (derivation->output-path drv)))
760 (string=? (readlink (string-append out "/symlink")) in)))))
761
858e9282 762(test-assert "build-expression->derivation with #:references-graphs"
9c629a27
LC
763 (let* ((input (add-text-to-store %store "foo" "hello"
764 (list %bash %mkdir)))
765 (builder '(copy-file "input" %output))
858e9282 766 (drv (build-expression->derivation %store "references-graphs"
dd1a5a15 767 builder
858e9282 768 #:references-graphs
9c629a27 769 `(("input" . ,input))))
59688fc4 770 (out (derivation->output-path drv)))
9c629a27
LC
771 (define (deps path . deps)
772 (let ((count (length deps)))
773 (string-append path "\n\n" (number->string count) "\n"
774 (string-join (sort deps string<?) "\n")
775 (if (zero? count) "" "\n"))))
776
777 (and (build-derivations %store (list drv))
778 (equal? (call-with-input-file out get-string-all)
779 (string-concatenate
780 (map cdr
781 (sort (map (lambda (p d)
782 (cons p (apply deps p d)))
783 (list input %bash %mkdir)
784 (list (list %bash %mkdir)
785 '() '()))
786 (lambda (x y)
787 (match x
788 ((p1 . _)
789 (match y
790 ((p2 . _)
791 (string<? p1 p2)))))))))))))
792
e387ab7c
LC
793
794(test-equal "map-derivation"
795 "hello"
796 (let* ((joke (package-derivation %store guile-1.8))
797 (good (package-derivation %store %bootstrap-guile))
798 (drv1 (build-expression->derivation %store "original-drv1"
e387ab7c 799 #f ; systematically fail
e387ab7c
LC
800 #:guile-for-build joke))
801 (drv2 (build-expression->derivation %store "original-drv2"
e387ab7c
LC
802 '(call-with-output-file %output
803 (lambda (p)
dd1a5a15 804 (display "hello" p)))))
e387ab7c 805 (drv3 (build-expression->derivation %store "drv-to-remap"
e387ab7c
LC
806 '(let ((in (assoc-ref
807 %build-inputs "in")))
808 (copy-file in %output))
dd1a5a15 809 #:inputs `(("in" ,drv1))
e387ab7c
LC
810 #:guile-for-build joke))
811 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
812 (,joke . ,good))))
813 (out (derivation->output-path drv4)))
814 (and (build-derivations %store (list (pk 'remapped drv4)))
815 (call-with-input-file out get-string-all))))
816
a716e36d
LC
817(test-equal "map-derivation, sources"
818 "hello"
819 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
820 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
821 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
822 (drv1 (derivation %store "drv-to-remap"
823
824 ;; XXX: This wouldn't work in practice, but if
825 ;; we append "/bin/bash" then we can't replace
826 ;; it with the bootstrap bash, which is a
827 ;; single file.
828 (derivation->output-path bash-full)
829
830 `("-e" ,script1)
831 #:inputs `((,bash-full) (,script1))))
832 (drv2 (map-derivation %store drv1
833 `((,bash-full . ,%bash)
834 (,script1 . ,script2))))
835 (out (derivation->output-path drv2)))
836 (and (build-derivations %store (list (pk 'remapped* drv2)))
837 (call-with-input-file out get-string-all))))
838
341c6fdd
LC
839(test-end)
840
841\f
842(exit (= (test-runner-fail-count (test-runner-current)) 0))