gnu: waybar: Fix build.
[jackhill/guix/guix.git] / tests / build-utils.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
f0cc5e7e 2;;; Copyright © 2012, 2015, 2016, 2019 Ludovic Courtès <ludo@gnu.org>
0fb9a8df 3;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
b0e0d0e9 4;;;
233e7676 5;;; This file is part of GNU Guix.
b0e0d0e9 6;;;
233e7676 7;;; GNU Guix is free software; you can redistribute it and/or modify it
b0e0d0e9
LC
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.
11;;;
233e7676 12;;; GNU Guix is distributed in the hope that it will be useful, but
b0e0d0e9
LC
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.
16;;;
17;;; You should have received a copy of the GNU General Public License
233e7676 18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
b0e0d0e9
LC
19
20
21(define-module (test-build-utils)
de611138 22 #:use-module (guix tests)
b0e0d0e9 23 #:use-module (guix build utils)
7370c021
LC
24 #:use-module ((guix utils)
25 #:select (%current-system call-with-temporary-directory))
de611138
EB
26 #:use-module (gnu packages)
27 #:use-module (gnu packages bootstrap)
28 #:use-module (srfi srfi-34)
f0cc5e7e 29 #:use-module (srfi srfi-35)
de611138
EB
30 #:use-module (srfi srfi-64)
31 #:use-module (rnrs io ports)
32 #:use-module (ice-9 popen))
b0e0d0e9 33
de611138 34\f
b0e0d0e9
LC
35(test-begin "build-utils")
36
37(test-equal "alist-cons-before"
38 '((a . 1) (x . 42) (b . 2) (c . 3))
39 (alist-cons-before 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
40
41(test-equal "alist-cons-before, reference not found"
42 '((a . 1) (b . 2) (c . 3) (x . 42))
43 (alist-cons-before 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
44
45(test-equal "alist-cons-after"
46 '((a . 1) (b . 2) (x . 42) (c . 3))
47 (alist-cons-after 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
48
49(test-equal "alist-cons-after, reference not found"
50 '((a . 1) (b . 2) (c . 3) (x . 42))
51 (alist-cons-after 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
52
53(test-equal "alist-replace"
54 '((a . 1) (b . 77) (c . 3))
55 (alist-replace 'b 77 '((a . 1) (b . 2) (c . 3))))
56
57(test-assert "alist-replace, key not found"
58 (not (false-if-exception
59 (alist-replace 'z 77 '((a . 1) (b . 2) (c . 3))))))
60
91133c2d
LC
61(test-equal "fold-port-matches"
62 (make-list 3 "Guix")
63 (call-with-input-string "Guix is cool, Guix rocks, and it uses Guile, Guix!"
64 (lambda (port)
65 (fold-port-matches cons '() "Guix" port))))
66
67(test-equal "fold-port-matches, trickier"
68 (reverse '("Guix" "guix" "Guix" "guiX" "Guix"))
69 (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
70 (lambda (port)
71 (fold-port-matches cons '()
72 (list (char-set #\G #\g)
73 (char-set #\u)
74 (char-set #\i)
75 (char-set #\x #\X))
76 port))))
77
78(test-equal "fold-port-matches, with unmatched chars"
79 '("Guix" #\, #\space
80 "guix" #\, #\space
81 #\G #\u #\i "Guix" "guiX" #\, #\space
82 "Guix")
83 (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
84 (lambda (port)
85 (reverse
86 (fold-port-matches cons '()
87 (list (char-set #\G #\g)
88 (char-set #\u)
89 (char-set #\i)
90 (char-set #\x #\X))
91 port
92 cons)))))
93
7370c021
LC
94(test-equal "wrap-program, one input, multiple calls"
95 "hello world\n"
96 (call-with-temporary-directory
97 (lambda (directory)
98 (let ((bash (search-bootstrap-binary "bash" (%current-system)))
99 (foo (string-append directory "/foo")))
100
101 (call-with-output-file foo
102 (lambda (p)
103 (format p
104 "#!~a~%echo \"${GUIX_FOO} ${GUIX_BAR}\"~%"
105 bash)))
106 (chmod foo #o777)
107
108 ;; wrap-program uses `which' to find bash for the wrapper shebang, but
109 ;; it can't know about the bootstrap bash in the store, since it's not
110 ;; named "bash". Help it out a bit by providing a symlink it this
111 ;; package's output.
22f95e02
LC
112 (with-environment-variable "PATH" (dirname bash)
113 (wrap-program foo `("GUIX_FOO" prefix ("hello")))
114 (wrap-program foo `("GUIX_BAR" prefix ("world")))
115
116 ;; The bootstrap Bash is linked against an old libc and would abort
117 ;; with an assertion failure when trying to load incompatible locale
118 ;; data.
119 (unsetenv "LOCPATH")
120
121 (let* ((pipe (open-input-pipe foo))
122 (str (get-string-all pipe)))
123 (with-directory-excursion directory
124 (for-each delete-file '("foo" ".foo-real")))
125 (and (zero? (close-pipe pipe))
126 str)))))))
127
f0cc5e7e
LC
128(test-assert "invoke/quiet, success"
129 (begin
130 (invoke/quiet "true")
131 #t))
132
133(test-assert "invoke/quiet, failure"
134 (guard (c ((message-condition? c)
135 (string-contains (condition-message c) "This is an error.")))
136 (invoke/quiet "sh" "-c" "echo This is an error. ; false")
137 #f))
138
139(test-assert "invoke/quiet, failure, message on stderr"
140 (guard (c ((message-condition? c)
141 (string-contains (condition-message c)
142 "This is another error.")))
143 (invoke/quiet "sh" "-c" "echo This is another error. >&2 ; false")
144 #f))
de611138 145
0fb9a8df
RW
146(let ((script-contents "\
147#!/anything/cabbage-bash-1.2.3/bin/sh
148
149echo hello world"))
150
151 (test-equal "wrap-script, simple case"
152 (string-append
153 (format #f "\
f5698dfb 154#!~a --no-auto-compile
0fb9a8df
RW
155#!#; Guix wrapper
156#\\-~s
157#\\-~s
158"
f5698dfb 159 (which "guile")
0fb9a8df
RW
160 '(begin (let ((current (getenv "GUIX_FOO")))
161 (setenv "GUIX_FOO"
162 (if current
163 (string-append "/some/path:/some/other/path"
164 ":" current)
165 "/some/path:/some/other/path"))))
166 '(let ((cl (command-line)))
167 (apply execl "/anything/cabbage-bash-1.2.3/bin/sh"
168 (car cl)
169 (cons (car cl)
170 (append '("") cl)))))
171 script-contents)
172 (call-with-temporary-directory
173 (lambda (directory)
174 (let ((script-file-name (string-append directory "/foo")))
175 (call-with-output-file script-file-name
176 (lambda (port)
177 (format port script-contents)))
178 (chmod script-file-name #o777)
f5698dfb
LC
179 (wrap-script script-file-name
180 `("GUIX_FOO" prefix ("/some/path"
181 "/some/other/path")))
0fb9a8df
RW
182 (let ((str (call-with-input-file script-file-name get-string-all)))
183 (with-directory-excursion directory
184 (delete-file "foo"))
185 str))))))
186
187(let ((script-contents "\
188#!/anything/cabbage-bash-1.2.3/bin/python3 -and -args
189# vim:fileencoding=utf-8
190print('hello world')"))
191
192 (test-equal "wrap-script, with encoding declaration"
193 (string-append
194 (format #f "\
195#!MYGUILE --no-auto-compile
196#!#; # vim:fileencoding=utf-8
197#\\-~s
198#\\-~s
199"
200 '(begin (let ((current (getenv "GUIX_FOO")))
201 (setenv "GUIX_FOO"
202 (if current
203 (string-append "/some/path:/some/other/path"
204 ":" current)
205 "/some/path:/some/other/path"))))
206 `(let ((cl (command-line)))
207 (apply execl "/anything/cabbage-bash-1.2.3/bin/python3"
208 (car cl)
209 (cons (car cl)
210 (append '("" "-and" "-args") cl)))))
211 script-contents)
212 (call-with-temporary-directory
213 (lambda (directory)
214 (let ((script-file-name (string-append directory "/foo")))
215 (call-with-output-file script-file-name
216 (lambda (port)
217 (format port script-contents)))
218 (chmod script-file-name #o777)
219
220 (wrap-script script-file-name
221 #:guile "MYGUILE"
222 `("GUIX_FOO" prefix ("/some/path"
223 "/some/other/path")))
224 (let ((str (call-with-input-file script-file-name get-string-all)))
225 (with-directory-excursion directory
226 (delete-file "foo"))
227 str))))))
228
229(test-assert "wrap-script, raises condition"
230 (call-with-temporary-directory
231 (lambda (directory)
232 (let ((script-file-name (string-append directory "/foo")))
233 (call-with-output-file script-file-name
234 (lambda (port)
235 (format port "This is not a script")))
236 (chmod script-file-name #o777)
1d64afbd
LC
237 (guard (c ((wrap-error? c) #t))
238 (wrap-script script-file-name
239 #:guile "MYGUILE"
240 `("GUIX_FOO" prefix ("/some/path"
241 "/some/other/path")))
242 #f)))))
0fb9a8df 243
b0e0d0e9 244(test-end)