gnu: lvm2: Make sure compiled objects are stripped.
[jackhill/guix/guix.git] / tests / gexp.scm
index 740d746..214e7a5 100644 (file)
@@ -1,5 +1,5 @@
 ;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2014, 2015, 2016 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))
       (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)
     (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   ((store-lift 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
                  (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)
                             get-bytevector-all))))
                 files))))))
 
+(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-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 #:references-graphs"
   (mlet* %store-monad
-      ((one (text-file "one" "hello, world"))
+      ((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"
        (g-guile -> (derivation->output-path drv)))
     (return (and ok?
                  (equal? (call-with-input-file g-one read) (list one))
-                 (equal? (call-with-input-file g-two read)
-                         (list one (derivation->output-path two "chbouib")))
+                 (lset= string=?
+                        (call-with-input-file g-two read)
+                        (list one (derivation->output-path two "chbouib")))
 
                  ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash.
-                 (equal? (call-with-input-file g-guile read)
-                         (list (derivation->output-path guile-drv) bash))))))
+                 (lset= string=?
+                        (call-with-input-file g-guile read)
+                        (list (derivation->output-path guile-drv) bash))))))
 
 (test-assertm "gexp->derivation #:allowed-references"
   (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
       (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)))
       (return (and (zero? (close-pipe pipe))
                    (= (expt n 2) (string->number str)))))))
 
+(test-assertm "program-file"
+  (let* ((n      (random (expt 2 50)))
+         (exp    (with-imported-modules '((guix build utils))
+                   (gexp (begin
+                           (use-modules (guix build utils))
+                           (display (ungexp n))))))
+         (file   (program-file "program" exp
+                               #:guile %bootstrap-guile)))
+    (mlet* %store-monad ((drv (lower-object file))
+                         (out -> (derivation->output-path drv)))
+      (mbegin %store-monad
+        (built-derivations (list drv))
+        (let* ((pipe  (open-input-pipe out))
+               (str   (get-string-all pipe)))
+          (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 ((store-lift 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
                                             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 ((store-lift 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"
                                 #~(begin
     (parameterize ((%current-target-system "fooooo"))
       (derivation? (run-with-store %store mval)))))
 
+(test-assertm "lower-object"
+  (mlet %store-monad ((drv1 (lower-object %bootstrap-guile))
+                      (drv2 (lower-object (package-source coreutils)))
+                      (item (lower-object (plain-file "foo" "Hello!"))))
+    (return (and (derivation? drv1) (derivation? drv2)
+                 (store-path? item)))))
+
+(test-assertm "lower-object, computed-file"
+  (let* ((text     (plain-file "foo" "Hello!"))
+         (exp      #~(begin
+                       (mkdir #$output)
+                       (symlink #$%bootstrap-guile
+                                (string-append #$output "/guile"))
+                       (symlink #$text (string-append #$output "/text"))))
+         (computed (computed-file "computed" exp)))
+    (mlet* %store-monad ((text      (lower-object text))
+                         (guile-drv (lower-object %bootstrap-guile))
+                         (comp-drv  (lower-object computed))
+                         (comp ->   (derivation->output-path comp-drv)))
+      (mbegin %store-monad
+        (built-derivations (list comp-drv))
+        (return (and (string=? (readlink (string-append comp "/guile"))
+                               (derivation->output-path guile-drv))
+                     (string=? (readlink (string-append comp "/text"))
+                               text)))))))
+
 (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: