authenticate: Support reading the hash or key from stdin.
[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
LC
526
527 (let-values (((build download)
528 (derivation-prerequisites-to-build store drv))
529 ((build* download*)
530 (derivation-prerequisites-to-build store drv
531 #:use-substitutes? #f)))
532 (pk build download build* download*)
533 (and (null? build)
534 (equal? download (list output))
535 (null? download*)
536 (null? build*)))))
537
db393b33
LC
538(test-assert "build-expression->derivation with expression returning #f"
539 (let* ((builder '(begin
540 (mkdir %output)
541 #f)) ; fail!
dd1a5a15 542 (drv (build-expression->derivation %store "fail" builder))
59688fc4 543 (out-path (derivation->output-path drv)))
db393b33
LC
544 (guard (c ((nix-protocol-error? c)
545 ;; Note that the output path may exist at this point, but it
546 ;; is invalid.
31ef99a8
LC
547 (and (string-match "build .* failed"
548 (nix-protocol-error-message c))
549 (not (valid-path? %store out-path)))))
59688fc4 550 (build-derivations %store (list drv))
db393b33
LC
551 #f)))
552
9bc07f4d
LC
553(test-assert "build-expression->derivation with two outputs"
554 (let* ((builder '(begin
555 (call-with-output-file (assoc-ref %outputs "out")
556 (lambda (p)
557 (display '(hello) p)))
558 (call-with-output-file (assoc-ref %outputs "second")
559 (lambda (p)
560 (display '(world) p)))))
dd1a5a15 561 (drv (build-expression->derivation %store "double" builder
9bc07f4d
LC
562 #:outputs '("out"
563 "second")))
59688fc4 564 (succeeded? (build-derivations %store (list drv))))
9bc07f4d 565 (and succeeded?
59688fc4
LC
566 (let ((one (derivation->output-path drv))
567 (two (derivation->output-path drv "second")))
9bc07f4d
LC
568 (and (equal? '(hello) (call-with-input-file one read))
569 (equal? '(world) (call-with-input-file two read)))))))
570
ad1ebab3 571(test-skip (if %coreutils 0 1))
d9085c23
LC
572(test-assert "build-expression->derivation with one input"
573 (let* ((builder '(call-with-output-file %output
574 (lambda (p)
575 (let ((cu (assoc-ref %build-inputs "cu")))
576 (close 1)
577 (dup2 (port->fdes p) 1)
578 (execl (string-append cu "/bin/uname")
579 "uname" "-a")))))
dd1a5a15
LC
580 (drv (build-expression->derivation %store "uname" builder
581 #:inputs
2acb2cb6 582 `(("cu" ,%coreutils))))
59688fc4 583 (succeeded? (build-derivations %store (list drv))))
d9085c23 584 (and succeeded?
59688fc4 585 (let ((p (derivation->output-path drv)))
d9085c23
LC
586 (string-contains (call-with-input-file p read-line) "GNU")))))
587
99634e3f
LC
588(test-assert "imported-files"
589 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
590 ("a/b/c" . ,(search-path %load-path
591 "guix/derivations.scm"))
224f7ad6
LC
592 ("p/q" . ,(search-path %load-path "guix.scm"))
593 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
59688fc4
LC
594 (drv (imported-files %store files)))
595 (and (build-derivations %store (list drv))
596 (let ((dir (derivation->output-path drv)))
99634e3f
LC
597 (every (match-lambda
598 ((path . source)
599 (equal? (call-with-input-file (string-append dir "/" path)
600 get-bytevector-all)
601 (call-with-input-file source
602 get-bytevector-all))))
603 files)))))
604
d9024884
LC
605(test-assert "build-expression->derivation with modules"
606 (let* ((builder `(begin
607 (use-modules (guix build utils))
608 (let ((out (assoc-ref %outputs "out")))
609 (mkdir-p (string-append out "/guile/guix/nix"))
610 #t)))
59688fc4 611 (drv (build-expression->derivation %store "test-with-modules"
dd1a5a15 612 builder
d9024884
LC
613 #:modules
614 '((guix build utils)))))
59688fc4
LC
615 (and (build-derivations %store (list drv))
616 (let* ((p (derivation->output-path drv))
d9024884
LC
617 (s (stat (string-append p "/guile/guix/nix"))))
618 (eq? (stat:type s) 'directory)))))
619
813986ac
LC
620(test-assert "build-expression->derivation: same fixed-output path"
621 (let* ((builder1 '(call-with-output-file %output
622 (lambda (p)
623 (write "hello" p))))
624 (builder2 '(call-with-output-file (pk 'difference-here! %output)
625 (lambda (p)
626 (write "hello" p))))
627 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 628 (input1 (build-expression->derivation %store "fixed" builder1
813986ac
LC
629 #:hash hash
630 #:hash-algo 'sha256))
dd1a5a15 631 (input2 (build-expression->derivation %store "fixed" builder2
813986ac
LC
632 #:hash hash
633 #:hash-algo 'sha256))
634 (succeeded? (build-derivations %store (list input1 input2))))
635 (and succeeded?
59688fc4
LC
636 (not (string=? (derivation-file-name input1)
637 (derivation-file-name input2)))
638 (string=? (derivation->output-path input1)
639 (derivation->output-path input2)))))
813986ac 640
7bdd1f0e
LC
641(test-assert "build-expression->derivation with a fixed-output input"
642 (let* ((builder1 '(call-with-output-file %output
643 (lambda (p)
644 (write "hello" p))))
645 (builder2 '(call-with-output-file (pk 'difference-here! %output)
646 (lambda (p)
647 (write "hello" p))))
648 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 649 (input1 (build-expression->derivation %store "fixed" builder1
7bdd1f0e
LC
650 #:hash hash
651 #:hash-algo 'sha256))
dd1a5a15 652 (input2 (build-expression->derivation %store "fixed" builder2
7bdd1f0e
LC
653 #:hash hash
654 #:hash-algo 'sha256))
655 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
656 (call-with-output-file %output
657 (lambda (out)
658 (format #f "My input is ~a.~%" input)))))
dd1a5a15
LC
659 (final1 (build-expression->derivation %store "final" builder3
660 #:inputs
7bdd1f0e 661 `(("input" ,input1))))
dd1a5a15
LC
662 (final2 (build-expression->derivation %store "final" builder3
663 #:inputs
7bdd1f0e 664 `(("input" ,input2)))))
59688fc4
LC
665 (and (string=? (derivation->output-path final1)
666 (derivation->output-path final2))
667 (string=? (derivation->output-path final1)
668 (derivation-path->output-path
669 (derivation-file-name final1)))
7bdd1f0e
LC
670 (build-derivations %store (list final1 final2)))))
671
36bbbbd1
LC
672(test-assert "build-expression->derivation produces recursive fixed-output"
673 (let* ((builder '(begin
674 (use-modules (srfi srfi-26))
675 (mkdir %output)
676 (chdir %output)
677 (call-with-output-file "exe"
678 (cut display "executable" <>))
679 (chmod "exe" #o777)
680 (symlink "exe" "symlink")
681 (mkdir "subdir")))
682 (drv (build-expression->derivation %store "fixed-rec" builder
683 #:hash-algo 'sha256
684 #:hash (base32
685 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
686 #:recursive? #t)))
687 (and (build-derivations %store (list drv))
688 (let* ((dir (derivation->output-path drv))
689 (exe (string-append dir "/exe"))
690 (link (string-append dir "/symlink"))
691 (subdir (string-append dir "/subdir")))
692 (and (executable-file? exe)
693 (string=? "executable"
694 (call-with-input-file exe get-string-all))
695 (string=? "exe" (readlink link))
696 (file-is-directory? subdir))))))
697
698(test-assert "build-expression->derivation uses recursive fixed-output"
699 (let* ((builder '(call-with-output-file %output
700 (lambda (port)
701 (display "hello" port))))
702 (fixed (build-expression->derivation %store "small-fixed-rec"
703 builder
704 #:hash-algo 'sha256
705 #:hash (base32
706 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
707 #:recursive? #t))
708 (in (derivation->output-path fixed))
709 (builder `(begin
710 (mkdir %output)
711 (chdir %output)
712 (symlink ,in "symlink")))
713 (drv (build-expression->derivation %store "fixed-rec-user"
714 builder
715 #:inputs `(("fixed" ,fixed)))))
716 (and (build-derivations %store (list drv))
717 (let ((out (derivation->output-path drv)))
718 (string=? (readlink (string-append out "/symlink")) in)))))
719
858e9282 720(test-assert "build-expression->derivation with #:references-graphs"
9c629a27
LC
721 (let* ((input (add-text-to-store %store "foo" "hello"
722 (list %bash %mkdir)))
723 (builder '(copy-file "input" %output))
858e9282 724 (drv (build-expression->derivation %store "references-graphs"
dd1a5a15 725 builder
858e9282 726 #:references-graphs
9c629a27 727 `(("input" . ,input))))
59688fc4 728 (out (derivation->output-path drv)))
9c629a27
LC
729 (define (deps path . deps)
730 (let ((count (length deps)))
731 (string-append path "\n\n" (number->string count) "\n"
732 (string-join (sort deps string<?) "\n")
733 (if (zero? count) "" "\n"))))
734
735 (and (build-derivations %store (list drv))
736 (equal? (call-with-input-file out get-string-all)
737 (string-concatenate
738 (map cdr
739 (sort (map (lambda (p d)
740 (cons p (apply deps p d)))
741 (list input %bash %mkdir)
742 (list (list %bash %mkdir)
743 '() '()))
744 (lambda (x y)
745 (match x
746 ((p1 . _)
747 (match y
748 ((p2 . _)
749 (string<? p1 p2)))))))))))))
750
e387ab7c
LC
751
752(test-equal "map-derivation"
753 "hello"
754 (let* ((joke (package-derivation %store guile-1.8))
755 (good (package-derivation %store %bootstrap-guile))
756 (drv1 (build-expression->derivation %store "original-drv1"
e387ab7c 757 #f ; systematically fail
e387ab7c
LC
758 #:guile-for-build joke))
759 (drv2 (build-expression->derivation %store "original-drv2"
e387ab7c
LC
760 '(call-with-output-file %output
761 (lambda (p)
dd1a5a15 762 (display "hello" p)))))
e387ab7c 763 (drv3 (build-expression->derivation %store "drv-to-remap"
e387ab7c
LC
764 '(let ((in (assoc-ref
765 %build-inputs "in")))
766 (copy-file in %output))
dd1a5a15 767 #:inputs `(("in" ,drv1))
e387ab7c
LC
768 #:guile-for-build joke))
769 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
770 (,joke . ,good))))
771 (out (derivation->output-path drv4)))
772 (and (build-derivations %store (list (pk 'remapped drv4)))
773 (call-with-input-file out get-string-all))))
774
a716e36d
LC
775(test-equal "map-derivation, sources"
776 "hello"
777 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
778 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
779 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
780 (drv1 (derivation %store "drv-to-remap"
781
782 ;; XXX: This wouldn't work in practice, but if
783 ;; we append "/bin/bash" then we can't replace
784 ;; it with the bootstrap bash, which is a
785 ;; single file.
786 (derivation->output-path bash-full)
787
788 `("-e" ,script1)
789 #:inputs `((,bash-full) (,script1))))
790 (drv2 (map-derivation %store drv1
791 `((,bash-full . ,%bash)
792 (,script1 . ,script2))))
793 (out (derivation->output-path drv2)))
794 (and (build-derivations %store (list (pk 'remapped* drv2)))
795 (call-with-input-file out get-string-all))))
796
341c6fdd
LC
797(test-end)
798
799\f
800(exit (= (test-runner-fail-count (test-runner-current)) 0))