1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 Cyril Roelandt <tipecaml@gmail.com>
3 ;;; Copyright © 2014, 2015 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>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 ;; Avoid interference.
23 (unsetenv "http_proxy")
25 (define-module (test-lint)
26 #:use-module (guix tests)
27 #:use-module (guix download)
28 #:use-module (guix git-download)
29 #:use-module (guix build-system gnu)
30 #:use-module (guix packages)
31 #:use-module (guix scripts lint)
32 #:use-module (guix ui)
33 #:use-module (gnu packages)
34 #:use-module (gnu packages glib)
35 #:use-module (gnu packages pkg-config)
36 #:use-module (web server)
37 #:use-module (web server http)
38 #:use-module (web response)
39 #:use-module (ice-9 threads)
40 #:use-module (srfi srfi-9 gnu)
41 #:use-module (srfi srfi-64))
45 (define %http-server-port
46 ;; TCP port to use for the stub HTTP server.
50 ;; URL to use for 'home-page' tests.
51 (string-append "http://localhost:" (number->string %http-server-port)
55 ;; SHA256 of the empty string.
57 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
59 (define %http-server-socket
60 ;; Socket used by the Web server.
63 (let ((sock (socket PF_INET SOCK_STREAM 0)))
64 (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
66 (make-socket-address AF_INET INADDR_LOOPBACK
70 (let ((err (system-error-errno args)))
71 (format (current-error-port)
72 "warning: cannot run Web server for tests: ~a~%"
76 (define (http-write server client response body)
78 (let* ((response (write-response response client))
79 (port (response-port response)))
83 (write-response-body response body)))
85 (quit #t) ;exit the server thread
88 ;; Mutex and condition variable to synchronize with the HTTP server.
89 (define %http-server-lock (make-mutex))
90 (define %http-server-ready (make-condition-variable))
92 (define (http-open . args)
93 "Start listening for HTTP requests and signal %HTTP-SERVER-READY."
94 (with-mutex %http-server-lock
95 (let ((result (apply (@@ (web server http) http-open) args)))
96 (signal-condition-variable %http-server-ready)
99 (define-server-impl stub-http-server
100 ;; Stripped-down version of Guile's built-in HTTP server.
102 (@@ (web server http) http-read)
104 (@@ (web server http) http-close))
106 (define (call-with-http-server code data thunk)
107 "Call THUNK with an HTTP server running and returning CODE and DATA (a
108 string) on HTTP requests."
109 (define (server-body)
110 (define (handle request body)
111 (values (build-response #:code code
112 #:reason-phrase "Such is life")
117 (run-server handle stub-http-server
118 `(#:socket ,%http-server-socket)))
121 (with-mutex %http-server-lock
122 (let ((server (make-thread server-body)))
123 (wait-condition-variable %http-server-ready %http-server-lock)
124 ;; Normally SERVER exits automatically once it has received a request.
127 (define-syntax-rule (with-http-server code data body ...)
128 (call-with-http-server code data (lambda () body ...)))
131 (make-string 2000 #\a))
136 (define (call-with-warnings thunk)
137 (let ((port (open-output-string)))
138 (parameterize ((guix-warning-port port))
140 (get-output-string port)))
142 (define-syntax-rule (with-warnings body ...)
143 (call-with-warnings (lambda () body ...)))
145 (test-assert "description: not a string"
147 (string-contains (with-warnings
148 (let ((pkg (dummy-package "x"
149 (description 'foobar))))
150 (check-description-style pkg)))
151 "invalid description")))
153 (test-assert "description: not empty"
155 (string-contains (with-warnings
156 (let ((pkg (dummy-package "x"
158 (check-description-style pkg)))
159 "description should not be empty")))
161 (test-assert "description: valid Texinfo markup"
165 (check-description-style (dummy-package "x" (description "f{oo}b@r"))))
166 "Texinfo markup in description is invalid")))
168 (test-assert "description: does not start with an upper-case letter"
170 (string-contains (with-warnings
171 (let ((pkg (dummy-package "x"
172 (description "bad description."))))
173 (check-description-style pkg)))
174 "description should start with an upper-case letter")))
176 (test-assert "description: may start with a digit"
179 (let ((pkg (dummy-package "x"
180 (description "2-component library."))))
181 (check-description-style pkg)))))
183 (test-assert "description: may start with lower-case package name"
186 (let ((pkg (dummy-package "x"
187 (description "x is a dummy package."))))
188 (check-description-style pkg)))))
190 (test-assert "description: two spaces after end of sentence"
192 (string-contains (with-warnings
193 (let ((pkg (dummy-package "x"
194 (description "Bad. Quite bad."))))
195 (check-description-style pkg)))
196 "sentences in description should be followed by two spaces")))
198 (test-assert "description: end-of-sentence detection with abbreviations"
201 (let ((pkg (dummy-package "x"
203 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
204 (check-description-style pkg)))))
206 (test-assert "synopsis: not a string"
208 (string-contains (with-warnings
209 (let ((pkg (dummy-package "x"
211 (check-synopsis-style pkg)))
212 "invalid synopsis")))
214 (test-assert "synopsis: not empty"
216 (string-contains (with-warnings
217 (let ((pkg (dummy-package "x"
219 (check-synopsis-style pkg)))
220 "synopsis should not be empty")))
222 (test-assert "synopsis: does not start with an upper-case letter"
224 (string-contains (with-warnings
225 (let ((pkg (dummy-package "x"
226 (synopsis "bad synopsis."))))
227 (check-synopsis-style pkg)))
228 "synopsis should start with an upper-case letter")))
230 (test-assert "synopsis: may start with a digit"
233 (let ((pkg (dummy-package "x"
234 (synopsis "5-dimensional frobnicator"))))
235 (check-synopsis-style pkg)))))
237 (test-assert "synopsis: ends with a period"
239 (string-contains (with-warnings
240 (let ((pkg (dummy-package "x"
241 (synopsis "Bad synopsis."))))
242 (check-synopsis-style pkg)))
243 "no period allowed at the end of the synopsis")))
245 (test-assert "synopsis: ends with 'etc.'"
246 (string-null? (with-warnings
247 (let ((pkg (dummy-package "x"
248 (synopsis "Foo, bar, etc."))))
249 (check-synopsis-style pkg)))))
251 (test-assert "synopsis: starts with 'A'"
253 (string-contains (with-warnings
254 (let ((pkg (dummy-package "x"
255 (synopsis "A bad synopŝis"))))
256 (check-synopsis-style pkg)))
257 "no article allowed at the beginning of the synopsis")))
259 (test-assert "synopsis: starts with 'An'"
261 (string-contains (with-warnings
262 (let ((pkg (dummy-package "x"
263 (synopsis "An awful synopsis"))))
264 (check-synopsis-style pkg)))
265 "no article allowed at the beginning of the synopsis")))
267 (test-assert "synopsis: starts with 'a'"
269 (string-contains (with-warnings
270 (let ((pkg (dummy-package "x"
271 (synopsis "a bad synopsis"))))
272 (check-synopsis-style pkg)))
273 "no article allowed at the beginning of the synopsis")))
275 (test-assert "synopsis: starts with 'an'"
277 (string-contains (with-warnings
278 (let ((pkg (dummy-package "x"
279 (synopsis "an awful synopsis"))))
280 (check-synopsis-style pkg)))
281 "no article allowed at the beginning of the synopsis")))
283 (test-assert "synopsis: too long"
285 (string-contains (with-warnings
286 (let ((pkg (dummy-package "x"
287 (synopsis (make-string 80 #\x)))))
288 (check-synopsis-style pkg)))
289 "synopsis should be less than 80 characters long")))
291 (test-assert "synopsis: start with package name"
293 (string-contains (with-warnings
294 (let ((pkg (dummy-package "x"
296 (synopsis "foo, a nice package"))))
297 (check-synopsis-style pkg)))
298 "synopsis should not start with the package name")))
300 (test-assert "synopsis: start with package name prefix"
303 (let ((pkg (dummy-package "arb"
304 (synopsis "Arbitrary precision"))))
305 (check-synopsis-style pkg)))))
307 (test-assert "synopsis: start with abbreviation"
310 (let ((pkg (dummy-package "uucp"
311 ;; Same problem with "APL interpreter", etc.
312 (synopsis "UUCP implementation")
313 (description "Imagine this is Taylor UUCP."))))
314 (check-synopsis-style pkg)))))
316 (test-assert "inputs: pkg-config is probably a native input"
320 (let ((pkg (dummy-package "x"
321 (inputs `(("pkg-config" ,pkg-config))))))
322 (check-inputs-should-be-native pkg)))
323 "'pkg-config' should probably be a native input")))
325 (test-assert "inputs: glib:bin is probably a native input"
329 (let ((pkg (dummy-package "x"
330 (inputs `(("glib" ,glib "bin"))))))
331 (check-inputs-should-be-native pkg)))
332 "'glib:bin' should probably be a native input")))
334 (test-assert "patches: file names"
338 (let ((pkg (dummy-package "x"
341 (patches (list "/path/to/y.patch")))))))
342 (check-patch-file-names pkg)))
343 "file names of patches should start with the package name")))
345 (test-assert "patches: not found"
349 (let ((pkg (dummy-package "x"
353 (list (search-patch "this-patch-does-not-exist!"))))))))
354 (check-patch-file-names pkg)))
357 (test-assert "derivation: invalid arguments"
361 (let ((pkg (dummy-package "x"
363 '(#:imported-modules (invalid-module))))))
364 (check-derivation pkg)))
365 "failed to create derivation")))
367 (test-assert "license: invalid license"
370 (check-license (dummy-package "x" (license #f))))
373 (test-assert "home-page: wrong home-page"
378 (inherit (dummy-package "x"))
380 (check-home-page pkg)))
383 (test-assert "home-page: invalid URI"
388 (inherit (dummy-package "x"))
389 (home-page "foobar"))))
390 (check-home-page pkg)))
391 "invalid home page URL")))
393 (test-assert "home-page: host not found"
398 (inherit (dummy-package "x"))
399 (home-page "http://does-not-exist"))))
400 (check-home-page pkg)))
401 "domain not found")))
403 (test-skip (if %http-server-socket 0 1))
404 (test-assert "home-page: Connection refused"
409 (inherit (dummy-package "x"))
410 (home-page %local-url))))
411 (check-home-page pkg)))
412 "Connection refused")))
414 (test-skip (if %http-server-socket 0 1))
415 (test-equal "home-page: 200"
418 (with-http-server 200 %long-string
420 (inherit (dummy-package "x"))
421 (home-page %local-url))))
422 (check-home-page pkg)))))
424 (test-skip (if %http-server-socket 0 1))
425 (test-assert "home-page: 200 but short length"
429 (with-http-server 200 "This is too small."
431 (inherit (dummy-package "x"))
432 (home-page %local-url))))
433 (check-home-page pkg))))
434 "suspiciously small")))
436 (test-skip (if %http-server-socket 0 1))
437 (test-assert "home-page: 404"
441 (with-http-server 404 %long-string
443 (inherit (dummy-package "x"))
444 (home-page %local-url))))
445 (check-home-page pkg))))
446 "not reachable: 404")))
448 (test-assert "source-file-name"
452 (let ((pkg (dummy-package "x"
457 (uri "http://www.example.com/3.2.1.tar.gz")
458 (sha256 %null-sha256))))))
459 (check-source-file-name pkg)))
460 "file name should contain the package name")))
462 (test-assert "source-file-name: v prefix"
466 (let ((pkg (dummy-package "x"
471 (uri "http://www.example.com/v3.2.1.tar.gz")
472 (sha256 %null-sha256))))))
473 (check-source-file-name pkg)))
474 "file name should contain the package name")))
476 (test-assert "source-file-name: bad checkout"
480 (let ((pkg (dummy-package "x"
486 (url "http://www.example.com/x.git")
488 (sha256 %null-sha256))))))
489 (check-source-file-name pkg)))
490 "file name should contain the package name")))
492 (test-assert "source-file-name: good checkout"
497 (let ((pkg (dummy-package "x"
503 (url "http://git.example.com/x.git")
505 (file-name (string-append "x-" version))
506 (sha256 %null-sha256))))))
507 (check-source-file-name pkg)))
508 "file name should contain the package name"))))
510 (test-assert "source-file-name: valid"
515 (let ((pkg (dummy-package "x"
520 (uri "http://www.example.com/x-3.2.1.tar.gz")
521 (sha256 %null-sha256))))))
522 (check-source-file-name pkg)))
523 "file name should contain the package name"))))
525 (test-skip (if %http-server-socket 0 1))
526 (test-equal "source: 200"
529 (with-http-server 200 %long-string
531 (inherit (dummy-package "x"))
535 (sha256 %null-sha256))))))
536 (check-source pkg)))))
538 (test-skip (if %http-server-socket 0 1))
539 (test-assert "source: 200 but short length"
543 (with-http-server 200 "This is too small."
545 (inherit (dummy-package "x"))
549 (sha256 %null-sha256))))))
550 (check-source pkg))))
551 "suspiciously small")))
553 (test-skip (if %http-server-socket 0 1))
554 (test-assert "source: 404"
558 (with-http-server 404 %long-string
560 (inherit (dummy-package "x"))
564 (sha256 %null-sha256))))))
565 (check-source pkg))))
566 "not reachable: 404")))
569 (mock ((guix scripts lint) package-vulnerabilities (const '()))
571 (with-warnings (check-vulnerabilities (dummy-package "x"))))))
573 (test-assert "cve: one vulnerability"
574 (mock ((guix scripts lint) package-vulnerabilities
576 (list (make-struct (@@ (guix cve) <vulnerability>) 0
578 (list (cons (package-name package)
579 (package-version package)))))))
582 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))
583 "vulnerable to CVE-2015-1234")))
585 (test-assert "cve: one patched vulnerability"
586 (mock ((guix scripts lint) package-vulnerabilities
588 (list (make-struct (@@ (guix cve) <vulnerability>) 0
590 (list (cons (package-name package)
591 (package-version package)))))))
594 (check-vulnerabilities
600 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
602 (test-assert "cve: patched vulnerability in replacement"
603 (mock ((guix scripts lint) package-vulnerabilities
605 (list (make-struct (@@ (guix cve) <vulnerability>) 0
607 (list (cons (package-name package)
608 (package-version package)))))))
611 (check-vulnerabilities
613 "pi" (version "3.14") (source (dummy-origin))
614 (replacement (dummy-package
615 "pi" (version "3.14")
619 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))))
621 (test-assert "formatting: lonely parentheses"
626 dummy-package "ugly as hell!"
631 (test-assert "formatting: tabulation"
634 (check-formatting (dummy-package "leave the tab here: ")))
637 (test-assert "formatting: trailing white space"
640 ;; Leave the trailing white space on the next line!
641 (check-formatting (dummy-package "x")))
642 "trailing white space"))
644 (test-assert "formatting: long line"
648 (dummy-package "x" ;here is a stupid comment just to make a long line
652 (test-assert "formatting: alright"
655 (check-formatting (dummy-package "x")))))
660 ;; eval: (put 'with-http-server 'scheme-indent-function 2)
661 ;; eval: (put 'with-warnings 'scheme-indent-function 0)