1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2015, 2016, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
4 ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 (define-module (test build-utils)
23 #:use-module (guix tests)
24 #:use-module (guix build utils)
25 #:use-module ((guix utils)
26 #:select (%current-system call-with-temporary-directory))
27 #:use-module (gnu packages)
28 #:use-module (gnu packages bootstrap)
29 #:use-module (srfi srfi-34)
30 #:use-module (srfi srfi-35)
31 #:use-module (srfi srfi-64)
32 #:use-module (rnrs io ports)
33 #:use-module (ice-9 popen))
36 (test-begin "build-utils")
38 (test-equal "alist-cons-before"
39 '((a . 1) (x . 42) (b . 2) (c . 3))
40 (alist-cons-before 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
42 (test-equal "alist-cons-before, reference not found"
43 '((a . 1) (b . 2) (c . 3) (x . 42))
44 (alist-cons-before 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
46 (test-equal "alist-cons-after"
47 '((a . 1) (b . 2) (x . 42) (c . 3))
48 (alist-cons-after 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
50 (test-equal "alist-cons-after, reference not found"
51 '((a . 1) (b . 2) (c . 3) (x . 42))
52 (alist-cons-after 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
54 (test-equal "alist-replace"
55 '((a . 1) (b . 77) (c . 3))
56 (alist-replace 'b 77 '((a . 1) (b . 2) (c . 3))))
58 (test-assert "alist-replace, key not found"
59 (not (false-if-exception
60 (alist-replace 'z 77 '((a . 1) (b . 2) (c . 3))))))
62 (test-equal "fold-port-matches"
64 (call-with-input-string "Guix is cool, Guix rocks, and it uses Guile, Guix!"
66 (fold-port-matches cons '() "Guix" port))))
68 (test-equal "fold-port-matches, trickier"
69 (reverse '("Guix" "guix" "Guix" "guiX" "Guix"))
70 (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
72 (fold-port-matches cons '()
73 (list (char-set #\G #\g)
79 (test-equal "fold-port-matches, with unmatched chars"
82 #\G #\u #\i "Guix" "guiX" #\, #\space
84 (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
87 (fold-port-matches cons '()
88 (list (char-set #\G #\g)
95 (test-equal "wrap-program, one input, multiple calls"
97 (call-with-temporary-directory
99 (let ((bash (search-bootstrap-binary "bash" (%current-system)))
100 (foo (string-append directory "/foo")))
102 (call-with-output-file foo
105 "#!~a~%echo \"${GUIX_FOO} ${GUIX_BAR}\"~%"
109 ;; wrap-program uses `which' to find bash for the wrapper shebang, but
110 ;; it can't know about the bootstrap bash in the store, since it's not
111 ;; named "bash". Help it out a bit by providing a symlink it this
113 (with-environment-variable "PATH" (dirname bash)
114 (wrap-program foo `("GUIX_FOO" prefix ("hello")))
115 (wrap-program foo `("GUIX_BAR" prefix ("world")))
117 ;; The bootstrap Bash is linked against an old libc and would abort
118 ;; with an assertion failure when trying to load incompatible locale
122 (let* ((pipe (open-input-pipe foo))
123 (str (get-string-all pipe)))
124 (with-directory-excursion directory
125 (for-each delete-file '("foo" ".foo-real")))
126 (and (zero? (close-pipe pipe))
129 (test-assert "invoke/quiet, success"
131 (invoke/quiet "true")
134 (test-assert "invoke/quiet, failure"
135 (guard (c ((message-condition? c)
136 (string-contains (condition-message c) "This is an error.")))
137 (invoke/quiet "sh" "-c" "echo This is an error. ; false")
140 (test-assert "invoke/quiet, failure, message on stderr"
141 (guard (c ((message-condition? c)
142 (string-contains (condition-message c)
143 "This is another error.")))
144 (invoke/quiet "sh" "-c" "echo This is another error. >&2 ; false")
147 (let ((script-contents "\
148 #!/anything/cabbage-bash-1.2.3/bin/sh
152 (test-equal "wrap-script, simple case"
155 #!~a --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 (display script-contents port)))
179 (chmod script-file-name #o777)
180 (wrap-script script-file-name
181 `("GUIX_FOO" prefix ("/some/path"
182 "/some/other/path")))
183 (let ((str (call-with-input-file script-file-name get-string-all)))
184 (with-directory-excursion directory
188 (let ((script-contents "\
189 #!/anything/cabbage-bash-1.2.3/bin/python3 -and -args
190 # vim:fileencoding=utf-8
191 print('hello world')"))
193 (test-equal "wrap-script, with encoding declaration"
196 #!MYGUILE --no-auto-compile
197 #!#; # vim:fileencoding=utf-8
201 '(begin (let ((current (getenv "GUIX_FOO")))
204 (string-append "/some/path:/some/other/path"
206 "/some/path:/some/other/path"))))
207 `(let ((cl (command-line)))
208 (apply execl "/anything/cabbage-bash-1.2.3/bin/python3"
211 (append '("" "-and" "-args") cl)))))
213 (call-with-temporary-directory
215 (let ((script-file-name (string-append directory "/foo")))
216 (call-with-output-file script-file-name
218 (format port script-contents)))
219 (chmod script-file-name #o777)
221 (wrap-script script-file-name
223 `("GUIX_FOO" prefix ("/some/path"
224 "/some/other/path")))
225 (let ((str (call-with-input-file script-file-name get-string-all)))
226 (with-directory-excursion directory
230 (test-assert "wrap-script, raises condition"
231 (call-with-temporary-directory
233 (let ((script-file-name (string-append directory "/foo")))
234 (call-with-output-file script-file-name
236 (format port "This is not a script")))
237 (chmod script-file-name #o777)
238 (guard (c ((wrap-error? c) #t))
239 (wrap-script script-file-name
241 `("GUIX_FOO" prefix ("/some/path"
242 "/some/other/path")))
245 (test-equal "substitute*, text contains a NUL byte, UTF-8"
247 (with-fluids ((%default-port-encoding "UTF-8")
248 (%default-port-conversion-strategy 'error))
249 ;; The GNU libc is locale sensitive. Depending on the value of LANG, the
250 ;; test could fail with "string contains #\\nul character: ~S" or "cannot
251 ;; convert wide string to output locale".
252 (setlocale LC_ALL "en_US.UTF-8")
253 (call-with-temporary-output-file
256 (flush-output-port port)
262 (with-input-from-file file
264 (get-string-all (current-input-port))))))))