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>
8 ;;; This file is part of GNU Guix.
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.
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.
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/>.
23 ;; Avoid interference.
24 (unsetenv "http_proxy")
26 (define-module (test-lint)
27 #:use-module (guix tests)
28 #:use-module (guix download)
29 #:use-module (guix git-download)
30 #:use-module (guix build-system gnu)
31 #:use-module (guix packages)
32 #:use-module (guix scripts lint)
33 #:use-module (guix ui)
34 #:use-module (gnu packages)
35 #:use-module (gnu packages glib)
36 #:use-module (gnu packages pkg-config)
37 #:use-module (gnu packages python)
38 #:use-module (web server)
39 #:use-module (web server http)
40 #:use-module (web response)
41 #:use-module (ice-9 match)
42 #:use-module (ice-9 threads)
43 #:use-module (srfi srfi-9 gnu)
44 #:use-module (srfi srfi-64))
48 (define %http-server-port
49 ;; TCP port to use for the stub HTTP server.
53 ;; URL to use for 'home-page' tests.
54 (string-append "http://localhost:" (number->string %http-server-port)
58 ;; SHA256 of the empty string.
60 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
62 (define %http-server-socket
63 ;; Socket used by the Web server.
66 (let ((sock (socket PF_INET SOCK_STREAM 0)))
67 (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
69 (make-socket-address AF_INET INADDR_LOOPBACK
73 (let ((err (system-error-errno args)))
74 (format (current-error-port)
75 "warning: cannot run Web server for tests: ~a~%"
79 (define (http-write server client response body)
81 (let* ((response (write-response response client))
82 (port (response-port response)))
86 (write-response-body response body)))
88 (quit #t) ;exit the server thread
91 ;; Mutex and condition variable to synchronize with the HTTP server.
92 (define %http-server-lock (make-mutex))
93 (define %http-server-ready (make-condition-variable))
95 (define (http-open . args)
96 "Start listening for HTTP requests and signal %HTTP-SERVER-READY."
97 (with-mutex %http-server-lock
98 (let ((result (apply (@@ (web server http) http-open) args)))
99 (signal-condition-variable %http-server-ready)
102 (define-server-impl stub-http-server
103 ;; Stripped-down version of Guile's built-in HTTP server.
105 (@@ (web server http) http-read)
107 (@@ (web server http) http-close))
109 (define (call-with-http-server code data thunk)
110 "Call THUNK with an HTTP server running and returning CODE and DATA (a
111 string) on HTTP requests."
112 (define (server-body)
113 (define (handle request body)
114 (values (build-response #:code code
115 #:reason-phrase "Such is life")
120 (run-server handle stub-http-server
121 `(#:socket ,%http-server-socket)))
124 (with-mutex %http-server-lock
125 (let ((server (make-thread server-body)))
126 (wait-condition-variable %http-server-ready %http-server-lock)
127 ;; Normally SERVER exits automatically once it has received a request.
130 (define-syntax-rule (with-http-server code data body ...)
131 (call-with-http-server code data (lambda () body ...)))
134 (make-string 2000 #\a))
139 (define (call-with-warnings thunk)
140 (let ((port (open-output-string)))
141 (parameterize ((guix-warning-port port))
143 (get-output-string port)))
145 (define-syntax-rule (with-warnings body ...)
146 (call-with-warnings (lambda () body ...)))
148 (test-assert "description: not a string"
150 (string-contains (with-warnings
151 (let ((pkg (dummy-package "x"
152 (description 'foobar))))
153 (check-description-style pkg)))
154 "invalid description")))
156 (test-assert "description: not empty"
158 (string-contains (with-warnings
159 (let ((pkg (dummy-package "x"
161 (check-description-style pkg)))
162 "description should not be empty")))
164 (test-assert "description: valid Texinfo markup"
168 (check-description-style (dummy-package "x" (description "f{oo}b@r"))))
169 "Texinfo markup in description is invalid")))
171 (test-assert "description: does not start with an upper-case letter"
173 (string-contains (with-warnings
174 (let ((pkg (dummy-package "x"
175 (description "bad description."))))
176 (check-description-style pkg)))
177 "description should start with an upper-case letter")))
179 (test-assert "description: may start with a digit"
182 (let ((pkg (dummy-package "x"
183 (description "2-component library."))))
184 (check-description-style pkg)))))
186 (test-assert "description: may start with lower-case package name"
189 (let ((pkg (dummy-package "x"
190 (description "x is a dummy package."))))
191 (check-description-style pkg)))))
193 (test-assert "description: two spaces after end of sentence"
195 (string-contains (with-warnings
196 (let ((pkg (dummy-package "x"
197 (description "Bad. Quite bad."))))
198 (check-description-style pkg)))
199 "sentences in description should be followed by two spaces")))
201 (test-assert "description: end-of-sentence detection with abbreviations"
204 (let ((pkg (dummy-package "x"
206 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
207 (check-description-style pkg)))))
209 (test-assert "description: may not contain trademark signs"
211 (string-contains (with-warnings
212 (let ((pkg (dummy-package "x"
213 (description "Does The Right Thing™"))))
214 (check-description-style pkg)))
215 "should not contain trademark sign"))
217 (string-contains (with-warnings
218 (let ((pkg (dummy-package "x"
219 (description "Works with Format®"))))
220 (check-description-style pkg)))
221 "should not contain trademark sign"))))
223 (test-assert "description: suggest ornament instead of quotes"
225 (string-contains (with-warnings
226 (let ((pkg (dummy-package "x"
227 (description "This is a 'quoted' thing."))))
228 (check-description-style pkg)))
231 (test-assert "synopsis: not a string"
233 (string-contains (with-warnings
234 (let ((pkg (dummy-package "x"
236 (check-synopsis-style pkg)))
237 "invalid synopsis")))
239 (test-assert "synopsis: not empty"
241 (string-contains (with-warnings
242 (let ((pkg (dummy-package "x"
244 (check-synopsis-style pkg)))
245 "synopsis should not be empty")))
247 (test-assert "synopsis: does not start with an upper-case letter"
249 (string-contains (with-warnings
250 (let ((pkg (dummy-package "x"
251 (synopsis "bad synopsis."))))
252 (check-synopsis-style pkg)))
253 "synopsis should start with an upper-case letter")))
255 (test-assert "synopsis: may start with a digit"
258 (let ((pkg (dummy-package "x"
259 (synopsis "5-dimensional frobnicator"))))
260 (check-synopsis-style pkg)))))
262 (test-assert "synopsis: ends with a period"
264 (string-contains (with-warnings
265 (let ((pkg (dummy-package "x"
266 (synopsis "Bad synopsis."))))
267 (check-synopsis-style pkg)))
268 "no period allowed at the end of the synopsis")))
270 (test-assert "synopsis: ends with 'etc.'"
271 (string-null? (with-warnings
272 (let ((pkg (dummy-package "x"
273 (synopsis "Foo, bar, etc."))))
274 (check-synopsis-style pkg)))))
276 (test-assert "synopsis: starts with 'A'"
278 (string-contains (with-warnings
279 (let ((pkg (dummy-package "x"
280 (synopsis "A bad synopŝis"))))
281 (check-synopsis-style pkg)))
282 "no article allowed at the beginning of the synopsis")))
284 (test-assert "synopsis: starts with 'An'"
286 (string-contains (with-warnings
287 (let ((pkg (dummy-package "x"
288 (synopsis "An awful synopsis"))))
289 (check-synopsis-style pkg)))
290 "no article allowed at the beginning of the synopsis")))
292 (test-assert "synopsis: starts with 'a'"
294 (string-contains (with-warnings
295 (let ((pkg (dummy-package "x"
296 (synopsis "a bad synopsis"))))
297 (check-synopsis-style pkg)))
298 "no article allowed at the beginning of the synopsis")))
300 (test-assert "synopsis: starts with 'an'"
302 (string-contains (with-warnings
303 (let ((pkg (dummy-package "x"
304 (synopsis "an awful synopsis"))))
305 (check-synopsis-style pkg)))
306 "no article allowed at the beginning of the synopsis")))
308 (test-assert "synopsis: too long"
310 (string-contains (with-warnings
311 (let ((pkg (dummy-package "x"
312 (synopsis (make-string 80 #\x)))))
313 (check-synopsis-style pkg)))
314 "synopsis should be less than 80 characters long")))
316 (test-assert "synopsis: start with package name"
318 (string-contains (with-warnings
319 (let ((pkg (dummy-package "x"
321 (synopsis "foo, a nice package"))))
322 (check-synopsis-style pkg)))
323 "synopsis should not start with the package name")))
325 (test-assert "synopsis: start with package name prefix"
328 (let ((pkg (dummy-package "arb"
329 (synopsis "Arbitrary precision"))))
330 (check-synopsis-style pkg)))))
332 (test-assert "synopsis: start with abbreviation"
335 (let ((pkg (dummy-package "uucp"
336 ;; Same problem with "APL interpreter", etc.
337 (synopsis "UUCP implementation")
338 (description "Imagine this is Taylor UUCP."))))
339 (check-synopsis-style pkg)))))
341 (test-assert "inputs: pkg-config is probably a native input"
345 (let ((pkg (dummy-package "x"
346 (inputs `(("pkg-config" ,pkg-config))))))
347 (check-inputs-should-be-native pkg)))
348 "'pkg-config' should probably be a native input")))
350 (test-assert "inputs: glib:bin is probably a native input"
354 (let ((pkg (dummy-package "x"
355 (inputs `(("glib" ,glib "bin"))))))
356 (check-inputs-should-be-native pkg)))
357 "'glib:bin' should probably be a native input")))
360 "inputs: python-setuptools should not be an input at all (input)"
364 (let ((pkg (dummy-package "x"
365 (inputs `(("python-setuptools" ,python-setuptools))))))
366 (check-inputs-should-not-be-an-input-at-all pkg)))
367 "'python-setuptools' should probably not be an input at all")))
370 "inputs: python-setuptools should not be an input at all (native-input)"
374 (let ((pkg (dummy-package "x"
376 `(("python-setuptools" ,python-setuptools))))))
377 (check-inputs-should-not-be-an-input-at-all pkg)))
378 "'python-setuptools' should probably not be an input at all")))
381 "inputs: python-setuptools should not be an input at all (propagated-input)"
385 (let ((pkg (dummy-package "x"
387 `(("python-setuptools" ,python-setuptools))))))
388 (check-inputs-should-not-be-an-input-at-all pkg)))
389 "'python-setuptools' should probably not be an input at all")))
391 (test-assert "patches: file names"
395 (let ((pkg (dummy-package "x"
398 (patches (list "/path/to/y.patch")))))))
399 (check-patch-file-names pkg)))
400 "file names of patches should start with the package name")))
402 (test-assert "patches: not found"
406 (let ((pkg (dummy-package "x"
410 (list (search-patch "this-patch-does-not-exist!"))))))))
411 (check-patch-file-names pkg)))
414 (test-assert "derivation: invalid arguments"
418 (let ((pkg (dummy-package "x"
420 '(#:imported-modules (invalid-module))))))
421 (check-derivation pkg)))
422 "failed to create derivation")))
424 (test-assert "license: invalid license"
427 (check-license (dummy-package "x" (license #f))))
430 (test-assert "home-page: wrong home-page"
435 (inherit (dummy-package "x"))
437 (check-home-page pkg)))
440 (test-assert "home-page: invalid URI"
445 (inherit (dummy-package "x"))
446 (home-page "foobar"))))
447 (check-home-page pkg)))
448 "invalid home page URL")))
450 (test-assert "home-page: host not found"
455 (inherit (dummy-package "x"))
456 (home-page "http://does-not-exist"))))
457 (check-home-page pkg)))
458 "domain not found")))
460 (test-skip (if %http-server-socket 0 1))
461 (test-assert "home-page: Connection refused"
466 (inherit (dummy-package "x"))
467 (home-page %local-url))))
468 (check-home-page pkg)))
469 "Connection refused")))
471 (test-skip (if %http-server-socket 0 1))
472 (test-equal "home-page: 200"
475 (with-http-server 200 %long-string
477 (inherit (dummy-package "x"))
478 (home-page %local-url))))
479 (check-home-page pkg)))))
481 (test-skip (if %http-server-socket 0 1))
482 (test-assert "home-page: 200 but short length"
486 (with-http-server 200 "This is too small."
488 (inherit (dummy-package "x"))
489 (home-page %local-url))))
490 (check-home-page pkg))))
491 "suspiciously small")))
493 (test-skip (if %http-server-socket 0 1))
494 (test-assert "home-page: 404"
498 (with-http-server 404 %long-string
500 (inherit (dummy-package "x"))
501 (home-page %local-url))))
502 (check-home-page pkg))))
503 "not reachable: 404")))
505 (test-assert "source-file-name"
509 (let ((pkg (dummy-package "x"
514 (uri "http://www.example.com/3.2.1.tar.gz")
515 (sha256 %null-sha256))))))
516 (check-source-file-name pkg)))
517 "file name should contain the package name")))
519 (test-assert "source-file-name: v prefix"
523 (let ((pkg (dummy-package "x"
528 (uri "http://www.example.com/v3.2.1.tar.gz")
529 (sha256 %null-sha256))))))
530 (check-source-file-name pkg)))
531 "file name should contain the package name")))
533 (test-assert "source-file-name: bad checkout"
537 (let ((pkg (dummy-package "x"
543 (url "http://www.example.com/x.git")
545 (sha256 %null-sha256))))))
546 (check-source-file-name pkg)))
547 "file name should contain the package name")))
549 (test-assert "source-file-name: good checkout"
554 (let ((pkg (dummy-package "x"
560 (url "http://git.example.com/x.git")
562 (file-name (string-append "x-" version))
563 (sha256 %null-sha256))))))
564 (check-source-file-name pkg)))
565 "file name should contain the package name"))))
567 (test-assert "source-file-name: valid"
572 (let ((pkg (dummy-package "x"
577 (uri "http://www.example.com/x-3.2.1.tar.gz")
578 (sha256 %null-sha256))))))
579 (check-source-file-name pkg)))
580 "file name should contain the package name"))))
582 (test-skip (if %http-server-socket 0 1))
583 (test-equal "source: 200"
586 (with-http-server 200 %long-string
588 (inherit (dummy-package "x"))
592 (sha256 %null-sha256))))))
593 (check-source pkg)))))
595 (test-skip (if %http-server-socket 0 1))
596 (test-assert "source: 200 but short length"
600 (with-http-server 200 "This is too small."
602 (inherit (dummy-package "x"))
606 (sha256 %null-sha256))))))
607 (check-source pkg))))
608 "suspiciously small")))
610 (test-skip (if %http-server-socket 0 1))
611 (test-assert "source: 404"
615 (with-http-server 404 %long-string
617 (inherit (dummy-package "x"))
621 (sha256 %null-sha256))))))
622 (check-source pkg))))
623 "not reachable: 404")))
626 (mock ((guix scripts lint) package-vulnerabilities (const '()))
628 (with-warnings (check-vulnerabilities (dummy-package "x"))))))
630 (test-assert "cve: one vulnerability"
631 (mock ((guix scripts lint) package-vulnerabilities
633 (list (make-struct (@@ (guix cve) <vulnerability>) 0
635 (list (cons (package-name package)
636 (package-version package)))))))
639 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))
640 "vulnerable to CVE-2015-1234")))
642 (test-assert "cve: one patched vulnerability"
643 (mock ((guix scripts lint) package-vulnerabilities
645 (list (make-struct (@@ (guix cve) <vulnerability>) 0
647 (list (cons (package-name package)
648 (package-version package)))))))
651 (check-vulnerabilities
657 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
659 (test-assert "cve: vulnerability fixed in replacement version"
660 (mock ((guix scripts lint) package-vulnerabilities
662 (match (package-version package)
664 (list (make-struct (@@ (guix cve) <vulnerability>) 0
666 (list (cons (package-name package)
667 (package-version package))))))
670 (and (not (string-null?
672 (check-vulnerabilities
673 (dummy-package "foo" (version "0"))))))
676 (check-vulnerabilities
679 (replacement (dummy-package "foo" (version "1"))))))))))
681 (test-assert "cve: patched vulnerability in replacement"
682 (mock ((guix scripts lint) package-vulnerabilities
684 (list (make-struct (@@ (guix cve) <vulnerability>) 0
686 (list (cons (package-name package)
687 (package-version package)))))))
690 (check-vulnerabilities
692 "pi" (version "3.14") (source (dummy-origin))
693 (replacement (dummy-package
694 "pi" (version "3.14")
698 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))))
700 (test-assert "formatting: lonely parentheses"
705 dummy-package "ugly as hell!"
710 (test-assert "formatting: tabulation"
713 (check-formatting (dummy-package "leave the tab here: ")))
716 (test-assert "formatting: trailing white space"
719 ;; Leave the trailing white space on the next line!
720 (check-formatting (dummy-package "x")))
721 "trailing white space"))
723 (test-assert "formatting: long line"
727 (dummy-package "x" ;here is a stupid comment just to make a long line
731 (test-assert "formatting: alright"
734 (check-formatting (dummy-package "x")))))
739 ;; eval: (put 'with-http-server 'scheme-indent-function 2)
740 ;; eval: (put 'with-warnings 'scheme-indent-function 0)