1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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))
42 ;; Test the (guix gexp) module.
45 (open-connection-for-tests))
47 ;; Globally disable grafts because they can trigger early builds.
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))
60 (define* (gexp->sexp* exp #:optional target)
61 (run-with-store %store (gexp->sexp exp
63 #:guile-for-build (%guile-for-build)))
65 (define-syntax-rule (test-assertm name exp)
67 (run-with-store %store exp
68 #:guile-for-build (%guile-for-build))))
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)
75 `(#:guile ,%bootstrap-guile
76 #:modules ((guix build utils))
79 (use-modules (guix build utils))
80 (let* ((out (string-append (assoc-ref %outputs "out")
82 (effective-version))))
84 (call-with-output-file (string-append out "/hg2g.scm")
86 (write '(define-module (hg2g)
87 #:export (the-answer))
89 (write '(define the-answer 42) port)))))))))
96 (let ((exp (gexp (display "hello!"))))
98 (null? (gexp-inputs exp))
101 (test-equal "unquote"
102 '(display `(foo ,(+ 2 3)))
103 (let ((exp (gexp (display `(foo ,(+ 2 3))))))
105 (null? (gexp-inputs exp))
108 (test-assert "one input package"
109 (let ((exp (gexp (display (ungexp coreutils)))))
111 (match (gexp-inputs exp)
114 (equal? `(display ,(derivation->output-path
115 (package-derivation %store coreutils)))
116 (gexp->sexp* exp)))))
118 (test-assert "one input package, dotted list"
119 (let ((exp (gexp (coreutils . (ungexp coreutils)))))
121 (match (gexp-inputs exp)
124 (equal? `(coreutils . ,(derivation->output-path
125 (package-derivation %store coreutils)))
126 (gexp->sexp* exp)))))
128 (test-assert "one input origin"
129 (let ((exp (gexp (display (ungexp (package-source coreutils))))))
131 (match (gexp-inputs exp)
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)))))
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
146 (match (gexp-inputs exp)
149 (equal? `(display ,intd) (gexp->sexp* exp)))))
151 (test-assert "one local file, symlink"
152 (let ((file (search-path %load-path "guix.scm"))
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
163 (match (gexp-inputs exp)
166 (equal? `(display ,intd) (gexp->sexp* exp)))))
168 (false-if-exception (delete-file link))))))
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)))))
178 (test-assertm "local-file, #:select?"
179 (mlet* %store-monad ((select? -> (lambda (file stat)
180 (member (basename file)
183 (file -> (local-file ".." "directory"
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"))))))
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!")))
198 (match (gexp-inputs exp)
201 (equal? `(display ,expected) (gexp->sexp* exp)))))
203 (test-assert "same input twice"
204 (let ((exp (gexp (begin
205 (display (ungexp coreutils))
206 (display (ungexp coreutils))))))
208 (match (gexp-inputs exp)
211 (let ((e `(display ,(derivation->output-path
212 (package-derivation %store coreutils)))))
213 (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
215 (test-assert "two input packages, one derivation, one file"
216 (let* ((drv (build-expression->derivation
218 #:guile-for-build (package-derivation %store %bootstrap-guile)))
219 (txt (add-text-to-store %store "foo" "Hello, world!"))
221 (display (ungexp coreutils))
222 (display (ungexp %bootstrap-guile))
223 (display (ungexp drv))
224 (display (ungexp txt))))))
225 (define (match-input thing)
228 (eq? thing drv-or-pkg))))
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))))))
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))
250 (string-append (derivation->output-path drv)
252 (match (gexp-inputs exp)
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))
263 (string-append (derivation->output-path drv "debug")
265 (match (gexp-inputs exp)
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))
278 (string-append (derivation->output-path drv)
280 (match (gexp-inputs exp)
282 (eq? thing file))))))
284 (test-assert "ungexp + ungexp-native"
285 (let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
287 (ungexp-native glibc)
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))))
299 `((,%bootstrap-guile "out") (,glibc "out"))
300 (gexp-native-inputs exp))
302 `((,coreutils "out") (,binutils "out"))
304 (equal? `(list ,guile ,cu ,libc ,bu)
305 (gexp->sexp* exp target)))))
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))))
313 (test-equal "ungexp + ungexp-native, nested, special mixture"
314 `(() <> ((,coreutils "out")))
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))))
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))))
329 `((,%bootstrap-guile "out") (,coreutils "out"))
331 (equal? `(display '(,guile ,cu))
332 (gexp->sexp* exp)))))
334 (test-assert "input list + ungexp-native"
335 (let* ((target "mips64el-linux")
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))))
348 `((,%bootstrap-guile "out") (,coreutils "out"))
349 (gexp-native-inputs exp))
351 `((,glibc "out") (,binutils "out"))
353 (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu)))
354 (gexp->sexp* exp target)))))
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)
361 (derivation->output-path
362 (package-derivation %store %bootstrap-guile))))
363 (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
365 `((,glibc "debug") (,%bootstrap-guile "out"))
367 (equal? (gexp->sexp* exp)
368 `(list ,@(cons 5 outputs))))))
370 (test-assert "input list splicing + ungexp-native-splicing"
371 (let* ((inputs (list (gexp-input glibc "debug" #:native? #t)
373 (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
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")))))
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")))))
389 (test-equal "output list"
391 (let ((exp (gexp (begin (mkdir (ungexp output))
392 (mkdir (ungexp output "bar"))))))
393 (length (gexp-outputs exp)))) ;XXX: <output-ref> is private
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)))))
400 (append (gexp-outputs exp0) (gexp-outputs exp1))
402 (= 2 (length (gexp-outputs exp2))))))
404 (test-equal "output list, combined gexps, duplicate output"
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))))
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))))))
417 (append (gexp-outputs exp0) (gexp-outputs exp1))
419 (= 2 (length (gexp-outputs exp2))))))
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)))))
432 (test-assertm "gexp->file + file-append"
433 (mlet* %store-monad ((exp -> #~#$(file-append %bootstrap-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)))))
444 (test-assertm "gexp->file + #:splice?"
445 (mlet* %store-monad ((exp -> (list
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)))
454 (return (and (equal? `((define foo 'bar)
455 (define guile ,guile)
456 ,(call-with-input-string "" read))
457 (call-with-input-file out
459 (list (read port) (read port) (read port)))))
460 (equal? (list guile) refs)))))
462 (test-assertm "gexp->derivation"
463 (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
466 (mkdir (ungexp output))
467 (chdir (ungexp output))
469 (string-append (ungexp %bootstrap-guile)
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))))))
486 (test-assertm "gexp->derivation vs. grafts"
487 (mlet* %store-monad ((graft? (set-grafting #f))
488 (p0 -> (dummy-package "dummy"
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*))))))
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)
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"))
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"
528 (mkdir (ungexp output)))))))
529 (mlet %store-monad ((drv mdrv))
530 (return (string=? system (derivation-system drv))))))
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))
537 (stat (ungexp local))
538 (symlink (ungexp local)
540 (drv (gexp->derivation "local-file" exp)))
542 (built-derivations (list drv))
543 (return (string=? (readlink (derivation->output-path drv))
546 (test-assertm "gexp->derivation, cross-compilation"
547 (mlet* %store-monad ((target -> "mips64el-linux")
548 (exp -> (gexp (list (ungexp coreutils)
550 (xdrv (gexp->derivation "foo" exp
553 (derivation-file-name xdrv)))
554 (xcu (package->cross-derivation coreutils
556 (cu (package->derivation coreutils)))
557 (return (and (member (derivation-file-name xcu) refs)
558 (not (member (derivation-file-name cu) refs))))))
560 (test-assertm "gexp->derivation, ungexp-native"
561 (mlet* %store-monad ((target -> "mips64el-linux")
562 (exp -> (gexp (list (ungexp-native coreutils)
564 (xdrv (gexp->derivation "foo" exp
566 (drv (gexp->derivation "foo" exp)))
567 (return (string=? (derivation-file-name drv)
568 (derivation-file-name xdrv)))))
570 (test-assertm "gexp->derivation, ungexp + ungexp-native"
571 (mlet* %store-monad ((target -> "mips64el-linux")
572 (exp -> (gexp (list (ungexp-native coreutils)
575 (xdrv (gexp->derivation "foo" exp
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)))))
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
591 (drv (gexp->derivation "foo" exp)))
592 (return (string=? (derivation-file-name drv)
593 (derivation-file-name xdrv)))))
595 (test-assertm "gexp->derivation, store copy"
596 (let ((build-one #~(call-with-output-file #$output
598 (display "This is the one." port))))
599 (build-two (lambda (one)
602 (symlink #$one (string-append #$output "/one"))
603 (call-with-output-file (string-append #$output "/two")
605 (display "This is the second one." port))))))
607 (use-modules (guix build store-copy))
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
617 '((guix build store-copy)
618 (guix build utils))))
619 (ok? (built-derivations (list drv)))
620 (out -> (derivation->output-path drv)))
621 (let ((one (derivation->output-path one))
622 (two (derivation->output-path two)))
624 (file-exists? (string-append out "/" one))
625 (file-exists? (string-append out "/" two))
626 (file-exists? (string-append out "/" two "/two"))
627 (string=? (readlink (string-append out "/" two "/one"))
630 (test-assertm "imported-files"
632 ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
633 ("a/b/c" . ,(search-path %load-path
634 "guix/derivations.scm"))
635 ("p/q" . ,(search-path %load-path "guix.scm"))
636 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
637 (drv (imported-files files)))
639 (built-derivations (list drv))
640 (let ((dir (derivation->output-path drv)))
644 (equal? (call-with-input-file (string-append dir "/" path)
646 (call-with-input-file source
647 get-bytevector-all))))
650 (test-assertm "imported-files with file-like objects"
651 (mlet* %store-monad ((plain -> (plain-file "foo" "bar!"))
652 (q-scm -> (search-path %load-path "ice-9/q.scm"))
653 (files -> `(("a/b/c" . ,q-scm)
655 (drv (imported-files files)))
657 (built-derivations (list drv))
658 (mlet %store-monad ((dir -> (derivation->output-path drv))
659 (plain* (text-file "foo" "bar!"))
660 (q-scm* (interned-file q-scm "c")))
662 (and (string=? (readlink (string-append dir "/a/b/c"))
664 (string=? (readlink (string-append dir "/p/q"))
667 (test-equal "gexp-modules & ungexp"
669 ((@@ (guix gexp) gexp-modules)
670 #~(foo #$(with-imported-modules '((foo)) #~+)
671 #+(with-imported-modules '((bar)) #~-))))
673 (test-equal "gexp-modules & ungexp-splicing"
675 ((@@ (guix gexp) gexp-modules)
676 #~(foo #$@(list (with-imported-modules '((foo)) #~+)
677 (with-imported-modules '((bar)) #~-)))))
679 (test-equal "gexp-modules and literal Scheme object"
683 (test-assertm "gexp->derivation #:modules"
686 (use-modules (guix build utils))
687 (mkdir-p (string-append #$output "/guile/guix/nix"))
689 (drv (gexp->derivation "test-with-modules" build
690 #:modules '((guix build utils)))))
692 (built-derivations (list drv))
693 (let* ((p (derivation->output-path drv))
694 (s (stat (string-append p "/guile/guix/nix"))))
695 (return (eq? (stat:type s) 'directory))))))
697 (test-assertm "gexp->derivation & with-imported-modules"
698 ;; Same test as above, but using 'with-imported-modules'.
700 ((build -> (with-imported-modules '((guix build utils))
702 (use-modules (guix build utils))
703 (mkdir-p (string-append #$output "/guile/guix/nix"))
705 (drv (gexp->derivation "test-with-modules" build)))
707 (built-derivations (list drv))
708 (let* ((p (derivation->output-path drv))
709 (s (stat (string-append p "/guile/guix/nix"))))
710 (return (eq? (stat:type s) 'directory))))))
712 (test-assertm "gexp->derivation & nested with-imported-modules"
714 ((build1 -> (with-imported-modules '((guix build utils))
716 (use-modules (guix build utils))
717 (mkdir-p (string-append #$output "/guile/guix/nix"))
719 (build2 -> (with-imported-modules '((guix build bournish))
721 (use-modules (guix build bournish)
722 (system base compile))
724 (call-with-output-file (string-append #$output "/b")
727 (read-and-compile (open-input-string "cd /foo")
728 #:from %bournish-language
731 (drv (gexp->derivation "test-with-modules" build2)))
733 (built-derivations (list drv))
734 (let* ((p (derivation->output-path drv))
735 (s (stat (string-append p "/guile/guix/nix")))
736 (b (string-append p "/b")))
737 (return (and (eq? (stat:type s) 'directory)
738 (equal? '(chdir "/foo")
739 (call-with-input-file b read))))))))
741 (test-assertm "gexp->derivation & with-imported-module & computed module"
743 ((module -> (scheme-file "x" #~(;; splice!
744 (define-module (foo bar)
745 #:export (the-answer))
747 (define the-answer 42))
749 (build -> (with-imported-modules `(((foo bar) => ,module)
752 (use-modules (guix build utils)
755 (call-with-output-file #$output
757 (write the-answer port))))))
758 (drv (gexp->derivation "thing" build))
759 (out -> (derivation->output-path drv)))
761 (built-derivations (list drv))
762 (return (= 42 (call-with-input-file out read))))))
764 (test-equal "gexp-extensions & ungexp"
766 ((@@ (guix gexp) gexp-extensions)
767 #~(foo #$(with-extensions (list grep) #~+)
768 #+(with-extensions (list sed) #~-))))
770 (test-equal "gexp-extensions & ungexp-splicing"
772 ((@@ (guix gexp) gexp-extensions)
773 #~(foo #$@(list (with-extensions (list grep) #~+)
774 (with-imported-modules '((foo))
775 (with-extensions (list sed) #~-))))))
777 (test-equal "gexp-extensions and literal Scheme object"
779 ((@@ (guix gexp) gexp-extensions) #t))
781 (test-assertm "gexp->derivation & with-extensions"
782 ;; Create a fake Guile extension and make sure it is accessible both to the
783 ;; imported modules and to the derivation build script.
785 ((extension -> %extension-package)
786 (module -> (scheme-file "x" #~( ;; splice!
794 (build -> (with-extensions (list extension)
795 (with-imported-modules `((guix build utils)
798 (use-modules (guix build utils)
800 (call-with-output-file #$output
802 (write (list the-answer (multiply 2))
804 (drv (gexp->derivation "thingie" build
805 ;; %BOOTSTRAP-GUILE is 2.0.
806 #:effective-version "2.0"))
807 (out -> (derivation->output-path drv)))
809 (built-derivations (list drv))
810 (return (equal? '(42 84) (call-with-input-file out read))))))
812 (test-assertm "gexp->derivation #:references-graphs"
814 ((one (text-file "one" (random-text)))
815 (two (gexp->derivation "two"
816 #~(symlink #$one #$output:chbouib)))
817 (build -> (with-imported-modules '((guix build store-copy)
820 (use-modules (guix build store-copy))
821 (with-output-to-file #$output
823 (write (call-with-input-file "guile"
824 read-reference-graph))))
825 (with-output-to-file #$output:one
827 (write (call-with-input-file "one"
828 read-reference-graph))))
829 (with-output-to-file #$output:two
831 (write (call-with-input-file "two"
832 read-reference-graph)))))))
833 (drv (gexp->derivation "ref-graphs" build
834 #:references-graphs `(("one" ,one)
835 ("two" ,two "chbouib")
836 ("guile" ,%bootstrap-guile))))
837 (ok? (built-derivations (list drv)))
838 (guile-drv (package->derivation %bootstrap-guile))
839 (bash (interned-file (search-bootstrap-binary "bash"
841 "bash" #:recursive? #t))
842 (g-one -> (derivation->output-path drv "one"))
843 (g-two -> (derivation->output-path drv "two"))
844 (g-guile -> (derivation->output-path drv)))
846 (equal? (call-with-input-file g-one read) (list one))
848 (call-with-input-file g-two read)
849 (list one (derivation->output-path two "chbouib")))
851 ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash.
853 (call-with-input-file g-guile read)
854 (list (derivation->output-path guile-drv) bash))))))
856 (test-assertm "gexp->derivation #:allowed-references"
857 (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
861 (symlink #$output "self")
862 (symlink #$%bootstrap-guile
865 (list "out" %bootstrap-guile))))
866 (built-derivations (list drv))))
868 (test-assertm "gexp->derivation #:allowed-references, specific output"
869 (mlet* %store-monad ((in (gexp->derivation "thing"
872 (mkdir #$output:not-ok))))
873 (drv (gexp->derivation "allowed-refs"
878 (symlink #$output "self")
879 (symlink #$in:ok "ok"))
882 (gexp-input in "ok")))))
883 (built-derivations (list drv))))
885 (test-assert "gexp->derivation #:allowed-references, disallowed"
886 (let ((drv (run-with-store %store
887 (gexp->derivation "allowed-refs"
891 (symlink #$%bootstrap-guile "guile"))
892 #:allowed-references '()))))
893 (guard (c ((nix-protocol-error? c) #t))
894 (build-derivations %store (list drv))
897 (test-assertm "gexp->derivation #:disallowed-references, allowed"
898 (mlet %store-monad ((drv (gexp->derivation "disallowed-refs"
902 (symlink #$output "self")
903 (symlink #$%bootstrap-guile
905 #:disallowed-references '())))
906 (built-derivations (list drv))))
909 (test-assert "gexp->derivation #:disallowed-references"
910 (let ((drv (run-with-store %store
911 (gexp->derivation "disallowed-refs"
915 (symlink #$%bootstrap-guile "guile"))
916 #:disallowed-references (list %bootstrap-guile)))))
917 (guard (c ((nix-protocol-error? c) #t))
918 (build-derivations %store (list drv))
922 (string-append "#!" (derivation->output-path (%guile-for-build))
923 "/bin/guile --no-auto-compile"))
925 ;; If we're going to hit the silly shebang limit (128 chars on Linux-based
926 ;; systems), then skip the following test.
927 (test-skip (if (> (string-length shebang) 127) 2 0))
929 (test-assertm "gexp->script"
930 (mlet* %store-monad ((n -> (random (expt 2 50)))
933 (string-append (ungexp %bootstrap-guile)
936 '(display (expt (ungexp n) 2))))))
937 (drv (gexp->script "guile-thing" exp
938 #:guile %bootstrap-guile))
939 (out -> (derivation->output-path drv))
940 (done (built-derivations (list drv))))
941 (let* ((pipe (open-input-pipe out))
942 (str (get-string-all pipe)))
943 (return (and (zero? (close-pipe pipe))
944 (= (expt n 2) (string->number str)))))))
946 (test-assertm "gexp->script #:module-path"
947 (call-with-temporary-directory
950 "Fake (guix base32) module!")
952 (mkdir (string-append directory "/guix"))
953 (call-with-output-file (string-append directory "/guix/base32.scm")
955 (write `(begin (define-module (guix base32))
956 (define-public %fake! ,str))
959 (mlet* %store-monad ((exp -> (with-imported-modules '((guix base32))
961 (use-modules (guix base32))
962 (write (list %load-path
964 (drv (gexp->script "guile-thing" exp
965 #:guile %bootstrap-guile
966 #:module-path (list directory)))
967 (out -> (derivation->output-path drv))
968 (done (built-derivations (list drv))))
969 (let* ((pipe (open-input-pipe out))
971 (return (and (zero? (close-pipe pipe))
974 (and (string=? str* str)
975 (not (member directory load-path))))))))))))
977 (test-assertm "program-file"
978 (let* ((n (random (expt 2 50)))
979 (exp (with-imported-modules '((guix build utils))
981 (use-modules (guix build utils))
982 (display (ungexp n))))))
983 (file (program-file "program" exp
984 #:guile %bootstrap-guile)))
985 (mlet* %store-monad ((drv (lower-object file))
986 (out -> (derivation->output-path drv)))
988 (built-derivations (list drv))
989 (let* ((pipe (open-input-pipe out))
990 (str (get-string-all pipe)))
991 (return (and (zero? (close-pipe pipe))
992 (= n (string->number str)))))))))
994 (test-assertm "program-file #:module-path"
995 (call-with-temporary-directory
997 (define text (random-text))
999 (call-with-output-file (string-append directory "/stupid-module.scm")
1001 (write `(begin (define-module (stupid-module))
1002 (define-public %stupid-thing ,text))
1005 (let* ((exp (with-imported-modules '((stupid-module))
1007 (use-modules (stupid-module))
1008 (display %stupid-thing)))))
1009 (file (program-file "program" exp
1010 #:guile %bootstrap-guile
1011 #:module-path (list directory))))
1012 (mlet* %store-monad ((drv (lower-object file))
1013 (out -> (derivation->output-path drv)))
1014 (mbegin %store-monad
1015 (built-derivations (list drv))
1016 (let* ((pipe (open-input-pipe out))
1017 (str (get-string-all pipe)))
1018 (return (and (zero? (close-pipe pipe))
1019 (string=? text str))))))))))
1021 (test-assertm "program-file & with-extensions"
1022 (let* ((exp (with-extensions (list %extension-package)
1024 (use-modules (hg2g))
1025 (display the-answer)))))
1026 (file (program-file "program" exp
1027 #:guile %bootstrap-guile)))
1028 (mlet* %store-monad ((drv (lower-object file))
1029 (out -> (derivation->output-path drv)))
1030 (mbegin %store-monad
1031 (built-derivations (list drv))
1032 (let* ((pipe (open-input-pipe out))
1033 (str (get-string-all pipe)))
1034 (return (and (zero? (close-pipe pipe))
1035 (= 42 (string->number str)))))))))
1037 (test-assertm "scheme-file"
1038 (let* ((text (plain-file "foo" "Hello, world!"))
1039 (scheme (scheme-file "bar" #~(list "foo" #$text))))
1040 (mlet* %store-monad ((drv (lower-object scheme))
1041 (text (lower-object text))
1042 (out -> (derivation->output-path drv)))
1043 (mbegin %store-monad
1044 (built-derivations (list drv))
1045 (mlet %store-monad ((refs (references* out)))
1046 (return (and (equal? refs (list text))
1047 (equal? `(list "foo" ,text)
1048 (call-with-input-file out read)))))))))
1050 (test-assert "text-file*"
1051 (run-with-store %store
1053 ((drv (package->derivation %bootstrap-guile))
1054 (guile -> (derivation->output-path drv))
1055 (file (text-file "bar" "This is bar."))
1056 (text (text-file* "foo"
1057 %bootstrap-guile "/bin/guile "
1058 (gexp-input %bootstrap-guile "out") "/bin/guile "
1061 (done (built-derivations (list text)))
1062 (out -> (derivation->output-path text))
1063 (refs (references* out)))
1064 ;; Make sure we get the right references and the right content.
1065 (return (and (lset= string=? refs (list guile file))
1066 (equal? (call-with-input-file out get-string-all)
1067 (string-append guile "/bin/guile "
1071 #:guile-for-build (package-derivation %store %bootstrap-guile)))
1073 (test-assertm "mixed-text-file"
1074 (mlet* %store-monad ((file -> (mixed-text-file "mixed"
1076 %bootstrap-guile "/bin"))
1077 (drv (lower-object file))
1078 (out -> (derivation->output-path drv))
1079 (guile-drv (package->derivation %bootstrap-guile))
1080 (guile -> (derivation->output-path guile-drv)))
1081 (mbegin %store-monad
1082 (built-derivations (list drv))
1083 (mlet %store-monad ((refs (references* out)))
1084 (return (and (string=? (string-append "export PATH=" guile "/bin")
1085 (call-with-input-file out get-string-all))
1086 (equal? refs (list guile))))))))
1088 (test-assert "gexp->derivation vs. %current-target-system"
1089 (let ((mval (gexp->derivation "foo"
1094 ;; The value of %CURRENT-TARGET-SYSTEM at bind-time should have no
1096 (parameterize ((%current-target-system "fooooo"))
1097 (derivation? (run-with-store %store mval)))))
1099 (test-assertm "lower-object"
1100 (mlet %store-monad ((drv1 (lower-object %bootstrap-guile))
1101 (drv2 (lower-object (package-source coreutils)))
1102 (item (lower-object (plain-file "foo" "Hello!"))))
1103 (return (and (derivation? drv1) (derivation? drv2)
1104 (store-path? item)))))
1106 (test-assertm "lower-object, computed-file"
1107 (let* ((text (plain-file "foo" "Hello!"))
1110 (symlink #$%bootstrap-guile
1111 (string-append #$output "/guile"))
1112 (symlink #$text (string-append #$output "/text"))))
1113 (computed (computed-file "computed" exp)))
1114 (mlet* %store-monad ((text (lower-object text))
1115 (guile-drv (lower-object %bootstrap-guile))
1116 (comp-drv (lower-object computed))
1117 (comp -> (derivation->output-path comp-drv)))
1118 (mbegin %store-monad
1119 (built-derivations (list comp-drv))
1120 (return (and (string=? (readlink (string-append comp "/guile"))
1121 (derivation->output-path guile-drv))
1122 (string=? (readlink (string-append comp "/text"))
1125 (test-assert "lower-object & gexp-input-error?"
1126 (guard (c ((gexp-input-error? c)
1127 (gexp-error-invalid-input c)))
1128 (run-with-store %store
1129 (lower-object (current-module))
1130 #:guile-for-build (%guile-for-build))))
1132 (test-assert "printer"
1133 (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
1134 \"/bin/uname\"\\) [[:xdigit:]]+>$"
1135 (with-output-to-string
1138 (gexp (string-append (ungexp coreutils)
1141 (test-assert "printer vs. ungexp-splicing"
1142 (string-match "^#<gexp .* [[:xdigit:]]+>$"
1143 (with-output-to-string
1145 ;; #~(begin #$@#~())
1147 (gexp (begin (ungexp-splicing (gexp ())))))))))
1150 '(gexp (foo (ungexp bar) (ungexp baz "out")
1151 (ungexp (chbouib 42))
1152 (ungexp-splicing (list x y z))
1153 (ungexp-native foo) (ungexp-native foo "out")
1154 (ungexp-native (chbouib 42))
1155 (ungexp-native-splicing (list x y z))))
1156 '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
1157 #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
1162 ;; eval: (put 'test-assertm 'scheme-indent-function 1)