release.nix: Build the daemon.
[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
18633d4f 293 (or (package-derivation %store %bootstrap-coreutils&co)
8f3ecbd7 294 (nixpkgs-derivation "coreutils"))))
de4c3f26
LC
295
296(test-skip (if %coreutils 0 1))
297
298(test-assert "build derivation with coreutils"
299 (let* ((builder
300 (add-text-to-store %store "build-with-coreutils.sh"
301 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
302 '()))
303 (drv-path
98090557 304 (derivation %store "foo" (%current-system)
de4c3f26
LC
305 "/bin/sh" `(,builder)
306 `(("PATH" .
307 ,(string-append
308 (derivation-path->output-path %coreutils)
309 "/bin")))
310 `((,builder)
311 (,%coreutils))))
312 (succeeded?
313 (build-derivations %store (list drv-path))))
314 (and succeeded?
315 (let ((p (derivation-path->output-path drv-path)))
31ef99a8
LC
316 (and (valid-path? %store p)
317 (file-exists? (string-append p "/good")))))))
de4c3f26 318
813986ac 319(test-skip (if (%guile-for-build) 0 7))
9a20830e
LC
320
321(test-assert "build-expression->derivation and derivation-prerequisites"
322 (let-values (((drv-path drv)
323 (build-expression->derivation %store "fail" (%current-system)
324 #f '())))
325 (any (match-lambda
326 (($ <derivation-input> path)
327 (string=? path (%guile-for-build))))
328 (derivation-prerequisites drv))))
d9085c23
LC
329
330(test-assert "build-expression->derivation without inputs"
331 (let* ((builder '(begin
332 (mkdir %output)
333 (call-with-output-file (string-append %output "/test")
334 (lambda (p)
335 (display '(hello guix) p)))))
98090557 336 (drv-path (build-expression->derivation %store "goo" (%current-system)
d9085c23
LC
337 builder '()))
338 (succeeded? (build-derivations %store (list drv-path))))
339 (and succeeded?
340 (let ((p (derivation-path->output-path drv-path)))
341 (equal? '(hello guix)
342 (call-with-input-file (string-append p "/test") read))))))
343
9a20830e
LC
344(test-assert "build-expression->derivation and derivation-prerequisites-to-build"
345 (let-values (((drv-path drv)
346 (build-expression->derivation %store "fail" (%current-system)
347 #f '())))
348 ;; The only direct dependency is (%guile-for-build) and it's already
349 ;; built.
350 (null? (derivation-prerequisites-to-build %store drv))))
351
db393b33
LC
352(test-assert "build-expression->derivation with expression returning #f"
353 (let* ((builder '(begin
354 (mkdir %output)
355 #f)) ; fail!
356 (drv-path (build-expression->derivation %store "fail" (%current-system)
31ef99a8
LC
357 builder '()))
358 (out-path (derivation-path->output-path drv-path)))
db393b33
LC
359 (guard (c ((nix-protocol-error? c)
360 ;; Note that the output path may exist at this point, but it
361 ;; is invalid.
31ef99a8
LC
362 (and (string-match "build .* failed"
363 (nix-protocol-error-message c))
364 (not (valid-path? %store out-path)))))
db393b33
LC
365 (build-derivations %store (list drv-path))
366 #f)))
367
9bc07f4d
LC
368(test-assert "build-expression->derivation with two outputs"
369 (let* ((builder '(begin
370 (call-with-output-file (assoc-ref %outputs "out")
371 (lambda (p)
372 (display '(hello) p)))
373 (call-with-output-file (assoc-ref %outputs "second")
374 (lambda (p)
375 (display '(world) p)))))
376 (drv-path (build-expression->derivation %store "double"
98090557 377 (%current-system)
9bc07f4d
LC
378 builder '()
379 #:outputs '("out"
380 "second")))
381 (succeeded? (build-derivations %store (list drv-path))))
382 (and succeeded?
383 (let ((one (derivation-path->output-path drv-path))
384 (two (derivation-path->output-path drv-path "second")))
385 (and (equal? '(hello) (call-with-input-file one read))
386 (equal? '(world) (call-with-input-file two read)))))))
387
d9085c23
LC
388(test-assert "build-expression->derivation with one input"
389 (let* ((builder '(call-with-output-file %output
390 (lambda (p)
391 (let ((cu (assoc-ref %build-inputs "cu")))
392 (close 1)
393 (dup2 (port->fdes p) 1)
394 (execl (string-append cu "/bin/uname")
395 "uname" "-a")))))
98090557 396 (drv-path (build-expression->derivation %store "uname" (%current-system)
d9085c23 397 builder
2acb2cb6 398 `(("cu" ,%coreutils))))
d9085c23
LC
399 (succeeded? (build-derivations %store (list drv-path))))
400 (and succeeded?
401 (let ((p (derivation-path->output-path drv-path)))
402 (string-contains (call-with-input-file p read-line) "GNU")))))
403
99634e3f
LC
404(test-assert "imported-files"
405 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
406 ("a/b/c" . ,(search-path %load-path
407 "guix/derivations.scm"))
224f7ad6
LC
408 ("p/q" . ,(search-path %load-path "guix.scm"))
409 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
99634e3f
LC
410 (drv-path (imported-files %store files)))
411 (and (build-derivations %store (list drv-path))
412 (let ((dir (derivation-path->output-path drv-path)))
413 (every (match-lambda
414 ((path . source)
415 (equal? (call-with-input-file (string-append dir "/" path)
416 get-bytevector-all)
417 (call-with-input-file source
418 get-bytevector-all))))
419 files)))))
420
d9024884
LC
421(test-assert "build-expression->derivation with modules"
422 (let* ((builder `(begin
423 (use-modules (guix build utils))
424 (let ((out (assoc-ref %outputs "out")))
425 (mkdir-p (string-append out "/guile/guix/nix"))
426 #t)))
427 (drv-path (build-expression->derivation %store
428 "test-with-modules"
429 (%current-system)
430 builder '()
431 #:modules
432 '((guix build utils)))))
433 (and (build-derivations %store (list drv-path))
434 (let* ((p (derivation-path->output-path drv-path))
435 (s (stat (string-append p "/guile/guix/nix"))))
436 (eq? (stat:type s) 'directory)))))
437
813986ac
LC
438(test-assert "build-expression->derivation: same fixed-output path"
439 (let* ((builder1 '(call-with-output-file %output
440 (lambda (p)
441 (write "hello" p))))
442 (builder2 '(call-with-output-file (pk 'difference-here! %output)
443 (lambda (p)
444 (write "hello" p))))
445 (hash (sha256 (string->utf8 "hello")))
446 (input1 (build-expression->derivation %store "fixed"
447 (%current-system)
448 builder1 '()
449 #:hash hash
450 #:hash-algo 'sha256))
451 (input2 (build-expression->derivation %store "fixed"
452 (%current-system)
453 builder2 '()
454 #:hash hash
455 #:hash-algo 'sha256))
456 (succeeded? (build-derivations %store (list input1 input2))))
457 (and succeeded?
458 (not (string=? input1 input2))
459 (string=? (derivation-path->output-path input1)
460 (derivation-path->output-path input2)))))
461
7bdd1f0e
LC
462(test-assert "build-expression->derivation with a fixed-output input"
463 (let* ((builder1 '(call-with-output-file %output
464 (lambda (p)
465 (write "hello" p))))
466 (builder2 '(call-with-output-file (pk 'difference-here! %output)
467 (lambda (p)
468 (write "hello" p))))
469 (hash (sha256 (string->utf8 "hello")))
470 (input1 (build-expression->derivation %store "fixed"
471 (%current-system)
472 builder1 '()
473 #:hash hash
474 #:hash-algo 'sha256))
475 (input2 (build-expression->derivation %store "fixed"
476 (%current-system)
477 builder2 '()
478 #:hash hash
479 #:hash-algo 'sha256))
480 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
481 (call-with-output-file %output
482 (lambda (out)
483 (format #f "My input is ~a.~%" input)))))
484 (final1 (build-expression->derivation %store "final"
485 (%current-system)
486 builder3
487 `(("input" ,input1))))
488 (final2 (build-expression->derivation %store "final"
489 (%current-system)
490 builder3
491 `(("input" ,input2)))))
492 (and (string=? (derivation-path->output-path final1)
493 (derivation-path->output-path final2))
494 (build-derivations %store (list final1 final2)))))
495
341c6fdd
LC
496(test-end)
497
498\f
499(exit (= (test-runner-fail-count (test-runner-current)) 0))
500
501;;; Local Variables:
502;;; eval: (put 'test-assert 'scheme-indent-function 1)
db393b33 503;;; eval: (put 'guard 'scheme-indent-function 1)
341c6fdd 504;;; End: