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