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