gnu: Add Guile-Bash.
[jackhill/guix/guix.git] / tests / graph.scm
CommitLineData
88856916 1;;; GNU Guix --- Functional package management for GNU
f88282af 2;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org>
88856916
LC
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
19(define-module (test-graph)
20 #:use-module (guix tests)
8fb58371 21 #:use-module (guix graph)
88856916
LC
22 #:use-module (guix scripts graph)
23 #:use-module (guix packages)
24 #:use-module (guix derivations)
25 #:use-module (guix store)
26 #:use-module (guix monads)
ef8de985 27 #:use-module (guix grafts)
88856916 28 #:use-module (guix build-system gnu)
923d846c 29 #:use-module (guix build-system trivial)
88856916 30 #:use-module (guix gexp)
923d846c 31 #:use-module (guix utils)
88856916 32 #:use-module (gnu packages)
923d846c
LC
33 #:use-module (gnu packages base)
34 #:use-module (gnu packages guile)
88856916
LC
35 #:use-module (gnu packages bootstrap)
36 #:use-module (ice-9 match)
37 #:use-module (srfi srfi-1)
38 #:use-module (srfi srfi-11)
39 #:use-module (srfi srfi-26)
40 #:use-module (srfi srfi-64))
41
42(define %store
43 (open-connection-for-tests))
44
ef8de985
LC
45;; Globally disable grafts because they can trigger early builds.
46(%graft? #f)
47
88856916
LC
48(define (make-recording-backend)
49 "Return a <graph-backend> and a thunk that returns the recorded nodes and
50edges."
51 (let ((nodes '())
52 (edges '()))
53 (define (record-node id label port)
54 (set! nodes (cons (list id label) nodes)))
55 (define (record-edge source target port)
56 (set! edges (cons (list source target) edges)))
57 (define (return)
58 (values (reverse nodes) (reverse edges)))
59
51377437
RW
60 (values (graph-backend "test" "This is the test backend."
61 (const #t) (const #t)
88856916
LC
62 record-node record-edge)
63 return)))
64
65(define (package->tuple package)
66 "Return a tuple representing PACKAGE as produced by %PACKAGE-NODE-TYPE."
67 (list (object-address package)
68 (package-full-name package)))
69
70(define (edge->tuple source target)
71 "Likewise for an edge from SOURCE to TARGET."
72 (list (object-address source)
73 (object-address target)))
74
75\f
76(test-begin "graph")
77
78(test-assert "package DAG"
79 (let-values (((backend nodes+edges) (make-recording-backend)))
80 (let* ((p1 (dummy-package "p1"))
81 (p2 (dummy-package "p2" (inputs `(("p1" ,p1)))))
82 (p3 (dummy-package "p3" (inputs `(("p2" ,p2) ("p1", p1))))))
83 (run-with-store %store
84 (export-graph (list p3) 'port
85 #:node-type %package-node-type
86 #:backend backend))
87 ;; We should see nothing more than these 3 packages.
88 (let-values (((nodes edges) (nodes+edges)))
89 (and (equal? nodes (map package->tuple (list p3 p2 p1)))
90 (equal? edges
91 (map edge->tuple
92 (list p3 p3 p2)
93 (list p2 p1 p1))))))))
94
95(test-assert "bag-emerged DAG"
96 (let-values (((backend nodes+edges) (make-recording-backend)))
f88282af
LC
97 (let* ((o (dummy-origin (method (lambda _
98 (text-file "foo" "bar")))))
99 (p (dummy-package "p" (source o)))
100 (implicit (map (match-lambda
101 ((label package) package))
102 (standard-packages))))
88856916
LC
103 (run-with-store %store
104 (export-graph (list p) 'port
105 #:node-type %bag-emerged-node-type
106 #:backend backend))
107 ;; We should see exactly P and IMPLICIT, with one edge from P to each
f88282af 108 ;; element of IMPLICIT. O must not appear among NODES.
88856916
LC
109 (let-values (((nodes edges) (nodes+edges)))
110 (and (equal? (match nodes
111 (((labels names) ...)
112 names))
113 (map package-full-name (cons p implicit)))
114 (equal? (match edges
115 (((sources destinations) ...)
116 (zip (map store-path-package-name sources)
117 (map store-path-package-name destinations))))
118 (map (lambda (destination)
119 (list "p-0.drv"
120 (string-append
121 (package-full-name destination)
122 ".drv")))
123 implicit)))))))
124
923d846c 125(test-assert "bag DAG" ;a big town in Iraq
88856916
LC
126 (let-values (((backend nodes+edges) (make-recording-backend)))
127 (let ((p (dummy-package "p")))
128 (run-with-store %store
129 (export-graph (list p) 'port
130 #:node-type %bag-node-type
131 #:backend backend))
132 ;; We should see P, its implicit inputs as well as the whole DAG, which
133 ;; should include bootstrap binaries.
134 (let-values (((nodes edges) (nodes+edges)))
135 (every (lambda (name)
136 (find (cut string=? name <>)
137 (match nodes
138 (((labels names) ...)
139 names))))
140 (match %bootstrap-inputs
141 (((labels packages) ...)
142 (map package-full-name packages))))))))
143
38b92daa
LC
144(test-assert "bag DAG, including origins"
145 (let-values (((backend nodes+edges) (make-recording-backend)))
146 (let* ((m (lambda* (uri hash-type hash name #:key system)
147 (text-file "foo-1.2.3.tar.gz" "This is a fake!")))
148 (o (origin (method m) (uri "the-uri") (sha256 #vu8(0 1 2))))
149 (p (dummy-package "p" (source o))))
150 (run-with-store %store
151 (export-graph (list p) 'port
152 #:node-type %bag-with-origins-node-type
153 #:backend backend))
154 ;; We should see O among the nodes, with an edge coming from P.
155 (let-values (((nodes edges) (nodes+edges)))
156 (run-with-store %store
157 (mlet %store-monad ((o* (lower-object o))
51385362
LC
158 (p* (lower-object p))
159 (g (lower-object (default-guile))))
38b92daa
LC
160 (return
161 (and (find (match-lambda
162 ((file "the-uri") #t)
163 (_ #f))
164 nodes)
165 (find (match-lambda
166 ((source target)
167 (and (string=? source (derivation-file-name p*))
168 (string=? target o*))))
51385362
LC
169 edges)
170
171 ;; There must also be an edge from O to G.
172 (find (match-lambda
173 ((source target)
174 (and (string=? source o*)
175 (string=? target (derivation-file-name g)))))
38b92daa
LC
176 edges)))))))))
177
88856916
LC
178(test-assert "derivation DAG"
179 (let-values (((backend nodes+edges) (make-recording-backend)))
180 (run-with-store %store
181 (mlet* %store-monad ((txt (text-file "text-file" "Hello!"))
182 (guile (package->derivation %bootstrap-guile))
183 (drv (gexp->derivation "output"
184 #~(symlink #$txt #$output)
185 #:guile-for-build
186 guile)))
187 ;; We should get at least these 3 nodes and corresponding edges.
188 (mbegin %store-monad
189 (export-graph (list drv) 'port
190 #:node-type %derivation-node-type
191 #:backend backend)
192 (let-values (((nodes edges) (nodes+edges)))
193 ;; XXX: For some reason we need to throw in some 'basename'.
194 (return (and (match nodes
195 (((ids labels) ...)
196 (let ((ids (map basename ids)))
197 (every (lambda (item)
198 (member (basename item) ids))
199 (list txt
200 (derivation-file-name drv)
201 (derivation-file-name guile))))))
202 (every (cut member <>
203 (map (lambda (edge)
204 (map basename edge))
205 edges))
206 (list (map (compose basename derivation-file-name)
207 (list drv guile))
208 (list (basename (derivation-file-name drv))
209 (basename txt))))))))))))
210
211(test-assert "reference DAG"
212 (let-values (((backend nodes+edges) (make-recording-backend)))
213 (run-with-store %store
214 (mlet* %store-monad ((txt (text-file "text-file" "Hello!"))
215 (guile (package->derivation %bootstrap-guile))
216 (drv (gexp->derivation "output"
217 #~(symlink #$txt #$output)
218 #:guile-for-build
219 guile))
220 (out -> (derivation->output-path drv)))
221 ;; We should see only OUT and TXT, with an edge from the former to the
222 ;; latter.
223 (mbegin %store-monad
224 (built-derivations (list drv))
225 (export-graph (list (derivation->output-path drv)) 'port
226 #:node-type %reference-node-type
227 #:backend backend)
228 (let-values (((nodes edges) (nodes+edges)))
229 (return
230 (and (equal? (match nodes
231 (((ids labels) ...)
232 ids))
233 (list out txt))
234 (equal? edges `((,out ,txt)))))))))))
235
7f8fec0f
LC
236(test-assert "referrer DAG"
237 (let-values (((backend nodes+edges) (make-recording-backend)))
238 (run-with-store %store
239 (mlet* %store-monad ((txt (text-file "referrer-node" (random-text)))
240 (drv (gexp->derivation "referrer"
241 #~(symlink #$txt #$output)))
242 (out -> (derivation->output-path drv)))
243 ;; We should see only TXT and OUT, with an edge from the former to the
244 ;; latter.
245 (mbegin %store-monad
246 (built-derivations (list drv))
247 (export-graph (list txt) 'port
248 #:node-type %referrer-node-type
249 #:backend backend)
250 (let-values (((nodes edges) (nodes+edges)))
251 (return
252 (and (equal? (match nodes
253 (((ids labels) ...)
254 ids))
255 (list txt out))
256 (equal? edges `((,txt ,out)))))))))))
257
923d846c
LC
258(test-assert "node-edges"
259 (run-with-store %store
260 (let ((packages (fold-packages cons '())))
261 (mlet %store-monad ((edges (node-edges %package-node-type packages)))
724eaef1 262 (return (and (null? (edges sed))
923d846c
LC
263 (lset= eq?
264 (edges guile-2.0)
265 (match (package-direct-inputs guile-2.0)
266 (((labels packages _ ...) ...)
267 packages)))))))))
268
269(test-assert "node-transitive-edges + node-back-edges"
270 (run-with-store %store
271 (let ((packages (fold-packages cons '()))
272 (bootstrap? (lambda (package)
273 (string-contains
274 (location-file (package-location package))
275 "bootstrap.scm")))
276 (trivial? (lambda (package)
277 (eq? (package-build-system package)
278 trivial-build-system))))
279 (mlet %store-monad ((edges (node-back-edges %bag-node-type packages)))
280 (let* ((glibc (canonical-package glibc))
281 (dependents (node-transitive-edges (list glibc) edges))
282 (diff (lset-difference eq? packages dependents)))
283 ;; All the packages depend on libc, except bootstrap packages and
284 ;; some that use TRIVIAL-BUILD-SYSTEM.
285 (return (null? (remove (lambda (package)
286 (or (trivial? package)
287 (bootstrap? package)))
288 diff))))))))
289
88d5858f
LC
290(test-assert "node-transitive-edges, no duplicates"
291 (run-with-store %store
292 (let* ((p0 (dummy-package "p0"))
293 (p1a (dummy-package "p1a" (inputs `(("p0" ,p0)))))
294 (p1b (dummy-package "p1b" (inputs `(("p0" ,p0)))))
295 (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b))))))
296 (mlet %store-monad ((edges (node-edges %package-node-type
297 (list p2 p1a p1b p0))))
298 (return (lset= eq? (node-transitive-edges (list p2) edges)
299 (list p1a p1b p0)))))))
300
e144e342
LC
301(test-equal "node-reachable-count"
302 '(3 3)
303 (run-with-store %store
304 (let* ((p0 (dummy-package "p0"))
305 (p1a (dummy-package "p1a" (inputs `(("p0" ,p0)))))
306 (p1b (dummy-package "p1b" (inputs `(("p0" ,p0)))))
307 (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b))))))
308 (mlet* %store-monad ((all -> (list p2 p1a p1b p0))
309 (edges (node-edges %package-node-type all))
310 (back (node-back-edges %package-node-type all)))
311 (return (list (node-reachable-count (list p2) edges)
312 (node-reachable-count (list p0) back)))))))
313
88856916 314(test-end "graph")