Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / tests / lint.scm
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>
6 ;;;
7 ;;; This file is part of GNU Guix.
8 ;;;
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.
13 ;;;
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.
18 ;;;
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/>.
21
22 ;; Avoid interference.
23 (unsetenv "http_proxy")
24
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))
41
42 ;; Test the linter.
43
44 (define %http-server-port
45 ;; TCP port to use for the stub HTTP server.
46 9999)
47
48 (define %local-url
49 ;; URL to use for 'home-page' tests.
50 (string-append "http://localhost:" (number->string %http-server-port)
51 "/foo/bar"))
52
53 (define %null-sha256
54 ;; SHA256 of the empty string.
55 (base32
56 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
57
58 (define %http-server-socket
59 ;; Socket used by the Web server.
60 (catch 'system-error
61 (lambda ()
62 (let ((sock (socket PF_INET SOCK_STREAM 0)))
63 (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
64 (bind sock
65 (make-socket-address AF_INET INADDR_LOOPBACK
66 %http-server-port))
67 sock))
68 (lambda args
69 (let ((err (system-error-errno args)))
70 (format (current-error-port)
71 "warning: cannot run Web server for tests: ~a~%"
72 (strerror err))
73 #f))))
74
75 (define (http-write server client response body)
76 "Write RESPONSE."
77 (let* ((response (write-response response client))
78 (port (response-port response)))
79 (cond
80 ((not body)) ;pass
81 (else
82 (write-response-body response body)))
83 (close-port port)
84 (quit #t) ;exit the server thread
85 (values)))
86
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))
90
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)
96 result)))
97
98 (define-server-impl stub-http-server
99 ;; Stripped-down version of Guile's built-in HTTP server.
100 http-open
101 (@@ (web server http) http-read)
102 http-write
103 (@@ (web server http) http-close))
104
105 (define (call-with-http-server code thunk)
106 "Call THUNK with an HTTP server running and returning CODE on HTTP
107 requests."
108 (define (server-body)
109 (define (handle request body)
110 (values (build-response #:code code
111 #:reason-phrase "Such is life")
112 "Hello, world."))
113
114 (catch 'quit
115 (lambda ()
116 (run-server handle stub-http-server
117 `(#:socket ,%http-server-socket)))
118 (const #t)))
119
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.
124 (thunk))))
125
126 (define-syntax-rule (with-http-server code body ...)
127 (call-with-http-server code (lambda () body ...)))
128
129 \f
130 (test-begin "lint")
131
132 (define (call-with-warnings thunk)
133 (let ((port (open-output-string)))
134 (parameterize ((guix-warning-port port))
135 (thunk))
136 (get-output-string port)))
137
138 (define-syntax-rule (with-warnings body ...)
139 (call-with-warnings (lambda () body ...)))
140
141 (test-assert "description: not empty"
142 (->bool
143 (string-contains (with-warnings
144 (let ((pkg (dummy-package "x"
145 (description ""))))
146 (check-description-style pkg)))
147 "description should not be empty")))
148
149 (test-assert "description: valid Texinfo markup"
150 (->bool
151 (string-contains
152 (with-warnings
153 (check-description-style (dummy-package "x" (description "f{oo}b@r"))))
154 "Texinfo markup in description is invalid")))
155
156 (test-assert "description: does not start with an upper-case letter"
157 (->bool
158 (string-contains (with-warnings
159 (let ((pkg (dummy-package "x"
160 (description "bad description."))))
161 (check-description-style pkg)))
162 "description should start with an upper-case letter")))
163
164 (test-assert "description: may start with a digit"
165 (string-null?
166 (with-warnings
167 (let ((pkg (dummy-package "x"
168 (description "2-component library."))))
169 (check-description-style pkg)))))
170
171 (test-assert "description: may start with lower-case package name"
172 (string-null?
173 (with-warnings
174 (let ((pkg (dummy-package "x"
175 (description "x is a dummy package."))))
176 (check-description-style pkg)))))
177
178 (test-assert "description: two spaces after end of sentence"
179 (->bool
180 (string-contains (with-warnings
181 (let ((pkg (dummy-package "x"
182 (description "Bad. Quite bad."))))
183 (check-description-style pkg)))
184 "sentences in description should be followed by two spaces")))
185
186 (test-assert "description: end-of-sentence detection with abbreviations"
187 (string-null?
188 (with-warnings
189 (let ((pkg (dummy-package "x"
190 (description
191 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
192 (check-description-style pkg)))))
193
194 (test-assert "synopsis: not empty"
195 (->bool
196 (string-contains (with-warnings
197 (let ((pkg (dummy-package "x"
198 (synopsis ""))))
199 (check-synopsis-style pkg)))
200 "synopsis should not be empty")))
201
202 (test-assert "synopsis: does not start with an upper-case letter"
203 (->bool
204 (string-contains (with-warnings
205 (let ((pkg (dummy-package "x"
206 (synopsis "bad synopsis."))))
207 (check-synopsis-style pkg)))
208 "synopsis should start with an upper-case letter")))
209
210 (test-assert "synopsis: may start with a digit"
211 (string-null?
212 (with-warnings
213 (let ((pkg (dummy-package "x"
214 (synopsis "5-dimensional frobnicator"))))
215 (check-synopsis-style pkg)))))
216
217 (test-assert "synopsis: ends with a period"
218 (->bool
219 (string-contains (with-warnings
220 (let ((pkg (dummy-package "x"
221 (synopsis "Bad synopsis."))))
222 (check-synopsis-style pkg)))
223 "no period allowed at the end of the synopsis")))
224
225 (test-assert "synopsis: ends with 'etc.'"
226 (string-null? (with-warnings
227 (let ((pkg (dummy-package "x"
228 (synopsis "Foo, bar, etc."))))
229 (check-synopsis-style pkg)))))
230
231 (test-assert "synopsis: starts with 'A'"
232 (->bool
233 (string-contains (with-warnings
234 (let ((pkg (dummy-package "x"
235 (synopsis "A bad synopŝis"))))
236 (check-synopsis-style pkg)))
237 "no article allowed at the beginning of the synopsis")))
238
239 (test-assert "synopsis: starts with 'An'"
240 (->bool
241 (string-contains (with-warnings
242 (let ((pkg (dummy-package "x"
243 (synopsis "An awful synopsis"))))
244 (check-synopsis-style pkg)))
245 "no article allowed at the beginning of the synopsis")))
246
247 (test-assert "synopsis: starts with 'a'"
248 (->bool
249 (string-contains (with-warnings
250 (let ((pkg (dummy-package "x"
251 (synopsis "a bad synopsis"))))
252 (check-synopsis-style pkg)))
253 "no article allowed at the beginning of the synopsis")))
254
255 (test-assert "synopsis: starts with 'an'"
256 (->bool
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")))
262
263 (test-assert "synopsis: too long"
264 (->bool
265 (string-contains (with-warnings
266 (let ((pkg (dummy-package "x"
267 (synopsis (make-string 80 #\x)))))
268 (check-synopsis-style pkg)))
269 "synopsis should be less than 80 characters long")))
270
271 (test-assert "synopsis: start with package name"
272 (->bool
273 (string-contains (with-warnings
274 (let ((pkg (dummy-package "x"
275 (name "foo")
276 (synopsis "foo, a nice package"))))
277 (check-synopsis-style pkg)))
278 "synopsis should not start with the package name")))
279
280 (test-assert "synopsis: start with package name prefix"
281 (string-null?
282 (with-warnings
283 (let ((pkg (dummy-package "arb"
284 (synopsis "Arbitrary precision"))))
285 (check-synopsis-style pkg)))))
286
287 (test-assert "synopsis: start with abbreviation"
288 (string-null?
289 (with-warnings
290 (let ((pkg (dummy-package "uucp"
291 ;; Same problem with "APL interpreter", etc.
292 (synopsis "UUCP implementation")
293 (description "Imagine this is Taylor UUCP."))))
294 (check-synopsis-style pkg)))))
295
296 (test-assert "inputs: pkg-config is probably a native input"
297 (->bool
298 (string-contains
299 (with-warnings
300 (let ((pkg (dummy-package "x"
301 (inputs `(("pkg-config" ,pkg-config))))))
302 (check-inputs-should-be-native pkg)))
303 "pkg-config should probably be a native input")))
304
305 (test-assert "patches: file names"
306 (->bool
307 (string-contains
308 (with-warnings
309 (let ((pkg (dummy-package "x"
310 (source
311 (dummy-origin
312 (patches (list "/path/to/y.patch")))))))
313 (check-patch-file-names pkg)))
314 "file names of patches should start with the package name")))
315
316 (test-assert "patches: not found"
317 (->bool
318 (string-contains
319 (with-warnings
320 (let ((pkg (dummy-package "x"
321 (source
322 (dummy-origin
323 (patches
324 (list (search-patch "this-patch-does-not-exist!"))))))))
325 (check-patch-file-names pkg)))
326 "patch not found")))
327
328 (test-assert "derivation: invalid arguments"
329 (->bool
330 (string-contains
331 (with-warnings
332 (let ((pkg (dummy-package "x"
333 (arguments
334 '(#:imported-modules (invalid-module))))))
335 (check-derivation pkg)))
336 "failed to create derivation")))
337
338 (test-assert "license: invalid license"
339 (string-contains
340 (with-warnings
341 (check-license (dummy-package "x" (license #f))))
342 "invalid license"))
343
344 (test-assert "home-page: wrong home-page"
345 (->bool
346 (string-contains
347 (with-warnings
348 (let ((pkg (package
349 (inherit (dummy-package "x"))
350 (home-page #f))))
351 (check-home-page pkg)))
352 "invalid")))
353
354 (test-assert "home-page: invalid URI"
355 (->bool
356 (string-contains
357 (with-warnings
358 (let ((pkg (package
359 (inherit (dummy-package "x"))
360 (home-page "foobar"))))
361 (check-home-page pkg)))
362 "invalid home page URL")))
363
364 (test-assert "home-page: host not found"
365 (->bool
366 (string-contains
367 (with-warnings
368 (let ((pkg (package
369 (inherit (dummy-package "x"))
370 (home-page "http://does-not-exist"))))
371 (check-home-page pkg)))
372 "domain not found")))
373
374 (test-skip (if %http-server-socket 0 1))
375 (test-assert "home-page: Connection refused"
376 (->bool
377 (string-contains
378 (with-warnings
379 (let ((pkg (package
380 (inherit (dummy-package "x"))
381 (home-page %local-url))))
382 (check-home-page pkg)))
383 "Connection refused")))
384
385 (test-skip (if %http-server-socket 0 1))
386 (test-equal "home-page: 200"
387 ""
388 (with-warnings
389 (with-http-server 200
390 (let ((pkg (package
391 (inherit (dummy-package "x"))
392 (home-page %local-url))))
393 (check-home-page pkg)))))
394
395 (test-skip (if %http-server-socket 0 1))
396 (test-assert "home-page: 404"
397 (->bool
398 (string-contains
399 (with-warnings
400 (with-http-server 404
401 (let ((pkg (package
402 (inherit (dummy-package "x"))
403 (home-page %local-url))))
404 (check-home-page pkg))))
405 "not reachable: 404")))
406
407 (test-assert "source-file-name"
408 (->bool
409 (string-contains
410 (with-warnings
411 (let ((pkg (dummy-package "x"
412 (version "3.2.1")
413 (source
414 (origin
415 (method url-fetch)
416 (uri "http://www.example.com/3.2.1.tar.gz")
417 (sha256 %null-sha256))))))
418 (check-source-file-name pkg)))
419 "file name should contain the package name")))
420
421 (test-assert "source-file-name: v prefix"
422 (->bool
423 (string-contains
424 (with-warnings
425 (let ((pkg (dummy-package "x"
426 (version "3.2.1")
427 (source
428 (origin
429 (method url-fetch)
430 (uri "http://www.example.com/v3.2.1.tar.gz")
431 (sha256 %null-sha256))))))
432 (check-source-file-name pkg)))
433 "file name should contain the package name")))
434
435 (test-assert "source-file-name: bad checkout"
436 (->bool
437 (string-contains
438 (with-warnings
439 (let ((pkg (dummy-package "x"
440 (version "3.2.1")
441 (source
442 (origin
443 (method git-fetch)
444 (uri (git-reference
445 (url "http://www.example.com/x.git")
446 (commit "0")))
447 (sha256 %null-sha256))))))
448 (check-source-file-name pkg)))
449 "file name should contain the package name")))
450
451 (test-assert "source-file-name: good checkout"
452 (not
453 (->bool
454 (string-contains
455 (with-warnings
456 (let ((pkg (dummy-package "x"
457 (version "3.2.1")
458 (source
459 (origin
460 (method git-fetch)
461 (uri (git-reference
462 (url "http://git.example.com/x.git")
463 (commit "0")))
464 (file-name (string-append "x-" version))
465 (sha256 %null-sha256))))))
466 (check-source-file-name pkg)))
467 "file name should contain the package name"))))
468
469 (test-assert "source-file-name: valid"
470 (not
471 (->bool
472 (string-contains
473 (with-warnings
474 (let ((pkg (dummy-package "x"
475 (version "3.2.1")
476 (source
477 (origin
478 (method url-fetch)
479 (uri "http://www.example.com/x-3.2.1.tar.gz")
480 (sha256 %null-sha256))))))
481 (check-source-file-name pkg)))
482 "file name should contain the package name"))))
483
484 (test-skip (if %http-server-socket 0 1))
485 (test-equal "source: 200"
486 ""
487 (with-warnings
488 (with-http-server 200
489 (let ((pkg (package
490 (inherit (dummy-package "x"))
491 (source (origin
492 (method url-fetch)
493 (uri %local-url)
494 (sha256 %null-sha256))))))
495 (check-source pkg)))))
496
497 (test-skip (if %http-server-socket 0 1))
498 (test-assert "source: 404"
499 (->bool
500 (string-contains
501 (with-warnings
502 (with-http-server 404
503 (let ((pkg (package
504 (inherit (dummy-package "x"))
505 (source (origin
506 (method url-fetch)
507 (uri %local-url)
508 (sha256 %null-sha256))))))
509 (check-source pkg))))
510 "not reachable: 404")))
511
512 (test-assert "cve"
513 (mock ((guix scripts lint) package-vulnerabilities (const '()))
514 (string-null?
515 (with-warnings (check-vulnerabilities (dummy-package "x"))))))
516
517 (test-assert "cve: one vulnerability"
518 (mock ((guix scripts lint) package-vulnerabilities
519 (lambda (package)
520 (list (make-struct (@@ (guix cve) <vulnerability>) 0
521 "CVE-2015-1234"
522 (list (cons (package-name package)
523 (package-version package)))))))
524 (string-contains
525 (with-warnings
526 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))
527 "vulnerable to CVE-2015-1234")))
528
529 (test-assert "cve: one patched vulnerability"
530 (mock ((guix scripts lint) package-vulnerabilities
531 (lambda (package)
532 (list (make-struct (@@ (guix cve) <vulnerability>) 0
533 "CVE-2015-1234"
534 (list (cons (package-name package)
535 (package-version package)))))))
536 (string-null?
537 (with-warnings
538 (check-vulnerabilities
539 (dummy-package "pi"
540 (version "3.14")
541 (source
542 (dummy-origin
543 (patches
544 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
545
546 (test-assert "formatting: lonely parentheses"
547 (string-contains
548 (with-warnings
549 (check-formatting
550 (
551 dummy-package "ugly as hell!"
552 )
553 ))
554 "lonely"))
555
556 (test-assert "formatting: tabulation"
557 (string-contains
558 (with-warnings
559 (check-formatting (dummy-package "leave the tab here: ")))
560 "tabulation"))
561
562 (test-assert "formatting: trailing white space"
563 (string-contains
564 (with-warnings
565 ;; Leave the trailing white space on the next line!
566 (check-formatting (dummy-package "x")))
567 "trailing white space"))
568
569 (test-assert "formatting: long line"
570 (string-contains
571 (with-warnings
572 (check-formatting
573 (dummy-package "x" ;here is a stupid comment just to make a long line
574 )))
575 "too long"))
576
577 (test-assert "formatting: alright"
578 (string-null?
579 (with-warnings
580 (check-formatting (dummy-package "x")))))
581
582 (test-end "lint")
583
584 ;; Local Variables:
585 ;; eval: (put 'with-http-server 'scheme-indent-function 1)
586 ;; eval: (put 'with-warnings 'scheme-indent-function 0)
587 ;; End: