doc: Explain that "guix import" and "guix refresh" may need GnuPG.
[jackhill/guix/guix.git] / tests / derivations.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
f9e8a123 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 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 18
94d92c77
LC
19(unsetenv "http_proxy")
20
341c6fdd
LC
21(define-module (test-derivations)
22 #:use-module (guix derivations)
ef8de985 23 #:use-module (guix grafts)
26bbbb95 24 #:use-module (guix store)
de4c3f26 25 #:use-module (guix utils)
ca719424 26 #:use-module (gcrypt hash)
ddc29a78 27 #:use-module (guix base32)
c1bc358f 28 #:use-module (guix tests)
94d92c77 29 #:use-module (guix tests http)
36bbbbd1
LC
30 #:use-module ((guix packages) #:select (package-derivation base32))
31 #:use-module ((guix build utils) #:select (executable-file?))
59a43334 32 #:use-module ((gnu packages) #:select (search-bootstrap-binary))
1ffa7090 33 #:use-module (gnu packages bootstrap)
e387ab7c 34 #:use-module ((gnu packages guile) #:select (guile-1.8))
b37eb5ed 35 #:use-module (srfi srfi-1)
fb3eec83 36 #:use-module (srfi srfi-11)
341c6fdd 37 #:use-module (srfi srfi-26)
99634e3f 38 #:use-module (srfi srfi-34)
341c6fdd 39 #:use-module (srfi srfi-64)
fb3eec83 40 #:use-module (rnrs io ports)
749c6567 41 #:use-module (rnrs bytevectors)
dd36b51b 42 #:use-module (web uri)
b37eb5ed 43 #:use-module (ice-9 rdelim)
db393b33 44 #:use-module (ice-9 regex)
99634e3f
LC
45 #:use-module (ice-9 ftw)
46 #:use-module (ice-9 match))
341c6fdd 47
26bbbb95 48(define %store
c1bc358f 49 (open-connection-for-tests))
b272c474 50
ef8de985
LC
51;; Globally disable grafts because they can trigger early builds.
52(%graft? #f)
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
94d92c77
LC
81;; Avoid collisions with other tests.
82(%http-server-port 10500)
83
ef8de985 84\f
341c6fdd
LC
85(test-begin "derivations")
86
87(test-assert "parse & export"
33594aa4
LC
88 (let* ((f (search-path %load-path "tests/test.drv"))
89 (b1 (call-with-input-file f get-bytevector-all))
5cf4b26d
LC
90 (d1 (read-derivation (open-bytevector-input-port b1)
91 identity))
341c6fdd 92 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
5cf4b26d
LC
93 (d2 (read-derivation (open-bytevector-input-port b2)
94 identity)))
341c6fdd
LC
95 (and (equal? b1 b2)
96 (equal? d1 d2))))
97
5b0c9d16 98(test-skip (if %store 0 12))
b37eb5ed 99
d1b1c424 100(test-assert "add-to-store, flat"
9d6fae65
LC
101 ;; Use 'readlink*' in case spec.scm is a symlink, as is the case when Guile
102 ;; was installed with Stow.
103 (let* ((file (readlink*
104 (search-path %load-path "language/tree-il/spec.scm")))
a9ebd9ef 105 (drv (add-to-store %store "flat-test" #f "sha256" file)))
d1b1c424 106 (and (eq? 'regular (stat:type (stat drv)))
31ef99a8 107 (valid-path? %store drv)
d1b1c424
LC
108 (equal? (call-with-input-file file get-bytevector-all)
109 (call-with-input-file drv get-bytevector-all)))))
110
b37eb5ed 111(test-assert "add-to-store, recursive"
9d6fae65
LC
112 (let* ((dir (dirname
113 (readlink* (search-path %load-path
114 "language/tree-il/spec.scm"))))
a9ebd9ef 115 (drv (add-to-store %store "dir-tree-test" #t "sha256" dir)))
b37eb5ed 116 (and (eq? 'directory (stat:type (stat drv)))
31ef99a8 117 (valid-path? %store drv)
b37eb5ed
LC
118 (equal? (directory-contents dir)
119 (directory-contents drv)))))
26bbbb95
LC
120
121(test-assert "derivation with no inputs"
31ef99a8 122 (let* ((builder (add-text-to-store %store "my-builder.sh"
97d3998e 123 "echo hello, world\n"
31ef99a8 124 '()))
59688fc4 125 (drv (derivation %store "foo"
97d3998e 126 %bash `("-e" ,builder)
a987d2c0 127 #:env-vars '(("HOME" . "/homeless")))))
59688fc4
LC
128 (and (store-path? (derivation-file-name drv))
129 (valid-path? %store (derivation-file-name drv)))))
26bbbb95 130
fb3eec83 131(test-assert "build derivation with 1 source"
59688fc4
LC
132 (let* ((builder (add-text-to-store %store "my-builder.sh"
133 "echo hello, world > \"$out\"\n"
134 '()))
135 (drv (derivation %store "foo"
136 %bash `(,builder)
137 #:env-vars '(("HOME" . "/homeless")
138 ("zzz" . "Z!")
139 ("AAA" . "A!"))
9e64302d 140 #:sources `(,%bash ,builder)))
59688fc4
LC
141 (succeeded?
142 (build-derivations %store (list drv))))
fb3eec83 143 (and succeeded?
59688fc4 144 (let ((path (derivation->output-path drv)))
31ef99a8
LC
145 (and (valid-path? %store path)
146 (string=? (call-with-input-file path read-line)
147 "hello, world"))))))
fb3eec83 148
0e510971
LC
149(test-assert "derivation fails but keep going"
150 ;; In keep-going mode, 'build-derivations' should fail because of D1, but it
151 ;; must return only after D2 has succeeded.
152 (with-store store
153 (let* ((d1 (derivation %store "fails"
154 %bash `("-c" "false")
9e64302d 155 #:sources (list %bash)))
0e510971
LC
156 (d2 (build-expression->derivation %store "sleep-then-succeed"
157 `(begin
158 ,(random-text)
159 ;; XXX: Hopefully that's long
160 ;; enough that D1 has already
161 ;; failed.
162 (sleep 2)
163 (mkdir %output)))))
164 (set-build-options %store
165 #:use-substitutes? #f
166 #:keep-going? #t)
f9e8a123
LC
167 (guard (c ((store-protocol-error? c)
168 (and (= 100 (store-protocol-error-status c))
169 (string-contains (store-protocol-error-message c)
0e510971
LC
170 (derivation-file-name d1))
171 (not (valid-path? %store (derivation->output-path d1)))
172 (valid-path? %store (derivation->output-path d2)))))
173 (build-derivations %store (list d1 d2))
174 #f))))
175
44d43c7a
LC
176(test-assert "identical files are deduplicated"
177 (let* ((build1 (add-text-to-store %store "one.sh"
178 "echo hello, world > \"$out\"\n"
179 '()))
180 (build2 (add-text-to-store %store "two.sh"
181 "# Hey!\necho hello, world > \"$out\"\n"
182 '()))
183 (drv1 (derivation %store "foo"
184 %bash `(,build1)
9e64302d 185 #:sources `(,%bash ,build1)))
44d43c7a
LC
186 (drv2 (derivation %store "bar"
187 %bash `(,build2)
9e64302d 188 #:sources `(,%bash ,build2))))
44d43c7a
LC
189 (and (build-derivations %store (list drv1 drv2))
190 (let ((file1 (derivation->output-path drv1))
191 (file2 (derivation->output-path drv2)))
192 (and (valid-path? %store file1) (valid-path? %store file2)
193 (string=? (call-with-input-file file1 get-string-all)
194 "hello, world\n")
195 (= (stat:ino (lstat file1))
196 (stat:ino (lstat file2))))))))
197
f9aefa2d
LC
198(test-equal "built-in-builders"
199 '("download")
200 (built-in-builders %store))
201
94d92c77
LC
202(test-assert "unknown built-in builder"
203 (let ((drv (derivation %store "ohoh" "builtin:does-not-exist" '())))
f9e8a123
LC
204 (guard (c ((store-protocol-error? c)
205 (string-contains (store-protocol-error-message c) "failed")))
94d92c77
LC
206 (build-derivations %store (list drv))
207 #f)))
208
6ea10db9 209(unless (http-server-can-listen?)
94d92c77
LC
210 (test-skip 1))
211(test-assert "'download' built-in builder"
212 (let ((text (random-text)))
213 (with-http-server 200 text
214 (let* ((drv (derivation %store "world"
215 "builtin:download" '()
216 #:env-vars `(("url"
217 . ,(object->string (%local-url))))
218 #:hash-algo 'sha256
219 #:hash (sha256 (string->utf8 text)))))
220 (and (build-derivations %store (list drv))
221 (string=? (call-with-input-file (derivation->output-path drv)
222 get-string-all)
223 text))))))
224
6ea10db9 225(unless (http-server-can-listen?)
94d92c77
LC
226 (test-skip 1))
227(test-assert "'download' built-in builder, invalid hash"
228 (with-http-server 200 "hello, world!"
229 (let* ((drv (derivation %store "world"
230 "builtin:download" '()
231 #:env-vars `(("url"
232 . ,(object->string (%local-url))))
233 #:hash-algo 'sha256
234 #:hash (sha256 (random-bytevector 100))))) ;wrong
f9e8a123
LC
235 (guard (c ((store-protocol-error? c)
236 (string-contains (store-protocol-error-message c) "failed")))
94d92c77
LC
237 (build-derivations %store (list drv))
238 #f))))
239
6ea10db9 240(unless (http-server-can-listen?)
94d92c77
LC
241 (test-skip 1))
242(test-assert "'download' built-in builder, not found"
243 (with-http-server 404 "not found"
244 (let* ((drv (derivation %store "will-never-be-found"
245 "builtin:download" '()
246 #:env-vars `(("url"
247 . ,(object->string (%local-url))))
248 #:hash-algo 'sha256
249 #:hash (sha256 (random-bytevector 100)))))
f9e8a123
LC
250 (guard (c ((store-protocol-error? c)
251 (string-contains (store-protocol-error-message (pk c)) "failed")))
94d92c77
LC
252 (build-derivations %store (list drv))
253 #f))))
254
255(test-assert "'download' built-in builder, not fixed-output"
256 (let* ((source (add-text-to-store %store "hello" "hi!"))
257 (url (string-append "file://" source))
258 (drv (derivation %store "world"
259 "builtin:download" '()
260 #:env-vars `(("url" . ,(object->string url))))))
f9e8a123
LC
261 (guard (c ((store-protocol-error? c)
262 (string-contains (store-protocol-error-message c) "failed")))
94d92c77
LC
263 (build-derivations %store (list drv))
264 #f)))
265
6ea10db9 266(unless (http-server-can-listen?)
9b5364a3
LC
267 (test-skip 1))
268(test-assert "'download' built-in builder, check mode"
269 ;; Make sure rebuilding the 'builtin:download' derivation in check mode
270 ;; works. See <http://bugs.gnu.org/25089>.
271 (let* ((text (random-text))
272 (drv (derivation %store "world"
273 "builtin:download" '()
274 #:env-vars `(("url"
275 . ,(object->string (%local-url))))
276 #:hash-algo 'sha256
277 #:hash (sha256 (string->utf8 text)))))
278 (and (with-http-server 200 text
279 (build-derivations %store (list drv)))
280 (with-http-server 200 text
281 (build-derivations %store (list drv)
282 (build-mode check)))
283 (string=? (call-with-input-file (derivation->output-path drv)
284 get-string-all)
285 text))))
286
e786293e
LC
287(test-equal "derivation-name"
288 "foo-0.0"
289 (let ((drv (derivation %store "foo-0.0" %bash '())))
290 (derivation-name drv)))
291
0b6af195
LC
292(test-equal "derivation-output-names"
293 '(("out") ("bar" "chbouib"))
294 (let ((drv1 (derivation %store "foo-0.0" %bash '()))
295 (drv2 (derivation %store "foo-0.0" %bash '()
296 #:outputs '("bar" "chbouib"))))
297 (list (derivation-output-names drv1)
298 (derivation-output-names drv2))))
299
fc93e309
LC
300(test-assert "offloadable-derivation?"
301 (and (offloadable-derivation? (derivation %store "foo" %bash '()))
4a6aeb67
LC
302 (offloadable-derivation? ;see <http://bugs.gnu.org/18747>
303 (derivation %store "foo" %bash '()
304 #:substitutable? #f))
fc93e309
LC
305 (not (offloadable-derivation?
306 (derivation %store "foo" %bash '()
307 #:local-build? #t)))))
308
4a6aeb67
LC
309(test-assert "substitutable-derivation?"
310 (and (substitutable-derivation? (derivation %store "foo" %bash '()))
311 (substitutable-derivation? ;see <http://bugs.gnu.org/18747>
312 (derivation %store "foo" %bash '()
cfc5d398 313 #:local-build? #t))
4a6aeb67
LC
314 (not (substitutable-derivation?
315 (derivation %store "foo" %bash '()
316 #:substitutable? #f)))))
317
99e17dc9
LC
318(test-assert "fixed-output-derivation?"
319 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
320 "echo -n hello > $out" '()))
321 (hash (sha256 (string->utf8 "hello")))
322 (drv (derivation %store "fixed"
323 %bash `(,builder)
9e64302d 324 #:sources (list builder)
99e17dc9
LC
325 #:hash hash #:hash-algo 'sha256)))
326 (fixed-output-derivation? drv)))
327
749c6567
LC
328(test-assert "fixed-output derivation"
329 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
330 "echo -n hello > $out" '()))
331 (hash (sha256 (string->utf8 "hello")))
59688fc4 332 (drv (derivation %store "fixed"
97d3998e 333 %bash `(,builder)
9e64302d 334 #:sources `(,builder) ;optional
749c6567 335 #:hash hash #:hash-algo 'sha256))
59688fc4 336 (succeeded? (build-derivations %store (list drv))))
749c6567 337 (and succeeded?
59688fc4 338 (let ((p (derivation->output-path drv)))
82058eff
LC
339 (and (equal? (string->utf8 "hello")
340 (call-with-input-file p get-bytevector-all))
341 (bytevector? (query-path-hash %store p)))))))
749c6567 342
813986ac
LC
343(test-assert "fixed-output derivation: output paths are equal"
344 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
345 "echo -n hello > $out" '()))
346 (builder2 (add-text-to-store %store "fixed-builder2.sh"
347 "echo hey; echo -n hello > $out" '()))
348 (hash (sha256 (string->utf8 "hello")))
59688fc4 349 (drv1 (derivation %store "fixed"
97d3998e 350 %bash `(,builder1)
813986ac 351 #:hash hash #:hash-algo 'sha256))
59688fc4 352 (drv2 (derivation %store "fixed"
97d3998e 353 %bash `(,builder2)
813986ac 354 #:hash hash #:hash-algo 'sha256))
59688fc4 355 (succeeded? (build-derivations %store (list drv1 drv2))))
813986ac 356 (and succeeded?
59688fc4
LC
357 (equal? (derivation->output-path drv1)
358 (derivation->output-path drv2)))))
813986ac 359
36bbbbd1
LC
360(test-assert "fixed-output derivation, recursive"
361 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
362 "echo -n hello > $out" '()))
363 (hash (sha256 (string->utf8 "hello")))
364 (drv (derivation %store "fixed-rec"
365 %bash `(,builder)
9e64302d 366 #:sources (list builder)
36bbbbd1
LC
367 #:hash (base32 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
368 #:hash-algo 'sha256
369 #:recursive? #t))
370 (succeeded? (build-derivations %store (list drv))))
371 (and succeeded?
372 (let ((p (derivation->output-path drv)))
373 (and (equal? (string->utf8 "hello")
374 (call-with-input-file p get-bytevector-all))
375 (bytevector? (query-path-hash %store p)))))))
376
813986ac
LC
377(test-assert "derivation with a fixed-output input"
378 ;; A derivation D using a fixed-output derivation F doesn't has the same
379 ;; output path when passed F or F', as long as F and F' have the same output
380 ;; path.
381 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
382 "echo -n hello > $out" '()))
383 (builder2 (add-text-to-store %store "fixed-builder2.sh"
384 "echo hey; echo -n hello > $out" '()))
385 (hash (sha256 (string->utf8 "hello")))
a987d2c0 386 (fixed1 (derivation %store "fixed"
97d3998e 387 %bash `(,builder1)
813986ac 388 #:hash hash #:hash-algo 'sha256))
a987d2c0 389 (fixed2 (derivation %store "fixed"
97d3998e 390 %bash `(,builder2)
813986ac 391 #:hash hash #:hash-algo 'sha256))
59688fc4 392 (fixed-out (derivation->output-path fixed1))
813986ac
LC
393 (builder3 (add-text-to-store
394 %store "final-builder.sh"
395 ;; Use Bash hackery to avoid Coreutils.
396 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
a987d2c0 397 (final1 (derivation %store "final"
97d3998e 398 %bash `(,builder3)
a987d2c0 399 #:env-vars `(("in" . ,fixed-out))
9e64302d
LC
400 #:sources (list %bash builder3)
401 #:inputs (list (derivation-input fixed1))))
a987d2c0 402 (final2 (derivation %store "final"
97d3998e 403 %bash `(,builder3)
a987d2c0 404 #:env-vars `(("in" . ,fixed-out))
9e64302d
LC
405 #:sources (list %bash builder3)
406 #:inputs (list (derivation-input fixed2))))
813986ac
LC
407 (succeeded? (build-derivations %store
408 (list final1 final2))))
409 (and succeeded?
59688fc4
LC
410 (equal? (derivation->output-path final1)
411 (derivation->output-path final2)))))
813986ac 412
26889644
LC
413(test-assert "derivation with duplicate fixed-output inputs"
414 ;; Here we create a derivation that has two inputs, both of which are
415 ;; fixed-output leading to the same result. This test ensures the hash of
416 ;; that derivation is correctly computed, namely that duplicate inputs are
417 ;; coalesced. See <https://bugs.gnu.org/36777>.
418 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
419 "echo -n hello > $out" '()))
420 (builder2 (add-text-to-store %store "fixed-builder2.sh"
421 "echo hey; echo -n hello > $out" '()))
422 (hash (sha256 (string->utf8 "hello")))
423 (fixed1 (derivation %store "fixed"
424 %bash `(,builder1)
425 #:hash hash #:hash-algo 'sha256))
426 (fixed2 (derivation %store "fixed"
427 %bash `(,builder2)
428 #:hash hash #:hash-algo 'sha256))
429 (builder3 (add-text-to-store %store "builder.sh"
430 "echo fake builder"))
431 (final (derivation %store "final"
432 %bash `(,builder3)
433 #:sources (list %bash builder3)
434 #:inputs (list (derivation-input fixed1)
435 (derivation-input fixed2)))))
436 (and (derivation? final)
437 (match (derivation-inputs final)
438 (((= derivation-input-derivation one)
439 (= derivation-input-derivation two))
440 (and (not (string=? (derivation-file-name one)
441 (derivation-file-name two)))
442 (string=? (derivation->output-path one)
443 (derivation->output-path two))))))))
444
7946c4e7
LC
445(test-assert "multiple-output derivation"
446 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
447 "echo one > $out ; echo two > $second"
448 '()))
59688fc4 449 (drv (derivation %store "fixed"
97d3998e 450 %bash `(,builder)
a987d2c0
LC
451 #:env-vars '(("HOME" . "/homeless")
452 ("zzz" . "Z!")
453 ("AAA" . "A!"))
9e64302d 454 #:sources `(,%bash ,builder)
7946c4e7 455 #:outputs '("out" "second")))
59688fc4 456 (succeeded? (build-derivations %store (list drv))))
7946c4e7 457 (and succeeded?
59688fc4
LC
458 (let ((one (derivation->output-path drv "out"))
459 (two (derivation->output-path drv "second")))
7244a5f7 460 (and (lset= equal?
59688fc4 461 (derivation->output-paths drv)
7244a5f7
LC
462 `(("out" . ,one) ("second" . ,two)))
463 (eq? 'one (call-with-input-file one read))
7946c4e7
LC
464 (eq? 'two (call-with-input-file two read)))))))
465
4b1786aa
LC
466(test-assert "multiple-output derivation, non-alphabetic order"
467 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
468 ;; path computation must reorder them first.
469 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
470 "echo one > $out ; echo two > $AAA"
471 '()))
59688fc4 472 (drv (derivation %store "fixed"
97d3998e 473 %bash `(,builder)
9e64302d 474 #:sources `(,%bash ,builder)
4b1786aa 475 #:outputs '("out" "AAA")))
59688fc4 476 (succeeded? (build-derivations %store (list drv))))
4b1786aa 477 (and succeeded?
59688fc4
LC
478 (let ((one (derivation->output-path drv "out"))
479 (two (derivation->output-path drv "AAA")))
4b1786aa
LC
480 (and (eq? 'one (call-with-input-file one read))
481 (eq? 'two (call-with-input-file two read)))))))
482
97507ebe
LC
483(test-assert "read-derivation vs. derivation"
484 ;; Make sure 'derivation' and 'read-derivation' return objects that are
485 ;; identical.
486 (let* ((sources (unfold (cut >= <> 10)
487 (lambda (n)
488 (add-text-to-store %store
489 (format #f "input~a" n)
490 (random-text)))
491 1+
492 0))
493 (inputs (map (lambda (file)
494 (derivation %store "derivation-input"
495 %bash '()
9e64302d 496 #:sources `(,%bash ,file)))
97507ebe
LC
497 sources))
498 (builder (add-text-to-store %store "builder.sh"
499 "echo one > $one ; echo two > $two"
500 '()))
501 (drv (derivation %store "derivation"
502 %bash `(,builder)
9e64302d
LC
503 #:sources `(,%bash ,builder ,@sources)
504 #:inputs (map derivation-input inputs)
97507ebe
LC
505 #:outputs '("two" "one")))
506 (drv* (call-with-input-file (derivation-file-name drv)
507 read-derivation)))
508 (equal? drv* drv)))
509
d0dc4907
LC
510(test-assert "multiple-output derivation, derivation-path->output-path"
511 (let* ((builder (add-text-to-store %store "builder.sh"
512 "echo one > $out ; echo two > $second"
513 '()))
514 (drv (derivation %store "multiple"
515 %bash `(,builder)
516 #:outputs '("out" "second")))
517 (drv-file (derivation-file-name drv))
518 (one (derivation->output-path drv "out"))
519 (two (derivation->output-path drv "second"))
520 (first (derivation-path->output-path drv-file "out"))
521 (second (derivation-path->output-path drv-file "second")))
522 (and (not (string=? one two))
523 (string-suffix? "-second" two)
524 (string=? first one)
525 (string=? second two))))
526
d66ac374
LC
527(test-assert "user of multiple-output derivation"
528 ;; Check whether specifying several inputs coming from the same
529 ;; multiple-output derivation works.
530 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
531 "echo one > $out ; echo two > $two"
532 '()))
a987d2c0 533 (mdrv (derivation %store "multiple-output"
97d3998e 534 %bash `(,builder1)
9e64302d 535 #:sources (list %bash builder1)
d66ac374
LC
536 #:outputs '("out" "two")))
537 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
538 "read x < $one;
539 read y < $two;
540 echo \"($x $y)\" > $out"
541 '()))
542 (udrv (derivation %store "multiple-output-user"
97d3998e 543 %bash `(,builder2)
a987d2c0 544 #:env-vars `(("one"
59688fc4 545 . ,(derivation->output-path
a987d2c0
LC
546 mdrv "out"))
547 ("two"
59688fc4 548 . ,(derivation->output-path
a987d2c0 549 mdrv "two")))
9e64302d
LC
550 #:sources (list %bash builder2)
551 ;; two occurrences of MDRV:
552 #:inputs
553 (list (derivation-input mdrv)
554 (derivation-input mdrv '("two"))))))
d66ac374 555 (and (build-derivations %store (list (pk 'udrv udrv)))
59688fc4 556 (let ((p (derivation->output-path udrv)))
d66ac374
LC
557 (and (valid-path? %store p)
558 (equal? '(one two) (call-with-input-file p read)))))))
559
858e9282 560(test-assert "derivation with #:references-graphs"
5b0c9d16
LC
561 (let* ((input1 (add-text-to-store %store "foo" "hello"
562 (list %bash)))
563 (input2 (add-text-to-store %store "bar"
564 (number->string (random 7777))
565 (list input1)))
566 (builder (add-text-to-store %store "build-graph"
567 (format #f "
568~a $out
569 (while read l ; do echo $l ; done) < bash > $out/bash
570 (while read l ; do echo $l ; done) < input1 > $out/input1
571 (while read l ; do echo $l ; done) < input2 > $out/input2"
572 %mkdir)
573 (list %mkdir)))
574 (drv (derivation %store "closure-graphs"
575 %bash `(,builder)
858e9282 576 #:references-graphs
5b0c9d16
LC
577 `(("bash" . ,%bash)
578 ("input1" . ,input1)
579 ("input2" . ,input2))
9e64302d 580 #:sources (list %bash builder)))
59688fc4 581 (out (derivation->output-path drv)))
5b0c9d16
LC
582 (define (deps path . deps)
583 (let ((count (length deps)))
584 (string-append path "\n\n" (number->string count) "\n"
585 (string-join (sort deps string<?) "\n")
586 (if (zero? count) "" "\n"))))
587
588 (and (build-derivations %store (list drv))
589 (equal? (directory-contents out get-string-all)
590 `(("/bash" . ,(string-append %bash "\n\n0\n"))
591 ("/input1" . ,(if (string>? input1 %bash)
592 (string-append (deps %bash)
593 (deps input1 %bash))
594 (string-append (deps input1 %bash)
595 (deps %bash))))
596 ("/input2" . ,(string-concatenate
597 (map cdr
598 (sort
599 (map (lambda (p d)
600 (cons p (apply deps p d)))
601 (list %bash input1 input2)
602 (list '() (list %bash) (list input1)))
603 (lambda (x y)
604 (match x
605 ((p1 . _)
606 (match y
607 ((p2 . _)
608 (string<? p1 p2)))))))))))))))
609
b53be755
LC
610(test-assert "derivation #:allowed-references, ok"
611 (let ((drv (derivation %store "allowed" %bash
612 '("-c" "echo hello > $out")
9e64302d 613 #:sources (list %bash)
b53be755
LC
614 #:allowed-references '())))
615 (build-derivations %store (list drv))))
616
617(test-assert "derivation #:allowed-references, not allowed"
618 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
619 (drv (derivation %store "disallowed" %bash
620 `("-c" ,(string-append "echo " txt "> $out"))
9e64302d 621 #:sources (list %bash txt)
b53be755 622 #:allowed-references '())))
f9e8a123 623 (guard (c ((store-protocol-error? c)
b53be755
LC
624 ;; There's no specific error message to check for.
625 #t))
626 (build-derivations %store (list drv))
627 #f)))
628
629(test-assert "derivation #:allowed-references, self allowed"
630 (let ((drv (derivation %store "allowed" %bash
631 '("-c" "echo $out > $out")
9e64302d 632 #:sources (list %bash)
b53be755
LC
633 #:allowed-references '("out"))))
634 (build-derivations %store (list drv))))
635
636(test-assert "derivation #:allowed-references, self not allowed"
637 (let ((drv (derivation %store "disallowed" %bash
638 `("-c" ,"echo $out > $out")
9e64302d 639 #:sources (list %bash)
b53be755 640 #:allowed-references '())))
f9e8a123 641 (guard (c ((store-protocol-error? c)
b53be755
LC
642 ;; There's no specific error message to check for.
643 #t))
644 (build-derivations %store (list drv))
645 #f)))
646
35b5ca78
LC
647(test-assert "derivation #:disallowed-references, ok"
648 (let ((drv (derivation %store "disallowed" %bash
649 '("-c" "echo hello > $out")
9e64302d 650 #:sources (list %bash)
35b5ca78
LC
651 #:disallowed-references '("out"))))
652 (build-derivations %store (list drv))))
653
654(test-assert "derivation #:disallowed-references, not ok"
655 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
656 (drv (derivation %store "disdisallowed" %bash
657 `("-c" ,(string-append "echo " txt "> $out"))
9e64302d 658 #:sources (list %bash txt)
35b5ca78 659 #:disallowed-references (list txt))))
f9e8a123 660 (guard (c ((store-protocol-error? c)
35b5ca78
LC
661 ;; There's no specific error message to check for.
662 #t))
663 (build-derivations %store (list drv))
664 #f)))
665
a87d66f3
LC
666;; Here we should get the value of $GUIX_STATE_DIRECTORY that the daemon sees,
667;; which is a unique value for each test process; this value is the same as
668;; the one we see in the process executing this file since it is set by
669;; 'test-env'.
44ad3384 670(test-equal "derivation #:leaked-env-vars"
a87d66f3
LC
671 (getenv "GUIX_STATE_DIRECTORY")
672 (let* ((value (getenv "GUIX_STATE_DIRECTORY"))
44ad3384 673 (drv (derivation %store "leaked-env-vars" %bash
a87d66f3 674 '("-c" "echo -n $GUIX_STATE_DIRECTORY > $out")
44ad3384
LC
675 #:hash (sha256 (string->utf8 value))
676 #:hash-algo 'sha256
9e64302d 677 #:sources (list %bash)
a87d66f3 678 #:leaked-env-vars '("GUIX_STATE_DIRECTORY"))))
44ad3384
LC
679 (and (build-derivations %store (list drv))
680 (call-with-input-file (derivation->output-path drv)
681 get-string-all))))
682
de4c3f26
LC
683\f
684(define %coreutils
8f3ecbd7 685 (false-if-exception
12d720fd 686 (and (network-reachable?)
f073e523 687 (package-derivation %store %bootstrap-coreutils&co))))
de4c3f26
LC
688
689(test-skip (if %coreutils 0 1))
690
691(test-assert "build derivation with coreutils"
692 (let* ((builder
693 (add-text-to-store %store "build-with-coreutils.sh"
694 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
695 '()))
59688fc4 696 (drv
a987d2c0 697 (derivation %store "foo"
97d3998e 698 %bash `(,builder)
a987d2c0
LC
699 #:env-vars `(("PATH" .
700 ,(string-append
59688fc4 701 (derivation->output-path %coreutils)
a987d2c0 702 "/bin")))
9e64302d
LC
703 #:sources (list builder)
704 #:inputs (list (derivation-input %coreutils))))
de4c3f26 705 (succeeded?
59688fc4 706 (build-derivations %store (list drv))))
de4c3f26 707 (and succeeded?
59688fc4 708 (let ((p (derivation->output-path drv)))
31ef99a8
LC
709 (and (valid-path? %store p)
710 (file-exists? (string-append p "/good")))))))
de4c3f26 711
9c629a27 712(test-skip (if (%guile-for-build) 0 8))
9a20830e 713
d26e1967
LC
714(test-equal "build-expression->derivation and invalid module name"
715 '(file-search-error "guix/module/that/does/not/exist.scm")
716 (guard (c ((file-search-error? c)
717 (list 'file-search-error
718 (file-search-error-file-name c))))
719 (build-expression->derivation %store "foo" #t
720 #:modules '((guix module that
721 does not exist)))))
722
9231ef12
LC
723(test-equal "build-expression->derivation and builder encoding"
724 '("UTF-8" #t)
725 (let* ((exp '(λ (α) (+ α 1)))
726 (drv (build-expression->derivation %store "foo" exp)))
727 (match (derivation-builder-arguments drv)
728 ((... builder)
8a8e2d2e
LC
729 (with-fluids ((%default-port-encoding "UTF-8"))
730 (call-with-input-file builder
731 (lambda (port)
732 (list (port-encoding port)
733 (->bool
734 (string-contains (get-string-all port)
735 "(λ (α) (+ α 1))"))))))))))
9231ef12 736
9a20830e 737(test-assert "build-expression->derivation and derivation-prerequisites"
dd1a5a15 738 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e 739 (any (match-lambda
5cf4b26d 740 (($ <derivation-input> (= derivation-file-name path))
59688fc4 741 (string=? path (derivation-file-name (%guile-for-build)))))
9a20830e 742 (derivation-prerequisites drv))))
d9085c23 743
49c0a8d6 744(test-assert "derivation-prerequisites and valid-derivation-input?"
3681db5d
LC
745 (let* ((a (build-expression->derivation %store "a" '(mkdir %output)))
746 (b (build-expression->derivation %store "b" `(list ,(random-text))))
747 (c (build-expression->derivation %store "c" `(mkdir %output)
748 #:inputs `(("a" ,a) ("b" ,b)))))
49c0a8d6
LC
749 ;; Make sure both A and %BOOTSTRAP-GUILE are built (the latter could have
750 ;; be removed by tests/guix-gc.sh.)
751 (build-derivations %store
752 (list a (package-derivation %store %bootstrap-guile)))
753
3681db5d
LC
754 (match (derivation-prerequisites c
755 (cut valid-derivation-input? %store
756 <>))
5cf4b26d 757 ((($ <derivation-input> (= derivation-file-name file) ("out")))
3681db5d
LC
758 (string=? file (derivation-file-name b)))
759 (x
760 (pk 'fail x #f)))))
761
d9085c23
LC
762(test-assert "build-expression->derivation without inputs"
763 (let* ((builder '(begin
764 (mkdir %output)
765 (call-with-output-file (string-append %output "/test")
766 (lambda (p)
767 (display '(hello guix) p)))))
dd1a5a15 768 (drv (build-expression->derivation %store "goo" builder))
59688fc4 769 (succeeded? (build-derivations %store (list drv))))
d9085c23 770 (and succeeded?
59688fc4 771 (let ((p (derivation->output-path drv)))
d9085c23
LC
772 (equal? '(hello guix)
773 (call-with-input-file (string-append p "/test") read))))))
774
969e678e
LC
775(test-assert "build-expression->derivation and max-silent-time"
776 (let* ((store (let ((s (open-connection)))
777 (set-build-options s #:max-silent-time 1)
778 s))
01e82af5 779 (builder '(begin (sleep 100) (mkdir %output) #t))
dd1a5a15 780 (drv (build-expression->derivation store "silent" builder))
59688fc4 781 (out-path (derivation->output-path drv)))
f9e8a123
LC
782 (guard (c ((store-protocol-error? c)
783 (and (string-contains (store-protocol-error-message c)
6c20d1d0
LC
784 "failed")
785 (not (valid-path? store out-path)))))
786 (build-derivations store (list drv))
787 #f)))
788
789(test-assert "build-expression->derivation and timeout"
790 (let* ((store (let ((s (open-connection)))
791 (set-build-options s #:timeout 1)
792 s))
793 (builder '(begin (sleep 100) (mkdir %output) #t))
794 (drv (build-expression->derivation store "slow" builder))
795 (out-path (derivation->output-path drv)))
f9e8a123
LC
796 (guard (c ((store-protocol-error? c)
797 (and (string-contains (store-protocol-error-message c)
969e678e
LC
798 "failed")
799 (not (valid-path? store out-path)))))
01e82af5
LC
800 (build-derivations store (list drv))
801 #f)))
969e678e 802
f8a9f99c
LC
803(test-assert "build-derivations with specific output"
804 (with-store store
805 (let* ((content (random-text)) ;contents of the output
806 (drv (build-expression->derivation
807 store "substitute-me"
808 `(begin ,content (exit 1)) ;would fail
809 #:outputs '("out" "one" "two")
810 #:guile-for-build
811 (package-derivation store %bootstrap-guile)))
812 (out (derivation->output-path drv)))
813 (with-derivation-substitute drv content
814 (set-build-options store #:use-substitutes? #t
815 #:substitute-urls (%test-substitute-urls))
816 (and (has-substitutes? store out)
817
818 ;; Ask for nothing but the "out" output of DRV.
819 (build-derivations store `((,drv . "out")))
820
7c690a47
LC
821 ;; Synonymous:
822 (build-derivations store (list (derivation-input drv '("out"))))
823
f8a9f99c 824 (valid-path? store out)
7c690a47
LC
825 (equal? (pk 'x content)
826 (pk 'y (call-with-input-file out get-string-all))))))))
f8a9f99c 827
ba04f80e 828(test-assert "build-expression->derivation and derivation-build-plan"
dd1a5a15 829 (let ((drv (build-expression->derivation %store "fail" #f)))
9a20830e
LC
830 ;; The only direct dependency is (%guile-for-build) and it's already
831 ;; built.
ba04f80e 832 (null? (derivation-build-plan %store (derivation-inputs drv)))))
9a20830e 833
ba04f80e 834(test-assert "derivation-build-plan when outputs already present"
5c7f8788 835 (let* ((builder `(begin ,(random-text) (mkdir %output) #t))
dd1a5a15 836 (input-drv (build-expression->derivation %store "input" builder))
5c7f8788 837 (input-path (derivation->output-path input-drv))
dd1a5a15
LC
838 (drv (build-expression->derivation %store "something" builder
839 #:inputs
59688fc4
LC
840 `(("i" ,input-drv))))
841 (output (derivation->output-path drv)))
5c7f8788
LC
842 ;; Assume these things are not already built.
843 (when (or (valid-path? %store input-path)
844 (valid-path? %store output))
845 (error "things already built" input-drv))
784bb1f3 846
ba04f80e
LC
847 (and (lset= equal?
848 (map derivation-file-name
849 (derivation-build-plan %store
850 (list (derivation-input drv))))
851 (list (derivation-file-name input-drv)
852 (derivation-file-name drv)))
784bb1f3
LC
853
854 ;; Build DRV and delete its input.
59688fc4 855 (build-derivations %store (list drv))
784bb1f3
LC
856 (delete-paths %store (list input-path))
857 (not (valid-path? %store input-path))
858
859 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
860 ;; prerequisite to build because DRV itself is already built.
ba04f80e
LC
861 (null? (derivation-build-plan %store
862 (list (derivation-input drv)))))))
784bb1f3 863
ba04f80e 864(test-assert "derivation-build-plan and substitutes"
59688fc4
LC
865 (let* ((store (open-connection))
866 (drv (build-expression->derivation store "prereq-subst"
dd1a5a15 867 (random 1000)))
e6740741 868 (output (derivation->output-path drv)))
dd36b51b 869
1950bf56 870 ;; Make sure substitutes are usable.
24f5aaaf
LC
871 (set-build-options store #:use-substitutes? #t
872 #:substitute-urls (%test-substitute-urls))
1950bf56 873
e6740741
LC
874 (with-derivation-narinfo drv
875 (let-values (((build download)
ba04f80e
LC
876 (derivation-build-plan store
877 (list (derivation-input drv))))
e6740741 878 ((build* download*)
ba04f80e
LC
879 (derivation-build-plan store
880 (list (derivation-input drv))
881 #:substitutable-info
882 (const #f))))
e6740741 883 (and (null? build)
2dc98729 884 (equal? (map substitutable-path download) (list output))
e6740741 885 (null? download*)
ba04f80e 886 (equal? (list drv) build*))))))
dd36b51b 887
ba04f80e 888(test-assert "derivation-build-plan and substitutes, non-substitutable build"
d2d0514b 889 (let* ((store (open-connection))
4a6aeb67 890 (drv (build-expression->derivation store "prereq-no-subst"
d2d0514b 891 (random 1000)
4a6aeb67 892 #:substitutable? #f))
d2d0514b
LC
893 (output (derivation->output-path drv)))
894
895 ;; Make sure substitutes are usable.
24f5aaaf
LC
896 (set-build-options store #:use-substitutes? #t
897 #:substitute-urls (%test-substitute-urls))
d2d0514b
LC
898
899 (with-derivation-narinfo drv
900 (let-values (((build download)
ba04f80e
LC
901 (derivation-build-plan store
902 (list (derivation-input drv)))))
d2d0514b 903 ;; Despite being available as a substitute, DRV will be built locally
4a6aeb67 904 ;; due to #:substitutable? #f.
d2d0514b
LC
905 (and (null? download)
906 (match build
ba04f80e
LC
907 (((= derivation-file-name build))
908 (string=? build (derivation-file-name drv)))))))))
d2d0514b 909
b1510fd8
LC
910(test-assert "derivation-build-plan and substitutes, non-substitutable dep"
911 (with-store store
912 (let* ((drv1 (build-expression->derivation store "prereq-no-subst"
913 (random 1000)
914 #:substitutable? #f))
915 (drv2 (build-expression->derivation store "substitutable"
916 (random 1000)
917 #:inputs `(("dep" ,drv1)))))
918
919 ;; Make sure substitutes are usable.
920 (set-build-options store #:use-substitutes? #t
921 #:substitute-urls (%test-substitute-urls))
922
923 (with-derivation-narinfo drv2
924 (sha256 => (make-bytevector 32 0))
925 (references => (list (derivation->output-path drv1)))
926
927 (let-values (((build download)
928 (derivation-build-plan store
929 (list (derivation-input drv2)))))
930 ;; Although DRV2 is available as a substitute, we must build its
931 ;; dependency, DRV1, due to #:substitutable? #f.
932 (and (match download
933 (((= substitutable-path item))
934 (string=? item (derivation->output-path drv2))))
935 (match build
936 (((= derivation-file-name build))
937 (string=? build (derivation-file-name drv1))))))))))
938
ba04f80e 939(test-assert "derivation-build-plan and substitutes, local build"
4a6aeb67
LC
940 (with-store store
941 (let* ((drv (build-expression->derivation store "prereq-subst-local"
942 (random 1000)
943 #:local-build? #t))
944 (output (derivation->output-path drv)))
945
946 ;; Make sure substitutes are usable.
24f5aaaf
LC
947 (set-build-options store #:use-substitutes? #t
948 #:substitute-urls (%test-substitute-urls))
4a6aeb67
LC
949
950 (with-derivation-narinfo drv
951 (let-values (((build download)
ba04f80e
LC
952 (derivation-build-plan store
953 (list (derivation-input drv)))))
cfc5d398 954 ;; #:local-build? is *not* synonymous with #:substitutable?, so we
4a6aeb67
LC
955 ;; must be able to substitute DRV's output.
956 ;; See <http://bugs.gnu.org/18747>.
957 (and (null? build)
958 (match download
2dc98729 959 (((= substitutable-path item))
4a6aeb67
LC
960 (string=? item (derivation->output-path drv))))))))))
961
ba04f80e 962(test-assert "derivation-build-plan in 'check' mode"
58c08df0
LC
963 (with-store store
964 (let* ((dep (build-expression->derivation store "dep"
965 `(begin ,(random-text)
966 (mkdir %output))))
967 (drv (build-expression->derivation store "to-check"
968 '(mkdir %output)
969 #:inputs `(("dep" ,dep)))))
970 (build-derivations store (list drv))
971 (delete-paths store (list (derivation->output-path dep)))
972
973 ;; In 'check' mode, DEP must be rebuilt.
ba04f80e
LC
974 (and (null? (derivation-build-plan store
975 (list (derivation-input drv))))
976 (lset= equal?
977 (derivation-build-plan store
978 (list (derivation-input drv))
979 #:mode (build-mode check))
980 (list drv dep))))))
58c08df0 981
bdb59b33
LC
982(test-assert "substitution-oracle and #:substitute? #f"
983 (with-store store
984 (let* ((dep (build-expression->derivation store "dep"
985 `(begin ,(random-text)
986 (mkdir %output))))
987 (drv (build-expression->derivation store "not-subst"
988 `(begin ,(random-text)
989 (mkdir %output))
990 #:substitutable? #f
991 #:inputs `(("dep" ,dep))))
992 (query #f))
993 (define (record-substitutable-path-query store paths)
994 (when query
995 (error "already called!" query))
996 (set! query paths)
997 '())
998
ef51ac21 999 (mock ((guix store) substitutable-path-info
bdb59b33
LC
1000 record-substitutable-path-query)
1001
1002 (let ((pred (substitution-oracle store (list drv))))
1003 (pred (derivation->output-path drv))))
1004
1005 ;; Make sure the oracle didn't try to get substitute info for DRV since
1006 ;; DRV is mark as non-substitutable. Assume that GUILE-FOR-BUILD is
1007 ;; already in store and thus not part of QUERY.
1008 (equal? (pk 'query query)
1009 (list (derivation->output-path dep))))))
1010
db393b33
LC
1011(test-assert "build-expression->derivation with expression returning #f"
1012 (let* ((builder '(begin
1013 (mkdir %output)
1014 #f)) ; fail!
dd1a5a15 1015 (drv (build-expression->derivation %store "fail" builder))
59688fc4 1016 (out-path (derivation->output-path drv)))
f9e8a123 1017 (guard (c ((store-protocol-error? c)
db393b33
LC
1018 ;; Note that the output path may exist at this point, but it
1019 ;; is invalid.
31ef99a8 1020 (and (string-match "build .* failed"
f9e8a123 1021 (store-protocol-error-message c))
31ef99a8 1022 (not (valid-path? %store out-path)))))
59688fc4 1023 (build-derivations %store (list drv))
db393b33
LC
1024 #f)))
1025
9bc07f4d
LC
1026(test-assert "build-expression->derivation with two outputs"
1027 (let* ((builder '(begin
1028 (call-with-output-file (assoc-ref %outputs "out")
1029 (lambda (p)
1030 (display '(hello) p)))
1031 (call-with-output-file (assoc-ref %outputs "second")
1032 (lambda (p)
1033 (display '(world) p)))))
dd1a5a15 1034 (drv (build-expression->derivation %store "double" builder
9bc07f4d
LC
1035 #:outputs '("out"
1036 "second")))
59688fc4 1037 (succeeded? (build-derivations %store (list drv))))
9bc07f4d 1038 (and succeeded?
59688fc4
LC
1039 (let ((one (derivation->output-path drv))
1040 (two (derivation->output-path drv "second")))
9bc07f4d
LC
1041 (and (equal? '(hello) (call-with-input-file one read))
1042 (equal? '(world) (call-with-input-file two read)))))))
1043
ad1ebab3 1044(test-skip (if %coreutils 0 1))
d9085c23
LC
1045(test-assert "build-expression->derivation with one input"
1046 (let* ((builder '(call-with-output-file %output
1047 (lambda (p)
1048 (let ((cu (assoc-ref %build-inputs "cu")))
1049 (close 1)
1050 (dup2 (port->fdes p) 1)
1051 (execl (string-append cu "/bin/uname")
1052 "uname" "-a")))))
dd1a5a15
LC
1053 (drv (build-expression->derivation %store "uname" builder
1054 #:inputs
2acb2cb6 1055 `(("cu" ,%coreutils))))
59688fc4 1056 (succeeded? (build-derivations %store (list drv))))
d9085c23 1057 (and succeeded?
59688fc4 1058 (let ((p (derivation->output-path drv)))
d9085c23
LC
1059 (string-contains (call-with-input-file p read-line) "GNU")))))
1060
d9024884
LC
1061(test-assert "build-expression->derivation with modules"
1062 (let* ((builder `(begin
1063 (use-modules (guix build utils))
1064 (let ((out (assoc-ref %outputs "out")))
1065 (mkdir-p (string-append out "/guile/guix/nix"))
1066 #t)))
59688fc4 1067 (drv (build-expression->derivation %store "test-with-modules"
dd1a5a15 1068 builder
d9024884
LC
1069 #:modules
1070 '((guix build utils)))))
59688fc4
LC
1071 (and (build-derivations %store (list drv))
1072 (let* ((p (derivation->output-path drv))
d9024884
LC
1073 (s (stat (string-append p "/guile/guix/nix"))))
1074 (eq? (stat:type s) 'directory)))))
1075
813986ac
LC
1076(test-assert "build-expression->derivation: same fixed-output path"
1077 (let* ((builder1 '(call-with-output-file %output
1078 (lambda (p)
1079 (write "hello" p))))
1080 (builder2 '(call-with-output-file (pk 'difference-here! %output)
1081 (lambda (p)
1082 (write "hello" p))))
1083 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 1084 (input1 (build-expression->derivation %store "fixed" builder1
813986ac
LC
1085 #:hash hash
1086 #:hash-algo 'sha256))
dd1a5a15 1087 (input2 (build-expression->derivation %store "fixed" builder2
813986ac
LC
1088 #:hash hash
1089 #:hash-algo 'sha256))
1090 (succeeded? (build-derivations %store (list input1 input2))))
1091 (and succeeded?
59688fc4
LC
1092 (not (string=? (derivation-file-name input1)
1093 (derivation-file-name input2)))
1094 (string=? (derivation->output-path input1)
1095 (derivation->output-path input2)))))
813986ac 1096
7bdd1f0e
LC
1097(test-assert "build-expression->derivation with a fixed-output input"
1098 (let* ((builder1 '(call-with-output-file %output
1099 (lambda (p)
1100 (write "hello" p))))
1101 (builder2 '(call-with-output-file (pk 'difference-here! %output)
1102 (lambda (p)
1103 (write "hello" p))))
1104 (hash (sha256 (string->utf8 "hello")))
dd1a5a15 1105 (input1 (build-expression->derivation %store "fixed" builder1
7bdd1f0e
LC
1106 #:hash hash
1107 #:hash-algo 'sha256))
dd1a5a15 1108 (input2 (build-expression->derivation %store "fixed" builder2
7bdd1f0e
LC
1109 #:hash hash
1110 #:hash-algo 'sha256))
1111 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
1112 (call-with-output-file %output
1113 (lambda (out)
1114 (format #f "My input is ~a.~%" input)))))
dd1a5a15
LC
1115 (final1 (build-expression->derivation %store "final" builder3
1116 #:inputs
7bdd1f0e 1117 `(("input" ,input1))))
dd1a5a15
LC
1118 (final2 (build-expression->derivation %store "final" builder3
1119 #:inputs
7bdd1f0e 1120 `(("input" ,input2)))))
59688fc4
LC
1121 (and (string=? (derivation->output-path final1)
1122 (derivation->output-path final2))
1123 (string=? (derivation->output-path final1)
1124 (derivation-path->output-path
1125 (derivation-file-name final1)))
7bdd1f0e
LC
1126 (build-derivations %store (list final1 final2)))))
1127
36bbbbd1
LC
1128(test-assert "build-expression->derivation produces recursive fixed-output"
1129 (let* ((builder '(begin
1130 (use-modules (srfi srfi-26))
1131 (mkdir %output)
1132 (chdir %output)
1133 (call-with-output-file "exe"
1134 (cut display "executable" <>))
1135 (chmod "exe" #o777)
1136 (symlink "exe" "symlink")
1137 (mkdir "subdir")))
1138 (drv (build-expression->derivation %store "fixed-rec" builder
1139 #:hash-algo 'sha256
1140 #:hash (base32
1141 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
1142 #:recursive? #t)))
1143 (and (build-derivations %store (list drv))
1144 (let* ((dir (derivation->output-path drv))
1145 (exe (string-append dir "/exe"))
1146 (link (string-append dir "/symlink"))
1147 (subdir (string-append dir "/subdir")))
1148 (and (executable-file? exe)
1149 (string=? "executable"
1150 (call-with-input-file exe get-string-all))
1151 (string=? "exe" (readlink link))
1152 (file-is-directory? subdir))))))
1153
1154(test-assert "build-expression->derivation uses recursive fixed-output"
1155 (let* ((builder '(call-with-output-file %output
1156 (lambda (port)
1157 (display "hello" port))))
1158 (fixed (build-expression->derivation %store "small-fixed-rec"
1159 builder
1160 #:hash-algo 'sha256
1161 #:hash (base32
1162 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
1163 #:recursive? #t))
1164 (in (derivation->output-path fixed))
1165 (builder `(begin
1166 (mkdir %output)
1167 (chdir %output)
1168 (symlink ,in "symlink")))
1169 (drv (build-expression->derivation %store "fixed-rec-user"
1170 builder
1171 #:inputs `(("fixed" ,fixed)))))
1172 (and (build-derivations %store (list drv))
1173 (let ((out (derivation->output-path drv)))
1174 (string=? (readlink (string-append out "/symlink")) in)))))
1175
858e9282 1176(test-assert "build-expression->derivation with #:references-graphs"
9c629a27
LC
1177 (let* ((input (add-text-to-store %store "foo" "hello"
1178 (list %bash %mkdir)))
1179 (builder '(copy-file "input" %output))
858e9282 1180 (drv (build-expression->derivation %store "references-graphs"
dd1a5a15 1181 builder
858e9282 1182 #:references-graphs
9c629a27 1183 `(("input" . ,input))))
59688fc4 1184 (out (derivation->output-path drv)))
9c629a27
LC
1185 (define (deps path . deps)
1186 (let ((count (length deps)))
1187 (string-append path "\n\n" (number->string count) "\n"
1188 (string-join (sort deps string<?) "\n")
1189 (if (zero? count) "" "\n"))))
1190
1191 (and (build-derivations %store (list drv))
1192 (equal? (call-with-input-file out get-string-all)
1193 (string-concatenate
1194 (map cdr
1195 (sort (map (lambda (p d)
1196 (cons p (apply deps p d)))
1197 (list input %bash %mkdir)
1198 (list (list %bash %mkdir)
1199 '() '()))
1200 (lambda (x y)
1201 (match x
1202 ((p1 . _)
1203 (match y
1204 ((p2 . _)
1205 (string<? p1 p2)))))))))))))
1206
8856f409
LC
1207(test-equal "derivation-properties"
1208 (list '() '((type . test)))
1209 (let ((drv1 (build-expression->derivation %store "bar"
1210 '(mkdir %output)))
1211 (drv2 (build-expression->derivation %store "foo"
1212 '(mkdir %output)
1213 #:properties '((type . test)))))
1214 (list (derivation-properties drv1)
1215 (derivation-properties drv2))))
1216
e387ab7c
LC
1217(test-equal "map-derivation"
1218 "hello"
1219 (let* ((joke (package-derivation %store guile-1.8))
1220 (good (package-derivation %store %bootstrap-guile))
1221 (drv1 (build-expression->derivation %store "original-drv1"
e387ab7c 1222 #f ; systematically fail
e387ab7c
LC
1223 #:guile-for-build joke))
1224 (drv2 (build-expression->derivation %store "original-drv2"
e387ab7c
LC
1225 '(call-with-output-file %output
1226 (lambda (p)
dd1a5a15 1227 (display "hello" p)))))
e387ab7c 1228 (drv3 (build-expression->derivation %store "drv-to-remap"
e387ab7c
LC
1229 '(let ((in (assoc-ref
1230 %build-inputs "in")))
1231 (copy-file in %output))
dd1a5a15 1232 #:inputs `(("in" ,drv1))
e387ab7c
LC
1233 #:guile-for-build joke))
1234 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
1235 (,joke . ,good))))
1236 (out (derivation->output-path drv4)))
1237 (and (build-derivations %store (list (pk 'remapped drv4)))
1238 (call-with-input-file out get-string-all))))
1239
a716e36d
LC
1240(test-equal "map-derivation, sources"
1241 "hello"
1242 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
1243 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
1244 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
1245 (drv1 (derivation %store "drv-to-remap"
1246
1247 ;; XXX: This wouldn't work in practice, but if
1248 ;; we append "/bin/bash" then we can't replace
1249 ;; it with the bootstrap bash, which is a
1250 ;; single file.
1251 (derivation->output-path bash-full)
1252
1253 `("-e" ,script1)
9e64302d
LC
1254 #:sources (list script1)
1255 #:inputs
1256 (list (derivation-input bash-full '("out")))))
a716e36d
LC
1257 (drv2 (map-derivation %store drv1
1258 `((,bash-full . ,%bash)
1259 (,script1 . ,script2))))
1260 (out (derivation->output-path drv2)))
1261 (and (build-derivations %store (list (pk 'remapped* drv2)))
1262 (call-with-input-file out get-string-all))))
1263
341c6fdd 1264(test-end)
9b5364a3
LC
1265
1266;; Local Variables:
1267;; eval: (put 'with-http-server 'scheme-indent-function 2)
1268;; End: