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