1 ;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
2 ;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of Guix.
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.
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.
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/>.
20 (define-module (test-derivations)
21 #:use-module (guix derivations)
22 #:use-module (guix store)
23 #:use-module (guix utils)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-11)
26 #:use-module (srfi srfi-26)
27 #:use-module (srfi srfi-34)
28 #:use-module (srfi srfi-64)
29 #:use-module (rnrs io ports)
30 #:use-module (rnrs bytevectors)
31 #:use-module (ice-9 rdelim)
32 #:use-module (ice-9 regex)
33 #:use-module (ice-9 ftw)
34 #:use-module (ice-9 match))
37 (false-if-exception (open-connection)))
39 (define (directory-contents dir)
40 "Return an alist representing the contents of DIR."
41 (define prefix-len (string-length dir))
42 (sort (file-system-fold (const #t) ; enter?
43 (lambda (path stat result) ; leaf
44 (alist-cons (string-drop path prefix-len)
45 (call-with-input-file path
48 (lambda (path stat result) result) ; down
49 (lambda (path stat result) result) ; up
50 (lambda (path stat result) result) ; skip
51 (lambda (path stat errno result) result) ; error
55 (string<? (car e1) (car e2)))))
57 (test-begin "derivations")
59 (test-assert "parse & export"
60 (let* ((f (search-path %load-path "tests/test.drv"))
61 (b1 (call-with-input-file f get-bytevector-all))
62 (d1 (read-derivation (open-bytevector-input-port b1)))
63 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
64 (d2 (read-derivation (open-bytevector-input-port b2))))
68 (test-skip (if %store 0 4))
70 (test-assert "add-to-store, flat"
71 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
72 (drv (add-to-store %store "flat-test" #t #f "sha256" file)))
73 (and (eq? 'regular (stat:type (stat drv)))
74 (valid-path? %store drv)
75 (equal? (call-with-input-file file get-bytevector-all)
76 (call-with-input-file drv get-bytevector-all)))))
78 (test-assert "add-to-store, recursive"
79 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
80 (drv (add-to-store %store "dir-tree-test" #t #t "sha256" dir)))
81 (and (eq? 'directory (stat:type (stat drv)))
82 (valid-path? %store drv)
83 (equal? (directory-contents dir)
84 (directory-contents drv)))))
86 (test-assert "derivation with no inputs"
87 (let* ((builder (add-text-to-store %store "my-builder.sh"
88 "#!/bin/sh\necho hello, world\n"
90 (drv-path (derivation %store "foo" (%current-system) builder
91 '() '(("HOME" . "/homeless")) '())))
92 (and (store-path? drv-path)
93 (valid-path? %store drv-path))))
95 (test-assert "build derivation with 1 source"
96 (let*-values (((builder)
97 (add-text-to-store %store "my-builder.sh"
98 "echo hello, world > \"$out\"\n"
101 (derivation %store "foo" (%current-system)
102 "/bin/sh" `(,builder)
103 '(("HOME" . "/homeless")
108 (build-derivations %store (list drv-path))))
110 (let ((path (derivation-output-path
111 (assoc-ref (derivation-outputs drv) "out"))))
112 (and (valid-path? %store path)
113 (string=? (call-with-input-file path read-line)
116 (test-assert "fixed-output derivation"
117 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
118 "echo -n hello > $out" '()))
119 (hash (sha256 (string->utf8 "hello")))
120 (drv-path (derivation %store "fixed" (%current-system)
121 "/bin/sh" `(,builder)
123 #:hash hash #:hash-algo 'sha256))
124 (succeeded? (build-derivations %store (list drv-path))))
126 (let ((p (derivation-path->output-path drv-path)))
127 (equal? (string->utf8 "hello")
128 (call-with-input-file p get-bytevector-all))))))
130 (test-assert "multiple-output derivation"
131 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
132 "echo one > $out ; echo two > $second"
134 (drv-path (derivation %store "fixed" (%current-system)
135 "/bin/sh" `(,builder)
136 '(("HOME" . "/homeless")
140 #:outputs '("out" "second")))
141 (succeeded? (build-derivations %store (list drv-path))))
143 (let ((one (derivation-path->output-path drv-path "out"))
144 (two (derivation-path->output-path drv-path "second")))
145 (and (eq? 'one (call-with-input-file one read))
146 (eq? 'two (call-with-input-file two read)))))))
148 (test-assert "multiple-output derivation, non-alphabetic order"
149 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
150 ;; path computation must reorder them first.
151 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
152 "echo one > $out ; echo two > $AAA"
154 (drv-path (derivation %store "fixed" (%current-system)
155 "/bin/sh" `(,builder)
158 #:outputs '("out" "AAA")))
159 (succeeded? (build-derivations %store (list drv-path))))
161 (let ((one (derivation-path->output-path drv-path "out"))
162 (two (derivation-path->output-path drv-path "AAA")))
163 (and (eq? 'one (call-with-input-file one read))
164 (eq? 'two (call-with-input-file two read)))))))
166 (test-assert "user of multiple-output derivation"
167 ;; Check whether specifying several inputs coming from the same
168 ;; multiple-output derivation works.
169 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
170 "echo one > $out ; echo two > $two"
172 (mdrv (derivation %store "multiple-output" (%current-system)
173 "/bin/sh" `(,builder1)
176 #:outputs '("out" "two")))
177 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
180 echo \"($x $y)\" > $out"
182 (udrv (derivation %store "multiple-output-user"
184 "/bin/sh" `(,builder2)
185 `(("one" . ,(derivation-path->output-path
187 ("two" . ,(derivation-path->output-path
190 ;; two occurrences of MDRV:
193 (and (build-derivations %store (list (pk 'udrv udrv)))
194 (let ((p (derivation-path->output-path udrv)))
195 (and (valid-path? %store p)
196 (equal? '(one two) (call-with-input-file p read)))))))
200 (false-if-exception (nixpkgs-derivation "coreutils")))
202 (test-skip (if %coreutils 0 1))
204 (test-assert "build derivation with coreutils"
206 (add-text-to-store %store "build-with-coreutils.sh"
207 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
210 (derivation %store "foo" (%current-system)
211 "/bin/sh" `(,builder)
214 (derivation-path->output-path %coreutils)
219 (build-derivations %store (list drv-path))))
221 (let ((p (derivation-path->output-path drv-path)))
222 (and (valid-path? %store p)
223 (file-exists? (string-append p "/good")))))))
225 (test-skip (if (%guile-for-build) 0 6))
227 (test-assert "build-expression->derivation and derivation-prerequisites"
228 (let-values (((drv-path drv)
229 (build-expression->derivation %store "fail" (%current-system)
232 (($ <derivation-input> path)
233 (string=? path (%guile-for-build))))
234 (derivation-prerequisites drv))))
236 (test-assert "build-expression->derivation without inputs"
237 (let* ((builder '(begin
239 (call-with-output-file (string-append %output "/test")
241 (display '(hello guix) p)))))
242 (drv-path (build-expression->derivation %store "goo" (%current-system)
244 (succeeded? (build-derivations %store (list drv-path))))
246 (let ((p (derivation-path->output-path drv-path)))
247 (equal? '(hello guix)
248 (call-with-input-file (string-append p "/test") read))))))
250 (test-assert "build-expression->derivation and derivation-prerequisites-to-build"
251 (let-values (((drv-path drv)
252 (build-expression->derivation %store "fail" (%current-system)
254 ;; The only direct dependency is (%guile-for-build) and it's already
256 (null? (derivation-prerequisites-to-build %store drv))))
258 (test-assert "build-expression->derivation with expression returning #f"
259 (let* ((builder '(begin
262 (drv-path (build-expression->derivation %store "fail" (%current-system)
264 (out-path (derivation-path->output-path drv-path)))
265 (guard (c ((nix-protocol-error? c)
266 ;; Note that the output path may exist at this point, but it
268 (and (string-match "build .* failed"
269 (nix-protocol-error-message c))
270 (not (valid-path? %store out-path)))))
271 (build-derivations %store (list drv-path))
274 (test-assert "build-expression->derivation with two outputs"
275 (let* ((builder '(begin
276 (call-with-output-file (assoc-ref %outputs "out")
278 (display '(hello) p)))
279 (call-with-output-file (assoc-ref %outputs "second")
281 (display '(world) p)))))
282 (drv-path (build-expression->derivation %store "double"
287 (succeeded? (build-derivations %store (list drv-path))))
289 (let ((one (derivation-path->output-path drv-path))
290 (two (derivation-path->output-path drv-path "second")))
291 (and (equal? '(hello) (call-with-input-file one read))
292 (equal? '(world) (call-with-input-file two read)))))))
294 (test-assert "build-expression->derivation with one input"
295 (let* ((builder '(call-with-output-file %output
297 (let ((cu (assoc-ref %build-inputs "cu")))
299 (dup2 (port->fdes p) 1)
300 (execl (string-append cu "/bin/uname")
302 (drv-path (build-expression->derivation %store "uname" (%current-system)
304 `(("cu" ,%coreutils))))
305 (succeeded? (build-derivations %store (list drv-path))))
307 (let ((p (derivation-path->output-path drv-path)))
308 (string-contains (call-with-input-file p read-line) "GNU")))))
310 (test-assert "imported-files"
311 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
312 ("a/b/c" . ,(search-path %load-path
313 "guix/derivations.scm"))
314 ("p/q" . ,(search-path %load-path "guix.scm"))
315 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
316 (drv-path (imported-files %store files)))
317 (and (build-derivations %store (list drv-path))
318 (let ((dir (derivation-path->output-path drv-path)))
321 (equal? (call-with-input-file (string-append dir "/" path)
323 (call-with-input-file source
324 get-bytevector-all))))
327 (test-skip (if (false-if-exception (getaddrinfo "ftp.gnu.org" "http"))
331 (test-assert "build-expression->derivation for fixed-output derivation"
332 (let* ((url "http://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz")
335 (use-modules (web client) (web uri)
336 (rnrs io ports) (srfi srfi-11))
337 (let-values (((resp bv)
338 (http-get (string->uri ,url) #:decode-body? #f)))
339 (call-with-output-file %output
341 (put-bytevector p bv))))))
342 (drv-path (build-expression->derivation
343 %store "hello-2.8.tar.gz" (%current-system) builder '()
344 #:hash (nix-base32-string->bytevector
345 "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6")
346 #:hash-algo 'sha256))
347 (succeeded? (build-derivations %store (list drv-path))))
349 (file-exists? (derivation-path->output-path drv-path)))))
354 (exit (= (test-runner-fail-count (test-runner-current)) 0))
357 ;;; eval: (put 'test-assert 'scheme-indent-function 1)
358 ;;; eval: (put 'guard 'scheme-indent-function 1)