build-utils.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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>
  6. ;;; Copyright © 2021 Brendan Tildesley <mail@brendan.scot>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; 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))
  34. (test-begin "build-utils")
  35. (test-equal "alist-cons-before"
  36. '((a . 1) (x . 42) (b . 2) (c . 3))
  37. (alist-cons-before 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
  38. (test-equal "alist-cons-before, reference not found"
  39. '((a . 1) (b . 2) (c . 3) (x . 42))
  40. (alist-cons-before 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
  41. (test-equal "alist-cons-after"
  42. '((a . 1) (b . 2) (x . 42) (c . 3))
  43. (alist-cons-after 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
  44. (test-equal "alist-cons-after, reference not found"
  45. '((a . 1) (b . 2) (c . 3) (x . 42))
  46. (alist-cons-after 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
  47. (test-equal "alist-replace"
  48. '((a . 1) (b . 77) (c . 3))
  49. (alist-replace 'b 77 '((a . 1) (b . 2) (c . 3))))
  50. (test-assert "alist-replace, key not found"
  51. (not (false-if-exception
  52. (alist-replace 'z 77 '((a . 1) (b . 2) (c . 3))))))
  53. (test-equal "fold-port-matches"
  54. (make-list 3 "Guix")
  55. (call-with-input-string "Guix is cool, Guix rocks, and it uses Guile, Guix!"
  56. (lambda (port)
  57. (fold-port-matches cons '() "Guix" port))))
  58. (test-equal "fold-port-matches, trickier"
  59. (reverse '("Guix" "guix" "Guix" "guiX" "Guix"))
  60. (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
  61. (lambda (port)
  62. (fold-port-matches cons '()
  63. (list (char-set #\G #\g)
  64. (char-set #\u)
  65. (char-set #\i)
  66. (char-set #\x #\X))
  67. port))))
  68. (test-equal "fold-port-matches, with unmatched chars"
  69. '("Guix" #\, #\space
  70. "guix" #\, #\space
  71. #\G #\u #\i "Guix" "guiX" #\, #\space
  72. "Guix")
  73. (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
  74. (lambda (port)
  75. (reverse
  76. (fold-port-matches cons '()
  77. (list (char-set #\G #\g)
  78. (char-set #\u)
  79. (char-set #\i)
  80. (char-set #\x #\X))
  81. port
  82. cons)))))
  83. (test-equal "wrap-program, one input, multiple calls"
  84. "hello world\n"
  85. (call-with-temporary-directory
  86. (lambda (directory)
  87. (let ((bash (search-bootstrap-binary "bash" (%current-system)))
  88. (foo (string-append directory "/foo")))
  89. (call-with-output-file foo
  90. (lambda (p)
  91. (format p
  92. "#!~a~%echo \"${GUIX_FOO} ${GUIX_BAR}\"~%"
  93. bash)))
  94. (chmod foo #o777)
  95. ;; wrap-program uses `which' to find bash for the wrapper shebang, but
  96. ;; it can't know about the bootstrap bash in the store, since it's not
  97. ;; named "bash". Help it out a bit by providing a symlink it this
  98. ;; package's output.
  99. (with-environment-variable "PATH" (dirname bash)
  100. (wrap-program foo `("GUIX_FOO" prefix ("hello")))
  101. (wrap-program foo `("GUIX_BAR" prefix ("world")))
  102. ;; The bootstrap Bash is linked against an old libc and would abort
  103. ;; with an assertion failure when trying to load incompatible locale
  104. ;; data.
  105. (unsetenv "LOCPATH")
  106. (let* ((pipe (open-input-pipe foo))
  107. (str (get-string-all pipe)))
  108. (with-directory-excursion directory
  109. (for-each delete-file '("foo" ".foo-real")))
  110. (and (zero? (close-pipe pipe))
  111. str)))))))
  112. (test-assert "invoke/quiet, success"
  113. (begin
  114. (invoke/quiet "true")
  115. #t))
  116. (test-assert "invoke/quiet, failure"
  117. (guard (c ((message-condition? c)
  118. (string-contains (condition-message c) "This is an error.")))
  119. (invoke/quiet "sh" "-c" "echo This is an error. ; false")
  120. #f))
  121. (test-assert "invoke/quiet, failure, message on stderr"
  122. (guard (c ((message-condition? c)
  123. (string-contains (condition-message c)
  124. "This is another error.")))
  125. (invoke/quiet "sh" "-c" "echo This is another error. >&2 ; false")
  126. #f))
  127. (let ((script-contents "\
  128. #!/anything/cabbage-bash-1.2.3/bin/sh
  129. echo hello world"))
  130. (test-equal "wrap-script, simple case"
  131. (string-append
  132. (format #f "\
  133. #!~a --no-auto-compile
  134. #!#; Guix wrapper
  135. #\\-~s
  136. #\\-~s
  137. "
  138. (which "guile")
  139. '(begin (let ((current (getenv "GUIX_FOO")))
  140. (setenv "GUIX_FOO"
  141. (if current
  142. (string-append "/some/path:/some/other/path"
  143. ":" current)
  144. "/some/path:/some/other/path"))))
  145. '(let ((cl (command-line)))
  146. (apply execl "/anything/cabbage-bash-1.2.3/bin/sh"
  147. (car cl) (append (quote ()) cl))))
  148. script-contents)
  149. (call-with-temporary-directory
  150. (lambda (directory)
  151. (let ((script-file-name (string-append directory "/foo")))
  152. (call-with-output-file script-file-name
  153. (lambda (port)
  154. (display script-contents port)))
  155. (chmod script-file-name #o777)
  156. (wrap-script script-file-name
  157. `("GUIX_FOO" prefix ("/some/path"
  158. "/some/other/path")))
  159. (let ((str (call-with-input-file script-file-name get-string-all)))
  160. (with-directory-excursion directory
  161. (delete-file "foo"))
  162. str))))))
  163. (let ((script-contents "\
  164. #!/anything/cabbage-bash-1.2.3/bin/python3 -and -args
  165. # vim:fileencoding=utf-8
  166. print('hello world')"))
  167. (test-equal "wrap-script, with encoding declaration"
  168. (string-append
  169. (format #f "\
  170. #!MYGUILE --no-auto-compile
  171. #!#; # vim:fileencoding=utf-8
  172. #\\-~s
  173. #\\-~s
  174. "
  175. '(begin (let ((current (getenv "GUIX_FOO")))
  176. (setenv "GUIX_FOO"
  177. (if current
  178. (string-append "/some/path:/some/other/path"
  179. ":" current)
  180. "/some/path:/some/other/path"))))
  181. `(let ((cl (command-line)))
  182. (apply execl "/anything/cabbage-bash-1.2.3/bin/python3"
  183. (car cl)
  184. (append '("-and" "-args") cl))))
  185. script-contents)
  186. (call-with-temporary-directory
  187. (lambda (directory)
  188. (let ((script-file-name (string-append directory "/foo")))
  189. (call-with-output-file script-file-name
  190. (lambda (port)
  191. (format port script-contents)))
  192. (chmod script-file-name #o777)
  193. (wrap-script script-file-name
  194. #:guile "MYGUILE"
  195. `("GUIX_FOO" prefix ("/some/path"
  196. "/some/other/path")))
  197. (let ((str (call-with-input-file script-file-name get-string-all)))
  198. (with-directory-excursion directory
  199. (delete-file "foo"))
  200. str))))))
  201. (test-assert "wrap-script, raises condition"
  202. (call-with-temporary-directory
  203. (lambda (directory)
  204. (let ((script-file-name (string-append directory "/foo")))
  205. (call-with-output-file script-file-name
  206. (lambda (port)
  207. (format port "This is not a script")))
  208. (chmod script-file-name #o777)
  209. (guard (c ((wrap-error? c) #t))
  210. (wrap-script script-file-name
  211. #:guile "MYGUILE"
  212. `("GUIX_FOO" prefix ("/some/path"
  213. "/some/other/path")))
  214. #f)))))
  215. (define (arg-test bash-args)
  216. (call-with-temporary-directory
  217. (lambda (directory)
  218. (let ((script-file-name (string-append directory "/bash-test.sh")))
  219. (call-with-output-file script-file-name
  220. (lambda (port)
  221. (display (string-append "\
  222. #!" (which "bash") bash-args "
  223. echo \"$#$0$*${A}\"")
  224. port)))
  225. (display "Unwrapped script contents:\n")
  226. (call-with-input-file script-file-name
  227. (lambda (port) (display (get-string-all port))))
  228. (newline) (newline)
  229. (chmod script-file-name #o777)
  230. (setenv "A" "A")
  231. (let* ((run-script (lambda _
  232. (open-pipe*
  233. OPEN_READ
  234. script-file-name "1" "2" "3 3" "4")))
  235. (pipe (run-script))
  236. (unwrapped-output (get-string-all pipe)))
  237. (close-pipe pipe)
  238. (wrap-script script-file-name `("A" = ("A\nA")))
  239. (display "Wrapped script contents:\n")
  240. (call-with-input-file script-file-name
  241. (lambda (port) (display (get-string-all port))))
  242. (newline) (newline)
  243. (let* ((pipe (run-script))
  244. (wrapped-output (get-string-all pipe)))
  245. (close-pipe pipe)
  246. (display "./bash-test.sh 1 2 3\\ 3 4 # Output:\n")
  247. (display unwrapped-output) (newline)
  248. (display "./bash-test.sh 1 2 3\\ 3 4 # Output (wrapped):\n")
  249. (display wrapped-output) (newline)
  250. (string=? (string-append unwrapped-output "A\n")
  251. wrapped-output)))))))
  252. (test-assert "wrap-script, argument handling"
  253. (arg-test ""))
  254. (test-assert "wrap-script, argument handling, bash --norc"
  255. (arg-test " --norc"))
  256. (test-equal "substitute*, text contains a NUL byte, UTF-8"
  257. "c\0d"
  258. (with-fluids ((%default-port-encoding "UTF-8")
  259. (%default-port-conversion-strategy 'error))
  260. ;; The GNU libc is locale sensitive. Depending on the value of LANG, the
  261. ;; test could fail with "string contains #\\nul character: ~S" or "cannot
  262. ;; convert wide string to output locale".
  263. (setlocale LC_ALL "en_US.UTF-8")
  264. (call-with-temporary-output-file
  265. (lambda (file port)
  266. (format port "a\0b")
  267. (flush-output-port port)
  268. (substitute* file
  269. (("a") "c")
  270. (("b") "d"))
  271. (with-input-from-file file
  272. (lambda _
  273. (get-string-all (current-input-port))))))))
  274. (test-equal "search-input-file: exception if not found"
  275. `((path)
  276. (file . "does-not-exist"))
  277. (guard (e ((search-error? e)
  278. `((path . ,(search-error-path e))
  279. (file . ,(search-error-file e)))))
  280. (search-input-file '() "does-not-exist")))
  281. (test-equal "search-input-file: can find if existent"
  282. (which "guile")
  283. (search-input-file
  284. `(("guile/bin" . ,(dirname (which "guile"))))
  285. "guile"))
  286. (test-equal "search-input-file: can search in multiple directories"
  287. (which "guile")
  288. (call-with-temporary-directory
  289. (lambda (directory)
  290. (search-input-file
  291. `(("irrelevant" . ,directory)
  292. ("guile/bin" . ,(dirname (which "guile"))))
  293. "guile"))))
  294. (test-end)