Thank Brandon.
[jackhill/guix/guix.git] / tests / packages.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
e509d152 2;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
e3ce5d70 3;;;
233e7676 4;;; This file is part of GNU Guix.
e3ce5d70 5;;;
233e7676 6;;; GNU Guix is free software; you can redistribute it and/or modify it
e3ce5d70
LC
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;;;
233e7676 11;;; GNU Guix is distributed in the hope that it will be useful, but
e3ce5d70
LC
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
233e7676 17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
e3ce5d70
LC
18
19
20(define-module (test-packages)
21 #:use-module (guix store)
22 #:use-module (guix utils)
f9cc8971 23 #:use-module (guix hash)
e3ce5d70
LC
24 #:use-module (guix derivations)
25 #:use-module (guix packages)
a18eda27 26 #:use-module (guix build-system)
be13fbfa 27 #:use-module (guix build-system trivial)
a3d73f59 28 #:use-module (guix build-system gnu)
59a43334 29 #:use-module (gnu packages)
1ffa7090
LC
30 #:use-module (gnu packages base)
31 #:use-module (gnu packages bootstrap)
e509d152 32 #:use-module (srfi srfi-11)
6b1891b0 33 #:use-module (srfi srfi-26)
9b222abe 34 #:use-module (srfi srfi-34)
6b1891b0 35 #:use-module (srfi srfi-64)
860a6f1a 36 #:use-module (rnrs io ports)
6b1891b0 37 #:use-module (ice-9 match))
e3ce5d70
LC
38
39;; Test the high-level packaging layer.
40
41(define %store
42 (false-if-exception (open-connection)))
43
81dbd783
LC
44(when %store
45 ;; Make sure we build everything by ourselves.
46 (set-build-options %store #:use-substitutes? #f))
47
14da91e2 48\f
e3ce5d70
LC
49(test-begin "packages")
50
a3d73f59
LC
51(define-syntax-rule (dummy-package name* extra-fields ...)
52 (package (name name*) (version "0") (source #f)
53 (build-system gnu-build-system)
d45122f5 54 (synopsis #f) (description #f)
1fb78cb2 55 (home-page #f) (license #f)
a3d73f59
LC
56 extra-fields ...))
57
d66c7096
LC
58(test-assert "package-field-location"
59 (let ()
60 (define (goto port line column)
61 (unless (and (= (port-column port) (- column 1))
62 (= (port-line port) (- line 1)))
63 (unless (eof-object? (get-char port))
64 (goto port line column))))
65
66 (define read-at
67 (match-lambda
68 (($ <location> file line column)
69 (call-with-input-file (search-path %load-path file)
70 (lambda (port)
71 (goto port line column)
72 (read port))))))
73
ee48b283
LC
74 ;; Until Guile 2.0.6 included, source properties were added only to pairs.
75 ;; Thus, check against both VALUE and (FIELD VALUE).
76 (and (member (read-at (package-field-location %bootstrap-guile 'name))
77 (let ((name (package-name %bootstrap-guile)))
78 (list name `(name ,name))))
79 (member (read-at (package-field-location %bootstrap-guile 'version))
80 (let ((version (package-version %bootstrap-guile)))
81 (list version `(version ,version))))
f903dc05 82 (not (package-field-location %bootstrap-guile 'does-not-exist)))))
d66c7096 83
0b8749b7
LC
84;; Make sure we don't change the file name to an absolute file name.
85(test-equal "package-field-location, relative file name"
86 (location-file (package-location %bootstrap-guile))
87 (with-fluids ((%file-port-name-canonicalization 'absolute))
88 (location-file (package-field-location %bootstrap-guile 'version))))
89
a3d73f59
LC
90(test-assert "package-transitive-inputs"
91 (let* ((a (dummy-package "a"))
92 (b (dummy-package "b"
93 (propagated-inputs `(("a" ,a)))))
94 (c (dummy-package "c"
95 (inputs `(("a" ,a)))))
96 (d (dummy-package "d"
97 (propagated-inputs `(("x" "something.drv")))))
98 (e (dummy-package "e"
99 (inputs `(("b" ,b) ("c" ,c) ("d" ,d))))))
100 (and (null? (package-transitive-inputs a))
101 (equal? `(("a" ,a)) (package-transitive-inputs b))
102 (equal? `(("a" ,a)) (package-transitive-inputs c))
103 (equal? (package-propagated-inputs d)
104 (package-transitive-inputs d))
105 (equal? `(("b" ,b) ("b/a" ,a) ("c" ,c)
106 ("d" ,d) ("d/x" "something.drv"))
107 (pk 'x (package-transitive-inputs e))))))
108
7357138b
LC
109(test-skip (if (not %store) 8 0))
110
111(test-assert "package-source-derivation, file"
112 (let* ((file (search-path %load-path "guix.scm"))
113 (package (package (inherit (dummy-package "p"))
114 (source file)))
115 (source (package-source-derivation %store
116 (package-source package))))
117 (and (store-path? source)
118 (valid-path? %store source)
119 (equal? (call-with-input-file source get-bytevector-all)
120 (call-with-input-file file get-bytevector-all)))))
121
122(test-assert "package-source-derivation, store path"
123 (let* ((file (add-to-store %store "guix.scm" #t "sha256"
124 (search-path %load-path "guix.scm")))
125 (package (package (inherit (dummy-package "p"))
126 (source file)))
127 (source (package-source-derivation %store
128 (package-source package))))
129 (string=? file source)))
e509d152 130
f80594cc
LC
131(test-assert "package-source-derivation, indirect store path"
132 (let* ((dir (add-to-store %store "guix-build" #t "sha256"
133 (dirname (search-path %load-path
134 "guix/build/utils.scm"))))
135 (package (package (inherit (dummy-package "p"))
136 (source (string-append dir "/utils.scm"))))
137 (source (package-source-derivation %store
138 (package-source package))))
139 (and (direct-store-path? source)
140 (string-suffix? "utils.scm" source))))
141
b29f947d
LC
142(unless (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV))
143 (test-skip 1))
f9cc8971
LC
144(test-equal "package-source-derivation, snippet"
145 "OK"
127ed6a9 146 (let* ((file (search-bootstrap-binary "guile-2.0.9.tar.xz"
f9cc8971
LC
147 (%current-system)))
148 (sha256 (call-with-input-file file port-sha256))
149 (fetch (lambda* (store url hash-algo hash
150 #:optional name #:key system)
151 (pk 'fetch url hash-algo hash name system)
152 (add-to-store store (basename url) #f "sha256" url)))
153 (source (bootstrap-origin
154 (origin
155 (method fetch)
156 (uri file)
157 (sha256 sha256)
158 (patch-inputs
159 `(("tar" ,%bootstrap-coreutils&co)
160 ("xz" ,%bootstrap-coreutils&co)
161 ("patch" ,%bootstrap-coreutils&co)))
7db9608d 162 (patch-guile %bootstrap-guile)
f9cc8971
LC
163 (modules '((guix build utils)))
164 (imported-modules modules)
165 (snippet '(begin
166 ;; We end up in 'bin', because it's the first
167 ;; directory, alphabetically. Not a very good
168 ;; example but hey.
169 (chmod "." #o777)
170 (symlink "guile" "guile-rocks")
171 (copy-recursively "../share/guile/2.0/scripts"
172 "scripts")
173
174 ;; These variables must exist.
175 (pk %build-inputs %outputs))))))
176 (package (package (inherit (dummy-package "with-snippet"))
177 (source source)
178 (build-system trivial-build-system)
179 (inputs
180 `(("tar" ,(search-bootstrap-binary "tar"
181 (%current-system)))
182 ("xz" ,(search-bootstrap-binary "xz"
183 (%current-system)))))
184 (arguments
185 `(#:guile ,%bootstrap-guile
186 #:builder
187 (let ((tar (assoc-ref %build-inputs "tar"))
188 (xz (assoc-ref %build-inputs "xz"))
189 (source (assoc-ref %build-inputs "source")))
190 (and (zero? (system* tar "xvf" source
191 "--use-compress-program" xz))
192 (string=? "guile" (readlink "bin/guile-rocks"))
193 (file-exists? "bin/scripts/compile.scm")
194 (let ((out (assoc-ref %outputs "out")))
195 (call-with-output-file out
196 (lambda (p)
197 (display "OK" p))))))))))
198 (drv (package-derivation %store package))
199 (out (derivation->output-path drv)))
200 (and (build-derivations %store (list (pk 'snippet-drv drv)))
201 (call-with-input-file out get-string-all))))
202
59688fc4
LC
203(test-assert "return value"
204 (let ((drv (package-derivation %store (dummy-package "p"))))
205 (and (derivation? drv)
206 (file-exists? (derivation-file-name drv)))))
be13fbfa 207
d510ab46
LC
208(test-assert "package-output"
209 (let* ((package (dummy-package "p"))
59688fc4
LC
210 (drv (package-derivation %store package)))
211 (and (derivation? drv)
212 (string=? (derivation->output-path drv)
d510ab46
LC
213 (package-output %store package "out")))))
214
be13fbfa
LC
215(test-assert "trivial"
216 (let* ((p (package (inherit (dummy-package "trivial"))
217 (build-system trivial-build-system)
218 (source #f)
219 (arguments
14da91e2
LC
220 `(#:guile ,%bootstrap-guile
221 #:builder
be13fbfa
LC
222 (begin
223 (mkdir %output)
224 (call-with-output-file (string-append %output "/test")
225 (lambda (p)
226 (display '(hello guix) p))))))))
227 (d (package-derivation %store p)))
228 (and (build-derivations %store (list d))
59688fc4 229 (let ((p (pk 'drv d (derivation->output-path d))))
be13fbfa
LC
230 (equal? '(hello guix)
231 (call-with-input-file (string-append p "/test") read))))))
e3ce5d70 232
860a6f1a
LC
233(test-assert "trivial with local file as input"
234 (let* ((i (search-path %load-path "ice-9/boot-9.scm"))
235 (p (package (inherit (dummy-package "trivial-with-input-file"))
236 (build-system trivial-build-system)
237 (source #f)
238 (arguments
239 `(#:guile ,%bootstrap-guile
240 #:builder (copy-file (assoc-ref %build-inputs "input")
241 %output)))
242 (inputs `(("input" ,i)))))
243 (d (package-derivation %store p)))
244 (and (build-derivations %store (list d))
59688fc4 245 (let ((p (pk 'drv d (derivation->output-path d))))
860a6f1a
LC
246 (equal? (call-with-input-file p get-bytevector-all)
247 (call-with-input-file i get-bytevector-all))))))
248
03761a44
LC
249(test-assert "trivial with source"
250 (let* ((i (search-path %load-path "ice-9/boot-9.scm"))
251 (p (package (inherit (dummy-package "trivial-with-source"))
252 (build-system trivial-build-system)
253 (source i)
254 (arguments
255 `(#:guile ,%bootstrap-guile
256 #:builder (copy-file (assoc-ref %build-inputs "source")
257 %output)))))
258 (d (package-derivation %store p)))
259 (and (build-derivations %store (list d))
260 (let ((p (derivation->output-path d)))
261 (equal? (call-with-input-file p get-bytevector-all)
262 (call-with-input-file i get-bytevector-all))))))
263
592ef6c8
LC
264(test-assert "trivial with system-dependent input"
265 (let* ((p (package (inherit (dummy-package "trivial-system-dependent-input"))
266 (build-system trivial-build-system)
267 (source #f)
268 (arguments
269 `(#:guile ,%bootstrap-guile
270 #:builder
271 (let ((out (assoc-ref %outputs "out"))
272 (bash (assoc-ref %build-inputs "bash")))
273 (zero? (system* bash "-c"
274 (format #f "echo hello > ~a" out))))))
dd6b9a37
LC
275 (inputs `(("bash" ,(search-bootstrap-binary "bash"
276 (%current-system)))))))
592ef6c8
LC
277 (d (package-derivation %store p)))
278 (and (build-derivations %store (list d))
59688fc4 279 (let ((p (pk 'drv d (derivation->output-path d))))
592ef6c8
LC
280 (eq? 'hello (call-with-input-file p read))))))
281
a18eda27
LC
282(test-assert "search paths"
283 (let* ((p (make-prompt-tag "return-search-paths"))
284 (s (build-system
285 (name "raw")
286 (description "Raw build system with direct store access")
287 (build (lambda* (store name source inputs
288 #:key outputs system search-paths)
289 search-paths))))
290 (x (list (search-path-specification
291 (variable "GUILE_LOAD_PATH")
292 (directories '("share/guile/site/2.0")))
293 (search-path-specification
294 (variable "GUILE_LOAD_COMPILED_PATH")
295 (directories '("share/guile/site/2.0")))))
296 (a (package (inherit (dummy-package "guile"))
297 (build-system s)
298 (native-search-paths x)))
299 (b (package (inherit (dummy-package "guile-foo"))
300 (build-system s)
301 (inputs `(("guile" ,a)))))
302 (c (package (inherit (dummy-package "guile-bar"))
303 (build-system s)
304 (inputs `(("guile" ,a)
305 ("guile-foo" ,b))))))
306 (let-syntax ((collect (syntax-rules ()
307 ((_ body ...)
308 (call-with-prompt p
309 (lambda ()
310 body ...)
311 (lambda (k search-paths)
312 search-paths))))))
313 (and (null? (collect (package-derivation %store a)))
314 (equal? x (collect (package-derivation %store b)))
315 (equal? x (collect (package-derivation %store c)))))))
316
9c1edabd 317(test-assert "package-cross-derivation"
59688fc4
LC
318 (let ((drv (package-cross-derivation %store (dummy-package "p")
319 "mips64el-linux-gnu")))
320 (and (derivation? drv)
321 (file-exists? (derivation-file-name drv)))))
9c1edabd 322
5dce8218
LC
323(test-assert "package-cross-derivation, trivial-build-system"
324 (let ((p (package (inherit (dummy-package "p"))
325 (build-system trivial-build-system)
326 (arguments '(#:builder (exit 1))))))
59688fc4
LC
327 (let ((drv (package-cross-derivation %store p "mips64el-linux-gnu")))
328 (derivation? drv))))
5dce8218 329
9b222abe
LC
330(test-assert "package-cross-derivation, no cross builder"
331 (let* ((b (build-system (inherit trivial-build-system)
332 (cross-build #f)))
333 (p (package (inherit (dummy-package "p"))
334 (build-system b))))
335 (guard (c ((package-cross-build-system-error? c)
336 (eq? (package-error-package c) p)))
337 (package-cross-derivation %store p "mips64el-linux-gnu")
338 #f)))
339
ad1ebab3
LC
340(unless (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV))
341 (test-skip 1))
9e782349
LC
342(test-assert "GNU Make, bootstrap"
343 ;; GNU Make is the first program built during bootstrap; we choose it
344 ;; here so that the test doesn't last for too long.
1ffa7090 345 (let ((gnu-make (@@ (gnu packages base) gnu-make-boot0)))
9e782349
LC
346 (and (package? gnu-make)
347 (or (location? (package-location gnu-make))
348 (not (package-location gnu-make)))
349 (let* ((drv (package-derivation %store gnu-make))
59688fc4 350 (out (derivation->output-path drv)))
14da91e2 351 (and (build-derivations %store (list drv))
9e782349 352 (file-exists? (string-append out "/bin/make")))))))
e3ce5d70 353
ba326ce4
LC
354(test-eq "fold-packages" hello
355 (fold-packages (lambda (p r)
356 (if (string=? (package-name p) "hello")
357 p
358 r))
359 #f))
360
6b1891b0
LC
361(test-assert "find-packages-by-name"
362 (match (find-packages-by-name "hello")
363 (((? (cut eq? hello <>))) #t)
364 (wrong (pk 'find-packages-by-name wrong #f))))
365
366(test-assert "find-packages-by-name with version"
367 (match (find-packages-by-name "hello" (package-version hello))
368 (((? (cut eq? hello <>))) #t)
369 (wrong (pk 'find-packages-by-name wrong #f))))
370
e3ce5d70
LC
371(test-end "packages")
372
373\f
374(exit (= (test-runner-fail-count (test-runner-current)) 0))
375
376;;; Local Variables:
a3d73f59 377;;; eval: (put 'dummy-package 'scheme-indent-function 1)
e3ce5d70 378;;; End: