1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2015, 2016, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (test-build-utils)
22 #:use-module (guix tests)
23 #:use-module (guix build utils)
24 #:use-module ((gnu build bootloader)
25 #:select (invoke/quiet))
26 #:use-module ((guix utils)
27 #:select (%current-system call-with-temporary-directory))
28 #:use-module (gnu packages)
29 #:use-module (gnu packages bootstrap)
30 #:use-module (srfi srfi-34)
31 #:use-module (srfi srfi-35)
32 #:use-module (srfi srfi-64)
33 #:use-module (rnrs io ports)
34 #:use-module (ice-9 popen))
37 (test-begin "build-utils")
39 (test-equal "alist-cons-before"
40 '((a . 1) (x . 42) (b . 2) (c . 3))
41 (alist-cons-before 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
43 (test-equal "alist-cons-before, reference not found"
44 '((a . 1) (b . 2) (c . 3) (x . 42))
45 (alist-cons-before 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
47 (test-equal "alist-cons-after"
48 '((a . 1) (b . 2) (x . 42) (c . 3))
49 (alist-cons-after 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
51 (test-equal "alist-cons-after, reference not found"
52 '((a . 1) (b . 2) (c . 3) (x . 42))
53 (alist-cons-after 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
55 (test-equal "alist-replace"
56 '((a . 1) (b . 77) (c . 3))
57 (alist-replace 'b 77 '((a . 1) (b . 2) (c . 3))))
59 (test-assert "alist-replace, key not found"
60 (not (false-if-exception
61 (alist-replace 'z 77 '((a . 1) (b . 2) (c . 3))))))
63 (test-equal "fold-port-matches"
65 (call-with-input-string "Guix is cool, Guix rocks, and it uses Guile, Guix!"
67 (fold-port-matches cons '() "Guix" port))))
69 (test-equal "fold-port-matches, trickier"
70 (reverse '("Guix" "guix" "Guix" "guiX" "Guix"))
71 (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
73 (fold-port-matches cons '()
74 (list (char-set #\G #\g)
80 (test-equal "fold-port-matches, with unmatched chars"
83 #\G #\u #\i "Guix" "guiX" #\, #\space
85 (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
88 (fold-port-matches cons '()
89 (list (char-set #\G #\g)
96 (test-equal "wrap-program, one input, multiple calls"
98 (call-with-temporary-directory
100 (let ((bash (search-bootstrap-binary "bash" (%current-system)))
101 (foo (string-append directory "/foo")))
103 (call-with-output-file foo
106 "#!~a~%echo \"${GUIX_FOO} ${GUIX_BAR}\"~%"
110 ;; wrap-program uses `which' to find bash for the wrapper shebang, but
111 ;; it can't know about the bootstrap bash in the store, since it's not
112 ;; named "bash". Help it out a bit by providing a symlink it this
114 (with-environment-variable "PATH" (dirname bash)
115 (wrap-program foo `("GUIX_FOO" prefix ("hello")))
116 (wrap-program foo `("GUIX_BAR" prefix ("world")))
118 ;; The bootstrap Bash is linked against an old libc and would abort
119 ;; with an assertion failure when trying to load incompatible locale
123 (let* ((pipe (open-input-pipe foo))
124 (str (get-string-all pipe)))
125 (with-directory-excursion directory
126 (for-each delete-file '("foo" ".foo-real")))
127 (and (zero? (close-pipe pipe))
130 (test-assert "invoke/quiet, success"
132 (invoke/quiet "true")
135 (test-assert "invoke/quiet, failure"
136 (guard (c ((message-condition? c)
137 (string-contains (condition-message c) "This is an error.")))
138 (invoke/quiet "sh" "-c" "echo This is an error. ; false")
141 (test-assert "invoke/quiet, failure, message on stderr"
142 (guard (c ((message-condition? c)
143 (string-contains (condition-message c)
144 "This is another error.")))
145 (invoke/quiet "sh" "-c" "echo This is another error. >&2 ; false")
148 (let ((script-contents "\
149 #!/anything/cabbage-bash-1.2.3/bin/sh
153 (test-equal "wrap-script, simple case"
156 #!GUILE --no-auto-compile
161 '(begin (let ((current (getenv "GUIX_FOO")))
164 (string-append "/some/path:/some/other/path"
166 "/some/path:/some/other/path"))))
167 '(let ((cl (command-line)))
168 (apply execl "/anything/cabbage-bash-1.2.3/bin/sh"
171 (append '("") cl)))))
173 (call-with-temporary-directory
175 (let ((script-file-name (string-append directory "/foo")))
176 (call-with-output-file script-file-name
178 (format port script-contents)))
179 (chmod script-file-name #o777)
181 (mock ((guix build utils) which (const "GUILE"))
182 (wrap-script script-file-name
183 `("GUIX_FOO" prefix ("/some/path"
184 "/some/other/path"))))
185 (let ((str (call-with-input-file script-file-name get-string-all)))
186 (with-directory-excursion directory
190 (let ((script-contents "\
191 #!/anything/cabbage-bash-1.2.3/bin/python3 -and -args
192 # vim:fileencoding=utf-8
193 print('hello world')"))
195 (test-equal "wrap-script, with encoding declaration"
198 #!MYGUILE --no-auto-compile
199 #!#; # vim:fileencoding=utf-8
203 '(begin (let ((current (getenv "GUIX_FOO")))
206 (string-append "/some/path:/some/other/path"
208 "/some/path:/some/other/path"))))
209 `(let ((cl (command-line)))
210 (apply execl "/anything/cabbage-bash-1.2.3/bin/python3"
213 (append '("" "-and" "-args") cl)))))
215 (call-with-temporary-directory
217 (let ((script-file-name (string-append directory "/foo")))
218 (call-with-output-file script-file-name
220 (format port script-contents)))
221 (chmod script-file-name #o777)
223 (wrap-script script-file-name
225 `("GUIX_FOO" prefix ("/some/path"
226 "/some/other/path")))
227 (let ((str (call-with-input-file script-file-name get-string-all)))
228 (with-directory-excursion directory
232 (test-assert "wrap-script, raises condition"
233 (call-with-temporary-directory
235 (let ((script-file-name (string-append directory "/foo")))
236 (call-with-output-file script-file-name
238 (format port "This is not a script")))
239 (chmod script-file-name #o777)
242 (wrap-script script-file-name
244 `("GUIX_FOO" prefix ("/some/path"
245 "/some/other/path"))))
247 (wrap-error? obj)))))))