linux-initrd: Move Linux module tree flattening to another derivation.
[jackhill/guix/guix.git] / tests / gexp.scm
CommitLineData
21b679f6
LC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2014 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU 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;;; GNU 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 GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (test-gexp)
20 #:use-module (guix store)
21 #:use-module (guix monads)
22 #:use-module (guix gexp)
23 #:use-module (guix derivations)
79c0c8cd 24 #:use-module (guix packages)
c1bc358f 25 #:use-module (guix tests)
21b679f6
LC
26 #:use-module (gnu packages)
27 #:use-module (gnu packages base)
28 #:use-module (gnu packages bootstrap)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-64)
31 #:use-module (rnrs io ports)
32 #:use-module (ice-9 match)
2cf0ea0d 33 #:use-module (ice-9 regex)
21b679f6
LC
34 #:use-module (ice-9 popen))
35
36;; Test the (guix gexp) module.
37
38(define %store
c1bc358f 39 (open-connection-for-tests))
21b679f6
LC
40
41;; For white-box testing.
42(define gexp-inputs (@@ (guix gexp) gexp-inputs))
667b2508 43(define gexp-native-inputs (@@ (guix gexp) gexp-native-inputs))
21b679f6
LC
44(define gexp->sexp (@@ (guix gexp) gexp->sexp))
45
667b2508 46(define* (gexp->sexp* exp #:optional target)
68a61e9f 47 (run-with-store %store (gexp->sexp exp
68a61e9f 48 #:target target)
c1bc358f 49 #:guile-for-build (%guile-for-build)))
21b679f6
LC
50
51(define-syntax-rule (test-assertm name exp)
52 (test-assert name
53 (run-with-store %store exp
c1bc358f 54 #:guile-for-build (%guile-for-build))))
21b679f6
LC
55
56\f
57(test-begin "gexp")
58
59(test-equal "no refs"
60 '(display "hello!")
61 (let ((exp (gexp (display "hello!"))))
62 (and (gexp? exp)
63 (null? (gexp-inputs exp))
64 (gexp->sexp* exp))))
65
66(test-equal "unquote"
67 '(display `(foo ,(+ 2 3)))
68 (let ((exp (gexp (display `(foo ,(+ 2 3))))))
69 (and (gexp? exp)
70 (null? (gexp-inputs exp))
71 (gexp->sexp* exp))))
72
73(test-assert "one input package"
74 (let ((exp (gexp (display (ungexp coreutils)))))
75 (and (gexp? exp)
76 (match (gexp-inputs exp)
77 (((p "out"))
78 (eq? p coreutils)))
79 (equal? `(display ,(derivation->output-path
80 (package-derivation %store coreutils)))
81 (gexp->sexp* exp)))))
82
79c0c8cd
LC
83(test-assert "one input origin"
84 (let ((exp (gexp (display (ungexp (package-source coreutils))))))
85 (and (gexp? exp)
86 (match (gexp-inputs exp)
87 (((o "out"))
88 (eq? o (package-source coreutils))))
89 (equal? `(display ,(derivation->output-path
90 (package-source-derivation
91 %store (package-source coreutils))))
92 (gexp->sexp* exp)))))
93
21b679f6
LC
94(test-assert "same input twice"
95 (let ((exp (gexp (begin
96 (display (ungexp coreutils))
97 (display (ungexp coreutils))))))
98 (and (gexp? exp)
99 (match (gexp-inputs exp)
100 (((p "out"))
101 (eq? p coreutils)))
102 (let ((e `(display ,(derivation->output-path
103 (package-derivation %store coreutils)))))
104 (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
105
106(test-assert "two input packages, one derivation, one file"
107 (let* ((drv (build-expression->derivation
108 %store "foo" 'bar
109 #:guile-for-build (package-derivation %store %bootstrap-guile)))
110 (txt (add-text-to-store %store "foo" "Hello, world!"))
111 (exp (gexp (begin
112 (display (ungexp coreutils))
113 (display (ungexp %bootstrap-guile))
114 (display (ungexp drv))
115 (display (ungexp txt))))))
116 (define (match-input thing)
117 (match-lambda
118 ((drv-or-pkg _ ...)
119 (eq? thing drv-or-pkg))))
120
121 (and (gexp? exp)
122 (= 4 (length (gexp-inputs exp)))
123 (every (lambda (input)
124 (find (match-input input) (gexp-inputs exp)))
125 (list drv coreutils %bootstrap-guile txt))
126 (let ((e0 `(display ,(derivation->output-path
127 (package-derivation %store coreutils))))
128 (e1 `(display ,(derivation->output-path
129 (package-derivation %store %bootstrap-guile))))
130 (e2 `(display ,(derivation->output-path drv)))
131 (e3 `(display ,txt)))
132 (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
133
667b2508
LC
134(test-assert "ungexp + ungexp-native"
135 (let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
136 (ungexp coreutils)
137 (ungexp-native glibc)
138 (ungexp binutils))))
139 (target "mips64el-linux")
140 (guile (derivation->output-path
141 (package-derivation %store %bootstrap-guile)))
142 (cu (derivation->output-path
143 (package-cross-derivation %store coreutils target)))
144 (libc (derivation->output-path
145 (package-derivation %store glibc)))
146 (bu (derivation->output-path
147 (package-cross-derivation %store binutils target))))
148 (and (lset= equal?
149 `((,%bootstrap-guile "out") (,glibc "out"))
150 (gexp-native-inputs exp))
151 (lset= equal?
152 `((,coreutils "out") (,binutils "out"))
153 (gexp-inputs exp))
154 (equal? `(list ,guile ,cu ,libc ,bu)
155 (gexp->sexp* exp target)))))
156
21b679f6
LC
157(test-assert "input list"
158 (let ((exp (gexp (display
159 '(ungexp (list %bootstrap-guile coreutils)))))
160 (guile (derivation->output-path
161 (package-derivation %store %bootstrap-guile)))
162 (cu (derivation->output-path
163 (package-derivation %store coreutils))))
164 (and (lset= equal?
165 `((,%bootstrap-guile "out") (,coreutils "out"))
166 (gexp-inputs exp))
167 (equal? `(display '(,guile ,cu))
168 (gexp->sexp* exp)))))
169
667b2508
LC
170(test-assert "input list + ungexp-native"
171 (let* ((target "mips64el-linux")
172 (exp (gexp (display
173 (cons '(ungexp-native (list %bootstrap-guile coreutils))
174 '(ungexp (list glibc binutils))))))
175 (guile (derivation->output-path
176 (package-derivation %store %bootstrap-guile)))
177 (cu (derivation->output-path
178 (package-derivation %store coreutils)))
179 (xlibc (derivation->output-path
180 (package-cross-derivation %store glibc target)))
181 (xbu (derivation->output-path
182 (package-cross-derivation %store binutils target))))
183 (and (lset= equal?
184 `((,%bootstrap-guile "out") (,coreutils "out"))
185 (gexp-native-inputs exp))
186 (lset= equal?
187 `((,glibc "out") (,binutils "out"))
188 (gexp-inputs exp))
189 (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu)))
190 (gexp->sexp* exp target)))))
191
21b679f6
LC
192(test-assert "input list splicing"
193 (let* ((inputs (list (list glibc "debug") %bootstrap-guile))
194 (outputs (list (derivation->output-path
195 (package-derivation %store glibc)
196 "debug")
197 (derivation->output-path
198 (package-derivation %store %bootstrap-guile))))
199 (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
200 (and (lset= equal?
201 `((,glibc "debug") (,%bootstrap-guile "out"))
202 (gexp-inputs exp))
203 (equal? (gexp->sexp* exp)
204 `(list ,@(cons 5 outputs))))))
205
667b2508
LC
206(test-assert "input list splicing + ungexp-native-splicing"
207 (let* ((inputs (list (list glibc "debug") %bootstrap-guile))
208 (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
209 (and (lset= equal?
210 `((,glibc "debug") (,%bootstrap-guile "out"))
211 (gexp-native-inputs exp))
212 (null? (gexp-inputs exp))
213 (equal? (gexp->sexp* exp) ;native
214 (gexp->sexp* exp "mips64el-linux")))))
215
21b679f6
LC
216(test-assertm "gexp->file"
217 (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
218 (guile (package-file %bootstrap-guile))
219 (sexp (gexp->sexp exp))
220 (drv (gexp->file "foo" exp))
221 (out -> (derivation->output-path drv))
222 (done (built-derivations (list drv)))
223 (refs ((store-lift references) out)))
224 (return (and (equal? sexp (call-with-input-file out read))
225 (equal? (list guile) refs)))))
226
227(test-assertm "gexp->derivation"
228 (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
229 (exp -> (gexp
230 (begin
231 (mkdir (ungexp output))
232 (chdir (ungexp output))
233 (symlink
234 (string-append (ungexp %bootstrap-guile)
235 "/bin/guile")
236 "foo")
237 (symlink (ungexp file)
238 (ungexp output "2nd")))))
239 (drv (gexp->derivation "foo" exp))
240 (out -> (derivation->output-path drv))
241 (out2 -> (derivation->output-path drv "2nd"))
242 (done (built-derivations (list drv)))
243 (refs ((store-lift references) out))
244 (refs2 ((store-lift references) out2))
245 (guile (package-file %bootstrap-guile "bin/guile")))
246 (return (and (string=? (readlink (string-append out "/foo")) guile)
247 (string=? (readlink out2) file)
248 (equal? refs (list (dirname (dirname guile))))
249 (equal? refs2 (list file))))))
250
251(test-assertm "gexp->derivation, composed gexps"
252 (mlet* %store-monad ((exp0 -> (gexp (begin
253 (mkdir (ungexp output))
254 (chdir (ungexp output)))))
255 (exp1 -> (gexp (symlink
256 (string-append (ungexp %bootstrap-guile)
257 "/bin/guile")
258 "foo")))
259 (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
260 (drv (gexp->derivation "foo" exp))
261 (out -> (derivation->output-path drv))
262 (done (built-derivations (list drv)))
263 (guile (package-file %bootstrap-guile "bin/guile")))
264 (return (string=? (readlink (string-append out "/foo"))
265 guile))))
266
5d098459
LC
267(test-assertm "gexp->derivation, default system"
268 ;; The default system should be the one at '>>=' time, not the one at
269 ;; invocation time. See <http://bugs.gnu.org/18002>.
270 (let ((system (%current-system))
271 (mdrv (parameterize ((%current-system "foobar64-linux"))
272 (gexp->derivation "foo"
273 (gexp
274 (mkdir (ungexp output)))))))
275 (mlet %store-monad ((drv mdrv))
276 (return (string=? system (derivation-system drv))))))
277
68a61e9f
LC
278(test-assertm "gexp->derivation, cross-compilation"
279 (mlet* %store-monad ((target -> "mips64el-linux")
280 (exp -> (gexp (list (ungexp coreutils)
281 (ungexp output))))
282 (xdrv (gexp->derivation "foo" exp
283 #:target target))
284 (refs ((store-lift references)
285 (derivation-file-name xdrv)))
286 (xcu (package->cross-derivation coreutils
287 target))
288 (cu (package->derivation coreutils)))
289 (return (and (member (derivation-file-name xcu) refs)
290 (not (member (derivation-file-name cu) refs))))))
291
667b2508
LC
292(test-assertm "gexp->derivation, ungexp-native"
293 (mlet* %store-monad ((target -> "mips64el-linux")
294 (exp -> (gexp (list (ungexp-native coreutils)
295 (ungexp output))))
296 (xdrv (gexp->derivation "foo" exp
297 #:target target))
298 (drv (gexp->derivation "foo" exp)))
299 (return (string=? (derivation-file-name drv)
300 (derivation-file-name xdrv)))))
301
302(test-assertm "gexp->derivation, ungexp + ungexp-native"
303 (mlet* %store-monad ((target -> "mips64el-linux")
304 (exp -> (gexp (list (ungexp-native coreutils)
305 (ungexp glibc)
306 (ungexp output))))
307 (xdrv (gexp->derivation "foo" exp
308 #:target target))
309 (refs ((store-lift references)
310 (derivation-file-name xdrv)))
311 (xglibc (package->cross-derivation glibc target))
312 (cu (package->derivation coreutils)))
313 (return (and (member (derivation-file-name cu) refs)
314 (member (derivation-file-name xglibc) refs)))))
315
316(test-assertm "gexp->derivation, ungexp-native + composed gexps"
317 (mlet* %store-monad ((target -> "mips64el-linux")
318 (exp0 -> (gexp (list 1 2
319 (ungexp coreutils))))
320 (exp -> (gexp (list 0 (ungexp-native exp0))))
321 (xdrv (gexp->derivation "foo" exp
322 #:target target))
323 (drv (gexp->derivation "foo" exp)))
324 (return (string=? (derivation-file-name drv)
325 (derivation-file-name xdrv)))))
326
c17b5ab4 327(define shebang
c1bc358f 328 (string-append "#!" (derivation->output-path (%guile-for-build))
c17b5ab4
LC
329 "/bin/guile --no-auto-compile"))
330
331;; If we're going to hit the silly shebang limit (128 chars on Linux-based
332;; systems), then skip the following test.
333(test-skip (if (> (string-length shebang) 127) 1 0))
334
21b679f6
LC
335(test-assertm "gexp->script"
336 (mlet* %store-monad ((n -> (random (expt 2 50)))
337 (exp -> (gexp
338 (system*
339 (string-append (ungexp %bootstrap-guile)
340 "/bin/guile")
341 "-c" (object->string
342 '(display (expt (ungexp n) 2))))))
343 (drv (gexp->script "guile-thing" exp
344 #:guile %bootstrap-guile))
345 (out -> (derivation->output-path drv))
346 (done (built-derivations (list drv))))
347 (let* ((pipe (open-input-pipe out))
348 (str (get-string-all pipe)))
349 (return (and (zero? (close-pipe pipe))
350 (= (expt n 2) (string->number str)))))))
351
2cf0ea0d
LC
352(test-assert "printer"
353 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
354 \"/bin/uname\"\\) [[:xdigit:]]+>$"
355 (with-output-to-string
356 (lambda ()
357 (write
358 (gexp (string-append (ungexp coreutils)
359 "/bin/uname")))))))
360
361(test-assert "printer vs. ungexp-splicing"
362 (string-match "^#<gexp .* [[:xdigit:]]+>$"
363 (with-output-to-string
364 (lambda ()
365 ;; #~(begin #$@#~())
366 (write
367 (gexp (begin (ungexp-splicing (gexp ())))))))))
368
21b679f6
LC
369(test-equal "sugar"
370 '(gexp (foo (ungexp bar) (ungexp baz "out")
371 (ungexp (chbouib 42))
667b2508
LC
372 (ungexp-splicing (list x y z))
373 (ungexp-native foo) (ungexp-native foo "out")
374 (ungexp-native (chbouib 42))
375 (ungexp-native-splicing (list x y z))))
376 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
377 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
21b679f6
LC
378
379(test-end "gexp")
380
381\f
382(exit (= (test-runner-fail-count (test-runner-current)) 0))
383
384;; Local Variables:
385;; eval: (put 'test-assertm 'scheme-indent-function 1)
386;; End: