channels.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  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-channels)
  19. #:use-module (guix channels)
  20. #:use-module (guix profiles)
  21. #:use-module ((guix build syscalls) #:select (mkdtemp!))
  22. #:use-module (guix tests)
  23. #:use-module (guix store)
  24. #:use-module ((guix grafts) #:select (%graft?))
  25. #:use-module (guix derivations)
  26. #:use-module (guix sets)
  27. #:use-module (guix gexp)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-26)
  30. #:use-module (srfi srfi-64)
  31. #:use-module (ice-9 match))
  32. (test-begin "channels")
  33. (define* (make-instance #:key
  34. (name 'fake)
  35. (commit "cafebabe")
  36. (spec #f))
  37. (define instance-dir (mkdtemp! "/tmp/checkout.XXXXXX"))
  38. (and spec
  39. (with-output-to-file (string-append instance-dir "/.guix-channel")
  40. (lambda _ (format #t "~a" spec))))
  41. (checkout->channel-instance instance-dir
  42. #:commit commit
  43. #:name name))
  44. (define instance--boring (make-instance))
  45. (define instance--no-deps
  46. (make-instance #:spec
  47. '(channel
  48. (version 0)
  49. (dependencies
  50. (channel
  51. (name test-channel)
  52. (url "https://example.com/test-channel"))))))
  53. (define instance--simple
  54. (make-instance #:spec
  55. '(channel
  56. (version 0)
  57. (dependencies
  58. (channel
  59. (name test-channel)
  60. (url "https://example.com/test-channel"))))))
  61. (define instance--with-dupes
  62. (make-instance #:spec
  63. '(channel
  64. (version 0)
  65. (dependencies
  66. (channel
  67. (name test-channel)
  68. (url "https://example.com/test-channel"))
  69. (channel
  70. (name test-channel)
  71. (url "https://example.com/test-channel")
  72. (commit "abc1234"))
  73. (channel
  74. (name test-channel)
  75. (url "https://example.com/test-channel-elsewhere"))))))
  76. (define read-channel-metadata
  77. (@@ (guix channels) read-channel-metadata))
  78. (test-equal "read-channel-metadata returns #f if .guix-channel does not exist"
  79. #f
  80. (read-channel-metadata instance--boring))
  81. (test-assert "read-channel-metadata returns <channel-metadata>"
  82. (every (@@ (guix channels) channel-metadata?)
  83. (map read-channel-metadata
  84. (list instance--no-deps
  85. instance--simple
  86. instance--with-dupes))))
  87. (test-assert "read-channel-metadata dependencies are channels"
  88. (let ((deps ((@@ (guix channels) channel-metadata-dependencies)
  89. (read-channel-metadata instance--simple))))
  90. (match deps
  91. (((? channel? dep)) #t)
  92. (_ #f))))
  93. (test-assert "latest-channel-instances includes channel dependencies"
  94. (let* ((channel (channel
  95. (name 'test)
  96. (url "test")))
  97. (test-dir (channel-instance-checkout instance--simple)))
  98. (mock ((guix git) latest-repository-commit
  99. (lambda* (store url #:key ref)
  100. (match url
  101. ("test" (values test-dir 'whatever))
  102. (_ (values "/not-important" 'not-important)))))
  103. (let ((instances (latest-channel-instances #f (list channel))))
  104. (and (eq? 2 (length instances))
  105. (lset= eq?
  106. '(test test-channel)
  107. (map (compose channel-name channel-instance-channel)
  108. instances)))))))
  109. (test-assert "latest-channel-instances excludes duplicate channel dependencies"
  110. (let* ((channel (channel
  111. (name 'test)
  112. (url "test")))
  113. (test-dir (channel-instance-checkout instance--with-dupes)))
  114. (mock ((guix git) latest-repository-commit
  115. (lambda* (store url #:key ref)
  116. (match url
  117. ("test" (values test-dir 'whatever))
  118. (_ (values "/not-important" 'not-important)))))
  119. (let ((instances (latest-channel-instances #f (list channel))))
  120. (and (eq? 2 (length instances))
  121. (lset= eq?
  122. '(test test-channel)
  123. (map (compose channel-name channel-instance-channel)
  124. instances))
  125. ;; only the most specific channel dependency should remain,
  126. ;; i.e. the one with a specified commit.
  127. (find (lambda (instance)
  128. (and (eq? (channel-name
  129. (channel-instance-channel instance))
  130. 'test-channel)
  131. (eq? (channel-commit
  132. (channel-instance-channel instance))
  133. 'abc1234)))
  134. instances))))))
  135. (test-assert "channel-instances->manifest"
  136. ;; Compute the manifest for a graph of instances and make sure we get a
  137. ;; derivation graph that mirrors the instance graph. This test also ensures
  138. ;; we don't try to access Git repositores at all at this stage.
  139. (let* ((spec (lambda deps
  140. `(channel (version 0)
  141. (dependencies
  142. ,@(map (lambda (dep)
  143. `(channel
  144. (name ,dep)
  145. (url "http://example.org")))
  146. deps)))))
  147. (guix (make-instance #:name 'guix))
  148. (instance0 (make-instance #:name 'a))
  149. (instance1 (make-instance #:name 'b #:spec (spec 'a)))
  150. (instance2 (make-instance #:name 'c #:spec (spec 'b)))
  151. (instance3 (make-instance #:name 'd #:spec (spec 'c 'a))))
  152. (%graft? #f) ;don't try to build stuff
  153. ;; Create 'build-self.scm' so that GUIX is recognized as the 'guix' channel.
  154. (let ((source (channel-instance-checkout guix)))
  155. (mkdir (string-append source "/build-aux"))
  156. (call-with-output-file (string-append source
  157. "/build-aux/build-self.scm")
  158. (lambda (port)
  159. (write '(begin
  160. (use-modules (guix) (gnu packages bootstrap))
  161. (lambda _
  162. (package->derivation %bootstrap-guile)))
  163. port))))
  164. (with-store store
  165. (let ()
  166. (define manifest
  167. (run-with-store store
  168. (channel-instances->manifest (list guix
  169. instance0 instance1
  170. instance2 instance3))))
  171. (define entries
  172. (manifest-entries manifest))
  173. (define (depends? drv in out)
  174. ;; Return true if DRV depends (directly or indirectly) on all of IN
  175. ;; and none of OUT.
  176. (let ((set (list->set
  177. (requisites store
  178. (list (derivation-file-name drv)))))
  179. (in (map derivation-file-name in))
  180. (out (map derivation-file-name out)))
  181. (and (every (cut set-contains? set <>) in)
  182. (not (any (cut set-contains? set <>) out)))))
  183. (define (lookup name)
  184. (run-with-store store
  185. (lower-object
  186. (manifest-entry-item
  187. (manifest-lookup manifest
  188. (manifest-pattern (name name)))))))
  189. (let ((drv-guix (lookup "guix"))
  190. (drv0 (lookup "a"))
  191. (drv1 (lookup "b"))
  192. (drv2 (lookup "c"))
  193. (drv3 (lookup "d")))
  194. (and (depends? drv-guix '() (list drv0 drv1 drv2 drv3))
  195. (depends? drv0
  196. (list) (list drv1 drv2 drv3))
  197. (depends? drv1
  198. (list drv0) (list drv2 drv3))
  199. (depends? drv2
  200. (list drv1) (list drv3))
  201. (depends? drv3
  202. (list drv2 drv0) (list))))))))
  203. (test-end "channels")