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, 2017, 2018 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 ;;; Copyright © 2017 Alex Kost <alezost@gmail.com>
8 ;;; Copyright © 2017 Efraim Flashner <efraim@flashner.co.il>
10 ;;; This file is part of GNU Guix.
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25 ;; Avoid interference.
26 (unsetenv "http_proxy")
28 (define-module (test-lint)
29 #:use-module (guix tests)
30 #:use-module (guix tests http)
31 #:use-module (guix download)
32 #:use-module (guix git-download)
33 #:use-module (guix build-system gnu)
34 #:use-module (guix packages)
35 #:use-module (guix scripts lint)
36 #:use-module (guix ui)
37 #:use-module (gnu packages)
38 #:use-module (gnu packages glib)
39 #:use-module (gnu packages pkg-config)
40 #:use-module (gnu packages python)
41 #:use-module (web uri)
42 #:use-module (web server)
43 #:use-module (web server http)
44 #:use-module (web response)
45 #:use-module (ice-9 match)
46 #:use-module (srfi srfi-9 gnu)
47 #:use-module (srfi srfi-64))
51 ;; Avoid collisions with other tests.
52 (%http-server-port 9999)
55 ;; SHA256 of the empty string.
57 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
60 (make-string 2000 #\a))
65 (define (call-with-warnings thunk)
66 (let ((port (open-output-string)))
67 (parameterize ((guix-warning-port port))
69 (get-output-string port)))
71 (define-syntax-rule (with-warnings body ...)
72 (call-with-warnings (lambda () body ...)))
74 (test-assert "description: not a string"
76 (string-contains (with-warnings
77 (let ((pkg (dummy-package "x"
78 (description 'foobar))))
79 (check-description-style pkg)))
80 "invalid description")))
82 (test-assert "description: not empty"
84 (string-contains (with-warnings
85 (let ((pkg (dummy-package "x"
87 (check-description-style pkg)))
88 "description should not be empty")))
90 (test-assert "description: valid Texinfo markup"
94 (check-description-style (dummy-package "x" (description "f{oo}b@r"))))
95 "Texinfo markup in description is invalid")))
97 (test-assert "description: does not start with an upper-case letter"
99 (string-contains (with-warnings
100 (let ((pkg (dummy-package "x"
101 (description "bad description."))))
102 (check-description-style pkg)))
103 "description should start with an upper-case letter")))
105 (test-assert "description: may start with a digit"
108 (let ((pkg (dummy-package "x"
109 (description "2-component library."))))
110 (check-description-style pkg)))))
112 (test-assert "description: may start with lower-case package name"
115 (let ((pkg (dummy-package "x"
116 (description "x is a dummy package."))))
117 (check-description-style pkg)))))
119 (test-assert "description: two spaces after end of sentence"
121 (string-contains (with-warnings
122 (let ((pkg (dummy-package "x"
123 (description "Bad. Quite bad."))))
124 (check-description-style pkg)))
125 "sentences in description should be followed by two spaces")))
127 (test-assert "description: end-of-sentence detection with abbreviations"
130 (let ((pkg (dummy-package "x"
132 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
133 (check-description-style pkg)))))
135 (test-assert "description: may not contain trademark signs"
137 (string-contains (with-warnings
138 (let ((pkg (dummy-package "x"
139 (description "Does The Right Thing™"))))
140 (check-description-style pkg)))
141 "should not contain trademark sign"))
143 (string-contains (with-warnings
144 (let ((pkg (dummy-package "x"
145 (description "Works with Format®"))))
146 (check-description-style pkg)))
147 "should not contain trademark sign"))))
149 (test-assert "description: suggest ornament instead of quotes"
151 (string-contains (with-warnings
152 (let ((pkg (dummy-package "x"
153 (description "This is a 'quoted' thing."))))
154 (check-description-style pkg)))
157 (test-assert "synopsis: not a string"
159 (string-contains (with-warnings
160 (let ((pkg (dummy-package "x"
162 (check-synopsis-style pkg)))
163 "invalid synopsis")))
165 (test-assert "synopsis: not empty"
167 (string-contains (with-warnings
168 (let ((pkg (dummy-package "x"
170 (check-synopsis-style pkg)))
171 "synopsis should not be empty")))
173 (test-assert "synopsis: valid Texinfo markup"
177 (check-synopsis-style (dummy-package "x" (synopsis "Bad $@ texinfo"))))
178 "Texinfo markup in synopsis is invalid")))
180 (test-assert "synopsis: does not start with an upper-case letter"
182 (string-contains (with-warnings
183 (let ((pkg (dummy-package "x"
184 (synopsis "bad synopsis."))))
185 (check-synopsis-style pkg)))
186 "synopsis should start with an upper-case letter")))
188 (test-assert "synopsis: may start with a digit"
191 (let ((pkg (dummy-package "x"
192 (synopsis "5-dimensional frobnicator"))))
193 (check-synopsis-style pkg)))))
195 (test-assert "synopsis: ends with a period"
197 (string-contains (with-warnings
198 (let ((pkg (dummy-package "x"
199 (synopsis "Bad synopsis."))))
200 (check-synopsis-style pkg)))
201 "no period allowed at the end of the synopsis")))
203 (test-assert "synopsis: ends with 'etc.'"
204 (string-null? (with-warnings
205 (let ((pkg (dummy-package "x"
206 (synopsis "Foo, bar, etc."))))
207 (check-synopsis-style pkg)))))
209 (test-assert "synopsis: starts with 'A'"
211 (string-contains (with-warnings
212 (let ((pkg (dummy-package "x"
213 (synopsis "A bad synopŝis"))))
214 (check-synopsis-style pkg)))
215 "no article allowed at the beginning of the synopsis")))
217 (test-assert "synopsis: starts with 'An'"
219 (string-contains (with-warnings
220 (let ((pkg (dummy-package "x"
221 (synopsis "An awful synopsis"))))
222 (check-synopsis-style pkg)))
223 "no article allowed at the beginning of the synopsis")))
225 (test-assert "synopsis: starts with 'a'"
227 (string-contains (with-warnings
228 (let ((pkg (dummy-package "x"
229 (synopsis "a bad synopsis"))))
230 (check-synopsis-style pkg)))
231 "no article allowed at the beginning of the synopsis")))
233 (test-assert "synopsis: starts with 'an'"
235 (string-contains (with-warnings
236 (let ((pkg (dummy-package "x"
237 (synopsis "an awful synopsis"))))
238 (check-synopsis-style pkg)))
239 "no article allowed at the beginning of the synopsis")))
241 (test-assert "synopsis: too long"
243 (string-contains (with-warnings
244 (let ((pkg (dummy-package "x"
245 (synopsis (make-string 80 #\x)))))
246 (check-synopsis-style pkg)))
247 "synopsis should be less than 80 characters long")))
249 (test-assert "synopsis: start with package name"
251 (string-contains (with-warnings
252 (let ((pkg (dummy-package "x"
254 (synopsis "foo, a nice package"))))
255 (check-synopsis-style pkg)))
256 "synopsis should not start with the package name")))
258 (test-assert "synopsis: start with package name prefix"
261 (let ((pkg (dummy-package "arb"
262 (synopsis "Arbitrary precision"))))
263 (check-synopsis-style pkg)))))
265 (test-assert "synopsis: start with abbreviation"
268 (let ((pkg (dummy-package "uucp"
269 ;; Same problem with "APL interpreter", etc.
270 (synopsis "UUCP implementation")
271 (description "Imagine this is Taylor UUCP."))))
272 (check-synopsis-style pkg)))))
274 (test-assert "inputs: pkg-config is probably a native input"
278 (let ((pkg (dummy-package "x"
279 (inputs `(("pkg-config" ,pkg-config))))))
280 (check-inputs-should-be-native pkg)))
281 "'pkg-config' should probably be a native input")))
283 (test-assert "inputs: glib:bin is probably a native input"
287 (let ((pkg (dummy-package "x"
288 (inputs `(("glib" ,glib "bin"))))))
289 (check-inputs-should-be-native pkg)))
290 "'glib:bin' should probably be a native input")))
293 "inputs: python-setuptools should not be an input at all (input)"
297 (let ((pkg (dummy-package "x"
298 (inputs `(("python-setuptools" ,python-setuptools))))))
299 (check-inputs-should-not-be-an-input-at-all pkg)))
300 "'python-setuptools' should probably not be an input at all")))
303 "inputs: python-setuptools should not be an input at all (native-input)"
307 (let ((pkg (dummy-package "x"
309 `(("python-setuptools" ,python-setuptools))))))
310 (check-inputs-should-not-be-an-input-at-all pkg)))
311 "'python-setuptools' should probably not be an input at all")))
314 "inputs: python-setuptools should not be an input at all (propagated-input)"
318 (let ((pkg (dummy-package "x"
320 `(("python-setuptools" ,python-setuptools))))))
321 (check-inputs-should-not-be-an-input-at-all pkg)))
322 "'python-setuptools' should probably not be an input at all")))
324 (test-assert "patches: file names"
328 (let ((pkg (dummy-package "x"
331 (patches (list "/path/to/y.patch")))))))
332 (check-patch-file-names pkg)))
333 "file names of patches should start with the package name")))
335 (test-assert "patches: file name too long"
339 (let ((pkg (dummy-package "x"
342 (patches (list (string-append "x-"
343 (make-string 100 #\a)
345 (check-patch-file-names pkg)))
346 "file name is too long")))
348 (test-assert "patches: not found"
352 (let ((pkg (dummy-package "x"
356 (list (search-patch "this-patch-does-not-exist!"))))))))
357 (check-patch-file-names pkg)))
360 (test-assert "derivation: invalid arguments"
364 (let ((pkg (dummy-package "x"
366 '(#:imported-modules (invalid-module))))))
367 (check-derivation pkg)))
368 "failed to create")))
370 (test-assert "license: invalid license"
373 (check-license (dummy-package "x" (license #f))))
376 (test-assert "home-page: wrong home-page"
381 (inherit (dummy-package "x"))
383 (check-home-page pkg)))
386 (test-assert "home-page: invalid URI"
391 (inherit (dummy-package "x"))
392 (home-page "foobar"))))
393 (check-home-page pkg)))
394 "invalid home page URL")))
396 (test-assert "home-page: host not found"
401 (inherit (dummy-package "x"))
402 (home-page "http://does-not-exist"))))
403 (check-home-page pkg)))
404 "domain not found")))
406 (test-skip (if (http-server-can-listen?) 0 1))
407 (test-assert "home-page: Connection refused"
412 (inherit (dummy-package "x"))
413 (home-page (%local-url)))))
414 (check-home-page pkg)))
415 "Connection refused")))
417 (test-skip (if (http-server-can-listen?) 0 1))
418 (test-equal "home-page: 200"
421 (with-http-server 200 %long-string
423 (inherit (dummy-package "x"))
424 (home-page (%local-url)))))
425 (check-home-page pkg)))))
427 (test-skip (if (http-server-can-listen?) 0 1))
428 (test-assert "home-page: 200 but short length"
432 (with-http-server 200 "This is too small."
434 (inherit (dummy-package "x"))
435 (home-page (%local-url)))))
436 (check-home-page pkg))))
437 "suspiciously small")))
439 (test-skip (if (http-server-can-listen?) 0 1))
440 (test-assert "home-page: 404"
444 (with-http-server 404 %long-string
446 (inherit (dummy-package "x"))
447 (home-page (%local-url)))))
448 (check-home-page pkg))))
449 "not reachable: 404")))
451 (test-skip (if (http-server-can-listen?) 0 1))
452 (test-assert "home-page: 301, invalid"
456 (with-http-server 301 %long-string
458 (inherit (dummy-package "x"))
459 (home-page (%local-url)))))
460 (check-home-page pkg))))
461 "invalid permanent redirect")))
463 (test-skip (if (http-server-can-listen?) 0 1))
464 (test-assert "home-page: 301 -> 200"
468 (with-http-server 200 %long-string
469 (let ((initial-url (%local-url)))
470 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
471 (with-http-server (301 `((location
472 . ,(string->uri initial-url))))
475 (inherit (dummy-package "x"))
476 (home-page (%local-url)))))
477 (check-home-page pkg)))))))
478 "permanent redirect")))
480 (test-skip (if (http-server-can-listen?) 0 1))
481 (test-assert "home-page: 301 -> 404"
485 (with-http-server 404 "booh!"
486 (let ((initial-url (%local-url)))
487 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
488 (with-http-server (301 `((location
489 . ,(string->uri initial-url))))
492 (inherit (dummy-package "x"))
493 (home-page (%local-url)))))
494 (check-home-page pkg)))))))
495 "not reachable: 404")))
497 (test-assert "source-file-name"
501 (let ((pkg (dummy-package "x"
506 (uri "http://www.example.com/3.2.1.tar.gz")
507 (sha256 %null-sha256))))))
508 (check-source-file-name pkg)))
509 "file name should contain the package name")))
511 (test-assert "source-file-name: v prefix"
515 (let ((pkg (dummy-package "x"
520 (uri "http://www.example.com/v3.2.1.tar.gz")
521 (sha256 %null-sha256))))))
522 (check-source-file-name pkg)))
523 "file name should contain the package name")))
525 (test-assert "source-file-name: bad checkout"
529 (let ((pkg (dummy-package "x"
535 (url "http://www.example.com/x.git")
537 (sha256 %null-sha256))))))
538 (check-source-file-name pkg)))
539 "file name should contain the package name")))
541 (test-assert "source-file-name: good checkout"
546 (let ((pkg (dummy-package "x"
552 (url "http://git.example.com/x.git")
554 (file-name (string-append "x-" version))
555 (sha256 %null-sha256))))))
556 (check-source-file-name pkg)))
557 "file name should contain the package name"))))
559 (test-assert "source-file-name: valid"
564 (let ((pkg (dummy-package "x"
569 (uri "http://www.example.com/x-3.2.1.tar.gz")
570 (sha256 %null-sha256))))))
571 (check-source-file-name pkg)))
572 "file name should contain the package name"))))
574 (test-skip (if (http-server-can-listen?) 0 1))
575 (test-equal "source: 200"
578 (with-http-server 200 %long-string
580 (inherit (dummy-package "x"))
584 (sha256 %null-sha256))))))
585 (check-source pkg)))))
587 (test-skip (if (http-server-can-listen?) 0 1))
588 (test-assert "source: 200 but short length"
592 (with-http-server 200 "This is too small."
594 (inherit (dummy-package "x"))
598 (sha256 %null-sha256))))))
599 (check-source pkg))))
600 "suspiciously small")))
602 (test-skip (if (http-server-can-listen?) 0 1))
603 (test-assert "source: 404"
607 (with-http-server 404 %long-string
609 (inherit (dummy-package "x"))
613 (sha256 %null-sha256))))))
614 (check-source pkg))))
615 "not reachable: 404")))
617 (test-skip (if (http-server-can-listen?) 0 1))
618 (test-equal "source: 301 -> 200"
621 (with-http-server 200 %long-string
622 (let ((initial-url (%local-url)))
623 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
624 (with-http-server (301 `((location . ,(string->uri initial-url))))
627 (inherit (dummy-package "x"))
631 (sha256 %null-sha256))))))
632 (check-source pkg))))))))
634 (test-skip (if (http-server-can-listen?) 0 1))
635 (test-assert "source: 301 -> 404"
639 (with-http-server 404 "booh!"
640 (let ((initial-url (%local-url)))
641 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
642 (with-http-server (301 `((location . ,(string->uri initial-url))))
645 (inherit (dummy-package "x"))
649 (sha256 %null-sha256))))))
650 (check-source pkg)))))))
651 "not reachable: 404")))
653 (test-assert "mirror-url"
656 (let ((source (origin
658 (uri "http://example.org/foo/bar.tar.gz")
659 (sha256 %null-sha256))))
660 (check-mirror-url (dummy-package "x" (source source)))))))
662 (test-assert "mirror-url: one suggestion"
665 (let ((source (origin
667 (uri "http://ftp.gnu.org/pub/gnu/foo/foo.tar.gz")
668 (sha256 %null-sha256))))
669 (check-mirror-url (dummy-package "x" (source source)))))
670 "mirror://gnu/foo/foo.tar.gz"))
673 (mock ((guix scripts lint) package-vulnerabilities (const '()))
675 (with-warnings (check-vulnerabilities (dummy-package "x"))))))
677 (test-assert "cve: one vulnerability"
678 (mock ((guix scripts lint) package-vulnerabilities
680 (list (make-struct (@@ (guix cve) <vulnerability>) 0
682 (list (cons (package-name package)
683 (package-version package)))))))
686 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))
687 "vulnerable to CVE-2015-1234")))
689 (test-assert "cve: one patched vulnerability"
690 (mock ((guix scripts lint) package-vulnerabilities
692 (list (make-struct (@@ (guix cve) <vulnerability>) 0
694 (list (cons (package-name package)
695 (package-version package)))))))
698 (check-vulnerabilities
704 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
706 (test-assert "cve: known safe from vulnerability"
707 (mock ((guix scripts lint) package-vulnerabilities
709 (list (make-struct (@@ (guix cve) <vulnerability>) 0
711 (list (cons (package-name package)
712 (package-version package)))))))
715 (check-vulnerabilities
718 (properties `((lint-hidden-cve . ("CVE-2015-1234"))))))))))
720 (test-assert "cve: vulnerability fixed in replacement version"
721 (mock ((guix scripts lint) package-vulnerabilities
723 (match (package-version package)
725 (list (make-struct (@@ (guix cve) <vulnerability>) 0
727 (list (cons (package-name package)
728 (package-version package))))))
731 (and (not (string-null?
733 (check-vulnerabilities
734 (dummy-package "foo" (version "0"))))))
737 (check-vulnerabilities
740 (replacement (dummy-package "foo" (version "1"))))))))))
742 (test-assert "cve: patched vulnerability in replacement"
743 (mock ((guix scripts lint) package-vulnerabilities
745 (list (make-struct (@@ (guix cve) <vulnerability>) 0
747 (list (cons (package-name package)
748 (package-version package)))))))
751 (check-vulnerabilities
753 "pi" (version "3.14") (source (dummy-origin))
754 (replacement (dummy-package
755 "pi" (version "3.14")
759 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))))
761 (test-assert "formatting: lonely parentheses"
766 dummy-package "ugly as hell!"
771 (test-assert "formatting: tabulation"
774 (check-formatting (dummy-package "leave the tab here: ")))
777 (test-assert "formatting: trailing white space"
780 ;; Leave the trailing white space on the next line!
781 (check-formatting (dummy-package "x")))
782 "trailing white space"))
784 (test-assert "formatting: long line"
788 (dummy-package "x" ;here is a stupid comment just to make a long line
792 (test-assert "formatting: alright"
795 (check-formatting (dummy-package "x")))))
800 ;; eval: (put 'with-http-server 'scheme-indent-function 2)
801 ;; eval: (put 'with-warnings 'scheme-indent-function 0)