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