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