;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
#:use-module (guix store)
#:use-module (guix monads)
#:use-module (guix gexp)
+ #:use-module (guix grafts)
#:use-module (guix derivations)
#:use-module (guix packages)
#:use-module (guix tests)
+ #:use-module ((guix build utils) #:select (with-directory-excursion))
#:use-module (gnu packages)
#:use-module (gnu packages base)
#:use-module (gnu packages bootstrap)
#:use-module (rnrs io ports)
#:use-module (ice-9 match)
#:use-module (ice-9 regex)
- #:use-module (ice-9 popen))
+ #:use-module (ice-9 popen)
+ #:use-module (ice-9 ftw))
;; Test the (guix gexp) module.
(define %store
(open-connection-for-tests))
+;; Globally disable grafts because they can trigger early builds.
+(%graft? #f)
+
;; For white-box testing.
(define (gexp-inputs x)
((@@ (guix gexp) gexp-inputs) x))
(package-derivation %store coreutils)))
(gexp->sexp* exp)))))
+(test-assert "one input package, dotted list"
+ (let ((exp (gexp (coreutils . (ungexp coreutils)))))
+ (and (gexp? exp)
+ (match (gexp-inputs exp)
+ (((p "out"))
+ (eq? p coreutils)))
+ (equal? `(coreutils . ,(derivation->output-path
+ (package-derivation %store coreutils)))
+ (gexp->sexp* exp)))))
+
(test-assert "one input origin"
(let ((exp (gexp (display (ungexp (package-source coreutils))))))
(and (gexp? exp)
(lambda ()
(false-if-exception (delete-file link))))))
+(test-equal "local-file, relative file name"
+ (canonicalize-path (search-path %load-path "guix/base32.scm"))
+ (let ((directory (dirname (search-path %load-path
+ "guix/build-system/gnu.scm"))))
+ (with-directory-excursion directory
+ (let ((file (local-file "../guix/base32.scm")))
+ (local-file-absolute-file-name file)))))
+
+(test-assertm "local-file, #:select?"
+ (mlet* %store-monad ((select? -> (lambda (file stat)
+ (member (basename file)
+ '("guix.scm" "tests"
+ "gexp.scm"))))
+ (file -> (local-file ".." "directory"
+ #:recursive? #t
+ #:select? select?))
+ (dir (lower-object file)))
+ (return (and (store-path? dir)
+ (equal? (scandir dir)
+ '("." ".." "guix.scm" "tests"))
+ (equal? (scandir (string-append dir "/tests"))
+ '("." ".." "gexp.scm"))))))
+
(test-assert "one plain file"
(let* ((file (plain-file "hi" "Hello, world!"))
(exp (gexp (display (ungexp file))))
(e3 `(display ,txt)))
(equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
+(test-assert "file-append"
+ (let* ((drv (package-derivation %store %bootstrap-guile))
+ (fa (file-append %bootstrap-guile "/bin/guile"))
+ (exp #~(here we go #$fa)))
+ (and (match (gexp->sexp* exp)
+ (('here 'we 'go (? string? result))
+ (string=? result
+ (string-append (derivation->output-path drv)
+ "/bin/guile"))))
+ (match (gexp-inputs exp)
+ (((thing "out"))
+ (eq? thing fa))))))
+
+(test-assert "file-append, output"
+ (let* ((drv (package-derivation %store glibc))
+ (fa (file-append glibc "/lib" "/debug"))
+ (exp #~(foo #$fa:debug)))
+ (and (match (gexp->sexp* exp)
+ (('foo (? string? result))
+ (string=? result
+ (string-append (derivation->output-path drv "debug")
+ "/lib/debug"))))
+ (match (gexp-inputs exp)
+ (((thing "debug"))
+ (eq? thing fa))))))
+
+(test-assert "file-append, nested"
+ (let* ((drv (package-derivation %store glibc))
+ (dir (file-append glibc "/bin"))
+ (slash (file-append dir "/"))
+ (file (file-append slash "getent"))
+ (exp #~(foo #$file)))
+ (and (match (gexp->sexp* exp)
+ (('foo (? string? result))
+ (string=? result
+ (string-append (derivation->output-path drv)
+ "/bin/getent"))))
+ (match (gexp-inputs exp)
+ (((thing "out"))
+ (eq? thing file))))))
+
(test-assert "ungexp + ungexp-native"
(let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
(ungexp coreutils)
(ungexp %bootstrap-guile)))))
(list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
+(test-equal "ungexp + ungexp-native, nested, special mixture"
+ `(() <> ((,coreutils "out")))
+
+ ;; (gexp-native-inputs exp) used to return '(), wrongfully.
+ (let* ((foo (gexp (foo (ungexp-native coreutils))))
+ (exp (gexp (bar (ungexp foo)))))
+ (list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
+
(test-assert "input list"
(let ((exp (gexp (display
'(ungexp (list %bootstrap-guile coreutils)))))
`(list ,@(cons 5 outputs))))))
(test-assert "input list splicing + ungexp-native-splicing"
- (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
+ (let* ((inputs (list (gexp-input glibc "debug" #:native? #t)
+ %bootstrap-guile))
(exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
(and (lset= equal?
`((,glibc "debug") (,%bootstrap-guile "out"))
(equal? (gexp->sexp* exp) ;native
(gexp->sexp* exp "mips64el-linux")))))
+(test-assert "gexp list splicing + ungexp-splicing"
+ (let* ((inner (gexp (ungexp-native glibc)))
+ (exp (gexp (list (ungexp-splicing (list inner))))))
+ (and (equal? `((,glibc "out")) (gexp-native-inputs exp))
+ (null? (gexp-inputs exp))
+ (equal? (gexp->sexp* exp) ;native
+ (gexp->sexp* exp "mips64el-linux")))))
+
(test-equal "output list"
2
(let ((exp (gexp (begin (mkdir (ungexp output))
(drv (gexp->file "foo" exp))
(out -> (derivation->output-path drv))
(done (built-derivations (list drv)))
- (refs ((store-lift references) out)))
+ (refs (references* out)))
(return (and (equal? sexp (call-with-input-file out read))
(equal? (list guile) refs)))))
+(test-assertm "gexp->file + file-append"
+ (mlet* %store-monad ((exp -> #~#$(file-append %bootstrap-guile
+ "/bin/guile"))
+ (guile (package-file %bootstrap-guile))
+ (drv (gexp->file "foo" exp))
+ (out -> (derivation->output-path drv))
+ (done (built-derivations (list drv)))
+ (refs (references* out)))
+ (return (and (equal? (string-append guile "/bin/guile")
+ (call-with-input-file out read))
+ (equal? (list guile) refs)))))
+
(test-assertm "gexp->derivation"
(mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
(exp -> (gexp
(out -> (derivation->output-path drv))
(out2 -> (derivation->output-path drv "2nd"))
(done (built-derivations (list drv)))
- (refs ((store-lift references) out))
- (refs2 ((store-lift references) out2))
+ (refs (references* out))
+ (refs2 (references* out2))
(guile (package-file %bootstrap-guile "bin/guile")))
(return (and (string=? (readlink (string-append out "/foo")) guile)
(string=? (readlink out2) file)
(equal? refs2 (list file))))))
(test-assertm "gexp->derivation vs. grafts"
- (mlet* %store-monad ((p0 -> (dummy-package "dummy"
+ (mlet* %store-monad ((graft? (set-grafting #f))
+ (p0 -> (dummy-package "dummy"
(arguments
'(#:implicit-inputs? #f))))
(r -> (package (inherit p0) (name "DuMMY")))
(exp0 -> (gexp (frob (ungexp p0) (ungexp output))))
(exp1 -> (gexp (frob (ungexp p1) (ungexp output))))
(void (set-guile-for-build %bootstrap-guile))
- (drv0 (gexp->derivation "t" exp0))
- (drv1 (gexp->derivation "t" exp1))
- (drv1* (gexp->derivation "t" exp1 #:graft? #f)))
+ (drv0 (gexp->derivation "t" exp0 #:graft? #t))
+ (drv1 (gexp->derivation "t" exp1 #:graft? #t))
+ (drv1* (gexp->derivation "t" exp1 #:graft? #f))
+ (_ (set-grafting graft?)))
(return (and (not (string=? (derivation->output-path drv0)
(derivation->output-path drv1)))
(string=? (derivation->output-path drv0)
(ungexp output))))
(xdrv (gexp->derivation "foo" exp
#:target target))
- (refs ((store-lift references)
+ (refs (references*
(derivation-file-name xdrv)))
(xcu (package->cross-derivation coreutils
target))
(ungexp output))))
(xdrv (gexp->derivation "foo" exp
#:target target))
- (refs ((store-lift references)
+ (refs (references*
(derivation-file-name xdrv)))
(xglibc (package->cross-derivation glibc target))
(cu (package->derivation coreutils)))
get-bytevector-all))))
files))))))
+(test-assertm "imported-files with file-like objects"
+ (mlet* %store-monad ((plain -> (plain-file "foo" "bar!"))
+ (q-scm -> (search-path %load-path "ice-9/q.scm"))
+ (files -> `(("a/b/c" . ,q-scm)
+ ("p/q" . ,plain)))
+ (drv (imported-files files)))
+ (mbegin %store-monad
+ (built-derivations (list drv))
+ (mlet %store-monad ((dir -> (derivation->output-path drv))
+ (plain* (text-file "foo" "bar!"))
+ (q-scm* (interned-file q-scm "c")))
+ (return
+ (and (string=? (readlink (string-append dir "/a/b/c"))
+ q-scm*)
+ (string=? (readlink (string-append dir "/p/q"))
+ plain*)))))))
+
+(test-equal "gexp-modules & ungexp"
+ '((bar) (foo))
+ ((@@ (guix gexp) gexp-modules)
+ #~(foo #$(with-imported-modules '((foo)) #~+)
+ #+(with-imported-modules '((bar)) #~-))))
+
+(test-equal "gexp-modules & ungexp-splicing"
+ '((foo) (bar))
+ ((@@ (guix gexp) gexp-modules)
+ #~(foo #$@(list (with-imported-modules '((foo)) #~+)
+ (with-imported-modules '((bar)) #~-)))))
+
+(test-equal "gexp-modules and literal Scheme object"
+ '()
+ (gexp-modules #t))
+
(test-assertm "gexp->derivation #:modules"
(mlet* %store-monad
((build -> #~(begin
(s (stat (string-append p "/guile/guix/nix"))))
(return (eq? (stat:type s) 'directory))))))
+(test-assertm "gexp->derivation & with-imported-modules"
+ ;; Same test as above, but using 'with-imported-modules'.
+ (mlet* %store-monad
+ ((build -> (with-imported-modules '((guix build utils))
+ #~(begin
+ (use-modules (guix build utils))
+ (mkdir-p (string-append #$output "/guile/guix/nix"))
+ #t)))
+ (drv (gexp->derivation "test-with-modules" build)))
+ (mbegin %store-monad
+ (built-derivations (list drv))
+ (let* ((p (derivation->output-path drv))
+ (s (stat (string-append p "/guile/guix/nix"))))
+ (return (eq? (stat:type s) 'directory))))))
+
+(test-assertm "gexp->derivation & nested with-imported-modules"
+ (mlet* %store-monad
+ ((build1 -> (with-imported-modules '((guix build utils))
+ #~(begin
+ (use-modules (guix build utils))
+ (mkdir-p (string-append #$output "/guile/guix/nix"))
+ #t)))
+ (build2 -> (with-imported-modules '((guix build bournish))
+ #~(begin
+ (use-modules (guix build bournish)
+ (system base compile))
+ #+build1
+ (call-with-output-file (string-append #$output "/b")
+ (lambda (port)
+ (write
+ (read-and-compile (open-input-string "cd /foo")
+ #:from %bournish-language
+ #:to 'scheme)
+ port))))))
+ (drv (gexp->derivation "test-with-modules" build2)))
+ (mbegin %store-monad
+ (built-derivations (list drv))
+ (let* ((p (derivation->output-path drv))
+ (s (stat (string-append p "/guile/guix/nix")))
+ (b (string-append p "/b")))
+ (return (and (eq? (stat:type s) 'directory)
+ (equal? '(chdir "/foo")
+ (call-with-input-file b read))))))))
+
+(test-assertm "gexp->derivation & with-imported-module & computed module"
+ (mlet* %store-monad
+ ((module -> (scheme-file "x" #~(begin
+ (define-module (foo bar)
+ #:export (the-answer))
+
+ (define the-answer 42))))
+ (build -> (with-imported-modules `(((foo bar) => ,module)
+ (guix build utils))
+ #~(begin
+ (use-modules (guix build utils)
+ (foo bar))
+ mkdir-p
+ (call-with-output-file #$output
+ (lambda (port)
+ (write the-answer port))))))
+ (drv (gexp->derivation "thing" build))
+ (out -> (derivation->output-path drv)))
+ (mbegin %store-monad
+ (built-derivations (list drv))
+ (return (= 42 (call-with-input-file out read))))))
+
(test-assertm "gexp->derivation #:references-graphs"
(mlet* %store-monad
((one (text-file "one" (random-text)))
(two (gexp->derivation "two"
#~(symlink #$one #$output:chbouib)))
- (drv (gexp->derivation "ref-graphs"
- #~(begin
- (use-modules (guix build store-copy))
- (with-output-to-file #$output
- (lambda ()
- (write (call-with-input-file "guile"
- read-reference-graph))))
- (with-output-to-file #$output:one
- (lambda ()
- (write (call-with-input-file "one"
- read-reference-graph))))
- (with-output-to-file #$output:two
- (lambda ()
- (write (call-with-input-file "two"
- read-reference-graph)))))
+ (build -> (with-imported-modules '((guix build store-copy)
+ (guix build utils))
+ #~(begin
+ (use-modules (guix build store-copy))
+ (with-output-to-file #$output
+ (lambda ()
+ (write (call-with-input-file "guile"
+ read-reference-graph))))
+ (with-output-to-file #$output:one
+ (lambda ()
+ (write (call-with-input-file "one"
+ read-reference-graph))))
+ (with-output-to-file #$output:two
+ (lambda ()
+ (write (call-with-input-file "two"
+ read-reference-graph)))))))
+ (drv (gexp->derivation "ref-graphs" build
#:references-graphs `(("one" ,one)
("two" ,two "chbouib")
- ("guile" ,%bootstrap-guile))
- #:modules '((guix build store-copy)
- (guix build utils))))
+ ("guile" ,%bootstrap-guile))))
(ok? (built-derivations (list drv)))
(guile-drv (package->derivation %bootstrap-guile))
(bash (interned-file (search-bootstrap-binary "bash"
(build-derivations %store (list drv))
#f)))
+(test-assertm "gexp->derivation #:disallowed-references, allowed"
+ (mlet %store-monad ((drv (gexp->derivation "disallowed-refs"
+ #~(begin
+ (mkdir #$output)
+ (chdir #$output)
+ (symlink #$output "self")
+ (symlink #$%bootstrap-guile
+ "guile"))
+ #:disallowed-references '())))
+ (built-derivations (list drv))))
+
+
+(test-assert "gexp->derivation #:disallowed-references"
+ (let ((drv (run-with-store %store
+ (gexp->derivation "disallowed-refs"
+ #~(begin
+ (mkdir #$output)
+ (chdir #$output)
+ (symlink #$%bootstrap-guile "guile"))
+ #:disallowed-references (list %bootstrap-guile)))))
+ (guard (c ((nix-protocol-error? c) #t))
+ (build-derivations %store (list drv))
+ #f)))
+
(define shebang
(string-append "#!" (derivation->output-path (%guile-for-build))
"/bin/guile --no-auto-compile"))
;; If we're going to hit the silly shebang limit (128 chars on Linux-based
;; systems), then skip the following test.
-(test-skip (if (> (string-length shebang) 127) 1 0))
+(test-skip (if (> (string-length shebang) 127) 2 0))
(test-assertm "gexp->script"
(mlet* %store-monad ((n -> (random (expt 2 50)))
(test-assertm "program-file"
(let* ((n (random (expt 2 50)))
- (exp (gexp (begin
- (use-modules (guix build utils))
- (display (ungexp n)))))
+ (exp (with-imported-modules '((guix build utils))
+ (gexp (begin
+ (use-modules (guix build utils))
+ (display (ungexp n))))))
(file (program-file "program" exp
- #:modules '((guix build utils))
#:guile %bootstrap-guile)))
(mlet* %store-monad ((drv (lower-object file))
(out -> (derivation->output-path drv)))
(return (and (zero? (close-pipe pipe))
(= n (string->number str)))))))))
+(test-assertm "scheme-file"
+ (let* ((text (plain-file "foo" "Hello, world!"))
+ (scheme (scheme-file "bar" #~(list "foo" #$text))))
+ (mlet* %store-monad ((drv (lower-object scheme))
+ (text (lower-object text))
+ (out -> (derivation->output-path drv)))
+ (mbegin %store-monad
+ (built-derivations (list drv))
+ (mlet %store-monad ((refs (references* out)))
+ (return (and (equal? refs (list text))
+ (equal? `(list "foo" ,text)
+ (call-with-input-file out read)))))))))
+
(test-assert "text-file*"
- (let ((references (store-lift references)))
- (run-with-store %store
- (mlet* %store-monad
- ((drv (package->derivation %bootstrap-guile))
- (guile -> (derivation->output-path drv))
- (file (text-file "bar" "This is bar."))
- (text (text-file* "foo"
- %bootstrap-guile "/bin/guile "
- (gexp-input %bootstrap-guile "out") "/bin/guile "
- drv "/bin/guile "
- file))
- (done (built-derivations (list text)))
- (out -> (derivation->output-path text))
- (refs (references out)))
- ;; Make sure we get the right references and the right content.
- (return (and (lset= string=? refs (list guile file))
- (equal? (call-with-input-file out get-string-all)
- (string-append guile "/bin/guile "
- guile "/bin/guile "
- guile "/bin/guile "
- file)))))
- #:guile-for-build (package-derivation %store %bootstrap-guile))))
+ (run-with-store %store
+ (mlet* %store-monad
+ ((drv (package->derivation %bootstrap-guile))
+ (guile -> (derivation->output-path drv))
+ (file (text-file "bar" "This is bar."))
+ (text (text-file* "foo"
+ %bootstrap-guile "/bin/guile "
+ (gexp-input %bootstrap-guile "out") "/bin/guile "
+ drv "/bin/guile "
+ file))
+ (done (built-derivations (list text)))
+ (out -> (derivation->output-path text))
+ (refs (references* out)))
+ ;; Make sure we get the right references and the right content.
+ (return (and (lset= string=? refs (list guile file))
+ (equal? (call-with-input-file out get-string-all)
+ (string-append guile "/bin/guile "
+ guile "/bin/guile "
+ guile "/bin/guile "
+ file)))))
+ #:guile-for-build (package-derivation %store %bootstrap-guile)))
+
+(test-assertm "mixed-text-file"
+ (mlet* %store-monad ((file -> (mixed-text-file "mixed"
+ "export PATH="
+ %bootstrap-guile "/bin"))
+ (drv (lower-object file))
+ (out -> (derivation->output-path drv))
+ (guile-drv (package->derivation %bootstrap-guile))
+ (guile -> (derivation->output-path guile-drv)))
+ (mbegin %store-monad
+ (built-derivations (list drv))
+ (mlet %store-monad ((refs (references* out)))
+ (return (and (string=? (string-append "export PATH=" guile "/bin")
+ (call-with-input-file out get-string-all))
+ (equal? refs (list guile))))))))
(test-assert "gexp->derivation vs. %current-target-system"
(let ((mval (gexp->derivation "foo"
(string=? (readlink (string-append comp "/text"))
text)))))))
+(test-assert "lower-object & gexp-input-error?"
+ (guard (c ((gexp-input-error? c)
+ (gexp-error-invalid-input c)))
+ (run-with-store %store
+ (lower-object (current-module))
+ #:guile-for-build (%guile-for-build))))
+
(test-assert "printer"
(string-match "^#<gexp \\(string-append .*#<package coreutils.*\
\"/bin/uname\"\\) [[:xdigit:]]+>$"
(test-end "gexp")
-\f
-(exit (= (test-runner-fail-count (test-runner-current)) 0))
-
;; Local Variables:
;; eval: (put 'test-assertm 'scheme-indent-function 1)
;; End: