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