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 pkg-config)
35 #:use-module (web server)
36 #:use-module (web server http)
37 #:use-module (web response)
38 #:use-module (ice-9 threads)
39 #:use-module (srfi srfi-9 gnu)
40 #:use-module (srfi srfi-64))
44 (define %http-server-port
45 ;; TCP port to use for the stub HTTP server.
49 ;; URL to use for 'home-page' tests.
50 (string-append "http://localhost:" (number->string %http-server-port)
54 ;; SHA256 of the empty string.
56 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
58 (define %http-server-socket
59 ;; Socket used by the Web server.
62 (let ((sock (socket PF_INET SOCK_STREAM 0)))
63 (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
65 (make-socket-address AF_INET INADDR_LOOPBACK
69 (let ((err (system-error-errno args)))
70 (format (current-error-port)
71 "warning: cannot run Web server for tests: ~a~%"
75 (define (http-write server client response body)
77 (let* ((response (write-response response client))
78 (port (response-port response)))
82 (write-response-body response body)))
84 (quit #t) ;exit the server thread
87 ;; Mutex and condition variable to synchronize with the HTTP server.
88 (define %http-server-lock (make-mutex))
89 (define %http-server-ready (make-condition-variable))
91 (define (http-open . args)
92 "Start listening for HTTP requests and signal %HTTP-SERVER-READY."
93 (with-mutex %http-server-lock
94 (let ((result (apply (@@ (web server http) http-open) args)))
95 (signal-condition-variable %http-server-ready)
98 (define-server-impl stub-http-server
99 ;; Stripped-down version of Guile's built-in HTTP server.
101 (@@ (web server http) http-read)
103 (@@ (web server http) http-close))
105 (define (call-with-http-server code thunk)
106 "Call THUNK with an HTTP server running and returning CODE on HTTP
108 (define (server-body)
109 (define (handle request body)
110 (values (build-response #:code code
111 #:reason-phrase "Such is life")
116 (run-server handle stub-http-server
117 `(#:socket ,%http-server-socket)))
120 (with-mutex %http-server-lock
121 (let ((server (make-thread server-body)))
122 (wait-condition-variable %http-server-ready %http-server-lock)
123 ;; Normally SERVER exits automatically once it has received a request.
126 (define-syntax-rule (with-http-server code body ...)
127 (call-with-http-server code (lambda () body ...)))
132 (define (call-with-warnings thunk)
133 (let ((port (open-output-string)))
134 (parameterize ((guix-warning-port port))
136 (get-output-string port)))
138 (define-syntax-rule (with-warnings body ...)
139 (call-with-warnings (lambda () body ...)))
141 (test-assert "description: not a string"
143 (string-contains (with-warnings
144 (let ((pkg (dummy-package "x"
145 (description 'foobar))))
146 (check-description-style pkg)))
147 "invalid description")))
149 (test-assert "description: not empty"
151 (string-contains (with-warnings
152 (let ((pkg (dummy-package "x"
154 (check-description-style pkg)))
155 "description should not be empty")))
157 (test-assert "description: valid Texinfo markup"
161 (check-description-style (dummy-package "x" (description "f{oo}b@r"))))
162 "Texinfo markup in description is invalid")))
164 (test-assert "description: does not start with an upper-case letter"
166 (string-contains (with-warnings
167 (let ((pkg (dummy-package "x"
168 (description "bad description."))))
169 (check-description-style pkg)))
170 "description should start with an upper-case letter")))
172 (test-assert "description: may start with a digit"
175 (let ((pkg (dummy-package "x"
176 (description "2-component library."))))
177 (check-description-style pkg)))))
179 (test-assert "description: may start with lower-case package name"
182 (let ((pkg (dummy-package "x"
183 (description "x is a dummy package."))))
184 (check-description-style pkg)))))
186 (test-assert "description: two spaces after end of sentence"
188 (string-contains (with-warnings
189 (let ((pkg (dummy-package "x"
190 (description "Bad. Quite bad."))))
191 (check-description-style pkg)))
192 "sentences in description should be followed by two spaces")))
194 (test-assert "description: end-of-sentence detection with abbreviations"
197 (let ((pkg (dummy-package "x"
199 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
200 (check-description-style pkg)))))
202 (test-assert "synopsis: not a string"
204 (string-contains (with-warnings
205 (let ((pkg (dummy-package "x"
207 (check-synopsis-style pkg)))
208 "invalid synopsis")))
210 (test-assert "synopsis: not empty"
212 (string-contains (with-warnings
213 (let ((pkg (dummy-package "x"
215 (check-synopsis-style pkg)))
216 "synopsis should not be empty")))
218 (test-assert "synopsis: does not start with an upper-case letter"
220 (string-contains (with-warnings
221 (let ((pkg (dummy-package "x"
222 (synopsis "bad synopsis."))))
223 (check-synopsis-style pkg)))
224 "synopsis should start with an upper-case letter")))
226 (test-assert "synopsis: may start with a digit"
229 (let ((pkg (dummy-package "x"
230 (synopsis "5-dimensional frobnicator"))))
231 (check-synopsis-style pkg)))))
233 (test-assert "synopsis: ends with a period"
235 (string-contains (with-warnings
236 (let ((pkg (dummy-package "x"
237 (synopsis "Bad synopsis."))))
238 (check-synopsis-style pkg)))
239 "no period allowed at the end of the synopsis")))
241 (test-assert "synopsis: ends with 'etc.'"
242 (string-null? (with-warnings
243 (let ((pkg (dummy-package "x"
244 (synopsis "Foo, bar, etc."))))
245 (check-synopsis-style pkg)))))
247 (test-assert "synopsis: starts with 'A'"
249 (string-contains (with-warnings
250 (let ((pkg (dummy-package "x"
251 (synopsis "A bad synopŝis"))))
252 (check-synopsis-style pkg)))
253 "no article allowed at the beginning of the synopsis")))
255 (test-assert "synopsis: starts with 'An'"
257 (string-contains (with-warnings
258 (let ((pkg (dummy-package "x"
259 (synopsis "An awful synopsis"))))
260 (check-synopsis-style pkg)))
261 "no article allowed at the beginning of the synopsis")))
263 (test-assert "synopsis: starts with 'a'"
265 (string-contains (with-warnings
266 (let ((pkg (dummy-package "x"
267 (synopsis "a bad synopsis"))))
268 (check-synopsis-style pkg)))
269 "no article allowed at the beginning of the synopsis")))
271 (test-assert "synopsis: starts with 'an'"
273 (string-contains (with-warnings
274 (let ((pkg (dummy-package "x"
275 (synopsis "an awful synopsis"))))
276 (check-synopsis-style pkg)))
277 "no article allowed at the beginning of the synopsis")))
279 (test-assert "synopsis: too long"
281 (string-contains (with-warnings
282 (let ((pkg (dummy-package "x"
283 (synopsis (make-string 80 #\x)))))
284 (check-synopsis-style pkg)))
285 "synopsis should be less than 80 characters long")))
287 (test-assert "synopsis: start with package name"
289 (string-contains (with-warnings
290 (let ((pkg (dummy-package "x"
292 (synopsis "foo, a nice package"))))
293 (check-synopsis-style pkg)))
294 "synopsis should not start with the package name")))
296 (test-assert "synopsis: start with package name prefix"
299 (let ((pkg (dummy-package "arb"
300 (synopsis "Arbitrary precision"))))
301 (check-synopsis-style pkg)))))
303 (test-assert "synopsis: start with abbreviation"
306 (let ((pkg (dummy-package "uucp"
307 ;; Same problem with "APL interpreter", etc.
308 (synopsis "UUCP implementation")
309 (description "Imagine this is Taylor UUCP."))))
310 (check-synopsis-style pkg)))))
312 (test-assert "inputs: pkg-config is probably a native input"
316 (let ((pkg (dummy-package "x"
317 (inputs `(("pkg-config" ,pkg-config))))))
318 (check-inputs-should-be-native pkg)))
319 "pkg-config should probably be a native input")))
321 (test-assert "patches: file names"
325 (let ((pkg (dummy-package "x"
328 (patches (list "/path/to/y.patch")))))))
329 (check-patch-file-names pkg)))
330 "file names of patches should start with the package name")))
332 (test-assert "patches: not found"
336 (let ((pkg (dummy-package "x"
340 (list (search-patch "this-patch-does-not-exist!"))))))))
341 (check-patch-file-names pkg)))
344 (test-assert "derivation: invalid arguments"
348 (let ((pkg (dummy-package "x"
350 '(#:imported-modules (invalid-module))))))
351 (check-derivation pkg)))
352 "failed to create derivation")))
354 (test-assert "license: invalid license"
357 (check-license (dummy-package "x" (license #f))))
360 (test-assert "home-page: wrong home-page"
365 (inherit (dummy-package "x"))
367 (check-home-page pkg)))
370 (test-assert "home-page: invalid URI"
375 (inherit (dummy-package "x"))
376 (home-page "foobar"))))
377 (check-home-page pkg)))
378 "invalid home page URL")))
380 (test-assert "home-page: host not found"
385 (inherit (dummy-package "x"))
386 (home-page "http://does-not-exist"))))
387 (check-home-page pkg)))
388 "domain not found")))
390 (test-skip (if %http-server-socket 0 1))
391 (test-assert "home-page: Connection refused"
396 (inherit (dummy-package "x"))
397 (home-page %local-url))))
398 (check-home-page pkg)))
399 "Connection refused")))
401 (test-skip (if %http-server-socket 0 1))
402 (test-equal "home-page: 200"
405 (with-http-server 200
407 (inherit (dummy-package "x"))
408 (home-page %local-url))))
409 (check-home-page pkg)))))
411 (test-skip (if %http-server-socket 0 1))
412 (test-assert "home-page: 404"
416 (with-http-server 404
418 (inherit (dummy-package "x"))
419 (home-page %local-url))))
420 (check-home-page pkg))))
421 "not reachable: 404")))
423 (test-assert "source-file-name"
427 (let ((pkg (dummy-package "x"
432 (uri "http://www.example.com/3.2.1.tar.gz")
433 (sha256 %null-sha256))))))
434 (check-source-file-name pkg)))
435 "file name should contain the package name")))
437 (test-assert "source-file-name: v prefix"
441 (let ((pkg (dummy-package "x"
446 (uri "http://www.example.com/v3.2.1.tar.gz")
447 (sha256 %null-sha256))))))
448 (check-source-file-name pkg)))
449 "file name should contain the package name")))
451 (test-assert "source-file-name: bad checkout"
455 (let ((pkg (dummy-package "x"
461 (url "http://www.example.com/x.git")
463 (sha256 %null-sha256))))))
464 (check-source-file-name pkg)))
465 "file name should contain the package name")))
467 (test-assert "source-file-name: good checkout"
472 (let ((pkg (dummy-package "x"
478 (url "http://git.example.com/x.git")
480 (file-name (string-append "x-" version))
481 (sha256 %null-sha256))))))
482 (check-source-file-name pkg)))
483 "file name should contain the package name"))))
485 (test-assert "source-file-name: valid"
490 (let ((pkg (dummy-package "x"
495 (uri "http://www.example.com/x-3.2.1.tar.gz")
496 (sha256 %null-sha256))))))
497 (check-source-file-name pkg)))
498 "file name should contain the package name"))))
500 (test-skip (if %http-server-socket 0 1))
501 (test-equal "source: 200"
504 (with-http-server 200
506 (inherit (dummy-package "x"))
510 (sha256 %null-sha256))))))
511 (check-source pkg)))))
513 (test-skip (if %http-server-socket 0 1))
514 (test-assert "source: 404"
518 (with-http-server 404
520 (inherit (dummy-package "x"))
524 (sha256 %null-sha256))))))
525 (check-source pkg))))
526 "not reachable: 404")))
529 (mock ((guix scripts lint) package-vulnerabilities (const '()))
531 (with-warnings (check-vulnerabilities (dummy-package "x"))))))
533 (test-assert "cve: one vulnerability"
534 (mock ((guix scripts lint) package-vulnerabilities
536 (list (make-struct (@@ (guix cve) <vulnerability>) 0
538 (list (cons (package-name package)
539 (package-version package)))))))
542 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))
543 "vulnerable to CVE-2015-1234")))
545 (test-assert "cve: one patched vulnerability"
546 (mock ((guix scripts lint) package-vulnerabilities
548 (list (make-struct (@@ (guix cve) <vulnerability>) 0
550 (list (cons (package-name package)
551 (package-version package)))))))
554 (check-vulnerabilities
560 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
562 (test-assert "formatting: lonely parentheses"
567 dummy-package "ugly as hell!"
572 (test-assert "formatting: tabulation"
575 (check-formatting (dummy-package "leave the tab here: ")))
578 (test-assert "formatting: trailing white space"
581 ;; Leave the trailing white space on the next line!
582 (check-formatting (dummy-package "x")))
583 "trailing white space"))
585 (test-assert "formatting: long line"
589 (dummy-package "x" ;here is a stupid comment just to make a long line
593 (test-assert "formatting: alright"
596 (check-formatting (dummy-package "x")))))
601 ;; eval: (put 'with-http-server 'scheme-indent-function 1)
602 ;; eval: (put 'with-warnings 'scheme-indent-function 0)