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