Merge remote-tracking branch 'origin/master' into core-updates
[jackhill/guix/guix.git] / tests / build-utils.scm
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 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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/>.
20
21
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))
34
35 \f
36 (test-begin "build-utils")
37
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))))
41
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))))
45
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))))
49
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))))
53
54 (test-equal "alist-replace"
55 '((a . 1) (b . 77) (c . 3))
56 (alist-replace 'b 77 '((a . 1) (b . 2) (c . 3))))
57
58 (test-assert "alist-replace, key not found"
59 (not (false-if-exception
60 (alist-replace 'z 77 '((a . 1) (b . 2) (c . 3))))))
61
62 (test-equal "fold-port-matches"
63 (make-list 3 "Guix")
64 (call-with-input-string "Guix is cool, Guix rocks, and it uses Guile, Guix!"
65 (lambda (port)
66 (fold-port-matches cons '() "Guix" port))))
67
68 (test-equal "fold-port-matches, trickier"
69 (reverse '("Guix" "guix" "Guix" "guiX" "Guix"))
70 (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
71 (lambda (port)
72 (fold-port-matches cons '()
73 (list (char-set #\G #\g)
74 (char-set #\u)
75 (char-set #\i)
76 (char-set #\x #\X))
77 port))))
78
79 (test-equal "fold-port-matches, with unmatched chars"
80 '("Guix" #\, #\space
81 "guix" #\, #\space
82 #\G #\u #\i "Guix" "guiX" #\, #\space
83 "Guix")
84 (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
85 (lambda (port)
86 (reverse
87 (fold-port-matches cons '()
88 (list (char-set #\G #\g)
89 (char-set #\u)
90 (char-set #\i)
91 (char-set #\x #\X))
92 port
93 cons)))))
94
95 (test-equal "wrap-program, one input, multiple calls"
96 "hello world\n"
97 (call-with-temporary-directory
98 (lambda (directory)
99 (let ((bash (search-bootstrap-binary "bash" (%current-system)))
100 (foo (string-append directory "/foo")))
101
102 (call-with-output-file foo
103 (lambda (p)
104 (format p
105 "#!~a~%echo \"${GUIX_FOO} ${GUIX_BAR}\"~%"
106 bash)))
107 (chmod foo #o777)
108
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
112 ;; package's output.
113 (with-environment-variable "PATH" (dirname bash)
114 (wrap-program foo `("GUIX_FOO" prefix ("hello")))
115 (wrap-program foo `("GUIX_BAR" prefix ("world")))
116
117 ;; The bootstrap Bash is linked against an old libc and would abort
118 ;; with an assertion failure when trying to load incompatible locale
119 ;; data.
120 (unsetenv "LOCPATH")
121
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))
127 str)))))))
128
129 (test-assert "invoke/quiet, success"
130 (begin
131 (invoke/quiet "true")
132 #t))
133
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")
138 #f))
139
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")
145 #f))
146
147 (let ((script-contents "\
148 #!/anything/cabbage-bash-1.2.3/bin/sh
149
150 echo hello world"))
151
152 (test-equal "wrap-script, simple case"
153 (string-append
154 (format #f "\
155 #!~a --no-auto-compile
156 #!#; Guix wrapper
157 #\\-~s
158 #\\-~s
159 "
160 (which "guile")
161 '(begin (let ((current (getenv "GUIX_FOO")))
162 (setenv "GUIX_FOO"
163 (if current
164 (string-append "/some/path:/some/other/path"
165 ":" current)
166 "/some/path:/some/other/path"))))
167 '(let ((cl (command-line)))
168 (apply execl "/anything/cabbage-bash-1.2.3/bin/sh"
169 (car cl)
170 (cons (car cl)
171 (append '("") cl)))))
172 script-contents)
173 (call-with-temporary-directory
174 (lambda (directory)
175 (let ((script-file-name (string-append directory "/foo")))
176 (call-with-output-file script-file-name
177 (lambda (port)
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
185 (delete-file "foo"))
186 str))))))
187
188 (let ((script-contents "\
189 #!/anything/cabbage-bash-1.2.3/bin/python3 -and -args
190 # vim:fileencoding=utf-8
191 print('hello world')"))
192
193 (test-equal "wrap-script, with encoding declaration"
194 (string-append
195 (format #f "\
196 #!MYGUILE --no-auto-compile
197 #!#; # vim:fileencoding=utf-8
198 #\\-~s
199 #\\-~s
200 "
201 '(begin (let ((current (getenv "GUIX_FOO")))
202 (setenv "GUIX_FOO"
203 (if current
204 (string-append "/some/path:/some/other/path"
205 ":" current)
206 "/some/path:/some/other/path"))))
207 `(let ((cl (command-line)))
208 (apply execl "/anything/cabbage-bash-1.2.3/bin/python3"
209 (car cl)
210 (cons (car cl)
211 (append '("" "-and" "-args") cl)))))
212 script-contents)
213 (call-with-temporary-directory
214 (lambda (directory)
215 (let ((script-file-name (string-append directory "/foo")))
216 (call-with-output-file script-file-name
217 (lambda (port)
218 (format port script-contents)))
219 (chmod script-file-name #o777)
220
221 (wrap-script script-file-name
222 #:guile "MYGUILE"
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
227 (delete-file "foo"))
228 str))))))
229
230 (test-assert "wrap-script, raises condition"
231 (call-with-temporary-directory
232 (lambda (directory)
233 (let ((script-file-name (string-append directory "/foo")))
234 (call-with-output-file script-file-name
235 (lambda (port)
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
240 #:guile "MYGUILE"
241 `("GUIX_FOO" prefix ("/some/path"
242 "/some/other/path")))
243 #f)))))
244
245 (test-equal "substitute*, text contains a NUL byte, UTF-8"
246 "c\0d"
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
254 (lambda (file port)
255 (format port "a\0b")
256 (flush-output-port port)
257
258 (substitute* file
259 (("a") "c")
260 (("b") "d"))
261
262 (with-input-from-file file
263 (lambda _
264 (get-string-all (current-input-port))))))))
265
266 (test-end)