store: Make 'direct-store-path?' public.
[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
a3d73f59
LC
84(test-assert "package-transitive-inputs"
85 (let* ((a (dummy-package "a"))
86 (b (dummy-package "b"
87 (propagated-inputs `(("a" ,a)))))
88 (c (dummy-package "c"
89 (inputs `(("a" ,a)))))
90 (d (dummy-package "d"
91 (propagated-inputs `(("x" "something.drv")))))
92 (e (dummy-package "e"
93 (inputs `(("b" ,b) ("c" ,c) ("d" ,d))))))
94 (and (null? (package-transitive-inputs a))
95 (equal? `(("a" ,a)) (package-transitive-inputs b))
96 (equal? `(("a" ,a)) (package-transitive-inputs c))
97 (equal? (package-propagated-inputs d)
98 (package-transitive-inputs d))
99 (equal? `(("b" ,b) ("b/a" ,a) ("c" ,c)
100 ("d" ,d) ("d/x" "something.drv"))
101 (pk 'x (package-transitive-inputs e))))))
102
7357138b
LC
103(test-skip (if (not %store) 8 0))
104
105(test-assert "package-source-derivation, file"
106 (let* ((file (search-path %load-path "guix.scm"))
107 (package (package (inherit (dummy-package "p"))
108 (source file)))
109 (source (package-source-derivation %store
110 (package-source package))))
111 (and (store-path? source)
112 (valid-path? %store source)
113 (equal? (call-with-input-file source get-bytevector-all)
114 (call-with-input-file file get-bytevector-all)))))
115
116(test-assert "package-source-derivation, store path"
117 (let* ((file (add-to-store %store "guix.scm" #t "sha256"
118 (search-path %load-path "guix.scm")))
119 (package (package (inherit (dummy-package "p"))
120 (source file)))
121 (source (package-source-derivation %store
122 (package-source package))))
123 (string=? file source)))
e509d152 124
f9cc8971
LC
125(test-equal "package-source-derivation, snippet"
126 "OK"
127 (let* ((file (search-bootstrap-binary "guile-2.0.7.tar.xz"
128 (%current-system)))
129 (sha256 (call-with-input-file file port-sha256))
130 (fetch (lambda* (store url hash-algo hash
131 #:optional name #:key system)
132 (pk 'fetch url hash-algo hash name system)
133 (add-to-store store (basename url) #f "sha256" url)))
134 (source (bootstrap-origin
135 (origin
136 (method fetch)
137 (uri file)
138 (sha256 sha256)
139 (patch-inputs
140 `(("tar" ,%bootstrap-coreutils&co)
141 ("xz" ,%bootstrap-coreutils&co)
142 ("patch" ,%bootstrap-coreutils&co)))
143 (patch-guile (package-derivation %store
144 %bootstrap-guile))
145 (modules '((guix build utils)))
146 (imported-modules modules)
147 (snippet '(begin
148 ;; We end up in 'bin', because it's the first
149 ;; directory, alphabetically. Not a very good
150 ;; example but hey.
151 (chmod "." #o777)
152 (symlink "guile" "guile-rocks")
153 (copy-recursively "../share/guile/2.0/scripts"
154 "scripts")
155
156 ;; These variables must exist.
157 (pk %build-inputs %outputs))))))
158 (package (package (inherit (dummy-package "with-snippet"))
159 (source source)
160 (build-system trivial-build-system)
161 (inputs
162 `(("tar" ,(search-bootstrap-binary "tar"
163 (%current-system)))
164 ("xz" ,(search-bootstrap-binary "xz"
165 (%current-system)))))
166 (arguments
167 `(#:guile ,%bootstrap-guile
168 #:builder
169 (let ((tar (assoc-ref %build-inputs "tar"))
170 (xz (assoc-ref %build-inputs "xz"))
171 (source (assoc-ref %build-inputs "source")))
172 (and (zero? (system* tar "xvf" source
173 "--use-compress-program" xz))
174 (string=? "guile" (readlink "bin/guile-rocks"))
175 (file-exists? "bin/scripts/compile.scm")
176 (let ((out (assoc-ref %outputs "out")))
177 (call-with-output-file out
178 (lambda (p)
179 (display "OK" p))))))))))
180 (drv (package-derivation %store package))
181 (out (derivation->output-path drv)))
182 (and (build-derivations %store (list (pk 'snippet-drv drv)))
183 (call-with-input-file out get-string-all))))
184
59688fc4
LC
185(test-assert "return value"
186 (let ((drv (package-derivation %store (dummy-package "p"))))
187 (and (derivation? drv)
188 (file-exists? (derivation-file-name drv)))))
be13fbfa 189
d510ab46
LC
190(test-assert "package-output"
191 (let* ((package (dummy-package "p"))
59688fc4
LC
192 (drv (package-derivation %store package)))
193 (and (derivation? drv)
194 (string=? (derivation->output-path drv)
d510ab46
LC
195 (package-output %store package "out")))))
196
be13fbfa
LC
197(test-assert "trivial"
198 (let* ((p (package (inherit (dummy-package "trivial"))
199 (build-system trivial-build-system)
200 (source #f)
201 (arguments
14da91e2
LC
202 `(#:guile ,%bootstrap-guile
203 #:builder
be13fbfa
LC
204 (begin
205 (mkdir %output)
206 (call-with-output-file (string-append %output "/test")
207 (lambda (p)
208 (display '(hello guix) p))))))))
209 (d (package-derivation %store p)))
210 (and (build-derivations %store (list d))
59688fc4 211 (let ((p (pk 'drv d (derivation->output-path d))))
be13fbfa
LC
212 (equal? '(hello guix)
213 (call-with-input-file (string-append p "/test") read))))))
e3ce5d70 214
860a6f1a
LC
215(test-assert "trivial with local file as input"
216 (let* ((i (search-path %load-path "ice-9/boot-9.scm"))
217 (p (package (inherit (dummy-package "trivial-with-input-file"))
218 (build-system trivial-build-system)
219 (source #f)
220 (arguments
221 `(#:guile ,%bootstrap-guile
222 #:builder (copy-file (assoc-ref %build-inputs "input")
223 %output)))
224 (inputs `(("input" ,i)))))
225 (d (package-derivation %store p)))
226 (and (build-derivations %store (list d))
59688fc4 227 (let ((p (pk 'drv d (derivation->output-path d))))
860a6f1a
LC
228 (equal? (call-with-input-file p get-bytevector-all)
229 (call-with-input-file i get-bytevector-all))))))
230
03761a44
LC
231(test-assert "trivial with source"
232 (let* ((i (search-path %load-path "ice-9/boot-9.scm"))
233 (p (package (inherit (dummy-package "trivial-with-source"))
234 (build-system trivial-build-system)
235 (source i)
236 (arguments
237 `(#:guile ,%bootstrap-guile
238 #:builder (copy-file (assoc-ref %build-inputs "source")
239 %output)))))
240 (d (package-derivation %store p)))
241 (and (build-derivations %store (list d))
242 (let ((p (derivation->output-path d)))
243 (equal? (call-with-input-file p get-bytevector-all)
244 (call-with-input-file i get-bytevector-all))))))
245
592ef6c8
LC
246(test-assert "trivial with system-dependent input"
247 (let* ((p (package (inherit (dummy-package "trivial-system-dependent-input"))
248 (build-system trivial-build-system)
249 (source #f)
250 (arguments
251 `(#:guile ,%bootstrap-guile
252 #:builder
253 (let ((out (assoc-ref %outputs "out"))
254 (bash (assoc-ref %build-inputs "bash")))
255 (zero? (system* bash "-c"
256 (format #f "echo hello > ~a" out))))))
dd6b9a37
LC
257 (inputs `(("bash" ,(search-bootstrap-binary "bash"
258 (%current-system)))))))
592ef6c8
LC
259 (d (package-derivation %store p)))
260 (and (build-derivations %store (list d))
59688fc4 261 (let ((p (pk 'drv d (derivation->output-path d))))
592ef6c8
LC
262 (eq? 'hello (call-with-input-file p read))))))
263
a18eda27
LC
264(test-assert "search paths"
265 (let* ((p (make-prompt-tag "return-search-paths"))
266 (s (build-system
267 (name "raw")
268 (description "Raw build system with direct store access")
269 (build (lambda* (store name source inputs
270 #:key outputs system search-paths)
271 search-paths))))
272 (x (list (search-path-specification
273 (variable "GUILE_LOAD_PATH")
274 (directories '("share/guile/site/2.0")))
275 (search-path-specification
276 (variable "GUILE_LOAD_COMPILED_PATH")
277 (directories '("share/guile/site/2.0")))))
278 (a (package (inherit (dummy-package "guile"))
279 (build-system s)
280 (native-search-paths x)))
281 (b (package (inherit (dummy-package "guile-foo"))
282 (build-system s)
283 (inputs `(("guile" ,a)))))
284 (c (package (inherit (dummy-package "guile-bar"))
285 (build-system s)
286 (inputs `(("guile" ,a)
287 ("guile-foo" ,b))))))
288 (let-syntax ((collect (syntax-rules ()
289 ((_ body ...)
290 (call-with-prompt p
291 (lambda ()
292 body ...)
293 (lambda (k search-paths)
294 search-paths))))))
295 (and (null? (collect (package-derivation %store a)))
296 (equal? x (collect (package-derivation %store b)))
297 (equal? x (collect (package-derivation %store c)))))))
298
9c1edabd 299(test-assert "package-cross-derivation"
59688fc4
LC
300 (let ((drv (package-cross-derivation %store (dummy-package "p")
301 "mips64el-linux-gnu")))
302 (and (derivation? drv)
303 (file-exists? (derivation-file-name drv)))))
9c1edabd 304
5dce8218
LC
305(test-assert "package-cross-derivation, trivial-build-system"
306 (let ((p (package (inherit (dummy-package "p"))
307 (build-system trivial-build-system)
308 (arguments '(#:builder (exit 1))))))
59688fc4
LC
309 (let ((drv (package-cross-derivation %store p "mips64el-linux-gnu")))
310 (derivation? drv))))
5dce8218 311
9b222abe
LC
312(test-assert "package-cross-derivation, no cross builder"
313 (let* ((b (build-system (inherit trivial-build-system)
314 (cross-build #f)))
315 (p (package (inherit (dummy-package "p"))
316 (build-system b))))
317 (guard (c ((package-cross-build-system-error? c)
318 (eq? (package-error-package c) p)))
319 (package-cross-derivation %store p "mips64el-linux-gnu")
320 #f)))
321
ad1ebab3
LC
322(unless (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV))
323 (test-skip 1))
9e782349
LC
324(test-assert "GNU Make, bootstrap"
325 ;; GNU Make is the first program built during bootstrap; we choose it
326 ;; here so that the test doesn't last for too long.
1ffa7090 327 (let ((gnu-make (@@ (gnu packages base) gnu-make-boot0)))
9e782349
LC
328 (and (package? gnu-make)
329 (or (location? (package-location gnu-make))
330 (not (package-location gnu-make)))
331 (let* ((drv (package-derivation %store gnu-make))
59688fc4 332 (out (derivation->output-path drv)))
14da91e2 333 (and (build-derivations %store (list drv))
9e782349 334 (file-exists? (string-append out "/bin/make")))))))
e3ce5d70 335
ba326ce4
LC
336(test-eq "fold-packages" hello
337 (fold-packages (lambda (p r)
338 (if (string=? (package-name p) "hello")
339 p
340 r))
341 #f))
342
6b1891b0
LC
343(test-assert "find-packages-by-name"
344 (match (find-packages-by-name "hello")
345 (((? (cut eq? hello <>))) #t)
346 (wrong (pk 'find-packages-by-name wrong #f))))
347
348(test-assert "find-packages-by-name with version"
349 (match (find-packages-by-name "hello" (package-version hello))
350 (((? (cut eq? hello <>))) #t)
351 (wrong (pk 'find-packages-by-name wrong #f))))
352
e3ce5d70
LC
353(test-end "packages")
354
355\f
356(exit (= (test-runner-fail-count (test-runner-current)) 0))
357
358;;; Local Variables:
a3d73f59 359;;; eval: (put 'dummy-package 'scheme-indent-function 1)
e3ce5d70 360;;; End: