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 ((guix utils)
25 #:select (%current-system call-with-temporary-directory))
26 #:use-module (gnu packages)
27 #:use-module (gnu packages bootstrap)
28 #:use-module (srfi srfi-34)
29 #:use-module (srfi srfi-35)
30 #:use-module (srfi srfi-64)
31 #:use-module (rnrs io ports)
32 #:use-module (ice-9 popen))
35 (test-begin "build-utils")
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))))
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))))
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))))
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))))
53 (test-equal "alist-replace"
54 '((a . 1) (b . 77) (c . 3))
55 (alist-replace 'b 77 '((a . 1) (b . 2) (c . 3))))
57 (test-assert "alist-replace, key not found"
58 (not (false-if-exception
59 (alist-replace 'z 77 '((a . 1) (b . 2) (c . 3))))))
61 (test-equal "fold-port-matches"
63 (call-with-input-string "Guix is cool, Guix rocks, and it uses Guile, Guix!"
65 (fold-port-matches cons '() "Guix" port))))
67 (test-equal "fold-port-matches, trickier"
68 (reverse '("Guix" "guix" "Guix" "guiX" "Guix"))
69 (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
71 (fold-port-matches cons '()
72 (list (char-set #\G #\g)
78 (test-equal "fold-port-matches, with unmatched chars"
81 #\G #\u #\i "Guix" "guiX" #\, #\space
83 (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
86 (fold-port-matches cons '()
87 (list (char-set #\G #\g)
94 (test-equal "wrap-program, one input, multiple calls"
96 (call-with-temporary-directory
98 (let ((bash (search-bootstrap-binary "bash" (%current-system)))
99 (foo (string-append directory "/foo")))
101 (call-with-output-file foo
104 "#!~a~%echo \"${GUIX_FOO} ${GUIX_BAR}\"~%"
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
112 (with-environment-variable "PATH" (dirname bash)
113 (wrap-program foo `("GUIX_FOO" prefix ("hello")))
114 (wrap-program foo `("GUIX_BAR" prefix ("world")))
116 ;; The bootstrap Bash is linked against an old libc and would abort
117 ;; with an assertion failure when trying to load incompatible locale
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))
128 (test-assert "invoke/quiet, success"
130 (invoke/quiet "true")
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")
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")
146 (let ((script-contents "\
147 #!/anything/cabbage-bash-1.2.3/bin/sh
151 (test-equal "wrap-script, simple case"
154 #!GUILE --no-auto-compile
159 '(begin (let ((current (getenv "GUIX_FOO")))
162 (string-append "/some/path:/some/other/path"
164 "/some/path:/some/other/path"))))
165 '(let ((cl (command-line)))
166 (apply execl "/anything/cabbage-bash-1.2.3/bin/sh"
169 (append '("") cl)))))
171 (call-with-temporary-directory
173 (let ((script-file-name (string-append directory "/foo")))
174 (call-with-output-file script-file-name
176 (format port script-contents)))
177 (chmod script-file-name #o777)
179 (mock ((guix build utils) which (const "GUILE"))
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")))