inferior.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-inferior)
  19. #:use-module (guix tests)
  20. #:use-module (guix inferior)
  21. #:use-module (guix packages)
  22. #:use-module (guix store)
  23. #:use-module (guix profiles)
  24. #:use-module (guix derivations)
  25. #:use-module (gnu packages)
  26. #:use-module (gnu packages bootstrap)
  27. #:use-module (gnu packages guile)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-34)
  30. #:use-module (srfi srfi-64)
  31. #:use-module (ice-9 match))
  32. (define %top-srcdir
  33. (dirname (search-path %load-path "guix.scm")))
  34. (define %top-builddir
  35. (dirname (search-path %load-compiled-path "guix.go")))
  36. (define %store
  37. (open-connection-for-tests))
  38. (define (manifest-entry->list entry)
  39. (list (manifest-entry-name entry)
  40. (manifest-entry-version entry)
  41. (manifest-entry-output entry)
  42. (manifest-entry-search-paths entry)
  43. (map manifest-entry->list (manifest-entry-dependencies entry))))
  44. (test-begin "inferior")
  45. (test-equal "open-inferior"
  46. '(42 #t)
  47. (let ((inferior (open-inferior %top-builddir
  48. #:command "scripts/guix")))
  49. (and (inferior? inferior)
  50. (let ((a (inferior-eval '(apply * '(6 7)) inferior))
  51. (b (inferior-eval '(@ (gnu packages base) coreutils)
  52. inferior)))
  53. (close-inferior inferior)
  54. (list a (inferior-object? b))))))
  55. (test-equal "&inferior-exception"
  56. '(a b c d)
  57. (let ((inferior (open-inferior %top-builddir
  58. #:command "scripts/guix")))
  59. (guard (c ((inferior-exception? c)
  60. (close-inferior inferior)
  61. (and (eq? inferior (inferior-exception-inferior c))
  62. (match (inferior-exception-stack c)
  63. (((_ (files lines columns)) ..1)
  64. (member "guix/repl.scm" files)))
  65. (inferior-exception-arguments c))))
  66. (inferior-eval '(throw 'a 'b 'c 'd) inferior)
  67. 'badness)))
  68. (test-equal "&inferior-exception, legacy mode"
  69. '(a b c d)
  70. ;; Omit #:command to open an inferior in "legacy" mode, where Guile runs
  71. ;; directly.
  72. (let ((inferior (open-inferior %top-builddir)))
  73. (guard (c ((inferior-exception? c)
  74. (close-inferior inferior)
  75. (and (eq? inferior (inferior-exception-inferior c))
  76. (inferior-exception-arguments c))))
  77. (inferior-eval '(throw 'a 'b 'c 'd) inferior)
  78. 'badness)))
  79. (test-equal "inferior-packages"
  80. (take (sort (fold-packages (lambda (package lst)
  81. (cons (list (package-name package)
  82. (package-version package)
  83. (package-home-page package)
  84. (package-location package))
  85. lst))
  86. '())
  87. (lambda (x y)
  88. (string<? (car x) (car y))))
  89. 10)
  90. (let* ((inferior (open-inferior %top-builddir
  91. #:command "scripts/guix"))
  92. (packages (inferior-packages inferior)))
  93. (and (every string? (map inferior-package-synopsis packages))
  94. (let ()
  95. (define result
  96. (take (sort (map (lambda (package)
  97. (list (inferior-package-name package)
  98. (inferior-package-version package)
  99. (inferior-package-home-page package)
  100. (inferior-package-location package)))
  101. packages)
  102. (lambda (x y)
  103. (string<? (car x) (car y))))
  104. 10))
  105. (close-inferior inferior)
  106. result))))
  107. (test-equal "inferior-available-packages"
  108. (take (sort (fold-available-packages
  109. (lambda* (name version result
  110. #:key supported? deprecated?
  111. #:allow-other-keys)
  112. (if (and supported? (not deprecated?))
  113. (alist-cons name version result)
  114. result))
  115. '())
  116. (lambda (x y)
  117. (string<? (car x) (car y))))
  118. 10)
  119. (let* ((inferior (open-inferior %top-builddir
  120. #:command "scripts/guix"))
  121. (packages (inferior-available-packages inferior)))
  122. (close-inferior inferior)
  123. (take (sort packages (lambda (x y)
  124. (string<? (car x) (car y))))
  125. 10)))
  126. (test-equal "lookup-inferior-packages"
  127. (let ((->list (lambda (package)
  128. (list (package-name package)
  129. (package-version package)
  130. (package-location package)))))
  131. (list (map ->list (find-packages-by-name "guile" #f))
  132. (map ->list (find-packages-by-name "guile" "2.2"))))
  133. (let* ((inferior (open-inferior %top-builddir
  134. #:command "scripts/guix"))
  135. (->list (lambda (package)
  136. (list (inferior-package-name package)
  137. (inferior-package-version package)
  138. (inferior-package-location package))))
  139. (lst1 (map ->list
  140. (lookup-inferior-packages inferior "guile")))
  141. (lst2 (map ->list
  142. (lookup-inferior-packages inferior
  143. "guile" "2.2"))))
  144. (close-inferior inferior)
  145. (list lst1 lst2)))
  146. (test-assert "lookup-inferior-packages and eq?-ness"
  147. (let* ((inferior (open-inferior %top-builddir
  148. #:command "scripts/guix"))
  149. (lst1 (lookup-inferior-packages inferior "guile"))
  150. (lst2 (lookup-inferior-packages inferior "guile")))
  151. (close-inferior inferior)
  152. (every eq? lst1 lst2)))
  153. (test-equal "inferior-package-inputs"
  154. (let ((->list (match-lambda
  155. ((label (? package? package) . rest)
  156. `(,label
  157. (package ,(package-name package)
  158. ,(package-version package)
  159. ,(package-location package))
  160. ,@rest)))))
  161. (list (map ->list (package-inputs guile-2.2))
  162. (map ->list (package-native-inputs guile-2.2))
  163. (map ->list (package-propagated-inputs guile-2.2))))
  164. (let* ((inferior (open-inferior %top-builddir
  165. #:command "scripts/guix"))
  166. (guile (first (lookup-inferior-packages inferior "guile")))
  167. (->list (match-lambda
  168. ((label (? inferior-package? package) . rest)
  169. `(,label
  170. (package ,(inferior-package-name package)
  171. ,(inferior-package-version package)
  172. ,(inferior-package-location package))
  173. ,@rest))))
  174. (result (list (map ->list (inferior-package-inputs guile))
  175. (map ->list
  176. (inferior-package-native-inputs guile))
  177. (map ->list
  178. (inferior-package-propagated-inputs
  179. guile)))))
  180. (close-inferior inferior)
  181. result))
  182. (test-equal "inferior-package-search-paths"
  183. (package-native-search-paths guile-3.0)
  184. (let* ((inferior (open-inferior %top-builddir
  185. #:command "scripts/guix"))
  186. (guile (first (lookup-inferior-packages inferior "guile")))
  187. (result (inferior-package-native-search-paths guile)))
  188. (close-inferior inferior)
  189. result))
  190. (test-equal "inferior-eval-with-store"
  191. (add-text-to-store %store "foo" "Hello, world!")
  192. (let* ((inferior (open-inferior %top-builddir
  193. #:command "scripts/guix")))
  194. (inferior-eval-with-store inferior %store
  195. '(lambda (store)
  196. (add-text-to-store store "foo"
  197. "Hello, world!")))))
  198. (test-assert "inferior-eval-with-store, &store-protocol-error"
  199. (let* ((inferior (open-inferior %top-builddir
  200. #:command "scripts/guix")))
  201. (guard (c ((store-protocol-error? c)
  202. (string-contains (store-protocol-error-message c)
  203. "invalid character")))
  204. (inferior-eval-with-store inferior %store
  205. '(lambda (store)
  206. (add-text-to-store store "we|rd/?!@"
  207. "uh uh")))
  208. #f)))
  209. (test-equal "inferior-eval-with-store, exception"
  210. '(the-answer = 42)
  211. (let ((inferior (open-inferior %top-builddir
  212. #:command "scripts/guix")))
  213. (guard (c ((inferior-exception? c)
  214. (close-inferior inferior)
  215. (inferior-exception-arguments c)))
  216. (inferior-eval-with-store inferior %store
  217. '(lambda (store)
  218. (throw 'the-answer '= 42))))))
  219. (test-equal "inferior-eval-with-store, not a procedure"
  220. 'wrong-type-arg
  221. (let ((inferior (open-inferior %top-builddir
  222. #:command "scripts/guix")))
  223. (guard (c ((inferior-exception? c)
  224. (close-inferior inferior)
  225. (car (inferior-exception-arguments c))))
  226. (inferior-eval-with-store inferior %store '(+ 1 2)))))
  227. (test-equal "inferior-package-derivation"
  228. (map derivation-file-name
  229. (list (package-derivation %store %bootstrap-guile "x86_64-linux")
  230. (package-derivation %store %bootstrap-guile "armhf-linux")))
  231. (let* ((inferior (open-inferior %top-builddir
  232. #:command "scripts/guix"))
  233. (packages (inferior-packages inferior))
  234. (guile (find (lambda (package)
  235. (string=? (package-name %bootstrap-guile)
  236. (inferior-package-name package)))
  237. packages)))
  238. (map derivation-file-name
  239. (list (inferior-package-derivation %store guile "x86_64-linux")
  240. (inferior-package-derivation %store guile "armhf-linux")))))
  241. (test-equal "inferior-package->manifest-entry"
  242. (manifest-entry->list (package->manifest-entry
  243. (first (find-best-packages-by-name "guile" #f))))
  244. (let* ((inferior (open-inferior %top-builddir
  245. #:command "scripts/guix"))
  246. (guile (first (lookup-inferior-packages inferior "guile")))
  247. (entry (inferior-package->manifest-entry guile)))
  248. (close-inferior inferior)
  249. (manifest-entry->list entry)))
  250. (test-equal "packages->manifest"
  251. (map manifest-entry->list
  252. (manifest-entries (packages->manifest
  253. (find-best-packages-by-name "guile" #f))))
  254. (let* ((inferior (open-inferior %top-builddir
  255. #:command "scripts/guix"))
  256. (guile (first (lookup-inferior-packages inferior "guile")))
  257. (manifest (packages->manifest (list guile))))
  258. (close-inferior inferior)
  259. (map manifest-entry->list (manifest-entries manifest))))
  260. (test-end "inferior")