gnu: util-linux: Remove dependency on Perl.
[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
de4c3f26
LC
393\f
394(define %coreutils
8f3ecbd7 395 (false-if-exception
ad1ebab3
LC
396 (and (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)
397 (or (package-derivation %store %bootstrap-coreutils&co)
398 (nixpkgs-derivation "coreutils")))))
de4c3f26
LC
399
400(test-skip (if %coreutils 0 1))
401
402(test-assert "build derivation with coreutils"
403 (let* ((builder
404 (add-text-to-store %store "build-with-coreutils.sh"
405 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
406 '()))
59688fc4 407 (drv
a987d2c0 408 (derivation %store "foo"
97d3998e 409 %bash `(,builder)
a987d2c0
LC
410 #:env-vars `(("PATH" .
411 ,(string-append
59688fc4 412 (derivation->output-path %coreutils)
a987d2c0
LC
413 "/bin")))
414 #:inputs `((,builder)
415 (,%coreutils))))
de4c3f26 416 (succeeded?
59688fc4 417 (build-derivations %store (list drv))))
de4c3f26 418 (and succeeded?
59688fc4 419 (let ((p (derivation->output-path drv)))
31ef99a8
LC
420 (and (valid-path? %store p)
421 (file-exists? (string-append p "/good")))))))
de4c3f26 422
9c629a27 423(test-skip (if (%guile-for-build) 0 8))
9a20830e
LC
424
425(test-assert "build-expression->derivation and derivation-prerequisites"
dd1a5a15 426 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e
LC
427 (any (match-lambda
428 (($ <derivation-input> path)
59688fc4 429 (string=? path (derivation-file-name (%guile-for-build)))))
9a20830e 430 (derivation-prerequisites drv))))
d9085c23
LC
431
432(test-assert "build-expression->derivation without inputs"
433 (let* ((builder '(begin
434 (mkdir %output)
435 (call-with-output-file (string-append %output "/test")
436 (lambda (p)
437 (display '(hello guix) p)))))
dd1a5a15 438 (drv (build-expression->derivation %store "goo" builder))
59688fc4 439 (succeeded? (build-derivations %store (list drv))))
d9085c23 440 (and succeeded?
59688fc4 441 (let ((p (derivation->output-path drv)))
d9085c23
LC
442 (equal? '(hello guix)
443 (call-with-input-file (string-append p "/test") read))))))
444
969e678e
LC
445(test-assert "build-expression->derivation and max-silent-time"
446 (let* ((store (let ((s (open-connection)))
447 (set-build-options s #:max-silent-time 1)
448 s))
01e82af5 449 (builder '(begin (sleep 100) (mkdir %output) #t))
dd1a5a15 450 (drv (build-expression->derivation store "silent" builder))
59688fc4 451 (out-path (derivation->output-path drv)))
6c20d1d0
LC
452 (guard (c ((nix-protocol-error? c)
453 (and (string-contains (nix-protocol-error-message c)
454 "failed")
455 (not (valid-path? store out-path)))))
456 (build-derivations store (list drv))
457 #f)))
458
459(test-assert "build-expression->derivation and timeout"
460 (let* ((store (let ((s (open-connection)))
461 (set-build-options s #:timeout 1)
462 s))
463 (builder '(begin (sleep 100) (mkdir %output) #t))
464 (drv (build-expression->derivation store "slow" builder))
465 (out-path (derivation->output-path drv)))
969e678e
LC
466 (guard (c ((nix-protocol-error? c)
467 (and (string-contains (nix-protocol-error-message c)
468 "failed")
469 (not (valid-path? store out-path)))))
01e82af5
LC
470 (build-derivations store (list drv))
471 #f)))
969e678e 472
9a20830e 473(test-assert "build-expression->derivation and derivation-prerequisites-to-build"
dd1a5a15 474 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e
LC
475 ;; The only direct dependency is (%guile-for-build) and it's already
476 ;; built.
477 (null? (derivation-prerequisites-to-build %store drv))))
478
784bb1f3 479(test-assert "derivation-prerequisites-to-build when outputs already present"
59688fc4 480 (let* ((builder '(begin (mkdir %output) #t))
dd1a5a15 481 (input-drv (build-expression->derivation %store "input" builder))
59688fc4
LC
482 (input-path (derivation-output-path
483 (assoc-ref (derivation-outputs input-drv)
484 "out")))
dd1a5a15
LC
485 (drv (build-expression->derivation %store "something" builder
486 #:inputs
59688fc4
LC
487 `(("i" ,input-drv))))
488 (output (derivation->output-path drv)))
784bb1f3
LC
489 ;; Make sure these things are not already built.
490 (when (valid-path? %store input-path)
491 (delete-paths %store (list input-path)))
492 (when (valid-path? %store output)
493 (delete-paths %store (list output)))
494
495 (and (equal? (map derivation-input-path
496 (derivation-prerequisites-to-build %store drv))
59688fc4 497 (list (derivation-file-name input-drv)))
784bb1f3
LC
498
499 ;; Build DRV and delete its input.
59688fc4 500 (build-derivations %store (list drv))
784bb1f3
LC
501 (delete-paths %store (list input-path))
502 (not (valid-path? %store input-path))
503
504 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
505 ;; prerequisite to build because DRV itself is already built.
506 (null? (derivation-prerequisites-to-build %store drv)))))
507
dd36b51b
LC
508(test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
509(test-assert "derivation-prerequisites-to-build and substitutes"
59688fc4
LC
510 (let* ((store (open-connection))
511 (drv (build-expression->derivation store "prereq-subst"
dd1a5a15 512 (random 1000)))
59688fc4
LC
513 (output (derivation->output-path drv))
514 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
dd36b51b
LC
515 (compose uri-path string->uri))))
516 ;; Create fake substituter data, to be read by `substitute-binary'.
517 (call-with-output-file (string-append dir "/nix-cache-info")
518 (lambda (p)
519 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
520 (%store-prefix))))
521 (call-with-output-file (string-append dir "/" (store-path-hash-part output)
522 ".narinfo")
523 (lambda (p)
524 (format p "StorePath: ~a
525URL: ~a
526Compression: none
527NarSize: 1234
528References:
529System: ~a
530Deriver: ~a~%"
531 output ; StorePath
532 (string-append dir "/example.nar") ; URL
533 (%current-system) ; System
59688fc4
LC
534 (basename
535 (derivation-file-name drv))))) ; Deriver
dd36b51b 536
1950bf56
LC
537 ;; Make sure substitutes are usable.
538 (set-build-options store #:use-substitutes? #t)
539
dd36b51b
LC
540 (let-values (((build download)
541 (derivation-prerequisites-to-build store drv))
542 ((build* download*)
543 (derivation-prerequisites-to-build store drv
544 #:use-substitutes? #f)))
545 (pk build download build* download*)
546 (and (null? build)
547 (equal? download (list output))
548 (null? download*)
549 (null? build*)))))
550
db393b33
LC
551(test-assert "build-expression->derivation with expression returning #f"
552 (let* ((builder '(begin
553 (mkdir %output)
554 #f)) ; fail!
dd1a5a15 555 (drv (build-expression->derivation %store "fail" builder))
59688fc4 556 (out-path (derivation->output-path drv)))
db393b33
LC
557 (guard (c ((nix-protocol-error? c)
558 ;; Note that the output path may exist at this point, but it
559 ;; is invalid.
31ef99a8
LC
560 (and (string-match "build .* failed"
561 (nix-protocol-error-message c))
562 (not (valid-path? %store out-path)))))
59688fc4 563 (build-derivations %store (list drv))
db393b33
LC
564 #f)))
565
9bc07f4d
LC
566(test-assert "build-expression->derivation with two outputs"
567 (let* ((builder '(begin
568 (call-with-output-file (assoc-ref %outputs "out")
569 (lambda (p)
570 (display '(hello) p)))
571 (call-with-output-file (assoc-ref %outputs "second")
572 (lambda (p)
573 (display '(world) p)))))
dd1a5a15 574 (drv (build-expression->derivation %store "double" builder
9bc07f4d
LC
575 #:outputs '("out"
576 "second")))
59688fc4 577 (succeeded? (build-derivations %store (list drv))))
9bc07f4d 578 (and succeeded?
59688fc4
LC
579 (let ((one (derivation->output-path drv))
580 (two (derivation->output-path drv "second")))
9bc07f4d
LC
581 (and (equal? '(hello) (call-with-input-file one read))
582 (equal? '(world) (call-with-input-file two read)))))))
583
ad1ebab3 584(test-skip (if %coreutils 0 1))
d9085c23
LC
585(test-assert "build-expression->derivation with one input"
586 (let* ((builder '(call-with-output-file %output
587 (lambda (p)
588 (let ((cu (assoc-ref %build-inputs "cu")))
589 (close 1)
590 (dup2 (port->fdes p) 1)
591 (execl (string-append cu "/bin/uname")
592 "uname" "-a")))))
dd1a5a15
LC
593 (drv (build-expression->derivation %store "uname" builder
594 #:inputs
2acb2cb6 595 `(("cu" ,%coreutils))))
59688fc4 596 (succeeded? (build-derivations %store (list drv))))
d9085c23 597 (and succeeded?
59688fc4 598 (let ((p (derivation->output-path drv)))
d9085c23
LC
599 (string-contains (call-with-input-file p read-line) "GNU")))))
600
99634e3f
LC
601(test-assert "imported-files"
602 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
603 ("a/b/c" . ,(search-path %load-path
604 "guix/derivations.scm"))
224f7ad6
LC
605 ("p/q" . ,(search-path %load-path "guix.scm"))
606 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
59688fc4
LC
607 (drv (imported-files %store files)))
608 (and (build-derivations %store (list drv))
609 (let ((dir (derivation->output-path drv)))
99634e3f
LC
610 (every (match-lambda
611 ((path . source)
612 (equal? (call-with-input-file (string-append dir "/" path)
613 get-bytevector-all)
614 (call-with-input-file source
615 get-bytevector-all))))
616 files)))))
617
d9024884
LC
618(test-assert "build-expression->derivation with modules"
619 (let* ((builder `(begin
620 (use-modules (guix build utils))
621 (let ((out (assoc-ref %outputs "out")))
622 (mkdir-p (string-append out "/guile/guix/nix"))
623 #t)))
59688fc4 624 (drv (build-expression->derivation %store "test-with-modules"
dd1a5a15 625 builder
d9024884
LC
626 #:modules
627 '((guix build utils)))))
59688fc4
LC
628 (and (build-derivations %store (list drv))
629 (let* ((p (derivation->output-path drv))
d9024884
LC
630 (s (stat (string-append p "/guile/guix/nix"))))
631 (eq? (stat:type s) 'directory)))))
632
813986ac
LC
633(test-assert "build-expression->derivation: same fixed-output path"
634 (let* ((builder1 '(call-with-output-file %output
635 (lambda (p)
636 (write "hello" p))))
637 (builder2 '(call-with-output-file (pk 'difference-here! %output)
638 (lambda (p)
639 (write "hello" p))))
640 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 641 (input1 (build-expression->derivation %store "fixed" builder1
813986ac
LC
642 #:hash hash
643 #:hash-algo 'sha256))
dd1a5a15 644 (input2 (build-expression->derivation %store "fixed" builder2
813986ac
LC
645 #:hash hash
646 #:hash-algo 'sha256))
647 (succeeded? (build-derivations %store (list input1 input2))))
648 (and succeeded?
59688fc4
LC
649 (not (string=? (derivation-file-name input1)
650 (derivation-file-name input2)))
651 (string=? (derivation->output-path input1)
652 (derivation->output-path input2)))))
813986ac 653
7bdd1f0e
LC
654(test-assert "build-expression->derivation with a fixed-output input"
655 (let* ((builder1 '(call-with-output-file %output
656 (lambda (p)
657 (write "hello" p))))
658 (builder2 '(call-with-output-file (pk 'difference-here! %output)
659 (lambda (p)
660 (write "hello" p))))
661 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 662 (input1 (build-expression->derivation %store "fixed" builder1
7bdd1f0e
LC
663 #:hash hash
664 #:hash-algo 'sha256))
dd1a5a15 665 (input2 (build-expression->derivation %store "fixed" builder2
7bdd1f0e
LC
666 #:hash hash
667 #:hash-algo 'sha256))
668 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
669 (call-with-output-file %output
670 (lambda (out)
671 (format #f "My input is ~a.~%" input)))))
dd1a5a15
LC
672 (final1 (build-expression->derivation %store "final" builder3
673 #:inputs
7bdd1f0e 674 `(("input" ,input1))))
dd1a5a15
LC
675 (final2 (build-expression->derivation %store "final" builder3
676 #:inputs
7bdd1f0e 677 `(("input" ,input2)))))
59688fc4
LC
678 (and (string=? (derivation->output-path final1)
679 (derivation->output-path final2))
680 (string=? (derivation->output-path final1)
681 (derivation-path->output-path
682 (derivation-file-name final1)))
7bdd1f0e
LC
683 (build-derivations %store (list final1 final2)))))
684
36bbbbd1
LC
685(test-assert "build-expression->derivation produces recursive fixed-output"
686 (let* ((builder '(begin
687 (use-modules (srfi srfi-26))
688 (mkdir %output)
689 (chdir %output)
690 (call-with-output-file "exe"
691 (cut display "executable" <>))
692 (chmod "exe" #o777)
693 (symlink "exe" "symlink")
694 (mkdir "subdir")))
695 (drv (build-expression->derivation %store "fixed-rec" builder
696 #:hash-algo 'sha256
697 #:hash (base32
698 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
699 #:recursive? #t)))
700 (and (build-derivations %store (list drv))
701 (let* ((dir (derivation->output-path drv))
702 (exe (string-append dir "/exe"))
703 (link (string-append dir "/symlink"))
704 (subdir (string-append dir "/subdir")))
705 (and (executable-file? exe)
706 (string=? "executable"
707 (call-with-input-file exe get-string-all))
708 (string=? "exe" (readlink link))
709 (file-is-directory? subdir))))))
710
711(test-assert "build-expression->derivation uses recursive fixed-output"
712 (let* ((builder '(call-with-output-file %output
713 (lambda (port)
714 (display "hello" port))))
715 (fixed (build-expression->derivation %store "small-fixed-rec"
716 builder
717 #:hash-algo 'sha256
718 #:hash (base32
719 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
720 #:recursive? #t))
721 (in (derivation->output-path fixed))
722 (builder `(begin
723 (mkdir %output)
724 (chdir %output)
725 (symlink ,in "symlink")))
726 (drv (build-expression->derivation %store "fixed-rec-user"
727 builder
728 #:inputs `(("fixed" ,fixed)))))
729 (and (build-derivations %store (list drv))
730 (let ((out (derivation->output-path drv)))
731 (string=? (readlink (string-append out "/symlink")) in)))))
732
858e9282 733(test-assert "build-expression->derivation with #:references-graphs"
9c629a27
LC
734 (let* ((input (add-text-to-store %store "foo" "hello"
735 (list %bash %mkdir)))
736 (builder '(copy-file "input" %output))
858e9282 737 (drv (build-expression->derivation %store "references-graphs"
dd1a5a15 738 builder
858e9282 739 #:references-graphs
9c629a27 740 `(("input" . ,input))))
59688fc4 741 (out (derivation->output-path drv)))
9c629a27
LC
742 (define (deps path . deps)
743 (let ((count (length deps)))
744 (string-append path "\n\n" (number->string count) "\n"
745 (string-join (sort deps string<?) "\n")
746 (if (zero? count) "" "\n"))))
747
748 (and (build-derivations %store (list drv))
749 (equal? (call-with-input-file out get-string-all)
750 (string-concatenate
751 (map cdr
752 (sort (map (lambda (p d)
753 (cons p (apply deps p d)))
754 (list input %bash %mkdir)
755 (list (list %bash %mkdir)
756 '() '()))
757 (lambda (x y)
758 (match x
759 ((p1 . _)
760 (match y
761 ((p2 . _)
762 (string<? p1 p2)))))))))))))
763
e387ab7c
LC
764
765(test-equal "map-derivation"
766 "hello"
767 (let* ((joke (package-derivation %store guile-1.8))
768 (good (package-derivation %store %bootstrap-guile))
769 (drv1 (build-expression->derivation %store "original-drv1"
e387ab7c 770 #f ; systematically fail
e387ab7c
LC
771 #:guile-for-build joke))
772 (drv2 (build-expression->derivation %store "original-drv2"
e387ab7c
LC
773 '(call-with-output-file %output
774 (lambda (p)
dd1a5a15 775 (display "hello" p)))))
e387ab7c 776 (drv3 (build-expression->derivation %store "drv-to-remap"
e387ab7c
LC
777 '(let ((in (assoc-ref
778 %build-inputs "in")))
779 (copy-file in %output))
dd1a5a15 780 #:inputs `(("in" ,drv1))
e387ab7c
LC
781 #:guile-for-build joke))
782 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
783 (,joke . ,good))))
784 (out (derivation->output-path drv4)))
785 (and (build-derivations %store (list (pk 'remapped drv4)))
786 (call-with-input-file out get-string-all))))
787
a716e36d
LC
788(test-equal "map-derivation, sources"
789 "hello"
790 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
791 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
792 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
793 (drv1 (derivation %store "drv-to-remap"
794
795 ;; XXX: This wouldn't work in practice, but if
796 ;; we append "/bin/bash" then we can't replace
797 ;; it with the bootstrap bash, which is a
798 ;; single file.
799 (derivation->output-path bash-full)
800
801 `("-e" ,script1)
802 #:inputs `((,bash-full) (,script1))))
803 (drv2 (map-derivation %store drv1
804 `((,bash-full . ,%bash)
805 (,script1 . ,script2))))
806 (out (derivation->output-path drv2)))
807 (and (build-derivations %store (list (pk 'remapped* drv2)))
808 (call-with-input-file out get-string-all))))
809
341c6fdd
LC
810(test-end)
811
812\f
813(exit (= (test-runner-fail-count (test-runner-current)) 0))