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