Debian Codegraph @

tests/derivations.scm

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