system: Tell module-init-tools where to look for modules.
[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)
32 #:use-module (ice-9 popen))
33
34;; Test the (guix gexp) module.
35
36(define %store
37 (open-connection))
38
39;; For white-box testing.
40(define gexp-inputs (@@ (guix gexp) gexp-inputs))
41(define gexp->sexp (@@ (guix gexp) gexp->sexp))
42
43(define guile-for-build
44 (package-derivation %store %bootstrap-guile))
45
46;; Make it the default.
47(%guile-for-build guile-for-build)
48
49(define (gexp->sexp* exp)
50 (run-with-store %store (gexp->sexp exp)
51 #:guile-for-build guile-for-build))
52
53(define-syntax-rule (test-assertm name exp)
54 (test-assert name
55 (run-with-store %store exp
56 #:guile-for-build guile-for-build)))
57
58\f
59(test-begin "gexp")
60
61(test-equal "no refs"
62 '(display "hello!")
63 (let ((exp (gexp (display "hello!"))))
64 (and (gexp? exp)
65 (null? (gexp-inputs exp))
66 (gexp->sexp* exp))))
67
68(test-equal "unquote"
69 '(display `(foo ,(+ 2 3)))
70 (let ((exp (gexp (display `(foo ,(+ 2 3))))))
71 (and (gexp? exp)
72 (null? (gexp-inputs exp))
73 (gexp->sexp* exp))))
74
75(test-assert "one input package"
76 (let ((exp (gexp (display (ungexp coreutils)))))
77 (and (gexp? exp)
78 (match (gexp-inputs exp)
79 (((p "out"))
80 (eq? p coreutils)))
81 (equal? `(display ,(derivation->output-path
82 (package-derivation %store coreutils)))
83 (gexp->sexp* exp)))))
84
79c0c8cd
LC
85(test-assert "one input origin"
86 (let ((exp (gexp (display (ungexp (package-source coreutils))))))
87 (and (gexp? exp)
88 (match (gexp-inputs exp)
89 (((o "out"))
90 (eq? o (package-source coreutils))))
91 (equal? `(display ,(derivation->output-path
92 (package-source-derivation
93 %store (package-source coreutils))))
94 (gexp->sexp* exp)))))
95
21b679f6
LC
96(test-assert "same input twice"
97 (let ((exp (gexp (begin
98 (display (ungexp coreutils))
99 (display (ungexp coreutils))))))
100 (and (gexp? exp)
101 (match (gexp-inputs exp)
102 (((p "out"))
103 (eq? p coreutils)))
104 (let ((e `(display ,(derivation->output-path
105 (package-derivation %store coreutils)))))
106 (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
107
108(test-assert "two input packages, one derivation, one file"
109 (let* ((drv (build-expression->derivation
110 %store "foo" 'bar
111 #:guile-for-build (package-derivation %store %bootstrap-guile)))
112 (txt (add-text-to-store %store "foo" "Hello, world!"))
113 (exp (gexp (begin
114 (display (ungexp coreutils))
115 (display (ungexp %bootstrap-guile))
116 (display (ungexp drv))
117 (display (ungexp txt))))))
118 (define (match-input thing)
119 (match-lambda
120 ((drv-or-pkg _ ...)
121 (eq? thing drv-or-pkg))))
122
123 (and (gexp? exp)
124 (= 4 (length (gexp-inputs exp)))
125 (every (lambda (input)
126 (find (match-input input) (gexp-inputs exp)))
127 (list drv coreutils %bootstrap-guile txt))
128 (let ((e0 `(display ,(derivation->output-path
129 (package-derivation %store coreutils))))
130 (e1 `(display ,(derivation->output-path
131 (package-derivation %store %bootstrap-guile))))
132 (e2 `(display ,(derivation->output-path drv)))
133 (e3 `(display ,txt)))
134 (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
135
136(test-assert "input list"
137 (let ((exp (gexp (display
138 '(ungexp (list %bootstrap-guile coreutils)))))
139 (guile (derivation->output-path
140 (package-derivation %store %bootstrap-guile)))
141 (cu (derivation->output-path
142 (package-derivation %store coreutils))))
143 (and (lset= equal?
144 `((,%bootstrap-guile "out") (,coreutils "out"))
145 (gexp-inputs exp))
146 (equal? `(display '(,guile ,cu))
147 (gexp->sexp* exp)))))
148
149(test-assert "input list splicing"
150 (let* ((inputs (list (list glibc "debug") %bootstrap-guile))
151 (outputs (list (derivation->output-path
152 (package-derivation %store glibc)
153 "debug")
154 (derivation->output-path
155 (package-derivation %store %bootstrap-guile))))
156 (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
157 (and (lset= equal?
158 `((,glibc "debug") (,%bootstrap-guile "out"))
159 (gexp-inputs exp))
160 (equal? (gexp->sexp* exp)
161 `(list ,@(cons 5 outputs))))))
162
163(test-assertm "gexp->file"
164 (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
165 (guile (package-file %bootstrap-guile))
166 (sexp (gexp->sexp exp))
167 (drv (gexp->file "foo" exp))
168 (out -> (derivation->output-path drv))
169 (done (built-derivations (list drv)))
170 (refs ((store-lift references) out)))
171 (return (and (equal? sexp (call-with-input-file out read))
172 (equal? (list guile) refs)))))
173
174(test-assertm "gexp->derivation"
175 (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
176 (exp -> (gexp
177 (begin
178 (mkdir (ungexp output))
179 (chdir (ungexp output))
180 (symlink
181 (string-append (ungexp %bootstrap-guile)
182 "/bin/guile")
183 "foo")
184 (symlink (ungexp file)
185 (ungexp output "2nd")))))
186 (drv (gexp->derivation "foo" exp))
187 (out -> (derivation->output-path drv))
188 (out2 -> (derivation->output-path drv "2nd"))
189 (done (built-derivations (list drv)))
190 (refs ((store-lift references) out))
191 (refs2 ((store-lift references) out2))
192 (guile (package-file %bootstrap-guile "bin/guile")))
193 (return (and (string=? (readlink (string-append out "/foo")) guile)
194 (string=? (readlink out2) file)
195 (equal? refs (list (dirname (dirname guile))))
196 (equal? refs2 (list file))))))
197
198(test-assertm "gexp->derivation, composed gexps"
199 (mlet* %store-monad ((exp0 -> (gexp (begin
200 (mkdir (ungexp output))
201 (chdir (ungexp output)))))
202 (exp1 -> (gexp (symlink
203 (string-append (ungexp %bootstrap-guile)
204 "/bin/guile")
205 "foo")))
206 (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
207 (drv (gexp->derivation "foo" exp))
208 (out -> (derivation->output-path drv))
209 (done (built-derivations (list drv)))
210 (guile (package-file %bootstrap-guile "bin/guile")))
211 (return (string=? (readlink (string-append out "/foo"))
212 guile))))
213
214(test-assertm "gexp->script"
215 (mlet* %store-monad ((n -> (random (expt 2 50)))
216 (exp -> (gexp
217 (system*
218 (string-append (ungexp %bootstrap-guile)
219 "/bin/guile")
220 "-c" (object->string
221 '(display (expt (ungexp n) 2))))))
222 (drv (gexp->script "guile-thing" exp
223 #:guile %bootstrap-guile))
224 (out -> (derivation->output-path drv))
225 (done (built-derivations (list drv))))
226 (let* ((pipe (open-input-pipe out))
227 (str (get-string-all pipe)))
228 (return (and (zero? (close-pipe pipe))
229 (= (expt n 2) (string->number str)))))))
230
231(test-equal "sugar"
232 '(gexp (foo (ungexp bar) (ungexp baz "out")
233 (ungexp (chbouib 42))
234 (ungexp-splicing (list x y z))))
235 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)))
236
237(test-end "gexp")
238
239\f
240(exit (= (test-runner-fail-count (test-runner-current)) 0))
241
242;; Local Variables:
243;; eval: (put 'test-assertm 'scheme-indent-function 1)
244;; End: