Augment `build-expression->derivation' with #:modules; add `http-fetch'.
[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)
b37eb5ed 24 #:use-module (srfi srfi-1)
fb3eec83 25 #:use-module (srfi srfi-11)
341c6fdd 26 #:use-module (srfi srfi-26)
99634e3f 27 #:use-module (srfi srfi-34)
341c6fdd 28 #:use-module (srfi srfi-64)
fb3eec83 29 #:use-module (rnrs io ports)
749c6567 30 #:use-module (rnrs bytevectors)
b37eb5ed 31 #:use-module (ice-9 rdelim)
99634e3f
LC
32 #:use-module (ice-9 ftw)
33 #:use-module (ice-9 match))
341c6fdd 34
26bbbb95
LC
35(define %store
36 (false-if-exception (open-connection)))
37
b37eb5ed
LC
38(define (directory-contents dir)
39 "Return an alist representing the contents of DIR."
40 (define prefix-len (string-length dir))
41 (sort (file-system-fold (const #t) ; enter?
42 (lambda (path stat result) ; leaf
43 (alist-cons (string-drop path prefix-len)
44 (call-with-input-file path
45 get-bytevector-all)
46 result))
47 (lambda (path stat result) result) ; down
48 (lambda (path stat result) result) ; up
49 (lambda (path stat result) result) ; skip
50 (lambda (path stat errno result) result) ; error
51 '()
52 dir)
53 (lambda (e1 e2)
54 (string<? (car e1) (car e2)))))
55
341c6fdd
LC
56(test-begin "derivations")
57
58(test-assert "parse & export"
33594aa4
LC
59 (let* ((f (search-path %load-path "tests/test.drv"))
60 (b1 (call-with-input-file f get-bytevector-all))
341c6fdd
LC
61 (d1 (read-derivation (open-bytevector-input-port b1)))
62 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
63 (d2 (read-derivation (open-bytevector-input-port b2))))
64 (and (equal? b1 b2)
65 (equal? d1 d2))))
66
b37eb5ed
LC
67(test-skip (if %store 0 4))
68
69(test-assert "add-to-store, recursive"
70 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
71 (drv (add-to-store %store "dir-tree-test" #t #t "sha256" dir)))
72 (and (eq? 'directory (stat:type (stat drv)))
73 (equal? (directory-contents dir)
74 (directory-contents drv)))))
26bbbb95
LC
75
76(test-assert "derivation with no inputs"
77 (let ((builder (add-text-to-store %store "my-builder.sh"
78 "#!/bin/sh\necho hello, world\n"
79 '())))
98090557 80 (store-path? (derivation %store "foo" (%current-system) builder
26bbbb95
LC
81 '() '(("HOME" . "/homeless")) '()))))
82
fb3eec83
LC
83(test-assert "build derivation with 1 source"
84 (let*-values (((builder)
85 (add-text-to-store %store "my-builder.sh"
de4c3f26 86 "echo hello, world > \"$out\"\n"
fb3eec83
LC
87 '()))
88 ((drv-path drv)
98090557 89 (derivation %store "foo" (%current-system)
fb3eec83 90 "/bin/sh" `(,builder)
af7f9e5f
LC
91 '(("HOME" . "/homeless")
92 ("zzz" . "Z!")
93 ("AAA" . "A!"))
fb3eec83
LC
94 `((,builder))))
95 ((succeeded?)
96 (build-derivations %store (list drv-path))))
97 (and succeeded?
98 (let ((path (derivation-output-path
99 (assoc-ref (derivation-outputs drv) "out"))))
100 (string=? (call-with-input-file path read-line)
101 "hello, world")))))
102
749c6567
LC
103(test-assert "fixed-output derivation"
104 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
105 "echo -n hello > $out" '()))
106 (hash (sha256 (string->utf8 "hello")))
98090557 107 (drv-path (derivation %store "fixed" (%current-system)
749c6567
LC
108 "/bin/sh" `(,builder)
109 '() `((,builder))
110 #:hash hash #:hash-algo 'sha256))
111 (succeeded? (build-derivations %store (list drv-path))))
112 (and succeeded?
113 (let ((p (derivation-path->output-path drv-path)))
114 (equal? (string->utf8 "hello")
115 (call-with-input-file p get-bytevector-all))))))
116
7946c4e7
LC
117(test-assert "multiple-output derivation"
118 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
119 "echo one > $out ; echo two > $second"
120 '()))
98090557 121 (drv-path (derivation %store "fixed" (%current-system)
7946c4e7
LC
122 "/bin/sh" `(,builder)
123 '(("HOME" . "/homeless")
124 ("zzz" . "Z!")
125 ("AAA" . "A!"))
126 `((,builder))
127 #:outputs '("out" "second")))
128 (succeeded? (build-derivations %store (list drv-path))))
129 (and succeeded?
130 (let ((one (derivation-path->output-path drv-path "out"))
131 (two (derivation-path->output-path drv-path "second")))
132 (and (eq? 'one (call-with-input-file one read))
133 (eq? 'two (call-with-input-file two read)))))))
134
de4c3f26
LC
135\f
136(define %coreutils
137 (false-if-exception (nixpkgs-derivation "coreutils")))
138
139(test-skip (if %coreutils 0 1))
140
141(test-assert "build derivation with coreutils"
142 (let* ((builder
143 (add-text-to-store %store "build-with-coreutils.sh"
144 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
145 '()))
146 (drv-path
98090557 147 (derivation %store "foo" (%current-system)
de4c3f26
LC
148 "/bin/sh" `(,builder)
149 `(("PATH" .
150 ,(string-append
151 (derivation-path->output-path %coreutils)
152 "/bin")))
153 `((,builder)
154 (,%coreutils))))
155 (succeeded?
156 (build-derivations %store (list drv-path))))
157 (and succeeded?
158 (let ((p (derivation-path->output-path drv-path)))
159 (file-exists? (string-append p "/good"))))))
160
99634e3f 161(test-skip (if (%guile-for-build) 0 4))
d9085c23
LC
162
163(test-assert "build-expression->derivation without inputs"
164 (let* ((builder '(begin
165 (mkdir %output)
166 (call-with-output-file (string-append %output "/test")
167 (lambda (p)
168 (display '(hello guix) p)))))
98090557 169 (drv-path (build-expression->derivation %store "goo" (%current-system)
d9085c23
LC
170 builder '()))
171 (succeeded? (build-derivations %store (list drv-path))))
172 (and succeeded?
173 (let ((p (derivation-path->output-path drv-path)))
174 (equal? '(hello guix)
175 (call-with-input-file (string-append p "/test") read))))))
176
9bc07f4d
LC
177(test-assert "build-expression->derivation with two outputs"
178 (let* ((builder '(begin
179 (call-with-output-file (assoc-ref %outputs "out")
180 (lambda (p)
181 (display '(hello) p)))
182 (call-with-output-file (assoc-ref %outputs "second")
183 (lambda (p)
184 (display '(world) p)))))
185 (drv-path (build-expression->derivation %store "double"
98090557 186 (%current-system)
9bc07f4d
LC
187 builder '()
188 #:outputs '("out"
189 "second")))
190 (succeeded? (build-derivations %store (list drv-path))))
191 (and succeeded?
192 (let ((one (derivation-path->output-path drv-path))
193 (two (derivation-path->output-path drv-path "second")))
194 (and (equal? '(hello) (call-with-input-file one read))
195 (equal? '(world) (call-with-input-file two read)))))))
196
d9085c23
LC
197(test-assert "build-expression->derivation with one input"
198 (let* ((builder '(call-with-output-file %output
199 (lambda (p)
200 (let ((cu (assoc-ref %build-inputs "cu")))
201 (close 1)
202 (dup2 (port->fdes p) 1)
203 (execl (string-append cu "/bin/uname")
204 "uname" "-a")))))
98090557 205 (drv-path (build-expression->derivation %store "uname" (%current-system)
d9085c23
LC
206 builder
207 `(("cu" . ,%coreutils))))
208 (succeeded? (build-derivations %store (list drv-path))))
209 (and succeeded?
210 (let ((p (derivation-path->output-path drv-path)))
211 (string-contains (call-with-input-file p read-line) "GNU")))))
212
99634e3f
LC
213(test-assert "imported-files"
214 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
215 ("a/b/c" . ,(search-path %load-path
216 "guix/derivations.scm"))
217 ("p/q" . ,(search-path %load-path "guix.scm"))))
218 (drv-path (imported-files %store files)))
219 (and (build-derivations %store (list drv-path))
220 (let ((dir (derivation-path->output-path drv-path)))
221 (every (match-lambda
222 ((path . source)
223 (equal? (call-with-input-file (string-append dir "/" path)
224 get-bytevector-all)
225 (call-with-input-file source
226 get-bytevector-all))))
227 files)))))
228
26b969de
LC
229(test-skip (if (false-if-exception (getaddrinfo "ftp.gnu.org" "http"))
230 0
231 1))
232
233(test-assert "build-expression->derivation for fixed-output derivation"
234 (let* ((url "http://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz")
0e383c76
LC
235 (builder
236 `(begin
237 (use-modules (web client) (web uri)
238 (rnrs io ports) (srfi srfi-11))
239 (let-values (((resp bv)
240 (http-get (string->uri ,url) #:decode-body? #f)))
241 (call-with-output-file %output
242 (lambda (p)
243 (put-bytevector p bv))))))
26b969de 244 (drv-path (build-expression->derivation
98090557 245 %store "hello-2.8.tar.gz" (%current-system) builder '()
26b969de
LC
246 #:hash (nix-base32-string->bytevector
247 "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6")
248 #:hash-algo 'sha256))
249 (succeeded? (build-derivations %store (list drv-path))))
250 (and succeeded?
251 (file-exists? (derivation-path->output-path drv-path)))))
252
341c6fdd
LC
253(test-end)
254
255\f
256(exit (= (test-runner-fail-count (test-runner-current)) 0))
257
258;;; Local Variables:
259;;; eval: (put 'test-assert 'scheme-indent-function 1)
260;;; End: