distro: sed: Patch references to /bin/sh in the test suite.
[jackhill/guix/guix.git] / tests / derivations.scm
CommitLineData
341c6fdd
LC
1;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
2;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of Guix.
5;;;
6;;; 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;;; 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 Guix. If not, see <http://www.gnu.org/licenses/>.
18
19
20(define-module (test-derivations)
21 #:use-module (guix derivations)
26bbbb95 22 #:use-module (guix store)
de4c3f26 23 #:use-module (guix utils)
ddc29a78 24 #:use-module (guix base32)
b272c474 25 #:use-module ((guix packages) #:select (package-derivation))
18633d4f 26 #:use-module (distro packages bootstrap)
b37eb5ed 27 #:use-module (srfi srfi-1)
fb3eec83 28 #:use-module (srfi srfi-11)
341c6fdd 29 #:use-module (srfi srfi-26)
99634e3f 30 #:use-module (srfi srfi-34)
341c6fdd 31 #:use-module (srfi srfi-64)
fb3eec83 32 #:use-module (rnrs io ports)
749c6567 33 #:use-module (rnrs bytevectors)
b37eb5ed 34 #:use-module (ice-9 rdelim)
db393b33 35 #:use-module (ice-9 regex)
99634e3f
LC
36 #:use-module (ice-9 ftw)
37 #:use-module (ice-9 match))
341c6fdd 38
26bbbb95
LC
39(define %store
40 (false-if-exception (open-connection)))
41
b272c474 42(when %store
81dbd783
LC
43 ;; Make sure we build everything by ourselves.
44 (set-build-options %store #:use-substitutes? #f)
45
b272c474
LC
46 ;; By default, use %BOOTSTRAP-GUILE for the current system.
47 (let ((drv (package-derivation %store %bootstrap-guile)))
48 (%guile-for-build drv)))
49
b37eb5ed
LC
50(define (directory-contents dir)
51 "Return an alist representing the contents of DIR."
52 (define prefix-len (string-length dir))
53 (sort (file-system-fold (const #t) ; enter?
54 (lambda (path stat result) ; leaf
55 (alist-cons (string-drop path prefix-len)
56 (call-with-input-file path
57 get-bytevector-all)
58 result))
59 (lambda (path stat result) result) ; down
60 (lambda (path stat result) result) ; up
61 (lambda (path stat result) result) ; skip
62 (lambda (path stat errno result) result) ; error
63 '()
64 dir)
65 (lambda (e1 e2)
66 (string<? (car e1) (car e2)))))
67
341c6fdd
LC
68(test-begin "derivations")
69
70(test-assert "parse & export"
33594aa4
LC
71 (let* ((f (search-path %load-path "tests/test.drv"))
72 (b1 (call-with-input-file f get-bytevector-all))
341c6fdd
LC
73 (d1 (read-derivation (open-bytevector-input-port b1)))
74 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
75 (d2 (read-derivation (open-bytevector-input-port b2))))
76 (and (equal? b1 b2)
77 (equal? d1 d2))))
78
29833b26 79(test-skip (if %store 0 11))
b37eb5ed 80
d1b1c424
LC
81(test-assert "add-to-store, flat"
82 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
83 (drv (add-to-store %store "flat-test" #t #f "sha256" file)))
84 (and (eq? 'regular (stat:type (stat drv)))
31ef99a8 85 (valid-path? %store drv)
d1b1c424
LC
86 (equal? (call-with-input-file file get-bytevector-all)
87 (call-with-input-file drv get-bytevector-all)))))
88
b37eb5ed
LC
89(test-assert "add-to-store, recursive"
90 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
91 (drv (add-to-store %store "dir-tree-test" #t #t "sha256" dir)))
92 (and (eq? 'directory (stat:type (stat drv)))
31ef99a8 93 (valid-path? %store drv)
b37eb5ed
LC
94 (equal? (directory-contents dir)
95 (directory-contents drv)))))
26bbbb95
LC
96
97(test-assert "derivation with no inputs"
31ef99a8
LC
98 (let* ((builder (add-text-to-store %store "my-builder.sh"
99 "#!/bin/sh\necho hello, world\n"
100 '()))
101 (drv-path (derivation %store "foo" (%current-system) builder
102 '() '(("HOME" . "/homeless")) '())))
103 (and (store-path? drv-path)
104 (valid-path? %store drv-path))))
26bbbb95 105
fb3eec83
LC
106(test-assert "build derivation with 1 source"
107 (let*-values (((builder)
108 (add-text-to-store %store "my-builder.sh"
de4c3f26 109 "echo hello, world > \"$out\"\n"
fb3eec83
LC
110 '()))
111 ((drv-path drv)
98090557 112 (derivation %store "foo" (%current-system)
fb3eec83 113 "/bin/sh" `(,builder)
af7f9e5f
LC
114 '(("HOME" . "/homeless")
115 ("zzz" . "Z!")
116 ("AAA" . "A!"))
fb3eec83
LC
117 `((,builder))))
118 ((succeeded?)
119 (build-derivations %store (list drv-path))))
120 (and succeeded?
121 (let ((path (derivation-output-path
122 (assoc-ref (derivation-outputs drv) "out"))))
31ef99a8
LC
123 (and (valid-path? %store path)
124 (string=? (call-with-input-file path read-line)
125 "hello, world"))))))
fb3eec83 126
860a6f1a
LC
127(test-assert "derivation with local file as input"
128 (let* ((builder (add-text-to-store
129 %store "my-builder.sh"
130 "(while read line ; do echo $line ; done) < $in > $out"
131 '()))
132 (input (search-path %load-path "ice-9/boot-9.scm"))
133 (drv-path (derivation %store "derivation-with-input-file"
134 (%current-system)
135 "/bin/sh" `(,builder)
136 `(("in"
137 ;; Cheat to pass the actual file
138 ;; name to the builder.
139 . ,(add-to-store %store
140 (basename input)
141 #t #t "sha256"
142 input)))
143 `((,builder)
144 (,input))))) ; ← local file name
145 (and (build-derivations %store (list drv-path))
146 (let ((p (derivation-path->output-path drv-path)))
147 (and (call-with-input-file p get-bytevector-all)
148 (call-with-input-file input get-bytevector-all))))))
149
749c6567
LC
150(test-assert "fixed-output derivation"
151 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
152 "echo -n hello > $out" '()))
153 (hash (sha256 (string->utf8 "hello")))
98090557 154 (drv-path (derivation %store "fixed" (%current-system)
749c6567 155 "/bin/sh" `(,builder)
813986ac
LC
156 '()
157 `((,builder)) ; optional
749c6567
LC
158 #:hash hash #:hash-algo 'sha256))
159 (succeeded? (build-derivations %store (list drv-path))))
160 (and succeeded?
161 (let ((p (derivation-path->output-path drv-path)))
82058eff
LC
162 (and (equal? (string->utf8 "hello")
163 (call-with-input-file p get-bytevector-all))
164 (bytevector? (query-path-hash %store p)))))))
749c6567 165
813986ac
LC
166(test-assert "fixed-output derivation: output paths are equal"
167 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
168 "echo -n hello > $out" '()))
169 (builder2 (add-text-to-store %store "fixed-builder2.sh"
170 "echo hey; echo -n hello > $out" '()))
171 (hash (sha256 (string->utf8 "hello")))
172 (drv-path1 (derivation %store "fixed" (%current-system)
173 "/bin/sh" `(,builder1)
174 '() `()
175 #:hash hash #:hash-algo 'sha256))
176 (drv-path2 (derivation %store "fixed" (%current-system)
177 "/bin/sh" `(,builder2)
178 '() `()
179 #:hash hash #:hash-algo 'sha256))
180 (succeeded? (build-derivations %store
181 (list drv-path1 drv-path2))))
182 (and succeeded?
183 (equal? (derivation-path->output-path drv-path1)
184 (derivation-path->output-path drv-path2)))))
185
186(test-assert "derivation with a fixed-output input"
187 ;; A derivation D using a fixed-output derivation F doesn't has the same
188 ;; output path when passed F or F', as long as F and F' have the same output
189 ;; path.
190 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
191 "echo -n hello > $out" '()))
192 (builder2 (add-text-to-store %store "fixed-builder2.sh"
193 "echo hey; echo -n hello > $out" '()))
194 (hash (sha256 (string->utf8 "hello")))
195 (fixed1 (derivation %store "fixed" (%current-system)
196 "/bin/sh" `(,builder1)
197 '() `()
198 #:hash hash #:hash-algo 'sha256))
199 (fixed2 (derivation %store "fixed" (%current-system)
200 "/bin/sh" `(,builder2)
201 '() `()
202 #:hash hash #:hash-algo 'sha256))
203 (fixed-out (derivation-path->output-path fixed1))
204 (builder3 (add-text-to-store
205 %store "final-builder.sh"
206 ;; Use Bash hackery to avoid Coreutils.
207 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
208 (final1 (derivation %store "final" (%current-system)
209 "/bin/sh" `(,builder3)
210 `(("in" . ,fixed-out))
211 `((,builder3) (,fixed1))))
212 (final2 (derivation %store "final" (%current-system)
213 "/bin/sh" `(,builder3)
214 `(("in" . ,fixed-out))
215 `((,builder3) (,fixed2))))
216 (succeeded? (build-derivations %store
217 (list final1 final2))))
218 (and succeeded?
219 (equal? (derivation-path->output-path final1)
220 (derivation-path->output-path final2)))))
221
7946c4e7
LC
222(test-assert "multiple-output derivation"
223 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
224 "echo one > $out ; echo two > $second"
225 '()))
98090557 226 (drv-path (derivation %store "fixed" (%current-system)
7946c4e7
LC
227 "/bin/sh" `(,builder)
228 '(("HOME" . "/homeless")
229 ("zzz" . "Z!")
230 ("AAA" . "A!"))
231 `((,builder))
232 #:outputs '("out" "second")))
233 (succeeded? (build-derivations %store (list drv-path))))
234 (and succeeded?
235 (let ((one (derivation-path->output-path drv-path "out"))
236 (two (derivation-path->output-path drv-path "second")))
237 (and (eq? 'one (call-with-input-file one read))
238 (eq? 'two (call-with-input-file two read)))))))
239
4b1786aa
LC
240(test-assert "multiple-output derivation, non-alphabetic order"
241 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
242 ;; path computation must reorder them first.
243 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
244 "echo one > $out ; echo two > $AAA"
245 '()))
246 (drv-path (derivation %store "fixed" (%current-system)
247 "/bin/sh" `(,builder)
248 '()
249 `((,builder))
250 #:outputs '("out" "AAA")))
251 (succeeded? (build-derivations %store (list drv-path))))
252 (and succeeded?
253 (let ((one (derivation-path->output-path drv-path "out"))
254 (two (derivation-path->output-path drv-path "AAA")))
255 (and (eq? 'one (call-with-input-file one read))
256 (eq? 'two (call-with-input-file two read)))))))
257
d66ac374
LC
258(test-assert "user of multiple-output derivation"
259 ;; Check whether specifying several inputs coming from the same
260 ;; multiple-output derivation works.
261 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
262 "echo one > $out ; echo two > $two"
263 '()))
264 (mdrv (derivation %store "multiple-output" (%current-system)
265 "/bin/sh" `(,builder1)
266 '()
267 `((,builder1))
268 #:outputs '("out" "two")))
269 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
270 "read x < $one;
271 read y < $two;
272 echo \"($x $y)\" > $out"
273 '()))
274 (udrv (derivation %store "multiple-output-user"
275 (%current-system)
276 "/bin/sh" `(,builder2)
277 `(("one" . ,(derivation-path->output-path
278 mdrv "out"))
279 ("two" . ,(derivation-path->output-path
280 mdrv "two")))
281 `((,builder2)
282 ;; two occurrences of MDRV:
283 (,mdrv)
284 (,mdrv "two")))))
285 (and (build-derivations %store (list (pk 'udrv udrv)))
286 (let ((p (derivation-path->output-path udrv)))
287 (and (valid-path? %store p)
288 (equal? '(one two) (call-with-input-file p read)))))))
289
de4c3f26
LC
290\f
291(define %coreutils
8f3ecbd7 292 (false-if-exception
ad1ebab3
LC
293 (and (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)
294 (or (package-derivation %store %bootstrap-coreutils&co)
295 (nixpkgs-derivation "coreutils")))))
de4c3f26
LC
296
297(test-skip (if %coreutils 0 1))
298
299(test-assert "build derivation with coreutils"
300 (let* ((builder
301 (add-text-to-store %store "build-with-coreutils.sh"
302 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
303 '()))
304 (drv-path
98090557 305 (derivation %store "foo" (%current-system)
de4c3f26
LC
306 "/bin/sh" `(,builder)
307 `(("PATH" .
308 ,(string-append
309 (derivation-path->output-path %coreutils)
310 "/bin")))
311 `((,builder)
312 (,%coreutils))))
313 (succeeded?
314 (build-derivations %store (list drv-path))))
315 (and succeeded?
316 (let ((p (derivation-path->output-path drv-path)))
31ef99a8
LC
317 (and (valid-path? %store p)
318 (file-exists? (string-append p "/good")))))))
de4c3f26 319
813986ac 320(test-skip (if (%guile-for-build) 0 7))
9a20830e
LC
321
322(test-assert "build-expression->derivation and derivation-prerequisites"
323 (let-values (((drv-path drv)
324 (build-expression->derivation %store "fail" (%current-system)
325 #f '())))
326 (any (match-lambda
327 (($ <derivation-input> path)
328 (string=? path (%guile-for-build))))
329 (derivation-prerequisites drv))))
d9085c23
LC
330
331(test-assert "build-expression->derivation without inputs"
332 (let* ((builder '(begin
333 (mkdir %output)
334 (call-with-output-file (string-append %output "/test")
335 (lambda (p)
336 (display '(hello guix) p)))))
98090557 337 (drv-path (build-expression->derivation %store "goo" (%current-system)
d9085c23
LC
338 builder '()))
339 (succeeded? (build-derivations %store (list drv-path))))
340 (and succeeded?
341 (let ((p (derivation-path->output-path drv-path)))
342 (equal? '(hello guix)
343 (call-with-input-file (string-append p "/test") read))))))
344
9a20830e
LC
345(test-assert "build-expression->derivation and derivation-prerequisites-to-build"
346 (let-values (((drv-path drv)
347 (build-expression->derivation %store "fail" (%current-system)
348 #f '())))
349 ;; The only direct dependency is (%guile-for-build) and it's already
350 ;; built.
351 (null? (derivation-prerequisites-to-build %store drv))))
352
db393b33
LC
353(test-assert "build-expression->derivation with expression returning #f"
354 (let* ((builder '(begin
355 (mkdir %output)
356 #f)) ; fail!
357 (drv-path (build-expression->derivation %store "fail" (%current-system)
31ef99a8
LC
358 builder '()))
359 (out-path (derivation-path->output-path drv-path)))
db393b33
LC
360 (guard (c ((nix-protocol-error? c)
361 ;; Note that the output path may exist at this point, but it
362 ;; is invalid.
31ef99a8
LC
363 (and (string-match "build .* failed"
364 (nix-protocol-error-message c))
365 (not (valid-path? %store out-path)))))
db393b33
LC
366 (build-derivations %store (list drv-path))
367 #f)))
368
9bc07f4d
LC
369(test-assert "build-expression->derivation with two outputs"
370 (let* ((builder '(begin
371 (call-with-output-file (assoc-ref %outputs "out")
372 (lambda (p)
373 (display '(hello) p)))
374 (call-with-output-file (assoc-ref %outputs "second")
375 (lambda (p)
376 (display '(world) p)))))
377 (drv-path (build-expression->derivation %store "double"
98090557 378 (%current-system)
9bc07f4d
LC
379 builder '()
380 #:outputs '("out"
381 "second")))
382 (succeeded? (build-derivations %store (list drv-path))))
383 (and succeeded?
384 (let ((one (derivation-path->output-path drv-path))
385 (two (derivation-path->output-path drv-path "second")))
386 (and (equal? '(hello) (call-with-input-file one read))
387 (equal? '(world) (call-with-input-file two read)))))))
388
ad1ebab3 389(test-skip (if %coreutils 0 1))
d9085c23
LC
390(test-assert "build-expression->derivation with one input"
391 (let* ((builder '(call-with-output-file %output
392 (lambda (p)
393 (let ((cu (assoc-ref %build-inputs "cu")))
394 (close 1)
395 (dup2 (port->fdes p) 1)
396 (execl (string-append cu "/bin/uname")
397 "uname" "-a")))))
98090557 398 (drv-path (build-expression->derivation %store "uname" (%current-system)
d9085c23 399 builder
2acb2cb6 400 `(("cu" ,%coreutils))))
d9085c23
LC
401 (succeeded? (build-derivations %store (list drv-path))))
402 (and succeeded?
403 (let ((p (derivation-path->output-path drv-path)))
404 (string-contains (call-with-input-file p read-line) "GNU")))))
405
99634e3f
LC
406(test-assert "imported-files"
407 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
408 ("a/b/c" . ,(search-path %load-path
409 "guix/derivations.scm"))
224f7ad6
LC
410 ("p/q" . ,(search-path %load-path "guix.scm"))
411 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
99634e3f
LC
412 (drv-path (imported-files %store files)))
413 (and (build-derivations %store (list drv-path))
414 (let ((dir (derivation-path->output-path drv-path)))
415 (every (match-lambda
416 ((path . source)
417 (equal? (call-with-input-file (string-append dir "/" path)
418 get-bytevector-all)
419 (call-with-input-file source
420 get-bytevector-all))))
421 files)))))
422
d9024884
LC
423(test-assert "build-expression->derivation with modules"
424 (let* ((builder `(begin
425 (use-modules (guix build utils))
426 (let ((out (assoc-ref %outputs "out")))
427 (mkdir-p (string-append out "/guile/guix/nix"))
428 #t)))
429 (drv-path (build-expression->derivation %store
430 "test-with-modules"
431 (%current-system)
432 builder '()
433 #:modules
434 '((guix build utils)))))
435 (and (build-derivations %store (list drv-path))
436 (let* ((p (derivation-path->output-path drv-path))
437 (s (stat (string-append p "/guile/guix/nix"))))
438 (eq? (stat:type s) 'directory)))))
439
813986ac
LC
440(test-assert "build-expression->derivation: same fixed-output path"
441 (let* ((builder1 '(call-with-output-file %output
442 (lambda (p)
443 (write "hello" p))))
444 (builder2 '(call-with-output-file (pk 'difference-here! %output)
445 (lambda (p)
446 (write "hello" p))))
447 (hash (sha256 (string->utf8 "hello")))
448 (input1 (build-expression->derivation %store "fixed"
449 (%current-system)
450 builder1 '()
451 #:hash hash
452 #:hash-algo 'sha256))
453 (input2 (build-expression->derivation %store "fixed"
454 (%current-system)
455 builder2 '()
456 #:hash hash
457 #:hash-algo 'sha256))
458 (succeeded? (build-derivations %store (list input1 input2))))
459 (and succeeded?
460 (not (string=? input1 input2))
461 (string=? (derivation-path->output-path input1)
462 (derivation-path->output-path input2)))))
463
7bdd1f0e
LC
464(test-assert "build-expression->derivation with a fixed-output input"
465 (let* ((builder1 '(call-with-output-file %output
466 (lambda (p)
467 (write "hello" p))))
468 (builder2 '(call-with-output-file (pk 'difference-here! %output)
469 (lambda (p)
470 (write "hello" p))))
471 (hash (sha256 (string->utf8 "hello")))
472 (input1 (build-expression->derivation %store "fixed"
473 (%current-system)
474 builder1 '()
475 #:hash hash
476 #:hash-algo 'sha256))
477 (input2 (build-expression->derivation %store "fixed"
478 (%current-system)
479 builder2 '()
480 #:hash hash
481 #:hash-algo 'sha256))
482 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
483 (call-with-output-file %output
484 (lambda (out)
485 (format #f "My input is ~a.~%" input)))))
486 (final1 (build-expression->derivation %store "final"
487 (%current-system)
488 builder3
489 `(("input" ,input1))))
490 (final2 (build-expression->derivation %store "final"
491 (%current-system)
492 builder3
493 `(("input" ,input2)))))
494 (and (string=? (derivation-path->output-path final1)
495 (derivation-path->output-path final2))
496 (build-derivations %store (list final1 final2)))))
497
341c6fdd
LC
498(test-end)
499
500\f
501(exit (= (test-runner-fail-count (test-runner-current)) 0))
502
503;;; Local Variables:
504;;; eval: (put 'test-assert 'scheme-indent-function 1)
db393b33 505;;; eval: (put 'guard 'scheme-indent-function 1)
341c6fdd 506;;; End: