gexp: Add #:target parameter to 'gexp->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)
21b679f6
LC
25 #:use-module (gnu packages)
26 #:use-module (gnu packages base)
27 #:use-module (gnu packages bootstrap)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-64)
30 #:use-module (rnrs io ports)
31 #:use-module (ice-9 match)
2cf0ea0d 32 #:use-module (ice-9 regex)
21b679f6
LC
33 #:use-module (ice-9 popen))
34
35;; Test the (guix gexp) module.
36
37(define %store
38 (open-connection))
39
40;; For white-box testing.
41(define gexp-inputs (@@ (guix gexp) gexp-inputs))
42(define gexp->sexp (@@ (guix gexp) gexp->sexp))
43
44(define guile-for-build
45 (package-derivation %store %bootstrap-guile))
46
47;; Make it the default.
48(%guile-for-build guile-for-build)
49
68a61e9f
LC
50(define* (gexp->sexp* exp #:optional
51 (system (%current-system)) target)
52 (run-with-store %store (gexp->sexp exp
53 #:system system
54 #:target target)
21b679f6
LC
55 #:guile-for-build guile-for-build))
56
57(define-syntax-rule (test-assertm name exp)
58 (test-assert name
59 (run-with-store %store exp
60 #:guile-for-build guile-for-build)))
61
62\f
63(test-begin "gexp")
64
65(test-equal "no refs"
66 '(display "hello!")
67 (let ((exp (gexp (display "hello!"))))
68 (and (gexp? exp)
69 (null? (gexp-inputs exp))
70 (gexp->sexp* exp))))
71
72(test-equal "unquote"
73 '(display `(foo ,(+ 2 3)))
74 (let ((exp (gexp (display `(foo ,(+ 2 3))))))
75 (and (gexp? exp)
76 (null? (gexp-inputs exp))
77 (gexp->sexp* exp))))
78
79(test-assert "one input package"
80 (let ((exp (gexp (display (ungexp coreutils)))))
81 (and (gexp? exp)
82 (match (gexp-inputs exp)
83 (((p "out"))
84 (eq? p coreutils)))
85 (equal? `(display ,(derivation->output-path
86 (package-derivation %store coreutils)))
87 (gexp->sexp* exp)))))
88
79c0c8cd
LC
89(test-assert "one input origin"
90 (let ((exp (gexp (display (ungexp (package-source coreutils))))))
91 (and (gexp? exp)
92 (match (gexp-inputs exp)
93 (((o "out"))
94 (eq? o (package-source coreutils))))
95 (equal? `(display ,(derivation->output-path
96 (package-source-derivation
97 %store (package-source coreutils))))
98 (gexp->sexp* exp)))))
99
21b679f6
LC
100(test-assert "same input twice"
101 (let ((exp (gexp (begin
102 (display (ungexp coreutils))
103 (display (ungexp coreutils))))))
104 (and (gexp? exp)
105 (match (gexp-inputs exp)
106 (((p "out"))
107 (eq? p coreutils)))
108 (let ((e `(display ,(derivation->output-path
109 (package-derivation %store coreutils)))))
110 (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
111
112(test-assert "two input packages, one derivation, one file"
113 (let* ((drv (build-expression->derivation
114 %store "foo" 'bar
115 #:guile-for-build (package-derivation %store %bootstrap-guile)))
116 (txt (add-text-to-store %store "foo" "Hello, world!"))
117 (exp (gexp (begin
118 (display (ungexp coreutils))
119 (display (ungexp %bootstrap-guile))
120 (display (ungexp drv))
121 (display (ungexp txt))))))
122 (define (match-input thing)
123 (match-lambda
124 ((drv-or-pkg _ ...)
125 (eq? thing drv-or-pkg))))
126
127 (and (gexp? exp)
128 (= 4 (length (gexp-inputs exp)))
129 (every (lambda (input)
130 (find (match-input input) (gexp-inputs exp)))
131 (list drv coreutils %bootstrap-guile txt))
132 (let ((e0 `(display ,(derivation->output-path
133 (package-derivation %store coreutils))))
134 (e1 `(display ,(derivation->output-path
135 (package-derivation %store %bootstrap-guile))))
136 (e2 `(display ,(derivation->output-path drv)))
137 (e3 `(display ,txt)))
138 (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
139
140(test-assert "input list"
141 (let ((exp (gexp (display
142 '(ungexp (list %bootstrap-guile coreutils)))))
143 (guile (derivation->output-path
144 (package-derivation %store %bootstrap-guile)))
145 (cu (derivation->output-path
146 (package-derivation %store coreutils))))
147 (and (lset= equal?
148 `((,%bootstrap-guile "out") (,coreutils "out"))
149 (gexp-inputs exp))
150 (equal? `(display '(,guile ,cu))
151 (gexp->sexp* exp)))))
152
153(test-assert "input list splicing"
154 (let* ((inputs (list (list glibc "debug") %bootstrap-guile))
155 (outputs (list (derivation->output-path
156 (package-derivation %store glibc)
157 "debug")
158 (derivation->output-path
159 (package-derivation %store %bootstrap-guile))))
160 (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
161 (and (lset= equal?
162 `((,glibc "debug") (,%bootstrap-guile "out"))
163 (gexp-inputs exp))
164 (equal? (gexp->sexp* exp)
165 `(list ,@(cons 5 outputs))))))
166
167(test-assertm "gexp->file"
168 (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
169 (guile (package-file %bootstrap-guile))
170 (sexp (gexp->sexp exp))
171 (drv (gexp->file "foo" exp))
172 (out -> (derivation->output-path drv))
173 (done (built-derivations (list drv)))
174 (refs ((store-lift references) out)))
175 (return (and (equal? sexp (call-with-input-file out read))
176 (equal? (list guile) refs)))))
177
178(test-assertm "gexp->derivation"
179 (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
180 (exp -> (gexp
181 (begin
182 (mkdir (ungexp output))
183 (chdir (ungexp output))
184 (symlink
185 (string-append (ungexp %bootstrap-guile)
186 "/bin/guile")
187 "foo")
188 (symlink (ungexp file)
189 (ungexp output "2nd")))))
190 (drv (gexp->derivation "foo" exp))
191 (out -> (derivation->output-path drv))
192 (out2 -> (derivation->output-path drv "2nd"))
193 (done (built-derivations (list drv)))
194 (refs ((store-lift references) out))
195 (refs2 ((store-lift references) out2))
196 (guile (package-file %bootstrap-guile "bin/guile")))
197 (return (and (string=? (readlink (string-append out "/foo")) guile)
198 (string=? (readlink out2) file)
199 (equal? refs (list (dirname (dirname guile))))
200 (equal? refs2 (list file))))))
201
202(test-assertm "gexp->derivation, composed gexps"
203 (mlet* %store-monad ((exp0 -> (gexp (begin
204 (mkdir (ungexp output))
205 (chdir (ungexp output)))))
206 (exp1 -> (gexp (symlink
207 (string-append (ungexp %bootstrap-guile)
208 "/bin/guile")
209 "foo")))
210 (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
211 (drv (gexp->derivation "foo" exp))
212 (out -> (derivation->output-path drv))
213 (done (built-derivations (list drv)))
214 (guile (package-file %bootstrap-guile "bin/guile")))
215 (return (string=? (readlink (string-append out "/foo"))
216 guile))))
217
5d098459
LC
218(test-assertm "gexp->derivation, default system"
219 ;; The default system should be the one at '>>=' time, not the one at
220 ;; invocation time. See <http://bugs.gnu.org/18002>.
221 (let ((system (%current-system))
222 (mdrv (parameterize ((%current-system "foobar64-linux"))
223 (gexp->derivation "foo"
224 (gexp
225 (mkdir (ungexp output)))))))
226 (mlet %store-monad ((drv mdrv))
227 (return (string=? system (derivation-system drv))))))
228
68a61e9f
LC
229(test-assertm "gexp->derivation, cross-compilation"
230 (mlet* %store-monad ((target -> "mips64el-linux")
231 (exp -> (gexp (list (ungexp coreutils)
232 (ungexp output))))
233 (xdrv (gexp->derivation "foo" exp
234 #:target target))
235 (refs ((store-lift references)
236 (derivation-file-name xdrv)))
237 (xcu (package->cross-derivation coreutils
238 target))
239 (cu (package->derivation coreutils)))
240 (return (and (member (derivation-file-name xcu) refs)
241 (not (member (derivation-file-name cu) refs))))))
242
c17b5ab4 243(define shebang
6c9e7b2b 244 (string-append "#!" (derivation->output-path guile-for-build)
c17b5ab4
LC
245 "/bin/guile --no-auto-compile"))
246
247;; If we're going to hit the silly shebang limit (128 chars on Linux-based
248;; systems), then skip the following test.
249(test-skip (if (> (string-length shebang) 127) 1 0))
250
21b679f6
LC
251(test-assertm "gexp->script"
252 (mlet* %store-monad ((n -> (random (expt 2 50)))
253 (exp -> (gexp
254 (system*
255 (string-append (ungexp %bootstrap-guile)
256 "/bin/guile")
257 "-c" (object->string
258 '(display (expt (ungexp n) 2))))))
259 (drv (gexp->script "guile-thing" exp
260 #:guile %bootstrap-guile))
261 (out -> (derivation->output-path drv))
262 (done (built-derivations (list drv))))
263 (let* ((pipe (open-input-pipe out))
264 (str (get-string-all pipe)))
265 (return (and (zero? (close-pipe pipe))
266 (= (expt n 2) (string->number str)))))))
267
2cf0ea0d
LC
268(test-assert "printer"
269 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
270 \"/bin/uname\"\\) [[:xdigit:]]+>$"
271 (with-output-to-string
272 (lambda ()
273 (write
274 (gexp (string-append (ungexp coreutils)
275 "/bin/uname")))))))
276
277(test-assert "printer vs. ungexp-splicing"
278 (string-match "^#<gexp .* [[:xdigit:]]+>$"
279 (with-output-to-string
280 (lambda ()
281 ;; #~(begin #$@#~())
282 (write
283 (gexp (begin (ungexp-splicing (gexp ())))))))))
284
21b679f6
LC
285(test-equal "sugar"
286 '(gexp (foo (ungexp bar) (ungexp baz "out")
287 (ungexp (chbouib 42))
288 (ungexp-splicing (list x y z))))
289 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)))
290
291(test-end "gexp")
292
293\f
294(exit (= (test-runner-fail-count (test-runner-current)) 0))
295
296;; Local Variables:
297;; eval: (put 'test-assertm 'scheme-indent-function 1)
298;; End: