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