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