1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (test-channels)
20 #:use-module (guix channels)
21 #:use-module (guix profiles)
22 #:use-module ((guix build syscalls) #:select (mkdtemp!))
23 #:use-module (guix tests)
24 #:use-module (guix store)
25 #:use-module ((guix grafts) #:select (%graft?))
26 #:use-module (guix derivations)
27 #:use-module (guix sets)
28 #:use-module (guix gexp)
29 #:use-module ((guix utils)
30 #:select (error-location? error-location location-line))
31 #:use-module ((guix build utils) #:select (which))
33 #:use-module (guix git)
34 #:use-module (guix tests git)
35 #:use-module (srfi srfi-1)
36 #:use-module (srfi srfi-26)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
39 #:use-module (srfi srfi-64)
40 #:use-module (ice-9 match))
42 (test-begin "channels")
44 (define* (make-instance #:key
48 (define instance-dir (mkdtemp! "/tmp/checkout.XXXXXX"))
50 (call-with-output-file (string-append instance-dir "/.guix-channel")
51 (lambda (port) (write spec port))))
52 (checkout->channel-instance instance-dir
56 (define instance--boring (make-instance))
57 (define instance--unsupported-version
59 '(channel (version 42) (dependencies whatever))))
60 (define instance--no-deps
62 '(channel (version 0))))
63 (define instance--sub-directory
65 '(channel (version 0) (directory "modules"))))
66 (define instance--simple
73 (url "https://example.com/test-channel"))))))
74 (define instance--with-dupes
81 (url "https://example.com/test-channel"))
84 (url "https://example.com/test-channel")
88 (url "https://example.com/test-channel-elsewhere"))))))
90 (define channel-instance-metadata
91 (@@ (guix channels) channel-instance-metadata))
92 (define channel-metadata-directory
93 (@@ (guix channels) channel-metadata-directory))
94 (define channel-metadata-dependencies
95 (@@ (guix channels) channel-metadata-dependencies))
98 (test-equal "channel-instance-metadata returns default if .guix-channel does not exist"
100 (let ((metadata (channel-instance-metadata instance--boring)))
101 (list (channel-metadata-directory metadata)
102 (channel-metadata-dependencies metadata))))
104 (test-equal "channel-instance-metadata and default dependencies"
106 (channel-metadata-dependencies (channel-instance-metadata instance--no-deps)))
108 (test-equal "channel-instance-metadata and directory"
110 (channel-metadata-directory
111 (channel-instance-metadata instance--sub-directory)))
113 (test-equal "channel-instance-metadata rejects unsupported version"
114 1 ;line number in the generated '.guix-channel'
115 (guard (c ((and (message-condition? c) (error-location? c))
116 (location-line (error-location c))))
117 (channel-instance-metadata instance--unsupported-version)))
119 (test-assert "channel-instance-metadata returns <channel-metadata>"
120 (every (@@ (guix channels) channel-metadata?)
121 (map channel-instance-metadata
122 (list instance--no-deps
124 instance--with-dupes))))
126 (test-assert "channel-instance-metadata dependencies are channels"
127 (let ((deps ((@@ (guix channels) channel-metadata-dependencies)
128 (channel-instance-metadata instance--simple))))
130 (((? channel? dep)) #t)
133 (test-assert "latest-channel-instances includes channel dependencies"
134 (let* ((channel (channel
137 (test-dir (channel-instance-checkout instance--simple)))
138 (mock ((guix git) latest-repository-commit
139 (lambda* (store url #:key ref)
141 ("test" (values test-dir 'whatever))
142 (_ (values "/not-important" 'not-important)))))
143 (let ((instances (latest-channel-instances #f (list channel))))
144 (and (eq? 2 (length instances))
147 (map (compose channel-name channel-instance-channel)
150 (test-assert "latest-channel-instances excludes duplicate channel dependencies"
151 (let* ((channel (channel
154 (test-dir (channel-instance-checkout instance--with-dupes)))
155 (mock ((guix git) latest-repository-commit
156 (lambda* (store url #:key ref)
158 ("test" (values test-dir 'whatever))
159 (_ (values "/not-important" 'not-important)))))
160 (let ((instances (latest-channel-instances #f (list channel))))
161 (and (= 2 (length instances))
164 (map (compose channel-name channel-instance-channel)
166 ;; only the most specific channel dependency should remain,
167 ;; i.e. the one with a specified commit.
168 (find (lambda (instance)
169 (and (eq? (channel-name
170 (channel-instance-channel instance))
172 (string=? (channel-commit
173 (channel-instance-channel instance))
177 (test-assert "channel-instances->manifest"
178 ;; Compute the manifest for a graph of instances and make sure we get a
179 ;; derivation graph that mirrors the instance graph. This test also ensures
180 ;; we don't try to access Git repositores at all at this stage.
181 (let* ((spec (lambda deps
182 `(channel (version 0)
187 (url "http://example.org")))
189 (guix (make-instance #:name 'guix))
190 (instance0 (make-instance #:name 'a))
191 (instance1 (make-instance #:name 'b #:spec (spec 'a)))
192 (instance2 (make-instance #:name 'c #:spec (spec 'b)))
193 (instance3 (make-instance #:name 'd #:spec (spec 'c 'a))))
194 (%graft? #f) ;don't try to build stuff
196 ;; Create 'build-self.scm' so that GUIX is recognized as the 'guix' channel.
197 (let ((source (channel-instance-checkout guix)))
198 (mkdir (string-append source "/build-aux"))
199 (call-with-output-file (string-append source
200 "/build-aux/build-self.scm")
203 (use-modules (guix) (gnu packages bootstrap))
206 (package->derivation %bootstrap-guile)))
212 (run-with-store store
213 (channel-instances->manifest (list guix
215 instance2 instance3))))
218 (manifest-entries manifest))
220 (define (depends? drv in out)
221 ;; Return true if DRV depends (directly or indirectly) on all of IN
223 (let ((set (list->set
225 (list (derivation-file-name drv)))))
226 (in (map derivation-file-name in))
227 (out (map derivation-file-name out)))
228 (and (every (cut set-contains? set <>) in)
229 (not (any (cut set-contains? set <>) out)))))
231 (define (lookup name)
232 (run-with-store store
235 (manifest-lookup manifest
236 (manifest-pattern (name name)))))))
238 (let ((drv-guix (lookup "guix"))
243 (and (depends? drv-guix '() (list drv0 drv1 drv2 drv3))
245 (list) (list drv1 drv2 drv3))
247 (list drv0) (list drv2 drv3))
249 (list drv1) (list drv3))
251 (list drv2 drv0) (list))))))))
253 (unless (which (git-command)) (test-skip 1))
254 (test-equal "channel-news, no news"
256 (with-temporary-git-repository directory
258 (commit "the commit"))
259 (with-repository directory repository
260 (let ((channel (channel (url (string-append "file://" directory))
262 (latest (reference-name->oid repository "HEAD")))
263 (channel-news-for-commit channel (oid->string latest))))))
265 (unless (which (git-command)) (test-skip 1))
266 (test-assert "channel-news, one entry"
267 (with-temporary-git-repository directory
268 `((add ".guix-channel"
270 '(channel (version 0)
271 (news-file "news.scm"))))
272 (commit "first commit")
273 (add "src/a.txt" "A")
274 (commit "second commit")
275 (tag "tag-for-first-news-entry")
277 ,(lambda (repository)
279 (reference-name->oid repository "HEAD")))
283 (entry (commit ,(oid->string previous))
284 (title (en "New file!")
285 (eo "Nova dosiero!"))
286 (body (en "Yeah, a.txt."))))))))
287 (commit "third commit")
288 (add "src/b.txt" "B")
289 (commit "fourth commit")
291 ,(lambda (repository)
294 (find-commit repository "second commit")))
296 (reference-name->oid repository "HEAD")))
300 (entry (commit ,(oid->string previous))
301 (title (en "Another file!"))
302 (body (en "Yeah, b.txt.")))
303 (entry (tag "tag-for-first-news-entry")
304 (title (en "Old news.")
306 (body (en "For a.txt"))))))))
307 (commit "fifth commit"))
308 (with-repository directory repository
309 (define (find-commit* message)
310 (oid->string (commit-id (find-commit repository message))))
312 (let ((channel (channel (url (string-append "file://" directory))
314 (commit1 (find-commit* "first commit"))
315 (commit2 (find-commit* "second commit"))
316 (commit3 (find-commit* "third commit"))
317 (commit4 (find-commit* "fourth commit"))
318 (commit5 (find-commit* "fifth commit")))
319 ;; First try fetching all the news up to a given commit.
320 (and (null? (channel-news-for-commit channel commit2))
322 (map channel-news-entry-commit
323 (channel-news-for-commit channel commit5))
324 (list commit2 commit4))
326 (map channel-news-entry-title
327 (channel-news-for-commit channel commit5))
328 '((("en" . "Another file!"))
329 (("en" . "Old news.") ("eo" . "Malnovaĵoj."))))
331 (map channel-news-entry-commit
332 (channel-news-for-commit channel commit3))
335 ;; Now fetch news entries that apply to a commit range.
337 (map channel-news-entry-commit
338 (channel-news-for-commit channel commit3 commit1))
341 (map channel-news-entry-commit
342 (channel-news-for-commit channel commit5 commit3))
345 (map channel-news-entry-commit
346 (channel-news-for-commit channel commit5 commit1))
347 (list commit4 commit2))
349 (map channel-news-entry-tag
350 (channel-news-for-commit channel commit5 commit1))
351 '(#f "tag-for-first-news-entry")))))))
353 (test-end "channels")