gexp: 'gexp->script' and 'gexp->file' have a new #:module-path parameter.
[jackhill/guix/guix.git] / tests / gexp.scm
CommitLineData
21b679f6 1;;; GNU Guix --- Functional package management for GNU
1ae16033 2;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
21b679f6
LC
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)
ef8de985 23 #:use-module (guix grafts)
21b679f6 24 #:use-module (guix derivations)
79c0c8cd 25 #:use-module (guix packages)
c1bc358f 26 #:use-module (guix tests)
4ff76a0a 27 #:use-module ((guix build utils) #:select (with-directory-excursion))
1ae16033 28 #:use-module ((guix utils) #:select (call-with-temporary-directory))
21b679f6
LC
29 #:use-module (gnu packages)
30 #:use-module (gnu packages base)
31 #:use-module (gnu packages bootstrap)
32 #:use-module (srfi srfi-1)
c8351d9a 33 #:use-module (srfi srfi-34)
21b679f6
LC
34 #:use-module (srfi srfi-64)
35 #:use-module (rnrs io ports)
36 #:use-module (ice-9 match)
2cf0ea0d 37 #:use-module (ice-9 regex)
0687fc9c
LC
38 #:use-module (ice-9 popen)
39 #:use-module (ice-9 ftw))
21b679f6
LC
40
41;; Test the (guix gexp) module.
42
43(define %store
c1bc358f 44 (open-connection-for-tests))
21b679f6 45
ef8de985
LC
46;; Globally disable grafts because they can trigger early builds.
47(%graft? #f)
48
21b679f6 49;; For white-box testing.
1f976033
LC
50(define (gexp-inputs x)
51 ((@@ (guix gexp) gexp-inputs) x))
52(define (gexp-native-inputs x)
53 ((@@ (guix gexp) gexp-native-inputs) x))
54(define (gexp-outputs x)
55 ((@@ (guix gexp) gexp-outputs) x))
56(define (gexp->sexp . x)
57 (apply (@@ (guix gexp) gexp->sexp) x))
21b679f6 58
667b2508 59(define* (gexp->sexp* exp #:optional target)
68a61e9f 60 (run-with-store %store (gexp->sexp exp
68a61e9f 61 #:target target)
c1bc358f 62 #:guile-for-build (%guile-for-build)))
21b679f6
LC
63
64(define-syntax-rule (test-assertm name exp)
65 (test-assert name
66 (run-with-store %store exp
c1bc358f 67 #:guile-for-build (%guile-for-build))))
21b679f6
LC
68
69\f
70(test-begin "gexp")
71
72(test-equal "no refs"
73 '(display "hello!")
74 (let ((exp (gexp (display "hello!"))))
75 (and (gexp? exp)
76 (null? (gexp-inputs exp))
77 (gexp->sexp* exp))))
78
79(test-equal "unquote"
80 '(display `(foo ,(+ 2 3)))
81 (let ((exp (gexp (display `(foo ,(+ 2 3))))))
82 (and (gexp? exp)
83 (null? (gexp-inputs exp))
84 (gexp->sexp* exp))))
85
86(test-assert "one input package"
87 (let ((exp (gexp (display (ungexp coreutils)))))
88 (and (gexp? exp)
89 (match (gexp-inputs exp)
90 (((p "out"))
91 (eq? p coreutils)))
92 (equal? `(display ,(derivation->output-path
93 (package-derivation %store coreutils)))
94 (gexp->sexp* exp)))))
5e2e4a51
LC
95
96(test-assert "one input package, dotted list"
97 (let ((exp (gexp (coreutils . (ungexp coreutils)))))
98 (and (gexp? exp)
99 (match (gexp-inputs exp)
100 (((p "out"))
101 (eq? p coreutils)))
102 (equal? `(coreutils . ,(derivation->output-path
103 (package-derivation %store coreutils)))
104 (gexp->sexp* exp)))))
21b679f6 105
79c0c8cd
LC
106(test-assert "one input origin"
107 (let ((exp (gexp (display (ungexp (package-source coreutils))))))
108 (and (gexp? exp)
109 (match (gexp-inputs exp)
110 (((o "out"))
111 (eq? o (package-source coreutils))))
112 (equal? `(display ,(derivation->output-path
113 (package-source-derivation
114 %store (package-source coreutils))))
115 (gexp->sexp* exp)))))
116
d9ae938f
LC
117(test-assert "one local file"
118 (let* ((file (search-path %load-path "guix.scm"))
119 (local (local-file file))
120 (exp (gexp (display (ungexp local))))
020f3e41 121 (intd (add-to-store %store (basename file) #f
d9ae938f
LC
122 "sha256" file)))
123 (and (gexp? exp)
124 (match (gexp-inputs exp)
125 (((x "out"))
126 (eq? x local)))
127 (equal? `(display ,intd) (gexp->sexp* exp)))))
128
7833db1f
LC
129(test-assert "one local file, symlink"
130 (let ((file (search-path %load-path "guix.scm"))
131 (link (tmpnam)))
132 (dynamic-wind
133 (const #t)
134 (lambda ()
135 (symlink (canonicalize-path file) link)
136 (let* ((local (local-file link "my-file" #:recursive? #f))
137 (exp (gexp (display (ungexp local))))
138 (intd (add-to-store %store "my-file" #f
139 "sha256" file)))
140 (and (gexp? exp)
141 (match (gexp-inputs exp)
142 (((x "out"))
143 (eq? x local)))
144 (equal? `(display ,intd) (gexp->sexp* exp)))))
145 (lambda ()
146 (false-if-exception (delete-file link))))))
147
4ff76a0a
LC
148(test-equal "local-file, relative file name"
149 (canonicalize-path (search-path %load-path "guix/base32.scm"))
150 (let ((directory (dirname (search-path %load-path
151 "guix/build-system/gnu.scm"))))
152 (with-directory-excursion directory
153 (let ((file (local-file "../guix/base32.scm")))
154 (local-file-absolute-file-name file)))))
155
0687fc9c
LC
156(test-assertm "local-file, #:select?"
157 (mlet* %store-monad ((select? -> (lambda (file stat)
158 (member (basename file)
159 '("guix.scm" "tests"
160 "gexp.scm"))))
161 (file -> (local-file ".." "directory"
162 #:recursive? #t
163 #:select? select?))
164 (dir (lower-object file)))
165 (return (and (store-path? dir)
166 (equal? (scandir dir)
167 '("." ".." "guix.scm" "tests"))
168 (equal? (scandir (string-append dir "/tests"))
169 '("." ".." "gexp.scm"))))))
170
558e8b11
LC
171(test-assert "one plain file"
172 (let* ((file (plain-file "hi" "Hello, world!"))
173 (exp (gexp (display (ungexp file))))
174 (expected (add-text-to-store %store "hi" "Hello, world!")))
175 (and (gexp? exp)
176 (match (gexp-inputs exp)
177 (((x "out"))
178 (eq? x file)))
179 (equal? `(display ,expected) (gexp->sexp* exp)))))
180
21b679f6
LC
181(test-assert "same input twice"
182 (let ((exp (gexp (begin
183 (display (ungexp coreutils))
184 (display (ungexp coreutils))))))
185 (and (gexp? exp)
186 (match (gexp-inputs exp)
187 (((p "out"))
188 (eq? p coreutils)))
189 (let ((e `(display ,(derivation->output-path
190 (package-derivation %store coreutils)))))
191 (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
192
193(test-assert "two input packages, one derivation, one file"
194 (let* ((drv (build-expression->derivation
195 %store "foo" 'bar
196 #:guile-for-build (package-derivation %store %bootstrap-guile)))
197 (txt (add-text-to-store %store "foo" "Hello, world!"))
198 (exp (gexp (begin
199 (display (ungexp coreutils))
200 (display (ungexp %bootstrap-guile))
201 (display (ungexp drv))
202 (display (ungexp txt))))))
203 (define (match-input thing)
204 (match-lambda
205 ((drv-or-pkg _ ...)
206 (eq? thing drv-or-pkg))))
207
208 (and (gexp? exp)
209 (= 4 (length (gexp-inputs exp)))
210 (every (lambda (input)
211 (find (match-input input) (gexp-inputs exp)))
212 (list drv coreutils %bootstrap-guile txt))
213 (let ((e0 `(display ,(derivation->output-path
214 (package-derivation %store coreutils))))
215 (e1 `(display ,(derivation->output-path
216 (package-derivation %store %bootstrap-guile))))
217 (e2 `(display ,(derivation->output-path drv)))
218 (e3 `(display ,txt)))
219 (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
220
a9e5e92f
LC
221(test-assert "file-append"
222 (let* ((drv (package-derivation %store %bootstrap-guile))
223 (fa (file-append %bootstrap-guile "/bin/guile"))
224 (exp #~(here we go #$fa)))
225 (and (match (gexp->sexp* exp)
226 (('here 'we 'go (? string? result))
227 (string=? result
228 (string-append (derivation->output-path drv)
229 "/bin/guile"))))
230 (match (gexp-inputs exp)
231 (((thing "out"))
232 (eq? thing fa))))))
233
234(test-assert "file-append, output"
235 (let* ((drv (package-derivation %store glibc))
236 (fa (file-append glibc "/lib" "/debug"))
237 (exp #~(foo #$fa:debug)))
238 (and (match (gexp->sexp* exp)
239 (('foo (? string? result))
240 (string=? result
241 (string-append (derivation->output-path drv "debug")
242 "/lib/debug"))))
243 (match (gexp-inputs exp)
244 (((thing "debug"))
245 (eq? thing fa))))))
246
247(test-assert "file-append, nested"
248 (let* ((drv (package-derivation %store glibc))
249 (dir (file-append glibc "/bin"))
250 (slash (file-append dir "/"))
251 (file (file-append slash "getent"))
252 (exp #~(foo #$file)))
253 (and (match (gexp->sexp* exp)
254 (('foo (? string? result))
255 (string=? result
256 (string-append (derivation->output-path drv)
257 "/bin/getent"))))
258 (match (gexp-inputs exp)
259 (((thing "out"))
260 (eq? thing file))))))
261
667b2508
LC
262(test-assert "ungexp + ungexp-native"
263 (let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
264 (ungexp coreutils)
265 (ungexp-native glibc)
266 (ungexp binutils))))
267 (target "mips64el-linux")
268 (guile (derivation->output-path
269 (package-derivation %store %bootstrap-guile)))
270 (cu (derivation->output-path
271 (package-cross-derivation %store coreutils target)))
272 (libc (derivation->output-path
273 (package-derivation %store glibc)))
274 (bu (derivation->output-path
275 (package-cross-derivation %store binutils target))))
276 (and (lset= equal?
277 `((,%bootstrap-guile "out") (,glibc "out"))
278 (gexp-native-inputs exp))
279 (lset= equal?
280 `((,coreutils "out") (,binutils "out"))
281 (gexp-inputs exp))
282 (equal? `(list ,guile ,cu ,libc ,bu)
283 (gexp->sexp* exp target)))))
284
1123759b
LC
285(test-equal "ungexp + ungexp-native, nested"
286 (list `((,%bootstrap-guile "out")) '<> `((,coreutils "out")))
287 (let* ((exp (gexp (list (ungexp-native (gexp (ungexp coreutils)))
288 (ungexp %bootstrap-guile)))))
289 (list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
290
5b14a790
LC
291(test-equal "ungexp + ungexp-native, nested, special mixture"
292 `(() <> ((,coreutils "out")))
293
294 ;; (gexp-native-inputs exp) used to return '(), wrongfully.
295 (let* ((foo (gexp (foo (ungexp-native coreutils))))
296 (exp (gexp (bar (ungexp foo)))))
297 (list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
298
21b679f6
LC
299(test-assert "input list"
300 (let ((exp (gexp (display
301 '(ungexp (list %bootstrap-guile coreutils)))))
302 (guile (derivation->output-path
303 (package-derivation %store %bootstrap-guile)))
304 (cu (derivation->output-path
305 (package-derivation %store coreutils))))
306 (and (lset= equal?
307 `((,%bootstrap-guile "out") (,coreutils "out"))
308 (gexp-inputs exp))
309 (equal? `(display '(,guile ,cu))
310 (gexp->sexp* exp)))))
311
667b2508
LC
312(test-assert "input list + ungexp-native"
313 (let* ((target "mips64el-linux")
314 (exp (gexp (display
315 (cons '(ungexp-native (list %bootstrap-guile coreutils))
316 '(ungexp (list glibc binutils))))))
317 (guile (derivation->output-path
318 (package-derivation %store %bootstrap-guile)))
319 (cu (derivation->output-path
320 (package-derivation %store coreutils)))
321 (xlibc (derivation->output-path
322 (package-cross-derivation %store glibc target)))
323 (xbu (derivation->output-path
324 (package-cross-derivation %store binutils target))))
325 (and (lset= equal?
326 `((,%bootstrap-guile "out") (,coreutils "out"))
327 (gexp-native-inputs exp))
328 (lset= equal?
329 `((,glibc "out") (,binutils "out"))
330 (gexp-inputs exp))
331 (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu)))
332 (gexp->sexp* exp target)))))
333
21b679f6 334(test-assert "input list splicing"
a482cfdc 335 (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
21b679f6
LC
336 (outputs (list (derivation->output-path
337 (package-derivation %store glibc)
338 "debug")
339 (derivation->output-path
340 (package-derivation %store %bootstrap-guile))))
341 (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
342 (and (lset= equal?
343 `((,glibc "debug") (,%bootstrap-guile "out"))
344 (gexp-inputs exp))
345 (equal? (gexp->sexp* exp)
346 `(list ,@(cons 5 outputs))))))
347
667b2508 348(test-assert "input list splicing + ungexp-native-splicing"
5b14a790
LC
349 (let* ((inputs (list (gexp-input glibc "debug" #:native? #t)
350 %bootstrap-guile))
0dbea56b
LC
351 (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
352 (and (lset= equal?
353 `((,glibc "debug") (,%bootstrap-guile "out"))
354 (gexp-native-inputs exp))
355 (null? (gexp-inputs exp))
356 (equal? (gexp->sexp* exp) ;native
357 (gexp->sexp* exp "mips64el-linux")))))
358
578dfbe0
LC
359(test-assert "gexp list splicing + ungexp-splicing"
360 (let* ((inner (gexp (ungexp-native glibc)))
361 (exp (gexp (list (ungexp-splicing (list inner))))))
362 (and (equal? `((,glibc "out")) (gexp-native-inputs exp))
363 (null? (gexp-inputs exp))
364 (equal? (gexp->sexp* exp) ;native
365 (gexp->sexp* exp "mips64el-linux")))))
366
4b23c466
LC
367(test-equal "output list"
368 2
369 (let ((exp (gexp (begin (mkdir (ungexp output))
370 (mkdir (ungexp output "bar"))))))
371 (length (gexp-outputs exp)))) ;XXX: <output-ref> is private
372
373(test-assert "output list, combined gexps"
374 (let* ((exp0 (gexp (mkdir (ungexp output))))
375 (exp1 (gexp (mkdir (ungexp output "foo"))))
376 (exp2 (gexp (begin (display "hi!") (ungexp exp0) (ungexp exp1)))))
377 (and (lset= equal?
378 (append (gexp-outputs exp0) (gexp-outputs exp1))
379 (gexp-outputs exp2))
380 (= 2 (length (gexp-outputs exp2))))))
381
7e75a673
LC
382(test-equal "output list, combined gexps, duplicate output"
383 1
384 (let* ((exp0 (gexp (mkdir (ungexp output))))
385 (exp1 (gexp (begin (mkdir (ungexp output)) (ungexp exp0))))
386 (exp2 (gexp (begin (mkdir (ungexp output)) (ungexp exp1)))))
387 (length (gexp-outputs exp2))))
388
f9efe568
LC
389(test-assert "output list + ungexp-splicing list, combined gexps"
390 (let* ((exp0 (gexp (mkdir (ungexp output))))
391 (exp1 (gexp (mkdir (ungexp output "foo"))))
392 (exp2 (gexp (begin (display "hi!")
393 (ungexp-splicing (list exp0 exp1))))))
394 (and (lset= equal?
395 (append (gexp-outputs exp0) (gexp-outputs exp1))
396 (gexp-outputs exp2))
397 (= 2 (length (gexp-outputs exp2))))))
398
21b679f6
LC
399(test-assertm "gexp->file"
400 (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
401 (guile (package-file %bootstrap-guile))
402 (sexp (gexp->sexp exp))
403 (drv (gexp->file "foo" exp))
404 (out -> (derivation->output-path drv))
405 (done (built-derivations (list drv)))
e74f64b9 406 (refs (references* out)))
21b679f6
LC
407 (return (and (equal? sexp (call-with-input-file out read))
408 (equal? (list guile) refs)))))
409
a9e5e92f
LC
410(test-assertm "gexp->file + file-append"
411 (mlet* %store-monad ((exp -> #~#$(file-append %bootstrap-guile
412 "/bin/guile"))
413 (guile (package-file %bootstrap-guile))
414 (drv (gexp->file "foo" exp))
415 (out -> (derivation->output-path drv))
416 (done (built-derivations (list drv)))
e74f64b9 417 (refs (references* out)))
a9e5e92f
LC
418 (return (and (equal? (string-append guile "/bin/guile")
419 (call-with-input-file out read))
420 (equal? (list guile) refs)))))
421
21b679f6
LC
422(test-assertm "gexp->derivation"
423 (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
424 (exp -> (gexp
425 (begin
426 (mkdir (ungexp output))
427 (chdir (ungexp output))
428 (symlink
429 (string-append (ungexp %bootstrap-guile)
430 "/bin/guile")
431 "foo")
432 (symlink (ungexp file)
433 (ungexp output "2nd")))))
434 (drv (gexp->derivation "foo" exp))
435 (out -> (derivation->output-path drv))
436 (out2 -> (derivation->output-path drv "2nd"))
437 (done (built-derivations (list drv)))
e74f64b9
LC
438 (refs (references* out))
439 (refs2 (references* out2))
21b679f6
LC
440 (guile (package-file %bootstrap-guile "bin/guile")))
441 (return (and (string=? (readlink (string-append out "/foo")) guile)
442 (string=? (readlink out2) file)
443 (equal? refs (list (dirname (dirname guile))))
444 (equal? refs2 (list file))))))
445
ce45eb4c 446(test-assertm "gexp->derivation vs. grafts"
ef8de985
LC
447 (mlet* %store-monad ((graft? (set-grafting #f))
448 (p0 -> (dummy-package "dummy"
ce45eb4c
LC
449 (arguments
450 '(#:implicit-inputs? #f))))
451 (r -> (package (inherit p0) (name "DuMMY")))
452 (p1 -> (package (inherit p0) (replacement r)))
453 (exp0 -> (gexp (frob (ungexp p0) (ungexp output))))
454 (exp1 -> (gexp (frob (ungexp p1) (ungexp output))))
455 (void (set-guile-for-build %bootstrap-guile))
ef8de985
LC
456 (drv0 (gexp->derivation "t" exp0 #:graft? #t))
457 (drv1 (gexp->derivation "t" exp1 #:graft? #t))
458 (drv1* (gexp->derivation "t" exp1 #:graft? #f))
459 (_ (set-grafting graft?)))
ce45eb4c
LC
460 (return (and (not (string=? (derivation->output-path drv0)
461 (derivation->output-path drv1)))
462 (string=? (derivation->output-path drv0)
463 (derivation->output-path drv1*))))))
464
21b679f6
LC
465(test-assertm "gexp->derivation, composed gexps"
466 (mlet* %store-monad ((exp0 -> (gexp (begin
467 (mkdir (ungexp output))
468 (chdir (ungexp output)))))
469 (exp1 -> (gexp (symlink
470 (string-append (ungexp %bootstrap-guile)
471 "/bin/guile")
472 "foo")))
473 (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
474 (drv (gexp->derivation "foo" exp))
475 (out -> (derivation->output-path drv))
476 (done (built-derivations (list drv)))
477 (guile (package-file %bootstrap-guile "bin/guile")))
478 (return (string=? (readlink (string-append out "/foo"))
479 guile))))
480
5d098459
LC
481(test-assertm "gexp->derivation, default system"
482 ;; The default system should be the one at '>>=' time, not the one at
483 ;; invocation time. See <http://bugs.gnu.org/18002>.
484 (let ((system (%current-system))
485 (mdrv (parameterize ((%current-system "foobar64-linux"))
486 (gexp->derivation "foo"
487 (gexp
488 (mkdir (ungexp output)))))))
489 (mlet %store-monad ((drv mdrv))
490 (return (string=? system (derivation-system drv))))))
491
d9ae938f
LC
492(test-assertm "gexp->derivation, local-file"
493 (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
020f3e41 494 (intd (interned-file file #:recursive? #f))
d9ae938f
LC
495 (local -> (local-file file))
496 (exp -> (gexp (begin
497 (stat (ungexp local))
498 (symlink (ungexp local)
499 (ungexp output)))))
500 (drv (gexp->derivation "local-file" exp)))
501 (mbegin %store-monad
502 (built-derivations (list drv))
503 (return (string=? (readlink (derivation->output-path drv))
504 intd)))))
505
68a61e9f
LC
506(test-assertm "gexp->derivation, cross-compilation"
507 (mlet* %store-monad ((target -> "mips64el-linux")
508 (exp -> (gexp (list (ungexp coreutils)
509 (ungexp output))))
510 (xdrv (gexp->derivation "foo" exp
511 #:target target))
e74f64b9 512 (refs (references*
68a61e9f
LC
513 (derivation-file-name xdrv)))
514 (xcu (package->cross-derivation coreutils
515 target))
516 (cu (package->derivation coreutils)))
517 (return (and (member (derivation-file-name xcu) refs)
518 (not (member (derivation-file-name cu) refs))))))
519
667b2508
LC
520(test-assertm "gexp->derivation, ungexp-native"
521 (mlet* %store-monad ((target -> "mips64el-linux")
522 (exp -> (gexp (list (ungexp-native coreutils)
523 (ungexp output))))
524 (xdrv (gexp->derivation "foo" exp
525 #:target target))
526 (drv (gexp->derivation "foo" exp)))
527 (return (string=? (derivation-file-name drv)
528 (derivation-file-name xdrv)))))
529
530(test-assertm "gexp->derivation, ungexp + ungexp-native"
531 (mlet* %store-monad ((target -> "mips64el-linux")
532 (exp -> (gexp (list (ungexp-native coreutils)
533 (ungexp glibc)
534 (ungexp output))))
535 (xdrv (gexp->derivation "foo" exp
536 #:target target))
e74f64b9 537 (refs (references*
667b2508
LC
538 (derivation-file-name xdrv)))
539 (xglibc (package->cross-derivation glibc target))
540 (cu (package->derivation coreutils)))
541 (return (and (member (derivation-file-name cu) refs)
542 (member (derivation-file-name xglibc) refs)))))
543
544(test-assertm "gexp->derivation, ungexp-native + composed gexps"
545 (mlet* %store-monad ((target -> "mips64el-linux")
546 (exp0 -> (gexp (list 1 2
547 (ungexp coreutils))))
548 (exp -> (gexp (list 0 (ungexp-native exp0))))
549 (xdrv (gexp->derivation "foo" exp
550 #:target target))
551 (drv (gexp->derivation "foo" exp)))
552 (return (string=? (derivation-file-name drv)
553 (derivation-file-name xdrv)))))
554
6fd1a796
LC
555(test-assertm "gexp->derivation, store copy"
556 (let ((build-one #~(call-with-output-file #$output
557 (lambda (port)
558 (display "This is the one." port))))
559 (build-two (lambda (one)
560 #~(begin
561 (mkdir #$output)
562 (symlink #$one (string-append #$output "/one"))
563 (call-with-output-file (string-append #$output "/two")
564 (lambda (port)
565 (display "This is the second one." port))))))
b53833b2
LC
566 (build-drv #~(begin
567 (use-modules (guix build store-copy))
6fd1a796 568
b53833b2
LC
569 (mkdir #$output)
570 (populate-store '("graph") #$output))))
6fd1a796
LC
571 (mlet* %store-monad ((one (gexp->derivation "one" build-one))
572 (two (gexp->derivation "two" (build-two one)))
b53833b2 573 (drv (gexp->derivation "store-copy" build-drv
6fd1a796 574 #:references-graphs
b53833b2 575 `(("graph" ,two))
6fd1a796
LC
576 #:modules
577 '((guix build store-copy)
578 (guix build utils))))
579 (ok? (built-derivations (list drv)))
580 (out -> (derivation->output-path drv)))
581 (let ((one (derivation->output-path one))
582 (two (derivation->output-path two)))
583 (return (and ok?
584 (file-exists? (string-append out "/" one))
585 (file-exists? (string-append out "/" two))
586 (file-exists? (string-append out "/" two "/two"))
587 (string=? (readlink (string-append out "/" two "/one"))
588 one)))))))
589
aa72d9af
LC
590(test-assertm "imported-files"
591 (mlet* %store-monad
592 ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
593 ("a/b/c" . ,(search-path %load-path
594 "guix/derivations.scm"))
595 ("p/q" . ,(search-path %load-path "guix.scm"))
596 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
597 (drv (imported-files files)))
598 (mbegin %store-monad
599 (built-derivations (list drv))
600 (let ((dir (derivation->output-path drv)))
601 (return
602 (every (match-lambda
603 ((path . source)
604 (equal? (call-with-input-file (string-append dir "/" path)
605 get-bytevector-all)
606 (call-with-input-file source
607 get-bytevector-all))))
608 files))))))
609
d938a58b
LC
610(test-assertm "imported-files with file-like objects"
611 (mlet* %store-monad ((plain -> (plain-file "foo" "bar!"))
612 (q-scm -> (search-path %load-path "ice-9/q.scm"))
613 (files -> `(("a/b/c" . ,q-scm)
614 ("p/q" . ,plain)))
615 (drv (imported-files files)))
616 (mbegin %store-monad
617 (built-derivations (list drv))
618 (mlet %store-monad ((dir -> (derivation->output-path drv))
619 (plain* (text-file "foo" "bar!"))
620 (q-scm* (interned-file q-scm "c")))
621 (return
622 (and (string=? (readlink (string-append dir "/a/b/c"))
623 q-scm*)
624 (string=? (readlink (string-append dir "/p/q"))
625 plain*)))))))
626
0bb9929e
LC
627(test-equal "gexp-modules & ungexp"
628 '((bar) (foo))
629 ((@@ (guix gexp) gexp-modules)
630 #~(foo #$(with-imported-modules '((foo)) #~+)
631 #+(with-imported-modules '((bar)) #~-))))
632
633(test-equal "gexp-modules & ungexp-splicing"
634 '((foo) (bar))
635 ((@@ (guix gexp) gexp-modules)
636 #~(foo #$@(list (with-imported-modules '((foo)) #~+)
637 (with-imported-modules '((bar)) #~-)))))
638
2363bdd7
LC
639(test-equal "gexp-modules and literal Scheme object"
640 '()
641 (gexp-modules #t))
642
aa72d9af
LC
643(test-assertm "gexp->derivation #:modules"
644 (mlet* %store-monad
645 ((build -> #~(begin
646 (use-modules (guix build utils))
647 (mkdir-p (string-append #$output "/guile/guix/nix"))
648 #t))
649 (drv (gexp->derivation "test-with-modules" build
650 #:modules '((guix build utils)))))
651 (mbegin %store-monad
652 (built-derivations (list drv))
653 (let* ((p (derivation->output-path drv))
654 (s (stat (string-append p "/guile/guix/nix"))))
655 (return (eq? (stat:type s) 'directory))))))
656
0bb9929e
LC
657(test-assertm "gexp->derivation & with-imported-modules"
658 ;; Same test as above, but using 'with-imported-modules'.
659 (mlet* %store-monad
660 ((build -> (with-imported-modules '((guix build utils))
661 #~(begin
662 (use-modules (guix build utils))
663 (mkdir-p (string-append #$output "/guile/guix/nix"))
664 #t)))
665 (drv (gexp->derivation "test-with-modules" build)))
666 (mbegin %store-monad
667 (built-derivations (list drv))
668 (let* ((p (derivation->output-path drv))
669 (s (stat (string-append p "/guile/guix/nix"))))
670 (return (eq? (stat:type s) 'directory))))))
671
672(test-assertm "gexp->derivation & nested with-imported-modules"
673 (mlet* %store-monad
674 ((build1 -> (with-imported-modules '((guix build utils))
675 #~(begin
676 (use-modules (guix build utils))
677 (mkdir-p (string-append #$output "/guile/guix/nix"))
678 #t)))
679 (build2 -> (with-imported-modules '((guix build bournish))
680 #~(begin
681 (use-modules (guix build bournish)
682 (system base compile))
683 #+build1
684 (call-with-output-file (string-append #$output "/b")
685 (lambda (port)
686 (write
687 (read-and-compile (open-input-string "cd /foo")
688 #:from %bournish-language
689 #:to 'scheme)
690 port))))))
691 (drv (gexp->derivation "test-with-modules" build2)))
692 (mbegin %store-monad
693 (built-derivations (list drv))
694 (let* ((p (derivation->output-path drv))
695 (s (stat (string-append p "/guile/guix/nix")))
696 (b (string-append p "/b")))
697 (return (and (eq? (stat:type s) 'directory)
698 (equal? '(chdir "/foo")
699 (call-with-input-file b read))))))))
700
d938a58b
LC
701(test-assertm "gexp->derivation & with-imported-module & computed module"
702 (mlet* %store-monad
703 ((module -> (scheme-file "x" #~(begin
704 (define-module (foo bar)
705 #:export (the-answer))
706
707 (define the-answer 42))))
708 (build -> (with-imported-modules `(((foo bar) => ,module)
709 (guix build utils))
710 #~(begin
711 (use-modules (guix build utils)
712 (foo bar))
713 mkdir-p
714 (call-with-output-file #$output
715 (lambda (port)
716 (write the-answer port))))))
717 (drv (gexp->derivation "thing" build))
718 (out -> (derivation->output-path drv)))
719 (mbegin %store-monad
720 (built-derivations (list drv))
721 (return (= 42 (call-with-input-file out read))))))
722
b53833b2
LC
723(test-assertm "gexp->derivation #:references-graphs"
724 (mlet* %store-monad
72cd8ec0 725 ((one (text-file "one" (random-text)))
b53833b2
LC
726 (two (gexp->derivation "two"
727 #~(symlink #$one #$output:chbouib)))
66a35ceb
LC
728 (build -> (with-imported-modules '((guix build store-copy)
729 (guix build utils))
730 #~(begin
731 (use-modules (guix build store-copy))
732 (with-output-to-file #$output
733 (lambda ()
734 (write (call-with-input-file "guile"
735 read-reference-graph))))
736 (with-output-to-file #$output:one
737 (lambda ()
738 (write (call-with-input-file "one"
739 read-reference-graph))))
740 (with-output-to-file #$output:two
741 (lambda ()
742 (write (call-with-input-file "two"
743 read-reference-graph)))))))
744 (drv (gexp->derivation "ref-graphs" build
b53833b2
LC
745 #:references-graphs `(("one" ,one)
746 ("two" ,two "chbouib")
66a35ceb 747 ("guile" ,%bootstrap-guile))))
b53833b2
LC
748 (ok? (built-derivations (list drv)))
749 (guile-drv (package->derivation %bootstrap-guile))
686784d0
LC
750 (bash (interned-file (search-bootstrap-binary "bash"
751 (%current-system))
752 "bash" #:recursive? #t))
b53833b2
LC
753 (g-one -> (derivation->output-path drv "one"))
754 (g-two -> (derivation->output-path drv "two"))
755 (g-guile -> (derivation->output-path drv)))
756 (return (and ok?
757 (equal? (call-with-input-file g-one read) (list one))
72cd8ec0
LC
758 (lset= string=?
759 (call-with-input-file g-two read)
760 (list one (derivation->output-path two "chbouib")))
686784d0
LC
761
762 ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash.
72cd8ec0
LC
763 (lset= string=?
764 (call-with-input-file g-guile read)
765 (list (derivation->output-path guile-drv) bash))))))
b53833b2 766
c8351d9a
LC
767(test-assertm "gexp->derivation #:allowed-references"
768 (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
769 #~(begin
770 (mkdir #$output)
771 (chdir #$output)
772 (symlink #$output "self")
773 (symlink #$%bootstrap-guile
774 "guile"))
775 #:allowed-references
776 (list "out" %bootstrap-guile))))
777 (built-derivations (list drv))))
778
accb682c
LC
779(test-assertm "gexp->derivation #:allowed-references, specific output"
780 (mlet* %store-monad ((in (gexp->derivation "thing"
781 #~(begin
782 (mkdir #$output:ok)
783 (mkdir #$output:not-ok))))
784 (drv (gexp->derivation "allowed-refs"
785 #~(begin
786 (pk #$in:not-ok)
787 (mkdir #$output)
788 (chdir #$output)
789 (symlink #$output "self")
790 (symlink #$in:ok "ok"))
791 #:allowed-references
792 (list "out"
793 (gexp-input in "ok")))))
794 (built-derivations (list drv))))
795
c8351d9a
LC
796(test-assert "gexp->derivation #:allowed-references, disallowed"
797 (let ((drv (run-with-store %store
798 (gexp->derivation "allowed-refs"
799 #~(begin
800 (mkdir #$output)
801 (chdir #$output)
802 (symlink #$%bootstrap-guile "guile"))
803 #:allowed-references '()))))
804 (guard (c ((nix-protocol-error? c) #t))
805 (build-derivations %store (list drv))
806 #f)))
807
3f4ecf32
LC
808(test-assertm "gexp->derivation #:disallowed-references, allowed"
809 (mlet %store-monad ((drv (gexp->derivation "disallowed-refs"
810 #~(begin
811 (mkdir #$output)
812 (chdir #$output)
813 (symlink #$output "self")
814 (symlink #$%bootstrap-guile
815 "guile"))
816 #:disallowed-references '())))
817 (built-derivations (list drv))))
818
819
820(test-assert "gexp->derivation #:disallowed-references"
821 (let ((drv (run-with-store %store
822 (gexp->derivation "disallowed-refs"
823 #~(begin
824 (mkdir #$output)
825 (chdir #$output)
826 (symlink #$%bootstrap-guile "guile"))
827 #:disallowed-references (list %bootstrap-guile)))))
828 (guard (c ((nix-protocol-error? c) #t))
829 (build-derivations %store (list drv))
830 #f)))
831
c17b5ab4 832(define shebang
c1bc358f 833 (string-append "#!" (derivation->output-path (%guile-for-build))
c17b5ab4
LC
834 "/bin/guile --no-auto-compile"))
835
836;; If we're going to hit the silly shebang limit (128 chars on Linux-based
837;; systems), then skip the following test.
47b3124a 838(test-skip (if (> (string-length shebang) 127) 2 0))
c17b5ab4 839
21b679f6
LC
840(test-assertm "gexp->script"
841 (mlet* %store-monad ((n -> (random (expt 2 50)))
842 (exp -> (gexp
843 (system*
844 (string-append (ungexp %bootstrap-guile)
845 "/bin/guile")
846 "-c" (object->string
847 '(display (expt (ungexp n) 2))))))
848 (drv (gexp->script "guile-thing" exp
849 #:guile %bootstrap-guile))
850 (out -> (derivation->output-path drv))
851 (done (built-derivations (list drv))))
852 (let* ((pipe (open-input-pipe out))
853 (str (get-string-all pipe)))
854 (return (and (zero? (close-pipe pipe))
855 (= (expt n 2) (string->number str)))))))
856
1ae16033
LC
857(test-assertm "gexp->script #:module-path"
858 (call-with-temporary-directory
859 (lambda (directory)
860 (define str
861 "Fake (guix base32) module!")
862
863 (mkdir (string-append directory "/guix"))
864 (call-with-output-file (string-append directory "/guix/base32.scm")
865 (lambda (port)
866 (write `(begin (define-module (guix base32))
867 (define-public %fake! ,str))
868 port)))
869
870 (mlet* %store-monad ((exp -> (with-imported-modules '((guix base32))
871 (gexp (begin
872 (use-modules (guix base32))
873 (write (list %load-path
874 %fake!))))))
875 (drv (gexp->script "guile-thing" exp
876 #:guile %bootstrap-guile
877 #:module-path (list directory)))
878 (out -> (derivation->output-path drv))
879 (done (built-derivations (list drv))))
880 (let* ((pipe (open-input-pipe out))
881 (data (read pipe)))
882 (return (and (zero? (close-pipe pipe))
883 (match data
884 ((load-path str*)
885 (and (string=? str* str)
886 (not (member directory load-path))))))))))))
887
15a01c72
LC
888(test-assertm "program-file"
889 (let* ((n (random (expt 2 50)))
0bb9929e
LC
890 (exp (with-imported-modules '((guix build utils))
891 (gexp (begin
892 (use-modules (guix build utils))
893 (display (ungexp n))))))
15a01c72 894 (file (program-file "program" exp
15a01c72
LC
895 #:guile %bootstrap-guile)))
896 (mlet* %store-monad ((drv (lower-object file))
897 (out -> (derivation->output-path drv)))
898 (mbegin %store-monad
899 (built-derivations (list drv))
900 (let* ((pipe (open-input-pipe out))
901 (str (get-string-all pipe)))
902 (return (and (zero? (close-pipe pipe))
903 (= n (string->number str)))))))))
904
e1c153e0
LC
905(test-assertm "scheme-file"
906 (let* ((text (plain-file "foo" "Hello, world!"))
907 (scheme (scheme-file "bar" #~(list "foo" #$text))))
908 (mlet* %store-monad ((drv (lower-object scheme))
909 (text (lower-object text))
910 (out -> (derivation->output-path drv)))
911 (mbegin %store-monad
912 (built-derivations (list drv))
e74f64b9 913 (mlet %store-monad ((refs (references* out)))
e1c153e0
LC
914 (return (and (equal? refs (list text))
915 (equal? `(list "foo" ,text)
916 (call-with-input-file out read)))))))))
917
462a3fa3 918(test-assert "text-file*"
e74f64b9
LC
919 (run-with-store %store
920 (mlet* %store-monad
921 ((drv (package->derivation %bootstrap-guile))
922 (guile -> (derivation->output-path drv))
923 (file (text-file "bar" "This is bar."))
924 (text (text-file* "foo"
925 %bootstrap-guile "/bin/guile "
926 (gexp-input %bootstrap-guile "out") "/bin/guile "
927 drv "/bin/guile "
928 file))
929 (done (built-derivations (list text)))
930 (out -> (derivation->output-path text))
931 (refs (references* out)))
932 ;; Make sure we get the right references and the right content.
933 (return (and (lset= string=? refs (list guile file))
934 (equal? (call-with-input-file out get-string-all)
935 (string-append guile "/bin/guile "
936 guile "/bin/guile "
937 guile "/bin/guile "
938 file)))))
939 #:guile-for-build (package-derivation %store %bootstrap-guile)))
462a3fa3 940
b751cde3
LC
941(test-assertm "mixed-text-file"
942 (mlet* %store-monad ((file -> (mixed-text-file "mixed"
943 "export PATH="
944 %bootstrap-guile "/bin"))
945 (drv (lower-object file))
946 (out -> (derivation->output-path drv))
947 (guile-drv (package->derivation %bootstrap-guile))
948 (guile -> (derivation->output-path guile-drv)))
949 (mbegin %store-monad
950 (built-derivations (list drv))
e74f64b9 951 (mlet %store-monad ((refs (references* out)))
b751cde3
LC
952 (return (and (string=? (string-append "export PATH=" guile "/bin")
953 (call-with-input-file out get-string-all))
954 (equal? refs (list guile))))))))
955
a8afb9ae
LC
956(test-assert "gexp->derivation vs. %current-target-system"
957 (let ((mval (gexp->derivation "foo"
958 #~(begin
959 (mkdir #$output)
960 (foo #+gnu-make))
961 #:target #f)))
962 ;; The value of %CURRENT-TARGET-SYSTEM at bind-time should have no
963 ;; influence.
964 (parameterize ((%current-target-system "fooooo"))
965 (derivation? (run-with-store %store mval)))))
966
c2b84676
LC
967(test-assertm "lower-object"
968 (mlet %store-monad ((drv1 (lower-object %bootstrap-guile))
969 (drv2 (lower-object (package-source coreutils)))
970 (item (lower-object (plain-file "foo" "Hello!"))))
971 (return (and (derivation? drv1) (derivation? drv2)
972 (store-path? item)))))
973
91937029
LC
974(test-assertm "lower-object, computed-file"
975 (let* ((text (plain-file "foo" "Hello!"))
976 (exp #~(begin
977 (mkdir #$output)
978 (symlink #$%bootstrap-guile
979 (string-append #$output "/guile"))
980 (symlink #$text (string-append #$output "/text"))))
981 (computed (computed-file "computed" exp)))
982 (mlet* %store-monad ((text (lower-object text))
983 (guile-drv (lower-object %bootstrap-guile))
984 (comp-drv (lower-object computed))
985 (comp -> (derivation->output-path comp-drv)))
986 (mbegin %store-monad
987 (built-derivations (list comp-drv))
988 (return (and (string=? (readlink (string-append comp "/guile"))
989 (derivation->output-path guile-drv))
990 (string=? (readlink (string-append comp "/text"))
991 text)))))))
992
3e43166f
LC
993(test-assert "lower-object & gexp-input-error?"
994 (guard (c ((gexp-input-error? c)
995 (gexp-error-invalid-input c)))
996 (run-with-store %store
997 (lower-object (current-module))
998 #:guile-for-build (%guile-for-build))))
999
2cf0ea0d
LC
1000(test-assert "printer"
1001 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
1002 \"/bin/uname\"\\) [[:xdigit:]]+>$"
1003 (with-output-to-string
1004 (lambda ()
1005 (write
1006 (gexp (string-append (ungexp coreutils)
1007 "/bin/uname")))))))
1008
1009(test-assert "printer vs. ungexp-splicing"
1010 (string-match "^#<gexp .* [[:xdigit:]]+>$"
1011 (with-output-to-string
1012 (lambda ()
1013 ;; #~(begin #$@#~())
1014 (write
1015 (gexp (begin (ungexp-splicing (gexp ())))))))))
1016
21b679f6
LC
1017(test-equal "sugar"
1018 '(gexp (foo (ungexp bar) (ungexp baz "out")
1019 (ungexp (chbouib 42))
667b2508
LC
1020 (ungexp-splicing (list x y z))
1021 (ungexp-native foo) (ungexp-native foo "out")
1022 (ungexp-native (chbouib 42))
1023 (ungexp-native-splicing (list x y z))))
1024 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
1025 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
21b679f6
LC
1026
1027(test-end "gexp")
1028
21b679f6
LC
1029;; Local Variables:
1030;; eval: (put 'test-assertm 'scheme-indent-function 1)
1031;; End: