Merge branch 'master' into python-build-system
[jackhill/guix/guix.git] / tests / lint.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 Cyril Roelandt <tipecaml@gmail.com>
3 ;;; Copyright © 2014, 2015, 2016 Eric Bavier <bavier@member.fsf.org>
4 ;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
5 ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
6 ;;; Copyright © 2016 Hartmut Goebel <h.goebel@crazy-compilers.com>
7 ;;;
8 ;;; This file is part of GNU Guix.
9 ;;;
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
14 ;;;
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22
23 ;; Avoid interference.
24 (unsetenv "http_proxy")
25
26 (define-module (test-lint)
27 #:use-module (guix tests)
28 #:use-module (guix tests http)
29 #:use-module (guix download)
30 #:use-module (guix git-download)
31 #:use-module (guix build-system gnu)
32 #:use-module (guix packages)
33 #:use-module (guix scripts lint)
34 #:use-module (guix ui)
35 #:use-module (gnu packages)
36 #:use-module (gnu packages glib)
37 #:use-module (gnu packages pkg-config)
38 #:use-module (gnu packages python)
39 #:use-module (web server)
40 #:use-module (web server http)
41 #:use-module (web response)
42 #:use-module (ice-9 match)
43 #:use-module (srfi srfi-9 gnu)
44 #:use-module (srfi srfi-64))
45
46 ;; Test the linter.
47
48 ;; Avoid collisions with other tests.
49 (%http-server-port 9999)
50
51 (define %null-sha256
52 ;; SHA256 of the empty string.
53 (base32
54 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
55
56 (define %long-string
57 (make-string 2000 #\a))
58
59 \f
60 (test-begin "lint")
61
62 (define (call-with-warnings thunk)
63 (let ((port (open-output-string)))
64 (parameterize ((guix-warning-port port))
65 (thunk))
66 (get-output-string port)))
67
68 (define-syntax-rule (with-warnings body ...)
69 (call-with-warnings (lambda () body ...)))
70
71 (test-assert "description: not a string"
72 (->bool
73 (string-contains (with-warnings
74 (let ((pkg (dummy-package "x"
75 (description 'foobar))))
76 (check-description-style pkg)))
77 "invalid description")))
78
79 (test-assert "description: not empty"
80 (->bool
81 (string-contains (with-warnings
82 (let ((pkg (dummy-package "x"
83 (description ""))))
84 (check-description-style pkg)))
85 "description should not be empty")))
86
87 (test-assert "description: valid Texinfo markup"
88 (->bool
89 (string-contains
90 (with-warnings
91 (check-description-style (dummy-package "x" (description "f{oo}b@r"))))
92 "Texinfo markup in description is invalid")))
93
94 (test-assert "description: does not start with an upper-case letter"
95 (->bool
96 (string-contains (with-warnings
97 (let ((pkg (dummy-package "x"
98 (description "bad description."))))
99 (check-description-style pkg)))
100 "description should start with an upper-case letter")))
101
102 (test-assert "description: may start with a digit"
103 (string-null?
104 (with-warnings
105 (let ((pkg (dummy-package "x"
106 (description "2-component library."))))
107 (check-description-style pkg)))))
108
109 (test-assert "description: may start with lower-case package name"
110 (string-null?
111 (with-warnings
112 (let ((pkg (dummy-package "x"
113 (description "x is a dummy package."))))
114 (check-description-style pkg)))))
115
116 (test-assert "description: two spaces after end of sentence"
117 (->bool
118 (string-contains (with-warnings
119 (let ((pkg (dummy-package "x"
120 (description "Bad. Quite bad."))))
121 (check-description-style pkg)))
122 "sentences in description should be followed by two spaces")))
123
124 (test-assert "description: end-of-sentence detection with abbreviations"
125 (string-null?
126 (with-warnings
127 (let ((pkg (dummy-package "x"
128 (description
129 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
130 (check-description-style pkg)))))
131
132 (test-assert "description: may not contain trademark signs"
133 (and (->bool
134 (string-contains (with-warnings
135 (let ((pkg (dummy-package "x"
136 (description "Does The Right Thing™"))))
137 (check-description-style pkg)))
138 "should not contain trademark sign"))
139 (->bool
140 (string-contains (with-warnings
141 (let ((pkg (dummy-package "x"
142 (description "Works with Format®"))))
143 (check-description-style pkg)))
144 "should not contain trademark sign"))))
145
146 (test-assert "description: suggest ornament instead of quotes"
147 (->bool
148 (string-contains (with-warnings
149 (let ((pkg (dummy-package "x"
150 (description "This is a 'quoted' thing."))))
151 (check-description-style pkg)))
152 "use @code")))
153
154 (test-assert "synopsis: not a string"
155 (->bool
156 (string-contains (with-warnings
157 (let ((pkg (dummy-package "x"
158 (synopsis #f))))
159 (check-synopsis-style pkg)))
160 "invalid synopsis")))
161
162 (test-assert "synopsis: not empty"
163 (->bool
164 (string-contains (with-warnings
165 (let ((pkg (dummy-package "x"
166 (synopsis ""))))
167 (check-synopsis-style pkg)))
168 "synopsis should not be empty")))
169
170 (test-assert "synopsis: does not start with an upper-case letter"
171 (->bool
172 (string-contains (with-warnings
173 (let ((pkg (dummy-package "x"
174 (synopsis "bad synopsis."))))
175 (check-synopsis-style pkg)))
176 "synopsis should start with an upper-case letter")))
177
178 (test-assert "synopsis: may start with a digit"
179 (string-null?
180 (with-warnings
181 (let ((pkg (dummy-package "x"
182 (synopsis "5-dimensional frobnicator"))))
183 (check-synopsis-style pkg)))))
184
185 (test-assert "synopsis: ends with a period"
186 (->bool
187 (string-contains (with-warnings
188 (let ((pkg (dummy-package "x"
189 (synopsis "Bad synopsis."))))
190 (check-synopsis-style pkg)))
191 "no period allowed at the end of the synopsis")))
192
193 (test-assert "synopsis: ends with 'etc.'"
194 (string-null? (with-warnings
195 (let ((pkg (dummy-package "x"
196 (synopsis "Foo, bar, etc."))))
197 (check-synopsis-style pkg)))))
198
199 (test-assert "synopsis: starts with 'A'"
200 (->bool
201 (string-contains (with-warnings
202 (let ((pkg (dummy-package "x"
203 (synopsis "A bad synopŝis"))))
204 (check-synopsis-style pkg)))
205 "no article allowed at the beginning of the synopsis")))
206
207 (test-assert "synopsis: starts with 'An'"
208 (->bool
209 (string-contains (with-warnings
210 (let ((pkg (dummy-package "x"
211 (synopsis "An awful synopsis"))))
212 (check-synopsis-style pkg)))
213 "no article allowed at the beginning of the synopsis")))
214
215 (test-assert "synopsis: starts with 'a'"
216 (->bool
217 (string-contains (with-warnings
218 (let ((pkg (dummy-package "x"
219 (synopsis "a bad synopsis"))))
220 (check-synopsis-style pkg)))
221 "no article allowed at the beginning of the synopsis")))
222
223 (test-assert "synopsis: starts with 'an'"
224 (->bool
225 (string-contains (with-warnings
226 (let ((pkg (dummy-package "x"
227 (synopsis "an awful synopsis"))))
228 (check-synopsis-style pkg)))
229 "no article allowed at the beginning of the synopsis")))
230
231 (test-assert "synopsis: too long"
232 (->bool
233 (string-contains (with-warnings
234 (let ((pkg (dummy-package "x"
235 (synopsis (make-string 80 #\x)))))
236 (check-synopsis-style pkg)))
237 "synopsis should be less than 80 characters long")))
238
239 (test-assert "synopsis: start with package name"
240 (->bool
241 (string-contains (with-warnings
242 (let ((pkg (dummy-package "x"
243 (name "foo")
244 (synopsis "foo, a nice package"))))
245 (check-synopsis-style pkg)))
246 "synopsis should not start with the package name")))
247
248 (test-assert "synopsis: start with package name prefix"
249 (string-null?
250 (with-warnings
251 (let ((pkg (dummy-package "arb"
252 (synopsis "Arbitrary precision"))))
253 (check-synopsis-style pkg)))))
254
255 (test-assert "synopsis: start with abbreviation"
256 (string-null?
257 (with-warnings
258 (let ((pkg (dummy-package "uucp"
259 ;; Same problem with "APL interpreter", etc.
260 (synopsis "UUCP implementation")
261 (description "Imagine this is Taylor UUCP."))))
262 (check-synopsis-style pkg)))))
263
264 (test-assert "inputs: pkg-config is probably a native input"
265 (->bool
266 (string-contains
267 (with-warnings
268 (let ((pkg (dummy-package "x"
269 (inputs `(("pkg-config" ,pkg-config))))))
270 (check-inputs-should-be-native pkg)))
271 "'pkg-config' should probably be a native input")))
272
273 (test-assert "inputs: glib:bin is probably a native input"
274 (->bool
275 (string-contains
276 (with-warnings
277 (let ((pkg (dummy-package "x"
278 (inputs `(("glib" ,glib "bin"))))))
279 (check-inputs-should-be-native pkg)))
280 "'glib:bin' should probably be a native input")))
281
282 (test-assert
283 "inputs: python-setuptools should not be an input at all (input)"
284 (->bool
285 (string-contains
286 (with-warnings
287 (let ((pkg (dummy-package "x"
288 (inputs `(("python-setuptools" ,python-setuptools))))))
289 (check-inputs-should-not-be-an-input-at-all pkg)))
290 "'python-setuptools' should probably not be an input at all")))
291
292 (test-assert
293 "inputs: python-setuptools should not be an input at all (native-input)"
294 (->bool
295 (string-contains
296 (with-warnings
297 (let ((pkg (dummy-package "x"
298 (native-inputs
299 `(("python-setuptools" ,python-setuptools))))))
300 (check-inputs-should-not-be-an-input-at-all pkg)))
301 "'python-setuptools' should probably not be an input at all")))
302
303 (test-assert
304 "inputs: python-setuptools should not be an input at all (propagated-input)"
305 (->bool
306 (string-contains
307 (with-warnings
308 (let ((pkg (dummy-package "x"
309 (propagated-inputs
310 `(("python-setuptools" ,python-setuptools))))))
311 (check-inputs-should-not-be-an-input-at-all pkg)))
312 "'python-setuptools' should probably not be an input at all")))
313
314 (test-assert "patches: file names"
315 (->bool
316 (string-contains
317 (with-warnings
318 (let ((pkg (dummy-package "x"
319 (source
320 (dummy-origin
321 (patches (list "/path/to/y.patch")))))))
322 (check-patch-file-names pkg)))
323 "file names of patches should start with the package name")))
324
325 (test-assert "patches: not found"
326 (->bool
327 (string-contains
328 (with-warnings
329 (let ((pkg (dummy-package "x"
330 (source
331 (dummy-origin
332 (patches
333 (list (search-patch "this-patch-does-not-exist!"))))))))
334 (check-patch-file-names pkg)))
335 "patch not found")))
336
337 (test-assert "derivation: invalid arguments"
338 (->bool
339 (string-contains
340 (with-warnings
341 (let ((pkg (dummy-package "x"
342 (arguments
343 '(#:imported-modules (invalid-module))))))
344 (check-derivation pkg)))
345 "failed to create derivation")))
346
347 (test-assert "license: invalid license"
348 (string-contains
349 (with-warnings
350 (check-license (dummy-package "x" (license #f))))
351 "invalid license"))
352
353 (test-assert "home-page: wrong home-page"
354 (->bool
355 (string-contains
356 (with-warnings
357 (let ((pkg (package
358 (inherit (dummy-package "x"))
359 (home-page #f))))
360 (check-home-page pkg)))
361 "invalid")))
362
363 (test-assert "home-page: invalid URI"
364 (->bool
365 (string-contains
366 (with-warnings
367 (let ((pkg (package
368 (inherit (dummy-package "x"))
369 (home-page "foobar"))))
370 (check-home-page pkg)))
371 "invalid home page URL")))
372
373 (test-assert "home-page: host not found"
374 (->bool
375 (string-contains
376 (with-warnings
377 (let ((pkg (package
378 (inherit (dummy-package "x"))
379 (home-page "http://does-not-exist"))))
380 (check-home-page pkg)))
381 "domain not found")))
382
383 (test-skip (if (force %http-server-socket) 0 1))
384 (test-assert "home-page: Connection refused"
385 (->bool
386 (string-contains
387 (with-warnings
388 (let ((pkg (package
389 (inherit (dummy-package "x"))
390 (home-page (%local-url)))))
391 (check-home-page pkg)))
392 "Connection refused")))
393
394 (test-skip (if (force %http-server-socket) 0 1))
395 (test-equal "home-page: 200"
396 ""
397 (with-warnings
398 (with-http-server 200 %long-string
399 (let ((pkg (package
400 (inherit (dummy-package "x"))
401 (home-page (%local-url)))))
402 (check-home-page pkg)))))
403
404 (test-skip (if (force %http-server-socket) 0 1))
405 (test-assert "home-page: 200 but short length"
406 (->bool
407 (string-contains
408 (with-warnings
409 (with-http-server 200 "This is too small."
410 (let ((pkg (package
411 (inherit (dummy-package "x"))
412 (home-page (%local-url)))))
413 (check-home-page pkg))))
414 "suspiciously small")))
415
416 (test-skip (if (force %http-server-socket) 0 1))
417 (test-assert "home-page: 404"
418 (->bool
419 (string-contains
420 (with-warnings
421 (with-http-server 404 %long-string
422 (let ((pkg (package
423 (inherit (dummy-package "x"))
424 (home-page (%local-url)))))
425 (check-home-page pkg))))
426 "not reachable: 404")))
427
428 (test-assert "source-file-name"
429 (->bool
430 (string-contains
431 (with-warnings
432 (let ((pkg (dummy-package "x"
433 (version "3.2.1")
434 (source
435 (origin
436 (method url-fetch)
437 (uri "http://www.example.com/3.2.1.tar.gz")
438 (sha256 %null-sha256))))))
439 (check-source-file-name pkg)))
440 "file name should contain the package name")))
441
442 (test-assert "source-file-name: v prefix"
443 (->bool
444 (string-contains
445 (with-warnings
446 (let ((pkg (dummy-package "x"
447 (version "3.2.1")
448 (source
449 (origin
450 (method url-fetch)
451 (uri "http://www.example.com/v3.2.1.tar.gz")
452 (sha256 %null-sha256))))))
453 (check-source-file-name pkg)))
454 "file name should contain the package name")))
455
456 (test-assert "source-file-name: bad checkout"
457 (->bool
458 (string-contains
459 (with-warnings
460 (let ((pkg (dummy-package "x"
461 (version "3.2.1")
462 (source
463 (origin
464 (method git-fetch)
465 (uri (git-reference
466 (url "http://www.example.com/x.git")
467 (commit "0")))
468 (sha256 %null-sha256))))))
469 (check-source-file-name pkg)))
470 "file name should contain the package name")))
471
472 (test-assert "source-file-name: good checkout"
473 (not
474 (->bool
475 (string-contains
476 (with-warnings
477 (let ((pkg (dummy-package "x"
478 (version "3.2.1")
479 (source
480 (origin
481 (method git-fetch)
482 (uri (git-reference
483 (url "http://git.example.com/x.git")
484 (commit "0")))
485 (file-name (string-append "x-" version))
486 (sha256 %null-sha256))))))
487 (check-source-file-name pkg)))
488 "file name should contain the package name"))))
489
490 (test-assert "source-file-name: valid"
491 (not
492 (->bool
493 (string-contains
494 (with-warnings
495 (let ((pkg (dummy-package "x"
496 (version "3.2.1")
497 (source
498 (origin
499 (method url-fetch)
500 (uri "http://www.example.com/x-3.2.1.tar.gz")
501 (sha256 %null-sha256))))))
502 (check-source-file-name pkg)))
503 "file name should contain the package name"))))
504
505 (test-skip (if (force %http-server-socket) 0 1))
506 (test-equal "source: 200"
507 ""
508 (with-warnings
509 (with-http-server 200 %long-string
510 (let ((pkg (package
511 (inherit (dummy-package "x"))
512 (source (origin
513 (method url-fetch)
514 (uri (%local-url))
515 (sha256 %null-sha256))))))
516 (check-source pkg)))))
517
518 (test-skip (if (force %http-server-socket) 0 1))
519 (test-assert "source: 200 but short length"
520 (->bool
521 (string-contains
522 (with-warnings
523 (with-http-server 200 "This is too small."
524 (let ((pkg (package
525 (inherit (dummy-package "x"))
526 (source (origin
527 (method url-fetch)
528 (uri (%local-url))
529 (sha256 %null-sha256))))))
530 (check-source pkg))))
531 "suspiciously small")))
532
533 (test-skip (if (force %http-server-socket) 0 1))
534 (test-assert "source: 404"
535 (->bool
536 (string-contains
537 (with-warnings
538 (with-http-server 404 %long-string
539 (let ((pkg (package
540 (inherit (dummy-package "x"))
541 (source (origin
542 (method url-fetch)
543 (uri (%local-url))
544 (sha256 %null-sha256))))))
545 (check-source pkg))))
546 "not reachable: 404")))
547
548 (test-assert "mirror-url"
549 (string-null?
550 (with-warnings
551 (let ((source (origin
552 (method url-fetch)
553 (uri "http://example.org/foo/bar.tar.gz")
554 (sha256 %null-sha256))))
555 (check-mirror-url (dummy-package "x" (source source)))))))
556
557 (test-assert "mirror-url: one suggestion"
558 (string-contains
559 (with-warnings
560 (let ((source (origin
561 (method url-fetch)
562 (uri "http://ftp.gnu.org/pub/gnu/foo/foo.tar.gz")
563 (sha256 %null-sha256))))
564 (check-mirror-url (dummy-package "x" (source source)))))
565 "mirror://gnu/foo/foo.tar.gz"))
566
567 (test-assert "cve"
568 (mock ((guix scripts lint) package-vulnerabilities (const '()))
569 (string-null?
570 (with-warnings (check-vulnerabilities (dummy-package "x"))))))
571
572 (test-assert "cve: one vulnerability"
573 (mock ((guix scripts lint) package-vulnerabilities
574 (lambda (package)
575 (list (make-struct (@@ (guix cve) <vulnerability>) 0
576 "CVE-2015-1234"
577 (list (cons (package-name package)
578 (package-version package)))))))
579 (string-contains
580 (with-warnings
581 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))
582 "vulnerable to CVE-2015-1234")))
583
584 (test-assert "cve: one patched vulnerability"
585 (mock ((guix scripts lint) package-vulnerabilities
586 (lambda (package)
587 (list (make-struct (@@ (guix cve) <vulnerability>) 0
588 "CVE-2015-1234"
589 (list (cons (package-name package)
590 (package-version package)))))))
591 (string-null?
592 (with-warnings
593 (check-vulnerabilities
594 (dummy-package "pi"
595 (version "3.14")
596 (source
597 (dummy-origin
598 (patches
599 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
600
601 (test-assert "cve: vulnerability fixed in replacement version"
602 (mock ((guix scripts lint) package-vulnerabilities
603 (lambda (package)
604 (match (package-version package)
605 ("0"
606 (list (make-struct (@@ (guix cve) <vulnerability>) 0
607 "CVE-2015-1234"
608 (list (cons (package-name package)
609 (package-version package))))))
610 ("1"
611 '()))))
612 (and (not (string-null?
613 (with-warnings
614 (check-vulnerabilities
615 (dummy-package "foo" (version "0"))))))
616 (string-null?
617 (with-warnings
618 (check-vulnerabilities
619 (dummy-package
620 "foo" (version "0")
621 (replacement (dummy-package "foo" (version "1"))))))))))
622
623 (test-assert "cve: patched vulnerability in replacement"
624 (mock ((guix scripts lint) package-vulnerabilities
625 (lambda (package)
626 (list (make-struct (@@ (guix cve) <vulnerability>) 0
627 "CVE-2015-1234"
628 (list (cons (package-name package)
629 (package-version package)))))))
630 (string-null?
631 (with-warnings
632 (check-vulnerabilities
633 (dummy-package
634 "pi" (version "3.14") (source (dummy-origin))
635 (replacement (dummy-package
636 "pi" (version "3.14")
637 (source
638 (dummy-origin
639 (patches
640 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))))
641
642 (test-assert "formatting: lonely parentheses"
643 (string-contains
644 (with-warnings
645 (check-formatting
646 (
647 dummy-package "ugly as hell!"
648 )
649 ))
650 "lonely"))
651
652 (test-assert "formatting: tabulation"
653 (string-contains
654 (with-warnings
655 (check-formatting (dummy-package "leave the tab here: ")))
656 "tabulation"))
657
658 (test-assert "formatting: trailing white space"
659 (string-contains
660 (with-warnings
661 ;; Leave the trailing white space on the next line!
662 (check-formatting (dummy-package "x")))
663 "trailing white space"))
664
665 (test-assert "formatting: long line"
666 (string-contains
667 (with-warnings
668 (check-formatting
669 (dummy-package "x" ;here is a stupid comment just to make a long line
670 )))
671 "too long"))
672
673 (test-assert "formatting: alright"
674 (string-null?
675 (with-warnings
676 (check-formatting (dummy-package "x")))))
677
678 (test-end "lint")
679
680 ;; Local Variables:
681 ;; eval: (put 'with-http-server 'scheme-indent-function 2)
682 ;; eval: (put 'with-warnings 'scheme-indent-function 0)
683 ;; End: