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