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