Merge branch 'nix'.
[jackhill/guix/guix.git] / tests / derivations.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
0b6af195 2;;; Copyright © 2012, 2013, 2014, 2015 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
44d43c7a
LC
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
e786293e
LC
176(test-equal "derivation-name"
177 "foo-0.0"
178 (let ((drv (derivation %store "foo-0.0" %bash '())))
179 (derivation-name drv)))
180
0b6af195
LC
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
fc93e309
LC
189(test-assert "offloadable-derivation?"
190 (and (offloadable-derivation? (derivation %store "foo" %bash '()))
191 (not (offloadable-derivation?
192 (derivation %store "foo" %bash '()
193 #:local-build? #t)))))
194
99e17dc9
LC
195(test-assert "fixed-output-derivation?"
196 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
197 "echo -n hello > $out" '()))
198 (hash (sha256 (string->utf8 "hello")))
199 (drv (derivation %store "fixed"
200 %bash `(,builder)
201 #:inputs `((,builder))
202 #:hash hash #:hash-algo 'sha256)))
203 (fixed-output-derivation? drv)))
204
749c6567
LC
205(test-assert "fixed-output derivation"
206 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
207 "echo -n hello > $out" '()))
208 (hash (sha256 (string->utf8 "hello")))
59688fc4 209 (drv (derivation %store "fixed"
97d3998e 210 %bash `(,builder)
a987d2c0 211 #:inputs `((,builder)) ; optional
749c6567 212 #:hash hash #:hash-algo 'sha256))
59688fc4 213 (succeeded? (build-derivations %store (list drv))))
749c6567 214 (and succeeded?
59688fc4 215 (let ((p (derivation->output-path drv)))
82058eff
LC
216 (and (equal? (string->utf8 "hello")
217 (call-with-input-file p get-bytevector-all))
218 (bytevector? (query-path-hash %store p)))))))
749c6567 219
813986ac
LC
220(test-assert "fixed-output derivation: output paths are equal"
221 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
222 "echo -n hello > $out" '()))
223 (builder2 (add-text-to-store %store "fixed-builder2.sh"
224 "echo hey; echo -n hello > $out" '()))
225 (hash (sha256 (string->utf8 "hello")))
59688fc4 226 (drv1 (derivation %store "fixed"
97d3998e 227 %bash `(,builder1)
813986ac 228 #:hash hash #:hash-algo 'sha256))
59688fc4 229 (drv2 (derivation %store "fixed"
97d3998e 230 %bash `(,builder2)
813986ac 231 #:hash hash #:hash-algo 'sha256))
59688fc4 232 (succeeded? (build-derivations %store (list drv1 drv2))))
813986ac 233 (and succeeded?
59688fc4
LC
234 (equal? (derivation->output-path drv1)
235 (derivation->output-path drv2)))))
813986ac 236
36bbbbd1
LC
237(test-assert "fixed-output derivation, recursive"
238 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
239 "echo -n hello > $out" '()))
240 (hash (sha256 (string->utf8 "hello")))
241 (drv (derivation %store "fixed-rec"
242 %bash `(,builder)
243 #:inputs `((,builder))
244 #:hash (base32 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
245 #:hash-algo 'sha256
246 #:recursive? #t))
247 (succeeded? (build-derivations %store (list drv))))
248 (and succeeded?
249 (let ((p (derivation->output-path drv)))
250 (and (equal? (string->utf8 "hello")
251 (call-with-input-file p get-bytevector-all))
252 (bytevector? (query-path-hash %store p)))))))
253
813986ac
LC
254(test-assert "derivation with a fixed-output input"
255 ;; A derivation D using a fixed-output derivation F doesn't has the same
256 ;; output path when passed F or F', as long as F and F' have the same output
257 ;; path.
258 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
259 "echo -n hello > $out" '()))
260 (builder2 (add-text-to-store %store "fixed-builder2.sh"
261 "echo hey; echo -n hello > $out" '()))
262 (hash (sha256 (string->utf8 "hello")))
a987d2c0 263 (fixed1 (derivation %store "fixed"
97d3998e 264 %bash `(,builder1)
813986ac 265 #:hash hash #:hash-algo 'sha256))
a987d2c0 266 (fixed2 (derivation %store "fixed"
97d3998e 267 %bash `(,builder2)
813986ac 268 #:hash hash #:hash-algo 'sha256))
59688fc4 269 (fixed-out (derivation->output-path fixed1))
813986ac
LC
270 (builder3 (add-text-to-store
271 %store "final-builder.sh"
272 ;; Use Bash hackery to avoid Coreutils.
273 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
a987d2c0 274 (final1 (derivation %store "final"
97d3998e 275 %bash `(,builder3)
a987d2c0 276 #:env-vars `(("in" . ,fixed-out))
bde2d9cf 277 #:inputs `((,%bash) (,builder3) (,fixed1))))
a987d2c0 278 (final2 (derivation %store "final"
97d3998e 279 %bash `(,builder3)
a987d2c0 280 #:env-vars `(("in" . ,fixed-out))
bde2d9cf 281 #:inputs `((,%bash) (,builder3) (,fixed2))))
813986ac
LC
282 (succeeded? (build-derivations %store
283 (list final1 final2))))
284 (and succeeded?
59688fc4
LC
285 (equal? (derivation->output-path final1)
286 (derivation->output-path final2)))))
813986ac 287
7946c4e7
LC
288(test-assert "multiple-output derivation"
289 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
290 "echo one > $out ; echo two > $second"
291 '()))
59688fc4 292 (drv (derivation %store "fixed"
97d3998e 293 %bash `(,builder)
a987d2c0
LC
294 #:env-vars '(("HOME" . "/homeless")
295 ("zzz" . "Z!")
296 ("AAA" . "A!"))
bde2d9cf 297 #:inputs `((,%bash) (,builder))
7946c4e7 298 #:outputs '("out" "second")))
59688fc4 299 (succeeded? (build-derivations %store (list drv))))
7946c4e7 300 (and succeeded?
59688fc4
LC
301 (let ((one (derivation->output-path drv "out"))
302 (two (derivation->output-path drv "second")))
7244a5f7 303 (and (lset= equal?
59688fc4 304 (derivation->output-paths drv)
7244a5f7
LC
305 `(("out" . ,one) ("second" . ,two)))
306 (eq? 'one (call-with-input-file one read))
7946c4e7
LC
307 (eq? 'two (call-with-input-file two read)))))))
308
4b1786aa
LC
309(test-assert "multiple-output derivation, non-alphabetic order"
310 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
311 ;; path computation must reorder them first.
312 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
313 "echo one > $out ; echo two > $AAA"
314 '()))
59688fc4 315 (drv (derivation %store "fixed"
97d3998e 316 %bash `(,builder)
bde2d9cf 317 #:inputs `((,%bash) (,builder))
4b1786aa 318 #:outputs '("out" "AAA")))
59688fc4 319 (succeeded? (build-derivations %store (list drv))))
4b1786aa 320 (and succeeded?
59688fc4
LC
321 (let ((one (derivation->output-path drv "out"))
322 (two (derivation->output-path drv "AAA")))
4b1786aa
LC
323 (and (eq? 'one (call-with-input-file one read))
324 (eq? 'two (call-with-input-file two read)))))))
325
d0dc4907
LC
326(test-assert "multiple-output derivation, derivation-path->output-path"
327 (let* ((builder (add-text-to-store %store "builder.sh"
328 "echo one > $out ; echo two > $second"
329 '()))
330 (drv (derivation %store "multiple"
331 %bash `(,builder)
332 #:outputs '("out" "second")))
333 (drv-file (derivation-file-name drv))
334 (one (derivation->output-path drv "out"))
335 (two (derivation->output-path drv "second"))
336 (first (derivation-path->output-path drv-file "out"))
337 (second (derivation-path->output-path drv-file "second")))
338 (and (not (string=? one two))
339 (string-suffix? "-second" two)
340 (string=? first one)
341 (string=? second two))))
342
d66ac374
LC
343(test-assert "user of multiple-output derivation"
344 ;; Check whether specifying several inputs coming from the same
345 ;; multiple-output derivation works.
346 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
347 "echo one > $out ; echo two > $two"
348 '()))
a987d2c0 349 (mdrv (derivation %store "multiple-output"
97d3998e 350 %bash `(,builder1)
bde2d9cf 351 #:inputs `((,%bash) (,builder1))
d66ac374
LC
352 #:outputs '("out" "two")))
353 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
354 "read x < $one;
355 read y < $two;
356 echo \"($x $y)\" > $out"
357 '()))
358 (udrv (derivation %store "multiple-output-user"
97d3998e 359 %bash `(,builder2)
a987d2c0 360 #:env-vars `(("one"
59688fc4 361 . ,(derivation->output-path
a987d2c0
LC
362 mdrv "out"))
363 ("two"
59688fc4 364 . ,(derivation->output-path
a987d2c0 365 mdrv "two")))
bde2d9cf
LC
366 #:inputs `((,%bash)
367 (,builder2)
a987d2c0
LC
368 ;; two occurrences of MDRV:
369 (,mdrv)
370 (,mdrv "two")))))
d66ac374 371 (and (build-derivations %store (list (pk 'udrv udrv)))
59688fc4 372 (let ((p (derivation->output-path udrv)))
d66ac374
LC
373 (and (valid-path? %store p)
374 (equal? '(one two) (call-with-input-file p read)))))))
375
858e9282 376(test-assert "derivation with #:references-graphs"
5b0c9d16
LC
377 (let* ((input1 (add-text-to-store %store "foo" "hello"
378 (list %bash)))
379 (input2 (add-text-to-store %store "bar"
380 (number->string (random 7777))
381 (list input1)))
382 (builder (add-text-to-store %store "build-graph"
383 (format #f "
384~a $out
385 (while read l ; do echo $l ; done) < bash > $out/bash
386 (while read l ; do echo $l ; done) < input1 > $out/input1
387 (while read l ; do echo $l ; done) < input2 > $out/input2"
388 %mkdir)
389 (list %mkdir)))
390 (drv (derivation %store "closure-graphs"
391 %bash `(,builder)
858e9282 392 #:references-graphs
5b0c9d16
LC
393 `(("bash" . ,%bash)
394 ("input1" . ,input1)
395 ("input2" . ,input2))
396 #:inputs `((,%bash) (,builder))))
59688fc4 397 (out (derivation->output-path drv)))
5b0c9d16
LC
398 (define (deps path . deps)
399 (let ((count (length deps)))
400 (string-append path "\n\n" (number->string count) "\n"
401 (string-join (sort deps string<?) "\n")
402 (if (zero? count) "" "\n"))))
403
404 (and (build-derivations %store (list drv))
405 (equal? (directory-contents out get-string-all)
406 `(("/bash" . ,(string-append %bash "\n\n0\n"))
407 ("/input1" . ,(if (string>? input1 %bash)
408 (string-append (deps %bash)
409 (deps input1 %bash))
410 (string-append (deps input1 %bash)
411 (deps %bash))))
412 ("/input2" . ,(string-concatenate
413 (map cdr
414 (sort
415 (map (lambda (p d)
416 (cons p (apply deps p d)))
417 (list %bash input1 input2)
418 (list '() (list %bash) (list input1)))
419 (lambda (x y)
420 (match x
421 ((p1 . _)
422 (match y
423 ((p2 . _)
424 (string<? p1 p2)))))))))))))))
425
b53be755
LC
426(test-assert "derivation #:allowed-references, ok"
427 (let ((drv (derivation %store "allowed" %bash
428 '("-c" "echo hello > $out")
429 #:inputs `((,%bash))
430 #:allowed-references '())))
431 (build-derivations %store (list drv))))
432
433(test-assert "derivation #:allowed-references, not allowed"
434 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
435 (drv (derivation %store "disallowed" %bash
436 `("-c" ,(string-append "echo " txt "> $out"))
437 #:inputs `((,%bash) (,txt))
438 #:allowed-references '())))
439 (guard (c ((nix-protocol-error? c)
440 ;; There's no specific error message to check for.
441 #t))
442 (build-derivations %store (list drv))
443 #f)))
444
445(test-assert "derivation #:allowed-references, self allowed"
446 (let ((drv (derivation %store "allowed" %bash
447 '("-c" "echo $out > $out")
448 #:inputs `((,%bash))
449 #:allowed-references '("out"))))
450 (build-derivations %store (list drv))))
451
452(test-assert "derivation #:allowed-references, self not allowed"
453 (let ((drv (derivation %store "disallowed" %bash
454 `("-c" ,"echo $out > $out")
455 #:inputs `((,%bash))
456 #:allowed-references '())))
457 (guard (c ((nix-protocol-error? c)
458 ;; There's no specific error message to check for.
459 #t))
460 (build-derivations %store (list drv))
461 #f)))
462
de4c3f26
LC
463\f
464(define %coreutils
8f3ecbd7 465 (false-if-exception
12d720fd 466 (and (network-reachable?)
ad1ebab3
LC
467 (or (package-derivation %store %bootstrap-coreutils&co)
468 (nixpkgs-derivation "coreutils")))))
de4c3f26
LC
469
470(test-skip (if %coreutils 0 1))
471
472(test-assert "build derivation with coreutils"
473 (let* ((builder
474 (add-text-to-store %store "build-with-coreutils.sh"
475 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
476 '()))
59688fc4 477 (drv
a987d2c0 478 (derivation %store "foo"
97d3998e 479 %bash `(,builder)
a987d2c0
LC
480 #:env-vars `(("PATH" .
481 ,(string-append
59688fc4 482 (derivation->output-path %coreutils)
a987d2c0
LC
483 "/bin")))
484 #:inputs `((,builder)
485 (,%coreutils))))
de4c3f26 486 (succeeded?
59688fc4 487 (build-derivations %store (list drv))))
de4c3f26 488 (and succeeded?
59688fc4 489 (let ((p (derivation->output-path drv)))
31ef99a8
LC
490 (and (valid-path? %store p)
491 (file-exists? (string-append p "/good")))))))
de4c3f26 492
9c629a27 493(test-skip (if (%guile-for-build) 0 8))
9a20830e
LC
494
495(test-assert "build-expression->derivation and derivation-prerequisites"
dd1a5a15 496 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e
LC
497 (any (match-lambda
498 (($ <derivation-input> path)
59688fc4 499 (string=? path (derivation-file-name (%guile-for-build)))))
9a20830e 500 (derivation-prerequisites drv))))
d9085c23 501
49c0a8d6 502(test-assert "derivation-prerequisites and valid-derivation-input?"
3681db5d
LC
503 (let* ((a (build-expression->derivation %store "a" '(mkdir %output)))
504 (b (build-expression->derivation %store "b" `(list ,(random-text))))
505 (c (build-expression->derivation %store "c" `(mkdir %output)
506 #:inputs `(("a" ,a) ("b" ,b)))))
49c0a8d6
LC
507 ;; Make sure both A and %BOOTSTRAP-GUILE are built (the latter could have
508 ;; be removed by tests/guix-gc.sh.)
509 (build-derivations %store
510 (list a (package-derivation %store %bootstrap-guile)))
511
3681db5d
LC
512 (match (derivation-prerequisites c
513 (cut valid-derivation-input? %store
514 <>))
515 ((($ <derivation-input> file ("out")))
516 (string=? file (derivation-file-name b)))
517 (x
518 (pk 'fail x #f)))))
519
d9085c23
LC
520(test-assert "build-expression->derivation without inputs"
521 (let* ((builder '(begin
522 (mkdir %output)
523 (call-with-output-file (string-append %output "/test")
524 (lambda (p)
525 (display '(hello guix) p)))))
dd1a5a15 526 (drv (build-expression->derivation %store "goo" builder))
59688fc4 527 (succeeded? (build-derivations %store (list drv))))
d9085c23 528 (and succeeded?
59688fc4 529 (let ((p (derivation->output-path drv)))
d9085c23
LC
530 (equal? '(hello guix)
531 (call-with-input-file (string-append p "/test") read))))))
532
969e678e
LC
533(test-assert "build-expression->derivation and max-silent-time"
534 (let* ((store (let ((s (open-connection)))
535 (set-build-options s #:max-silent-time 1)
536 s))
01e82af5 537 (builder '(begin (sleep 100) (mkdir %output) #t))
dd1a5a15 538 (drv (build-expression->derivation store "silent" builder))
59688fc4 539 (out-path (derivation->output-path drv)))
6c20d1d0
LC
540 (guard (c ((nix-protocol-error? c)
541 (and (string-contains (nix-protocol-error-message c)
542 "failed")
543 (not (valid-path? store out-path)))))
544 (build-derivations store (list drv))
545 #f)))
546
547(test-assert "build-expression->derivation and timeout"
548 (let* ((store (let ((s (open-connection)))
549 (set-build-options s #:timeout 1)
550 s))
551 (builder '(begin (sleep 100) (mkdir %output) #t))
552 (drv (build-expression->derivation store "slow" builder))
553 (out-path (derivation->output-path drv)))
969e678e
LC
554 (guard (c ((nix-protocol-error? c)
555 (and (string-contains (nix-protocol-error-message c)
556 "failed")
557 (not (valid-path? store out-path)))))
01e82af5
LC
558 (build-derivations store (list drv))
559 #f)))
969e678e 560
9a20830e 561(test-assert "build-expression->derivation and derivation-prerequisites-to-build"
dd1a5a15 562 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e
LC
563 ;; The only direct dependency is (%guile-for-build) and it's already
564 ;; built.
565 (null? (derivation-prerequisites-to-build %store drv))))
566
784bb1f3 567(test-assert "derivation-prerequisites-to-build when outputs already present"
59688fc4 568 (let* ((builder '(begin (mkdir %output) #t))
dd1a5a15 569 (input-drv (build-expression->derivation %store "input" builder))
59688fc4
LC
570 (input-path (derivation-output-path
571 (assoc-ref (derivation-outputs input-drv)
572 "out")))
dd1a5a15
LC
573 (drv (build-expression->derivation %store "something" builder
574 #:inputs
59688fc4
LC
575 `(("i" ,input-drv))))
576 (output (derivation->output-path drv)))
784bb1f3
LC
577 ;; Make sure these things are not already built.
578 (when (valid-path? %store input-path)
579 (delete-paths %store (list input-path)))
580 (when (valid-path? %store output)
581 (delete-paths %store (list output)))
582
583 (and (equal? (map derivation-input-path
584 (derivation-prerequisites-to-build %store drv))
59688fc4 585 (list (derivation-file-name input-drv)))
784bb1f3
LC
586
587 ;; Build DRV and delete its input.
59688fc4 588 (build-derivations %store (list drv))
784bb1f3
LC
589 (delete-paths %store (list input-path))
590 (not (valid-path? %store input-path))
591
592 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
593 ;; prerequisite to build because DRV itself is already built.
594 (null? (derivation-prerequisites-to-build %store drv)))))
595
dd36b51b 596(test-assert "derivation-prerequisites-to-build and substitutes"
59688fc4
LC
597 (let* ((store (open-connection))
598 (drv (build-expression->derivation store "prereq-subst"
dd1a5a15 599 (random 1000)))
e6740741 600 (output (derivation->output-path drv)))
dd36b51b 601
1950bf56
LC
602 ;; Make sure substitutes are usable.
603 (set-build-options store #:use-substitutes? #t)
604
e6740741
LC
605 (with-derivation-narinfo drv
606 (let-values (((build download)
607 (derivation-prerequisites-to-build store drv))
608 ((build* download*)
609 (derivation-prerequisites-to-build store drv
e9651e39
LC
610 #:substitutable?
611 (const #f))))
e6740741
LC
612 (and (null? build)
613 (equal? download (list output))
614 (null? download*)
615 (null? build*))))))
dd36b51b 616
d2d0514b
LC
617(test-assert "derivation-prerequisites-to-build and substitutes, local build"
618 (let* ((store (open-connection))
619 (drv (build-expression->derivation store "prereq-subst-local"
620 (random 1000)
621 ;; XXX: Adjust once
622 ;; <http://bugs.gnu.org/18747>
623 ;; is fixed.
624 #:local-build? #t))
625 (output (derivation->output-path drv)))
626
627 ;; Make sure substitutes are usable.
628 (set-build-options store #:use-substitutes? #t)
629
630 (with-derivation-narinfo drv
631 (let-values (((build download)
632 (derivation-prerequisites-to-build store drv)))
633 ;; Despite being available as a substitute, DRV will be built locally
634 ;; due to #:local-build?.
635 (and (null? download)
636 (match build
637 (((? derivation-input? input))
638 (string=? (derivation-input-path input)
639 (derivation-file-name drv)))))))))
640
db393b33
LC
641(test-assert "build-expression->derivation with expression returning #f"
642 (let* ((builder '(begin
643 (mkdir %output)
644 #f)) ; fail!
dd1a5a15 645 (drv (build-expression->derivation %store "fail" builder))
59688fc4 646 (out-path (derivation->output-path drv)))
db393b33
LC
647 (guard (c ((nix-protocol-error? c)
648 ;; Note that the output path may exist at this point, but it
649 ;; is invalid.
31ef99a8
LC
650 (and (string-match "build .* failed"
651 (nix-protocol-error-message c))
652 (not (valid-path? %store out-path)))))
59688fc4 653 (build-derivations %store (list drv))
db393b33
LC
654 #f)))
655
9bc07f4d
LC
656(test-assert "build-expression->derivation with two outputs"
657 (let* ((builder '(begin
658 (call-with-output-file (assoc-ref %outputs "out")
659 (lambda (p)
660 (display '(hello) p)))
661 (call-with-output-file (assoc-ref %outputs "second")
662 (lambda (p)
663 (display '(world) p)))))
dd1a5a15 664 (drv (build-expression->derivation %store "double" builder
9bc07f4d
LC
665 #:outputs '("out"
666 "second")))
59688fc4 667 (succeeded? (build-derivations %store (list drv))))
9bc07f4d 668 (and succeeded?
59688fc4
LC
669 (let ((one (derivation->output-path drv))
670 (two (derivation->output-path drv "second")))
9bc07f4d
LC
671 (and (equal? '(hello) (call-with-input-file one read))
672 (equal? '(world) (call-with-input-file two read)))))))
673
ad1ebab3 674(test-skip (if %coreutils 0 1))
d9085c23
LC
675(test-assert "build-expression->derivation with one input"
676 (let* ((builder '(call-with-output-file %output
677 (lambda (p)
678 (let ((cu (assoc-ref %build-inputs "cu")))
679 (close 1)
680 (dup2 (port->fdes p) 1)
681 (execl (string-append cu "/bin/uname")
682 "uname" "-a")))))
dd1a5a15
LC
683 (drv (build-expression->derivation %store "uname" builder
684 #:inputs
2acb2cb6 685 `(("cu" ,%coreutils))))
59688fc4 686 (succeeded? (build-derivations %store (list drv))))
d9085c23 687 (and succeeded?
59688fc4 688 (let ((p (derivation->output-path drv)))
d9085c23
LC
689 (string-contains (call-with-input-file p read-line) "GNU")))))
690
d9024884
LC
691(test-assert "build-expression->derivation with modules"
692 (let* ((builder `(begin
693 (use-modules (guix build utils))
694 (let ((out (assoc-ref %outputs "out")))
695 (mkdir-p (string-append out "/guile/guix/nix"))
696 #t)))
59688fc4 697 (drv (build-expression->derivation %store "test-with-modules"
dd1a5a15 698 builder
d9024884
LC
699 #:modules
700 '((guix build utils)))))
59688fc4
LC
701 (and (build-derivations %store (list drv))
702 (let* ((p (derivation->output-path drv))
d9024884
LC
703 (s (stat (string-append p "/guile/guix/nix"))))
704 (eq? (stat:type s) 'directory)))))
705
813986ac
LC
706(test-assert "build-expression->derivation: same fixed-output path"
707 (let* ((builder1 '(call-with-output-file %output
708 (lambda (p)
709 (write "hello" p))))
710 (builder2 '(call-with-output-file (pk 'difference-here! %output)
711 (lambda (p)
712 (write "hello" p))))
713 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 714 (input1 (build-expression->derivation %store "fixed" builder1
813986ac
LC
715 #:hash hash
716 #:hash-algo 'sha256))
dd1a5a15 717 (input2 (build-expression->derivation %store "fixed" builder2
813986ac
LC
718 #:hash hash
719 #:hash-algo 'sha256))
720 (succeeded? (build-derivations %store (list input1 input2))))
721 (and succeeded?
59688fc4
LC
722 (not (string=? (derivation-file-name input1)
723 (derivation-file-name input2)))
724 (string=? (derivation->output-path input1)
725 (derivation->output-path input2)))))
813986ac 726
7bdd1f0e
LC
727(test-assert "build-expression->derivation with a fixed-output input"
728 (let* ((builder1 '(call-with-output-file %output
729 (lambda (p)
730 (write "hello" p))))
731 (builder2 '(call-with-output-file (pk 'difference-here! %output)
732 (lambda (p)
733 (write "hello" p))))
734 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 735 (input1 (build-expression->derivation %store "fixed" builder1
7bdd1f0e
LC
736 #:hash hash
737 #:hash-algo 'sha256))
dd1a5a15 738 (input2 (build-expression->derivation %store "fixed" builder2
7bdd1f0e
LC
739 #:hash hash
740 #:hash-algo 'sha256))
741 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
742 (call-with-output-file %output
743 (lambda (out)
744 (format #f "My input is ~a.~%" input)))))
dd1a5a15
LC
745 (final1 (build-expression->derivation %store "final" builder3
746 #:inputs
7bdd1f0e 747 `(("input" ,input1))))
dd1a5a15
LC
748 (final2 (build-expression->derivation %store "final" builder3
749 #:inputs
7bdd1f0e 750 `(("input" ,input2)))))
59688fc4
LC
751 (and (string=? (derivation->output-path final1)
752 (derivation->output-path final2))
753 (string=? (derivation->output-path final1)
754 (derivation-path->output-path
755 (derivation-file-name final1)))
7bdd1f0e
LC
756 (build-derivations %store (list final1 final2)))))
757
36bbbbd1
LC
758(test-assert "build-expression->derivation produces recursive fixed-output"
759 (let* ((builder '(begin
760 (use-modules (srfi srfi-26))
761 (mkdir %output)
762 (chdir %output)
763 (call-with-output-file "exe"
764 (cut display "executable" <>))
765 (chmod "exe" #o777)
766 (symlink "exe" "symlink")
767 (mkdir "subdir")))
768 (drv (build-expression->derivation %store "fixed-rec" builder
769 #:hash-algo 'sha256
770 #:hash (base32
771 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
772 #:recursive? #t)))
773 (and (build-derivations %store (list drv))
774 (let* ((dir (derivation->output-path drv))
775 (exe (string-append dir "/exe"))
776 (link (string-append dir "/symlink"))
777 (subdir (string-append dir "/subdir")))
778 (and (executable-file? exe)
779 (string=? "executable"
780 (call-with-input-file exe get-string-all))
781 (string=? "exe" (readlink link))
782 (file-is-directory? subdir))))))
783
784(test-assert "build-expression->derivation uses recursive fixed-output"
785 (let* ((builder '(call-with-output-file %output
786 (lambda (port)
787 (display "hello" port))))
788 (fixed (build-expression->derivation %store "small-fixed-rec"
789 builder
790 #:hash-algo 'sha256
791 #:hash (base32
792 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
793 #:recursive? #t))
794 (in (derivation->output-path fixed))
795 (builder `(begin
796 (mkdir %output)
797 (chdir %output)
798 (symlink ,in "symlink")))
799 (drv (build-expression->derivation %store "fixed-rec-user"
800 builder
801 #:inputs `(("fixed" ,fixed)))))
802 (and (build-derivations %store (list drv))
803 (let ((out (derivation->output-path drv)))
804 (string=? (readlink (string-append out "/symlink")) in)))))
805
858e9282 806(test-assert "build-expression->derivation with #:references-graphs"
9c629a27
LC
807 (let* ((input (add-text-to-store %store "foo" "hello"
808 (list %bash %mkdir)))
809 (builder '(copy-file "input" %output))
858e9282 810 (drv (build-expression->derivation %store "references-graphs"
dd1a5a15 811 builder
858e9282 812 #:references-graphs
9c629a27 813 `(("input" . ,input))))
59688fc4 814 (out (derivation->output-path drv)))
9c629a27
LC
815 (define (deps path . deps)
816 (let ((count (length deps)))
817 (string-append path "\n\n" (number->string count) "\n"
818 (string-join (sort deps string<?) "\n")
819 (if (zero? count) "" "\n"))))
820
821 (and (build-derivations %store (list drv))
822 (equal? (call-with-input-file out get-string-all)
823 (string-concatenate
824 (map cdr
825 (sort (map (lambda (p d)
826 (cons p (apply deps p d)))
827 (list input %bash %mkdir)
828 (list (list %bash %mkdir)
829 '() '()))
830 (lambda (x y)
831 (match x
832 ((p1 . _)
833 (match y
834 ((p2 . _)
835 (string<? p1 p2)))))))))))))
836
e387ab7c 837
fb59e275
LC
838(test-assert "graft-derivation"
839 (let* ((build `(begin
840 (mkdir %output)
841 (chdir %output)
842 (symlink %output "self")
843 (call-with-output-file "text"
844 (lambda (output)
845 (format output "foo/~a/bar" ,%mkdir)))
846 (symlink ,%bash "sh")))
847 (orig (build-expression->derivation %store "graft" build
848 #:inputs `(("a" ,%bash)
849 ("b" ,%mkdir))))
850 (one (add-text-to-store %store "bash" "fake bash"))
851 (two (build-expression->derivation %store "mkdir"
852 '(call-with-output-file %output
853 (lambda (port)
854 (display "fake mkdir" port)))))
855 (graft (graft-derivation %store "graft" orig
969df974
LC
856 (list (graft
857 (origin %bash)
858 (replacement one))
859 (graft
860 (origin %mkdir)
861 (replacement two))))))
fb59e275
LC
862 (and (build-derivations %store (list graft))
863 (let ((two (derivation->output-path two))
864 (graft (derivation->output-path graft)))
865 (and (string=? (format #f "foo/~a/bar" two)
866 (call-with-input-file (string-append graft "/text")
867 get-string-all))
868 (string=? (readlink (string-append graft "/sh")) one)
869 (string=? (readlink (string-append graft "/self")) graft))))))
870
e387ab7c
LC
871(test-equal "map-derivation"
872 "hello"
873 (let* ((joke (package-derivation %store guile-1.8))
874 (good (package-derivation %store %bootstrap-guile))
875 (drv1 (build-expression->derivation %store "original-drv1"
e387ab7c 876 #f ; systematically fail
e387ab7c
LC
877 #:guile-for-build joke))
878 (drv2 (build-expression->derivation %store "original-drv2"
e387ab7c
LC
879 '(call-with-output-file %output
880 (lambda (p)
dd1a5a15 881 (display "hello" p)))))
e387ab7c 882 (drv3 (build-expression->derivation %store "drv-to-remap"
e387ab7c
LC
883 '(let ((in (assoc-ref
884 %build-inputs "in")))
885 (copy-file in %output))
dd1a5a15 886 #:inputs `(("in" ,drv1))
e387ab7c
LC
887 #:guile-for-build joke))
888 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
889 (,joke . ,good))))
890 (out (derivation->output-path drv4)))
891 (and (build-derivations %store (list (pk 'remapped drv4)))
892 (call-with-input-file out get-string-all))))
893
a716e36d
LC
894(test-equal "map-derivation, sources"
895 "hello"
896 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
897 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
898 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
899 (drv1 (derivation %store "drv-to-remap"
900
901 ;; XXX: This wouldn't work in practice, but if
902 ;; we append "/bin/bash" then we can't replace
903 ;; it with the bootstrap bash, which is a
904 ;; single file.
905 (derivation->output-path bash-full)
906
907 `("-e" ,script1)
908 #:inputs `((,bash-full) (,script1))))
909 (drv2 (map-derivation %store drv1
910 `((,bash-full . ,%bash)
911 (,script1 . ,script2))))
912 (out (derivation->output-path drv2)))
913 (and (build-derivations %store (list (pk 'remapped* drv2)))
914 (call-with-input-file out get-string-all))))
915
341c6fdd
LC
916(test-end)
917
918\f
919(exit (= (test-runner-fail-count (test-runner-current)) 0))