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>
5 ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (test build-utils)
24 #:use-module (guix tests)
25 #:use-module (guix build utils)
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 #!~a --no-auto-compile
162 '(begin (let ((current (getenv "GUIX_FOO")))
165 (string-append "/some/path:/some/other/path"
167 "/some/path:/some/other/path"))))
168 '(let ((cl (command-line)))
169 (apply execl "/anything/cabbage-bash-1.2.3/bin/sh"
172 (append '("") cl)))))
174 (call-with-temporary-directory
176 (let ((script-file-name (string-append directory "/foo")))
177 (call-with-output-file script-file-name
179 (display script-contents port)))
180 (chmod script-file-name #o777)
181 (wrap-script script-file-name
182 `("GUIX_FOO" prefix ("/some/path"
183 "/some/other/path")))
184 (let ((str (call-with-input-file script-file-name get-string-all)))
185 (with-directory-excursion directory
189 (let ((script-contents "\
190 #!/anything/cabbage-bash-1.2.3/bin/python3 -and -args
191 # vim:fileencoding=utf-8
192 print('hello world')"))
194 (test-equal "wrap-script, with encoding declaration"
197 #!MYGUILE --no-auto-compile
198 #!#; # vim:fileencoding=utf-8
202 '(begin (let ((current (getenv "GUIX_FOO")))
205 (string-append "/some/path:/some/other/path"
207 "/some/path:/some/other/path"))))
208 `(let ((cl (command-line)))
209 (apply execl "/anything/cabbage-bash-1.2.3/bin/python3"
212 (append '("" "-and" "-args") cl)))))
214 (call-with-temporary-directory
216 (let ((script-file-name (string-append directory "/foo")))
217 (call-with-output-file script-file-name
219 (format port script-contents)))
220 (chmod script-file-name #o777)
222 (wrap-script script-file-name
224 `("GUIX_FOO" prefix ("/some/path"
225 "/some/other/path")))
226 (let ((str (call-with-input-file script-file-name get-string-all)))
227 (with-directory-excursion directory
231 (test-assert "wrap-script, raises condition"
232 (call-with-temporary-directory
234 (let ((script-file-name (string-append directory "/foo")))
235 (call-with-output-file script-file-name
237 (format port "This is not a script")))
238 (chmod script-file-name #o777)
239 (guard (c ((wrap-error? c) #t))
240 (wrap-script script-file-name
242 `("GUIX_FOO" prefix ("/some/path"
243 "/some/other/path")))
246 (test-equal "substitute*, text contains a NUL byte, UTF-8"
248 (with-fluids ((%default-port-encoding "UTF-8")
249 (%default-port-conversion-strategy 'error))
250 ;; The GNU libc is locale sensitive. Depending on the value of LANG, the
251 ;; test could fail with "string contains #\\nul character: ~S" or "cannot
252 ;; convert wide string to output locale".
253 (setlocale LC_ALL "en_US.UTF-8")
254 (call-with-temporary-output-file
257 (flush-output-port port)
263 (with-input-from-file file
265 (get-string-all (current-input-port))))))))
267 (test-equal "search-input-file: exception if not found"
269 (file . "does-not-exist"))
270 (guard (e ((search-error? e)
271 `((path . ,(search-error-path e))
272 (file . ,(search-error-file e)))))
273 (search-input-file '() "does-not-exist")))
275 (test-equal "search-input-file: can find if existent"
278 `(("guile/bin" . ,(dirname (which "guile"))))
281 (test-equal "search-input-file: can search in multiple directories"
283 (call-with-temporary-directory
286 `(("irrelevant" . ,directory)
287 ("guile/bin" . ,(dirname (which "guile"))))