inferior.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 (gnu packages sqlite)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-34)
  31. #:use-module (srfi srfi-64)
  32. #:use-module (ice-9 match))
  33. (define %top-srcdir
  34. (dirname (search-path %load-path "guix.scm")))
  35. (define %top-builddir
  36. (dirname (search-path %load-compiled-path "guix.go")))
  37. (define %store
  38. (open-connection-for-tests))
  39. (define (manifest-entry->list entry)
  40. (list (manifest-entry-name entry)
  41. (manifest-entry-version entry)
  42. (manifest-entry-output entry)
  43. (manifest-entry-search-paths entry)
  44. (map manifest-entry->list (manifest-entry-dependencies entry))))
  45. (test-begin "inferior")
  46. (test-equal "open-inferior"
  47. '(42 #t)
  48. (let ((inferior (open-inferior %top-builddir
  49. #:command "scripts/guix")))
  50. (and (inferior? inferior)
  51. (let ((a (inferior-eval '(apply * '(6 7)) inferior))
  52. (b (inferior-eval '(@ (gnu packages base) coreutils)
  53. inferior)))
  54. (close-inferior inferior)
  55. (list a (inferior-object? b))))))
  56. (test-equal "&inferior-exception"
  57. '(a b c d)
  58. (let ((inferior (open-inferior %top-builddir
  59. #:command "scripts/guix")))
  60. (guard (c ((inferior-exception? c)
  61. (close-inferior inferior)
  62. (and (eq? inferior (inferior-exception-inferior c))
  63. (match (inferior-exception-stack c)
  64. (((_ (files lines columns)) ..1)
  65. (member "guix/repl.scm" files)))
  66. (inferior-exception-arguments c))))
  67. (inferior-eval '(throw 'a 'b 'c 'd) inferior)
  68. 'badness)))
  69. (test-equal "&inferior-exception, legacy mode"
  70. '(a b c d)
  71. ;; Omit #:command to open an inferior in "legacy" mode, where Guile runs
  72. ;; directly.
  73. (let ((inferior (open-inferior %top-builddir)))
  74. (guard (c ((inferior-exception? c)
  75. (close-inferior inferior)
  76. (and (eq? inferior (inferior-exception-inferior c))
  77. (inferior-exception-arguments c))))
  78. (inferior-eval '(throw 'a 'b 'c 'd) inferior)
  79. 'badness)))
  80. (test-equal "inferior-packages"
  81. (take (sort (fold-packages (lambda (package lst)
  82. (cons (list (package-name package)
  83. (package-version package)
  84. (package-home-page package)
  85. (package-location package))
  86. lst))
  87. '())
  88. (lambda (x y)
  89. (string<? (car x) (car y))))
  90. 10)
  91. (let* ((inferior (open-inferior %top-builddir
  92. #:command "scripts/guix"))
  93. (packages (inferior-packages inferior)))
  94. (and (every string? (map inferior-package-synopsis packages))
  95. (let ()
  96. (define result
  97. (take (sort (map (lambda (package)
  98. (list (inferior-package-name package)
  99. (inferior-package-version package)
  100. (inferior-package-home-page package)
  101. (inferior-package-location package)))
  102. packages)
  103. (lambda (x y)
  104. (string<? (car x) (car y))))
  105. 10))
  106. (close-inferior inferior)
  107. result))))
  108. (test-equal "inferior-available-packages"
  109. (take (sort (fold-available-packages
  110. (lambda* (name version result
  111. #:key supported? deprecated?
  112. #:allow-other-keys)
  113. (if (and supported? (not deprecated?))
  114. (alist-cons name version result)
  115. result))
  116. '())
  117. (lambda (x y)
  118. (string<? (car x) (car y))))
  119. 10)
  120. (let* ((inferior (open-inferior %top-builddir
  121. #:command "scripts/guix"))
  122. (packages (inferior-available-packages inferior)))
  123. (close-inferior inferior)
  124. (take (sort packages (lambda (x y)
  125. (string<? (car x) (car y))))
  126. 10)))
  127. (test-equal "lookup-inferior-packages"
  128. (let ((->list (lambda (package)
  129. (list (package-name package)
  130. (package-version package)
  131. (package-location package)))))
  132. (list (map ->list (find-packages-by-name "guile" #f))
  133. (map ->list (find-packages-by-name "guile" "2.2"))))
  134. (let* ((inferior (open-inferior %top-builddir
  135. #:command "scripts/guix"))
  136. (->list (lambda (package)
  137. (list (inferior-package-name package)
  138. (inferior-package-version package)
  139. (inferior-package-location package))))
  140. (lst1 (map ->list
  141. (lookup-inferior-packages inferior "guile")))
  142. (lst2 (map ->list
  143. (lookup-inferior-packages inferior
  144. "guile" "2.2"))))
  145. (close-inferior inferior)
  146. (list lst1 lst2)))
  147. (test-assert "lookup-inferior-packages and eq?-ness"
  148. (let* ((inferior (open-inferior %top-builddir
  149. #:command "scripts/guix"))
  150. (lst1 (lookup-inferior-packages inferior "guile"))
  151. (lst2 (lookup-inferior-packages inferior "guile")))
  152. (close-inferior inferior)
  153. (every eq? lst1 lst2)))
  154. (test-equal "inferior-package-inputs"
  155. (let ((->list (match-lambda
  156. ((label (? package? package) . rest)
  157. `(,label
  158. (package ,(package-name package)
  159. ,(package-version package)
  160. ,(package-location package))
  161. ,@rest)))))
  162. (list (map ->list (package-inputs guile-3.0-latest))
  163. (map ->list (package-native-inputs guile-3.0-latest))
  164. (map ->list (package-propagated-inputs guile-3.0-latest))))
  165. (let* ((inferior (open-inferior %top-builddir
  166. #:command "scripts/guix"))
  167. (guile (first (lookup-inferior-packages inferior "guile")))
  168. (->list (match-lambda
  169. ((label (? inferior-package? package) . rest)
  170. `(,label
  171. (package ,(inferior-package-name package)
  172. ,(inferior-package-version package)
  173. ,(inferior-package-location package))
  174. ,@rest))))
  175. (result (list (map ->list (inferior-package-inputs guile))
  176. (map ->list
  177. (inferior-package-native-inputs guile))
  178. (map ->list
  179. (inferior-package-propagated-inputs
  180. guile)))))
  181. (close-inferior inferior)
  182. result))
  183. (test-equal "inferior-package-search-paths"
  184. (package-native-search-paths guile-3.0)
  185. (let* ((inferior (open-inferior %top-builddir
  186. #:command "scripts/guix"))
  187. (guile (first (lookup-inferior-packages inferior "guile")))
  188. (result (inferior-package-native-search-paths guile)))
  189. (close-inferior inferior)
  190. result))
  191. (test-equal "inferior-eval-with-store"
  192. (add-text-to-store %store "foo" "Hello, world!")
  193. (let* ((inferior (open-inferior %top-builddir
  194. #:command "scripts/guix")))
  195. (inferior-eval-with-store inferior %store
  196. '(lambda (store)
  197. (add-text-to-store store "foo"
  198. "Hello, world!")))))
  199. (test-assert "inferior-eval-with-store, &store-protocol-error"
  200. (let* ((inferior (open-inferior %top-builddir
  201. #:command "scripts/guix")))
  202. (guard (c ((store-protocol-error? c)
  203. (string-contains (store-protocol-error-message c)
  204. "invalid character")))
  205. (inferior-eval-with-store inferior %store
  206. '(lambda (store)
  207. (add-text-to-store store "we|rd/?!@"
  208. "uh uh")))
  209. #f)))
  210. (test-equal "inferior-eval-with-store, exception"
  211. '(the-answer = 42)
  212. (let ((inferior (open-inferior %top-builddir
  213. #:command "scripts/guix")))
  214. (guard (c ((inferior-exception? c)
  215. (close-inferior inferior)
  216. (inferior-exception-arguments c)))
  217. (inferior-eval-with-store inferior %store
  218. '(lambda (store)
  219. (throw 'the-answer '= 42))))))
  220. (test-equal "inferior-eval-with-store, not a procedure"
  221. 'wrong-type-arg
  222. (let ((inferior (open-inferior %top-builddir
  223. #:command "scripts/guix")))
  224. (guard (c ((inferior-exception? c)
  225. (close-inferior inferior)
  226. (car (inferior-exception-arguments c))))
  227. (inferior-eval-with-store inferior %store '(+ 1 2)))))
  228. (test-equal "inferior-package-derivation"
  229. (map derivation-file-name
  230. (list (package-derivation %store %bootstrap-guile "x86_64-linux")
  231. (package-derivation %store %bootstrap-guile "armhf-linux")))
  232. (let* ((inferior (open-inferior %top-builddir
  233. #:command "scripts/guix"))
  234. (packages (inferior-packages inferior))
  235. (guile (find (lambda (package)
  236. (string=? (package-name %bootstrap-guile)
  237. (inferior-package-name package)))
  238. packages)))
  239. (map derivation-file-name
  240. (list (inferior-package-derivation %store guile "x86_64-linux")
  241. (inferior-package-derivation %store guile "armhf-linux")))))
  242. (unless (package-replacement sqlite)
  243. (test-skip 1))
  244. (test-equal "inferior-package-replacement"
  245. (package-derivation %store
  246. (package-replacement sqlite)
  247. "x86_64-linux")
  248. (let* ((inferior (open-inferior %top-builddir
  249. #:command "scripts/guix"))
  250. (packages (inferior-packages inferior)))
  251. (match (lookup-inferior-packages inferior
  252. (package-name sqlite)
  253. (package-version sqlite))
  254. ((inferior-sqlite rest ...)
  255. (inferior-package-derivation %store
  256. (inferior-package-replacement
  257. inferior-sqlite)
  258. "x86_64-linux")))))
  259. (test-equal "inferior-package->manifest-entry"
  260. (manifest-entry->list (package->manifest-entry
  261. (first (find-best-packages-by-name "guile" #f))))
  262. (let* ((inferior (open-inferior %top-builddir
  263. #:command "scripts/guix"))
  264. (guile (first (lookup-inferior-packages inferior "guile")))
  265. (entry (inferior-package->manifest-entry guile)))
  266. (close-inferior inferior)
  267. (manifest-entry->list entry)))
  268. (test-equal "packages->manifest"
  269. (map manifest-entry->list
  270. (manifest-entries (packages->manifest
  271. (find-best-packages-by-name "guile" #f))))
  272. (let* ((inferior (open-inferior %top-builddir
  273. #:command "scripts/guix"))
  274. (guile (first (lookup-inferior-packages inferior "guile")))
  275. (manifest (packages->manifest (list guile))))
  276. (close-inferior inferior)
  277. (map manifest-entry->list (manifest-entries manifest))))
  278. (test-end "inferior")