build-utils.scm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  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.
  11. ;;;
  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.
  16. ;;;
  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/>.
  19. (define-module (test-build-utils)
  20. #:use-module (guix tests)
  21. #:use-module (guix build utils)
  22. #:use-module ((guix utils)
  23. #:select (%current-system call-with-temporary-directory))
  24. #:use-module (gnu packages)
  25. #:use-module (gnu packages bootstrap)
  26. #:use-module (srfi srfi-34)
  27. #:use-module (srfi srfi-35)
  28. #:use-module (srfi srfi-64)
  29. #:use-module (rnrs io ports)
  30. #:use-module (ice-9 popen))
  31. (test-begin "build-utils")
  32. (test-equal "alist-cons-before"
  33. '((a . 1) (x . 42) (b . 2) (c . 3))
  34. (alist-cons-before 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
  35. (test-equal "alist-cons-before, reference not found"
  36. '((a . 1) (b . 2) (c . 3) (x . 42))
  37. (alist-cons-before 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
  38. (test-equal "alist-cons-after"
  39. '((a . 1) (b . 2) (x . 42) (c . 3))
  40. (alist-cons-after 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
  41. (test-equal "alist-cons-after, reference not found"
  42. '((a . 1) (b . 2) (c . 3) (x . 42))
  43. (alist-cons-after 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
  44. (test-equal "alist-replace"
  45. '((a . 1) (b . 77) (c . 3))
  46. (alist-replace 'b 77 '((a . 1) (b . 2) (c . 3))))
  47. (test-assert "alist-replace, key not found"
  48. (not (false-if-exception
  49. (alist-replace 'z 77 '((a . 1) (b . 2) (c . 3))))))
  50. (test-equal "fold-port-matches"
  51. (make-list 3 "Guix")
  52. (call-with-input-string "Guix is cool, Guix rocks, and it uses Guile, Guix!"
  53. (lambda (port)
  54. (fold-port-matches cons '() "Guix" port))))
  55. (test-equal "fold-port-matches, trickier"
  56. (reverse '("Guix" "guix" "Guix" "guiX" "Guix"))
  57. (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
  58. (lambda (port)
  59. (fold-port-matches cons '()
  60. (list (char-set #\G #\g)
  61. (char-set #\u)
  62. (char-set #\i)
  63. (char-set #\x #\X))
  64. port))))
  65. (test-equal "fold-port-matches, with unmatched chars"
  66. '("Guix" #\, #\space
  67. "guix" #\, #\space
  68. #\G #\u #\i "Guix" "guiX" #\, #\space
  69. "Guix")
  70. (call-with-input-string "Guix, guix, GuiGuixguiX, Guix"
  71. (lambda (port)
  72. (reverse
  73. (fold-port-matches cons '()
  74. (list (char-set #\G #\g)
  75. (char-set #\u)
  76. (char-set #\i)
  77. (char-set #\x #\X))
  78. port
  79. cons)))))
  80. (test-equal "wrap-program, one input, multiple calls"
  81. "hello world\n"
  82. (call-with-temporary-directory
  83. (lambda (directory)
  84. (let ((bash (search-bootstrap-binary "bash" (%current-system)))
  85. (foo (string-append directory "/foo")))
  86. (call-with-output-file foo
  87. (lambda (p)
  88. (format p
  89. "#!~a~%echo \"${GUIX_FOO} ${GUIX_BAR}\"~%"
  90. bash)))
  91. (chmod foo #o777)
  92. ;; wrap-program uses `which' to find bash for the wrapper shebang, but
  93. ;; it can't know about the bootstrap bash in the store, since it's not
  94. ;; named "bash". Help it out a bit by providing a symlink it this
  95. ;; package's output.
  96. (with-environment-variable "PATH" (dirname bash)
  97. (wrap-program foo `("GUIX_FOO" prefix ("hello")))
  98. (wrap-program foo `("GUIX_BAR" prefix ("world")))
  99. ;; The bootstrap Bash is linked against an old libc and would abort
  100. ;; with an assertion failure when trying to load incompatible locale
  101. ;; data.
  102. (unsetenv "LOCPATH")
  103. (let* ((pipe (open-input-pipe foo))
  104. (str (get-string-all pipe)))
  105. (with-directory-excursion directory
  106. (for-each delete-file '("foo" ".foo-real")))
  107. (and (zero? (close-pipe pipe))
  108. str)))))))
  109. (test-assert "invoke/quiet, success"
  110. (begin
  111. (invoke/quiet "true")
  112. #t))
  113. (test-assert "invoke/quiet, failure"
  114. (guard (c ((message-condition? c)
  115. (string-contains (condition-message c) "This is an error.")))
  116. (invoke/quiet "sh" "-c" "echo This is an error. ; false")
  117. #f))
  118. (test-assert "invoke/quiet, failure, message on stderr"
  119. (guard (c ((message-condition? c)
  120. (string-contains (condition-message c)
  121. "This is another error.")))
  122. (invoke/quiet "sh" "-c" "echo This is another error. >&2 ; false")
  123. #f))
  124. (let ((script-contents "\
  125. #!/anything/cabbage-bash-1.2.3/bin/sh
  126. echo hello world"))
  127. (test-equal "wrap-script, simple case"
  128. (string-append
  129. (format #f "\
  130. #!~a --no-auto-compile
  131. #!#; Guix wrapper
  132. #\\-~s
  133. #\\-~s
  134. "
  135. (which "guile")
  136. '(begin (let ((current (getenv "GUIX_FOO")))
  137. (setenv "GUIX_FOO"
  138. (if current
  139. (string-append "/some/path:/some/other/path"
  140. ":" current)
  141. "/some/path:/some/other/path"))))
  142. '(let ((cl (command-line)))
  143. (apply execl "/anything/cabbage-bash-1.2.3/bin/sh"
  144. (car cl)
  145. (cons (car cl)
  146. (append '("") cl)))))
  147. script-contents)
  148. (call-with-temporary-directory
  149. (lambda (directory)
  150. (let ((script-file-name (string-append directory "/foo")))
  151. (call-with-output-file script-file-name
  152. (lambda (port)
  153. (format port script-contents)))
  154. (chmod script-file-name #o777)
  155. (wrap-script script-file-name
  156. `("GUIX_FOO" prefix ("/some/path"
  157. "/some/other/path")))
  158. (let ((str (call-with-input-file script-file-name get-string-all)))
  159. (with-directory-excursion directory
  160. (delete-file "foo"))
  161. str))))))
  162. (let ((script-contents "\
  163. #!/anything/cabbage-bash-1.2.3/bin/python3 -and -args
  164. # vim:fileencoding=utf-8
  165. print('hello world')"))
  166. (test-equal "wrap-script, with encoding declaration"
  167. (string-append
  168. (format #f "\
  169. #!MYGUILE --no-auto-compile
  170. #!#; # vim:fileencoding=utf-8
  171. #\\-~s
  172. #\\-~s
  173. "
  174. '(begin (let ((current (getenv "GUIX_FOO")))
  175. (setenv "GUIX_FOO"
  176. (if current
  177. (string-append "/some/path:/some/other/path"
  178. ":" current)
  179. "/some/path:/some/other/path"))))
  180. `(let ((cl (command-line)))
  181. (apply execl "/anything/cabbage-bash-1.2.3/bin/python3"
  182. (car cl)
  183. (cons (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. (test-end)