1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
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 (tests-style)
20 #:use-module (guix packages)
21 #:use-module (guix scripts style)
22 #:use-module ((guix utils) #:select (call-with-temporary-directory))
23 #:use-module ((guix build utils) #:select (substitute*))
24 #:use-module (guix gexp) ;for the reader extension
25 #:use-module (guix diagnostics)
26 #:use-module (gnu packages acl)
27 #:use-module (gnu packages multiprecision)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-64)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 rdelim)
32 #:use-module (ice-9 pretty-print))
34 (define (call-with-test-package inputs proc)
35 (call-with-temporary-directory
37 (call-with-output-file (string-append directory "/my-packages.scm")
41 (define-module (my-packages)
43 #:use-module (guix licenses)
44 #:use-module (gnu packages acl)
45 #:use-module (gnu packages base)
46 #:use-module (gnu packages multiprecision)
47 #:use-module (srfi srfi-1))
54 (propagated-inputs '())))
56 (define (sdl-union . lst)
61 (define-public my-coreutils
65 (name "my-coreutils"))))
70 (define test-directory
71 ;; Directory where the package definition lives.
74 (define-syntax-rule (with-test-package fields exp ...)
75 (call-with-test-package fields
78 (string-append directory "/my-packages.scm"))
80 ;; Run as a separate process to make sure FILE is reloaded.
81 (system* "guix" "style" "-L" directory "my-coreutils")
85 (parameterize ((test-directory directory))
88 (define* (read-lines port line #:optional (count 1))
89 "Read COUNT lines from PORT, starting from LINE."
90 (let loop ((lines '())
92 (cond ((< (port-line port) (- line 1))
96 (string-concatenate-reverse lines))
98 (match (read-line port 'concat)
102 (loop (cons line lines) (- count 1))))))))
104 (define* (read-package-field package field #:optional (count 1))
105 (let* ((location (package-field-location package field))
106 (file (location-file location))
107 (line (location-line location)))
108 (call-with-input-file (if (string-prefix? "/" file)
110 (string-append (test-directory) "/"
113 (read-lines port line count)))))
115 (define-syntax-rule (test-pretty-print str args ...)
116 "Test equality after a round-trip where STR is passed to
117 'read-with-comments' and the resulting sexp is then passed to
118 'pretty-print-with-comments'."
120 (call-with-output-string
122 (let ((exp (call-with-input-string str
123 read-with-comments)))
124 (pretty-print-with-comments port exp args ...))))))
129 (test-equal "nothing to rewrite"
131 (with-test-package '()
132 (package-direct-inputs (@ (my-packages) my-coreutils))))
134 (test-equal "input labels, mismatch"
135 (list `(("foo" ,gmp) ("bar" ,acl))
136 " (inputs `((\"foo\" ,gmp) (\"bar\" ,acl)))\n")
137 (with-test-package '((inputs `(("foo" ,gmp) ("bar" ,acl))))
138 (list (package-direct-inputs (@ (my-packages) my-coreutils))
139 (read-package-field (@ (my-packages) my-coreutils) 'inputs))))
141 (test-equal "input labels, simple"
142 (list `(("gmp" ,gmp) ("acl" ,acl))
143 " (inputs (list gmp acl))\n")
144 (with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl))))
145 (list (package-direct-inputs (@ (my-packages) my-coreutils))
146 (read-package-field (@ (my-packages) my-coreutils) 'inputs))))
148 (test-equal "input labels, long list with one item per line"
149 (list (concatenate (make-list 4 `(("gmp" ,gmp) ("acl" ,acl))))
159 (with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl)
160 ("gmp" ,gmp) ("acl" ,acl)
161 ("gmp" ,gmp) ("acl" ,acl)
162 ("gmp" ,gmp) ("acl" ,acl))))
163 (list (package-direct-inputs (@ (my-packages) my-coreutils))
164 (read-package-field (@ (my-packages) my-coreutils) 'inputs 8))))
166 (test-equal "input labels, sdl-union"
169 (sdl-union 1 2 3 4)))\n"
170 (with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl)
171 ("sdl-union" ,(sdl-union 1 2 3 4)))))
172 (read-package-field (@ (my-packages) my-coreutils) 'inputs 2)))
174 (test-equal "input labels, output"
175 (list `(("gmp" ,gmp "debug") ("acl" ,acl))
176 " (inputs (list `(,gmp \"debug\") acl))\n")
177 (with-test-package '((inputs `(("gmp" ,gmp "debug") ("acl" ,acl))))
178 (list (package-direct-inputs (@ (my-packages) my-coreutils))
179 (read-package-field (@ (my-packages) my-coreutils) 'inputs))))
181 (test-equal "input labels, prepend"
182 (list `(("gmp" ,gmp) ("acl" ,acl))
184 (modify-inputs (package-propagated-inputs coreutils)
185 (prepend gmp acl)))\n")
186 (with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl)
187 ,@(package-propagated-inputs coreutils))))
188 (list (package-inputs (@ (my-packages) my-coreutils))
189 (read-package-field (@ (my-packages) my-coreutils) 'inputs 2))))
191 (test-equal "input labels, prepend + delete"
192 (list `(("gmp" ,gmp) ("acl" ,acl))
194 (modify-inputs (package-propagated-inputs coreutils)
196 (prepend gmp acl)))\n")
197 (with-test-package '((inputs `(("gmp" ,gmp)
199 ,@(alist-delete "gmp"
200 (package-propagated-inputs coreutils)))))
201 (list (package-inputs (@ (my-packages) my-coreutils))
202 (read-package-field (@ (my-packages) my-coreutils) 'inputs 3))))
204 (test-equal "input labels, prepend + delete multiple"
205 (list `(("gmp" ,gmp) ("acl" ,acl))
207 (modify-inputs (package-propagated-inputs coreutils)
208 (delete \"foo\" \"bar\" \"baz\")
209 (prepend gmp acl)))\n")
210 (with-test-package '((inputs `(("gmp" ,gmp)
213 (package-propagated-inputs coreutils)
214 '("foo" "bar" "baz")))))
215 (list (package-inputs (@ (my-packages) my-coreutils))
216 (read-package-field (@ (my-packages) my-coreutils) 'inputs 3))))
218 (test-equal "input labels, replace"
219 (list '() ;there's no "gmp" input to replace
221 (modify-inputs (package-propagated-inputs coreutils)
222 (replace \"gmp\" gmp)))\n")
223 (with-test-package '((inputs `(("gmp" ,gmp)
224 ,@(alist-delete "gmp"
225 (package-propagated-inputs coreutils)))))
226 (list (package-inputs (@ (my-packages) my-coreutils))
227 (read-package-field (@ (my-packages) my-coreutils) 'inputs 2))))
229 (test-equal "input labels, 'safe' policy"
230 (list `(("gmp" ,gmp) ("acl" ,acl))
232 (inputs (list gmp acl))\n")
233 (call-with-test-package '((inputs `(("GMP" ,gmp) ("ACL" ,acl)))
234 (arguments '())) ;no build system arguments
237 (string-append directory "/my-packages.scm"))
239 (system* "guix" "style" "-L" directory "my-coreutils"
240 "--input-simplification=safe")
243 (list (package-inputs (@ (my-packages) my-coreutils))
244 (read-package-field (@ (my-packages) my-coreutils) 'inputs)))))
246 (test-equal "input labels, 'safe' policy, nothing changed"
247 (list `(("GMP" ,gmp) ("ACL" ,acl))
249 (inputs `((\"GMP\" ,gmp) (\"ACL\" ,acl)))\n")
250 (call-with-test-package '((inputs `(("GMP" ,gmp) ("ACL" ,acl)))
251 ;; Non-empty argument list, so potentially unsafe
252 ;; input simplification.
255 (assoc-ref %build-inputs "GMP"))))
258 (string-append directory "/my-packages.scm"))
260 (system* "guix" "style" "-L" directory "my-coreutils"
261 "--input-simplification=safe")
264 (list (package-inputs (@ (my-packages) my-coreutils))
265 (read-package-field (@ (my-packages) my-coreutils) 'inputs)))))
267 (test-equal "input labels, margin comment"
268 (list `(("gmp" ,gmp))
270 " (inputs (list gmp)) ;margin comment\n"
271 " (native-inputs (list acl)) ;another one\n")
272 (call-with-test-package '((inputs `(("gmp" ,gmp)))
273 (native-inputs `(("acl" ,acl))))
276 (string-append directory "/my-packages.scm"))
279 (("\"gmp\"(.*)$" _ rest)
280 (string-append "\"gmp\"" (string-trim-right rest)
281 " ;margin comment\n"))
282 (("\"acl\"(.*)$" _ rest)
283 (string-append "\"acl\"" (string-trim-right rest)
287 (system* "guix" "style" "-L" directory "my-coreutils")
290 (list (package-inputs (@ (my-packages) my-coreutils))
291 (package-native-inputs (@ (my-packages) my-coreutils))
292 (read-package-field (@ (my-packages) my-coreutils) 'inputs)
293 (read-package-field (@ (my-packages) my-coreutils) 'native-inputs)))))
295 (test-equal "input labels, margin comment on long list"
296 (list (concatenate (make-list 4 `(("gmp" ,gmp) ("acl" ,acl))))
298 (list gmp ;margin comment
306 (call-with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl)
307 ("gmp" ,gmp) ("acl" ,acl)
308 ("gmp" ,gmp) ("acl" ,acl)
309 ("gmp" ,gmp) ("acl" ,acl))))
312 (string-append directory "/my-packages.scm"))
315 (("\"gmp\"(.*)$" _ rest)
316 (string-append "\"gmp\"" (string-trim-right rest)
317 " ;margin comment\n")))
320 (system* "guix" "style" "-L" directory "my-coreutils")
323 (list (package-inputs (@ (my-packages) my-coreutils))
324 (read-package-field (@ (my-packages) my-coreutils) 'inputs 8)))))
326 (test-equal "input labels, line comment"
327 (list `(("gmp" ,gmp) ("acl" ,acl))
332 (call-with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl))))
335 (string-append directory "/my-packages.scm"))
338 ((",gmp\\)(.*)$" _ rest)
339 (string-append ",gmp)\n ;; line comment!\n" rest)))
341 (system* "guix" "style" "-L" directory "my-coreutils")
344 (list (package-inputs (@ (my-packages) my-coreutils))
345 (read-package-field (@ (my-packages) my-coreutils) 'inputs 3)))))
347 (test-equal "input labels, modify-inputs and margin comment"
348 (list `(("gmp" ,gmp) ("acl" ,acl) ("mpfr" ,mpfr))
350 (modify-inputs (package-propagated-inputs coreutils)
351 (prepend gmp ;margin comment
354 (call-with-test-package '((inputs
355 `(("gmp" ,gmp) ("acl" ,acl) ("mpfr" ,mpfr)
356 ,@(package-propagated-inputs coreutils))))
359 (string-append directory "/my-packages.scm"))
362 ((",gmp\\)(.*)$" _ rest)
363 (string-append ",gmp) ;margin comment\n" rest))
364 ((",acl\\)(.*)$" _ rest)
365 (string-append ",acl) ;another one\n" rest)))
367 (system* "guix" "style" "-L" directory "my-coreutils")
370 (list (package-inputs (@ (my-packages) my-coreutils))
371 (read-package-field (@ (my-packages) my-coreutils) 'inputs 4)))))
373 (test-pretty-print "(list 1 2 3 4)")
374 (test-pretty-print "(list 1
380 (test-pretty-print "\
384 (test-pretty-print "\
389 (test-pretty-print "\
394 (test-pretty-print "\
403 (test-pretty-print "\
411 (test-pretty-print "\
413 ;; This is a procedure.
417 (test-pretty-print "\
418 #~(string-append #$coreutils \"/bin/uname\")")
420 (test-pretty-print "\
425 (test-pretty-print "\
426 (modify-phases %standard-phases
427 (add-after 'unpack 'post-unpack
430 (add-before 'check 'pre-check
431 (lambda* (#:key inputs #:allow-other-keys)
434 (test-pretty-print "\
435 (#:phases (modify-phases sdfsdf
440 (test-pretty-print "\
441 (description \"abcdefghijkl
445 (test-pretty-print "\
451 (test-pretty-print "\
453 \"abcdefghijklmnopqrstuvwxyz\")"
459 ;; eval: (put 'with-test-package 'scheme-indent-function 1)
460 ;; eval: (put 'call-with-test-package 'scheme-indent-function 1)