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>
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 "description: may not contain trademark signs"
208 (string-contains (with-warnings
209 (let ((pkg (dummy-package "x"
210 (description "Does The Right Thing™"))))
211 (check-description-style pkg)))
212 "should not contain trademark sign"))
214 (string-contains (with-warnings
215 (let ((pkg (dummy-package "x"
216 (description "Works with Format®"))))
217 (check-description-style pkg)))
218 "should not contain trademark sign"))))
220 (test-assert "synopsis: not a string"
222 (string-contains (with-warnings
223 (let ((pkg (dummy-package "x"
225 (check-synopsis-style pkg)))
226 "invalid synopsis")))
228 (test-assert "synopsis: not empty"
230 (string-contains (with-warnings
231 (let ((pkg (dummy-package "x"
233 (check-synopsis-style pkg)))
234 "synopsis should not be empty")))
236 (test-assert "synopsis: does not start with an upper-case letter"
238 (string-contains (with-warnings
239 (let ((pkg (dummy-package "x"
240 (synopsis "bad synopsis."))))
241 (check-synopsis-style pkg)))
242 "synopsis should start with an upper-case letter")))
244 (test-assert "synopsis: may start with a digit"
247 (let ((pkg (dummy-package "x"
248 (synopsis "5-dimensional frobnicator"))))
249 (check-synopsis-style pkg)))))
251 (test-assert "synopsis: ends with a period"
253 (string-contains (with-warnings
254 (let ((pkg (dummy-package "x"
255 (synopsis "Bad synopsis."))))
256 (check-synopsis-style pkg)))
257 "no period allowed at the end of the synopsis")))
259 (test-assert "synopsis: ends with 'etc.'"
260 (string-null? (with-warnings
261 (let ((pkg (dummy-package "x"
262 (synopsis "Foo, bar, etc."))))
263 (check-synopsis-style pkg)))))
265 (test-assert "synopsis: starts with 'A'"
267 (string-contains (with-warnings
268 (let ((pkg (dummy-package "x"
269 (synopsis "A bad synopŝis"))))
270 (check-synopsis-style pkg)))
271 "no article allowed at the beginning of the synopsis")))
273 (test-assert "synopsis: starts with 'An'"
275 (string-contains (with-warnings
276 (let ((pkg (dummy-package "x"
277 (synopsis "An awful synopsis"))))
278 (check-synopsis-style pkg)))
279 "no article allowed at the beginning of the synopsis")))
281 (test-assert "synopsis: starts with 'a'"
283 (string-contains (with-warnings
284 (let ((pkg (dummy-package "x"
285 (synopsis "a bad synopsis"))))
286 (check-synopsis-style pkg)))
287 "no article allowed at the beginning of the synopsis")))
289 (test-assert "synopsis: starts with 'an'"
291 (string-contains (with-warnings
292 (let ((pkg (dummy-package "x"
293 (synopsis "an awful synopsis"))))
294 (check-synopsis-style pkg)))
295 "no article allowed at the beginning of the synopsis")))
297 (test-assert "synopsis: too long"
299 (string-contains (with-warnings
300 (let ((pkg (dummy-package "x"
301 (synopsis (make-string 80 #\x)))))
302 (check-synopsis-style pkg)))
303 "synopsis should be less than 80 characters long")))
305 (test-assert "synopsis: start with package name"
307 (string-contains (with-warnings
308 (let ((pkg (dummy-package "x"
310 (synopsis "foo, a nice package"))))
311 (check-synopsis-style pkg)))
312 "synopsis should not start with the package name")))
314 (test-assert "synopsis: start with package name prefix"
317 (let ((pkg (dummy-package "arb"
318 (synopsis "Arbitrary precision"))))
319 (check-synopsis-style pkg)))))
321 (test-assert "synopsis: start with abbreviation"
324 (let ((pkg (dummy-package "uucp"
325 ;; Same problem with "APL interpreter", etc.
326 (synopsis "UUCP implementation")
327 (description "Imagine this is Taylor UUCP."))))
328 (check-synopsis-style pkg)))))
330 (test-assert "inputs: pkg-config is probably a native input"
334 (let ((pkg (dummy-package "x"
335 (inputs `(("pkg-config" ,pkg-config))))))
336 (check-inputs-should-be-native pkg)))
337 "'pkg-config' should probably be a native input")))
339 (test-assert "inputs: glib:bin is probably a native input"
343 (let ((pkg (dummy-package "x"
344 (inputs `(("glib" ,glib "bin"))))))
345 (check-inputs-should-be-native pkg)))
346 "'glib:bin' should probably be a native input")))
348 (test-assert "patches: file names"
352 (let ((pkg (dummy-package "x"
355 (patches (list "/path/to/y.patch")))))))
356 (check-patch-file-names pkg)))
357 "file names of patches should start with the package name")))
359 (test-assert "patches: not found"
363 (let ((pkg (dummy-package "x"
367 (list (search-patch "this-patch-does-not-exist!"))))))))
368 (check-patch-file-names pkg)))
371 (test-assert "derivation: invalid arguments"
375 (let ((pkg (dummy-package "x"
377 '(#:imported-modules (invalid-module))))))
378 (check-derivation pkg)))
379 "failed to create derivation")))
381 (test-assert "license: invalid license"
384 (check-license (dummy-package "x" (license #f))))
387 (test-assert "home-page: wrong home-page"
392 (inherit (dummy-package "x"))
394 (check-home-page pkg)))
397 (test-assert "home-page: invalid URI"
402 (inherit (dummy-package "x"))
403 (home-page "foobar"))))
404 (check-home-page pkg)))
405 "invalid home page URL")))
407 (test-assert "home-page: host not found"
412 (inherit (dummy-package "x"))
413 (home-page "http://does-not-exist"))))
414 (check-home-page pkg)))
415 "domain not found")))
417 (test-skip (if %http-server-socket 0 1))
418 (test-assert "home-page: Connection refused"
423 (inherit (dummy-package "x"))
424 (home-page %local-url))))
425 (check-home-page pkg)))
426 "Connection refused")))
428 (test-skip (if %http-server-socket 0 1))
429 (test-equal "home-page: 200"
432 (with-http-server 200 %long-string
434 (inherit (dummy-package "x"))
435 (home-page %local-url))))
436 (check-home-page pkg)))))
438 (test-skip (if %http-server-socket 0 1))
439 (test-assert "home-page: 200 but short length"
443 (with-http-server 200 "This is too small."
445 (inherit (dummy-package "x"))
446 (home-page %local-url))))
447 (check-home-page pkg))))
448 "suspiciously small")))
450 (test-skip (if %http-server-socket 0 1))
451 (test-assert "home-page: 404"
455 (with-http-server 404 %long-string
457 (inherit (dummy-package "x"))
458 (home-page %local-url))))
459 (check-home-page pkg))))
460 "not reachable: 404")))
462 (test-assert "source-file-name"
466 (let ((pkg (dummy-package "x"
471 (uri "http://www.example.com/3.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: v prefix"
480 (let ((pkg (dummy-package "x"
485 (uri "http://www.example.com/v3.2.1.tar.gz")
486 (sha256 %null-sha256))))))
487 (check-source-file-name pkg)))
488 "file name should contain the package name")))
490 (test-assert "source-file-name: bad checkout"
494 (let ((pkg (dummy-package "x"
500 (url "http://www.example.com/x.git")
502 (sha256 %null-sha256))))))
503 (check-source-file-name pkg)))
504 "file name should contain the package name")))
506 (test-assert "source-file-name: good checkout"
511 (let ((pkg (dummy-package "x"
517 (url "http://git.example.com/x.git")
519 (file-name (string-append "x-" version))
520 (sha256 %null-sha256))))))
521 (check-source-file-name pkg)))
522 "file name should contain the package name"))))
524 (test-assert "source-file-name: valid"
529 (let ((pkg (dummy-package "x"
534 (uri "http://www.example.com/x-3.2.1.tar.gz")
535 (sha256 %null-sha256))))))
536 (check-source-file-name pkg)))
537 "file name should contain the package name"))))
539 (test-skip (if %http-server-socket 0 1))
540 (test-equal "source: 200"
543 (with-http-server 200 %long-string
545 (inherit (dummy-package "x"))
549 (sha256 %null-sha256))))))
550 (check-source pkg)))))
552 (test-skip (if %http-server-socket 0 1))
553 (test-assert "source: 200 but short length"
557 (with-http-server 200 "This is too small."
559 (inherit (dummy-package "x"))
563 (sha256 %null-sha256))))))
564 (check-source pkg))))
565 "suspiciously small")))
567 (test-skip (if %http-server-socket 0 1))
568 (test-assert "source: 404"
572 (with-http-server 404 %long-string
574 (inherit (dummy-package "x"))
578 (sha256 %null-sha256))))))
579 (check-source pkg))))
580 "not reachable: 404")))
583 (mock ((guix scripts lint) package-vulnerabilities (const '()))
585 (with-warnings (check-vulnerabilities (dummy-package "x"))))))
587 (test-assert "cve: one vulnerability"
588 (mock ((guix scripts lint) package-vulnerabilities
590 (list (make-struct (@@ (guix cve) <vulnerability>) 0
592 (list (cons (package-name package)
593 (package-version package)))))))
596 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))
597 "vulnerable to CVE-2015-1234")))
599 (test-assert "cve: one patched vulnerability"
600 (mock ((guix scripts lint) package-vulnerabilities
602 (list (make-struct (@@ (guix cve) <vulnerability>) 0
604 (list (cons (package-name package)
605 (package-version package)))))))
608 (check-vulnerabilities
614 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
616 (test-assert "cve: patched vulnerability in replacement"
617 (mock ((guix scripts lint) package-vulnerabilities
619 (list (make-struct (@@ (guix cve) <vulnerability>) 0
621 (list (cons (package-name package)
622 (package-version package)))))))
625 (check-vulnerabilities
627 "pi" (version "3.14") (source (dummy-origin))
628 (replacement (dummy-package
629 "pi" (version "3.14")
633 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))))
635 (test-assert "formatting: lonely parentheses"
640 dummy-package "ugly as hell!"
645 (test-assert "formatting: tabulation"
648 (check-formatting (dummy-package "leave the tab here: ")))
651 (test-assert "formatting: trailing white space"
654 ;; Leave the trailing white space on the next line!
655 (check-formatting (dummy-package "x")))
656 "trailing white space"))
658 (test-assert "formatting: long line"
662 (dummy-package "x" ;here is a stupid comment just to make a long line
666 (test-assert "formatting: alright"
669 (check-formatting (dummy-package "x")))))
674 ;; eval: (put 'with-http-server 'scheme-indent-function 2)
675 ;; eval: (put 'with-warnings 'scheme-indent-function 0)