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