1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 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-union)
20 #:use-module (guix tests)
21 #:use-module (guix store)
22 #:use-module (guix utils)
23 #:use-module (guix derivations)
24 #:use-module (guix packages)
25 #:use-module (guix build union)
26 #:use-module ((guix build utils)
27 #:select (with-directory-excursion directory-exists?))
28 #:use-module (gnu packages bootstrap)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-64)
31 #:use-module (rnrs io ports)
32 #:use-module (ice-9 match))
34 ;; Exercise the (guix build union) module.
37 (open-connection-for-tests))
42 (test-assert "union-build with symlink to directory"
43 ;; http://bugs.gnu.org/17083
44 ;; Here both ONE and TWO provide an element called 'foo', but in ONE it's a
45 ;; directory whereas in TWO it's a symlink to a directory.
46 (let* ((one (build-expression->derivation
49 (use-modules (guix build utils) (srfi srfi-26))
50 (let ((foo (string-append %output "/foo")))
52 (call-with-output-file (string-append foo "/one")
53 (cut display "one" <>))))
54 #:modules '((guix build utils))))
55 (two (build-expression->derivation
58 (use-modules (guix build utils) (srfi srfi-26))
59 (let ((foo (string-append %output "/foo"))
60 (bar (string-append %output "/bar")))
62 (call-with-output-file (string-append bar "/two")
63 (cut display "two" <>))
65 #:modules '((guix build utils))))
67 (use-modules (guix build union))
69 (union-build (assoc-ref %outputs "out")
70 (list (assoc-ref %build-inputs "one")
71 (assoc-ref %build-inputs "two")))))
73 (build-expression->derivation %store "union-collision-symlink"
75 #:inputs `(("one" ,one) ("two" ,two))
76 #:modules '((guix build union)))))
77 (and (build-derivations %store (list drv))
78 (with-directory-excursion (pk (derivation->output-path drv))
80 (call-with-input-file "foo/one" get-string-all))
82 (call-with-input-file "foo/two" get-string-all))
84 (call-with-input-file "bar/two" get-string-all))
85 (not (file-exists? "bar/one")))))))
87 (test-skip (if (and %store (network-reachable?))
91 (test-assert "union-build"
92 (let* ((inputs (map (match-lambda
94 `(,name ,(package-derivation %store package))))
96 ;; Purposefully leave duplicate entries.
97 (append %bootstrap-inputs
98 (take %bootstrap-inputs 3))))
100 (use-modules (guix build union))
101 (union-build (assoc-ref %outputs "out")
102 (map cdr %build-inputs))))
104 (build-expression->derivation %store "union-test"
107 #:modules '((guix build union)))))
108 (and (build-derivations %store (list (pk 'drv drv)))
109 (with-directory-excursion (derivation->output-path drv)
110 (and (file-exists? "bin/touch")
111 (file-exists? "bin/gcc")
112 (file-exists? "bin/ld")
113 (file-exists? "lib/libc.so")
114 (directory-exists? "lib/gcc")
115 (file-exists? "include/unistd.h")
117 ;; The 'include/c++' sub-directory is only found in
118 ;; gcc-bootstrap, so it should be unified in a
119 ;; straightforward way, without traversing it.
120 (eq? 'symlink (stat:type (lstat "include/c++")))
122 ;; Conversely, several inputs have a 'bin' sub-directory, so
123 ;; unifying it requires traversing them all, and creating a
124 ;; new 'bin' sub-directory in the profile.
125 (eq? 'directory (stat:type (lstat "bin"))))))))
127 (test-assert "union-build collision first & last"
128 (let* ((guile (package-derivation %store %bootstrap-guile))
129 (fake (build-expression->derivation
132 (use-modules (guix build utils))
133 (let ((out (assoc-ref %outputs "out")))
134 (mkdir-p (string-append out "/bin"))
135 (call-with-output-file (string-append out "/bin/guile")
137 #:modules '((guix build utils))))
138 (builder (lambda (policy)
140 (use-modules (guix build union)
142 (union-build (assoc-ref %outputs "out")
143 (map cdr %build-inputs)
144 #:resolve-collision ,policy))))
146 (build-expression->derivation %store "union-first"
148 #:inputs `(("guile" ,guile)
150 #:modules '((guix build union))))
152 (build-expression->derivation %store "union-last"
154 #:inputs `(("guile" ,guile)
156 #:modules '((guix build union)))))
157 (and (build-derivations %store (list drv1 drv2))
158 (with-directory-excursion (derivation->output-path drv1)
159 (string=? (readlink "bin/guile")
160 (string-append (derivation->output-path guile)
162 (with-directory-excursion (derivation->output-path drv2)
163 (string=? (readlink "bin/guile")
164 (string-append (derivation->output-path fake)
167 (test-assert "union-build #:create-all-directories? #t"
168 (let* ((build `(begin
169 (use-modules (guix build union))
170 (union-build (assoc-ref %outputs "out")
171 (map cdr %build-inputs)
172 #:create-all-directories? #t)))
173 (input (package-derivation %store %bootstrap-guile))
174 (drv (build-expression->derivation %store "union-test-all-dirs"
176 #:modules '((guix build union))
177 #:inputs `(("g" ,input)))))
178 (and (build-derivations %store (list drv))
179 (with-directory-excursion (derivation->output-path drv)
180 ;; Even though there's only one input to the union,
181 ;; #:create-all-directories? #t must have created bin/ rather than
182 ;; making it a symlink to Guile's bin/.
183 (and (file-exists? "bin/guile")
184 (file-is-directory? "bin")
185 (eq? 'symlink (stat:type (lstat "bin/guile"))))))))
187 (letrec-syntax ((test-relative-file-name
189 ((_ (reference file => expected) rest ...)
191 (test-equal (string-append "relative-file-name "
194 (relative-file-name reference file))
195 (test-relative-file-name rest ...)))
198 (test-relative-file-name
199 ("/a/b" "/a/c/d" => "../c/d")
200 ("/a/b" "/a/b" => "")
201 ("/a/b" "/a" => "..")
202 ("/a/b" "/a/b/c/d" => "c/d")
203 ("/a/b/c" "/a/d/e/f" => "../../d/e/f")))