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 a string"
142 (->bool
143 (string-contains (with-warnings
144 (let ((pkg (dummy-package "x"
145 (description 'foobar))))
146 (check-description-style pkg)))
147 "invalid description")))
148
149 (test-assert "description: not empty"
150 (->bool
151 (string-contains (with-warnings
152 (let ((pkg (dummy-package "x"
153 (description ""))))
154 (check-description-style pkg)))
155 "description should not be empty")))
156
157 (test-assert "description: valid Texinfo markup"
158 (->bool
159 (string-contains
160 (with-warnings
161 (check-description-style (dummy-package "x" (description "f{oo}b@r"))))
162 "Texinfo markup in description is invalid")))
163
164 (test-assert "description: does not start with an upper-case letter"
165 (->bool
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")))
171
172 (test-assert "description: may start with a digit"
173 (string-null?
174 (with-warnings
175 (let ((pkg (dummy-package "x"
176 (description "2-component library."))))
177 (check-description-style pkg)))))
178
179 (test-assert "description: may start with lower-case package name"
180 (string-null?
181 (with-warnings
182 (let ((pkg (dummy-package "x"
183 (description "x is a dummy package."))))
184 (check-description-style pkg)))))
185
186 (test-assert "description: two spaces after end of sentence"
187 (->bool
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")))
193
194 (test-assert "description: end-of-sentence detection with abbreviations"
195 (string-null?
196 (with-warnings
197 (let ((pkg (dummy-package "x"
198 (description
199 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
200 (check-description-style pkg)))))
201
202 (test-assert "synopsis: not a string"
203 (->bool
204 (string-contains (with-warnings
205 (let ((pkg (dummy-package "x"
206 (synopsis #f))))
207 (check-synopsis-style pkg)))
208 "invalid synopsis")))
209
210 (test-assert "synopsis: not empty"
211 (->bool
212 (string-contains (with-warnings
213 (let ((pkg (dummy-package "x"
214 (synopsis ""))))
215 (check-synopsis-style pkg)))
216 "synopsis should not be empty")))
217
218 (test-assert "synopsis: does not start with an upper-case letter"
219 (->bool
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")))
225
226 (test-assert "synopsis: may start with a digit"
227 (string-null?
228 (with-warnings
229 (let ((pkg (dummy-package "x"
230 (synopsis "5-dimensional frobnicator"))))
231 (check-synopsis-style pkg)))))
232
233 (test-assert "synopsis: ends with a period"
234 (->bool
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")))
240
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)))))
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 synopŝis"))))
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: starts with 'a'"
264 (->bool
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")))
270
271 (test-assert "synopsis: starts with 'an'"
272 (->bool
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")))
278
279 (test-assert "synopsis: too long"
280 (->bool
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")))
286
287 (test-assert "synopsis: start with package name"
288 (->bool
289 (string-contains (with-warnings
290 (let ((pkg (dummy-package "x"
291 (name "foo")
292 (synopsis "foo, a nice package"))))
293 (check-synopsis-style pkg)))
294 "synopsis should not start with the package name")))
295
296 (test-assert "synopsis: start with package name prefix"
297 (string-null?
298 (with-warnings
299 (let ((pkg (dummy-package "arb"
300 (synopsis "Arbitrary precision"))))
301 (check-synopsis-style pkg)))))
302
303 (test-assert "synopsis: start with abbreviation"
304 (string-null?
305 (with-warnings
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)))))
311
312 (test-assert "inputs: pkg-config is probably a native input"
313 (->bool
314 (string-contains
315 (with-warnings
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")))
320
321 (test-assert "patches: file names"
322 (->bool
323 (string-contains
324 (with-warnings
325 (let ((pkg (dummy-package "x"
326 (source
327 (dummy-origin
328 (patches (list "/path/to/y.patch")))))))
329 (check-patch-file-names pkg)))
330 "file names of patches should start with the package name")))
331
332 (test-assert "patches: not found"
333 (->bool
334 (string-contains
335 (with-warnings
336 (let ((pkg (dummy-package "x"
337 (source
338 (dummy-origin
339 (patches
340 (list (search-patch "this-patch-does-not-exist!"))))))))
341 (check-patch-file-names pkg)))
342 "patch not found")))
343
344 (test-assert "derivation: invalid arguments"
345 (->bool
346 (string-contains
347 (with-warnings
348 (let ((pkg (dummy-package "x"
349 (arguments
350 '(#:imported-modules (invalid-module))))))
351 (check-derivation pkg)))
352 "failed to create derivation")))
353
354 (test-assert "license: invalid license"
355 (string-contains
356 (with-warnings
357 (check-license (dummy-package "x" (license #f))))
358 "invalid license"))
359
360 (test-assert "home-page: wrong home-page"
361 (->bool
362 (string-contains
363 (with-warnings
364 (let ((pkg (package
365 (inherit (dummy-package "x"))
366 (home-page #f))))
367 (check-home-page pkg)))
368 "invalid")))
369
370 (test-assert "home-page: invalid URI"
371 (->bool
372 (string-contains
373 (with-warnings
374 (let ((pkg (package
375 (inherit (dummy-package "x"))
376 (home-page "foobar"))))
377 (check-home-page pkg)))
378 "invalid home page URL")))
379
380 (test-assert "home-page: host not found"
381 (->bool
382 (string-contains
383 (with-warnings
384 (let ((pkg (package
385 (inherit (dummy-package "x"))
386 (home-page "http://does-not-exist"))))
387 (check-home-page pkg)))
388 "domain not found")))
389
390 (test-skip (if %http-server-socket 0 1))
391 (test-assert "home-page: Connection refused"
392 (->bool
393 (string-contains
394 (with-warnings
395 (let ((pkg (package
396 (inherit (dummy-package "x"))
397 (home-page %local-url))))
398 (check-home-page pkg)))
399 "Connection refused")))
400
401 (test-skip (if %http-server-socket 0 1))
402 (test-equal "home-page: 200"
403 ""
404 (with-warnings
405 (with-http-server 200
406 (let ((pkg (package
407 (inherit (dummy-package "x"))
408 (home-page %local-url))))
409 (check-home-page pkg)))))
410
411 (test-skip (if %http-server-socket 0 1))
412 (test-assert "home-page: 404"
413 (->bool
414 (string-contains
415 (with-warnings
416 (with-http-server 404
417 (let ((pkg (package
418 (inherit (dummy-package "x"))
419 (home-page %local-url))))
420 (check-home-page pkg))))
421 "not reachable: 404")))
422
423 (test-assert "source-file-name"
424 (->bool
425 (string-contains
426 (with-warnings
427 (let ((pkg (dummy-package "x"
428 (version "3.2.1")
429 (source
430 (origin
431 (method url-fetch)
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")))
436
437 (test-assert "source-file-name: v prefix"
438 (->bool
439 (string-contains
440 (with-warnings
441 (let ((pkg (dummy-package "x"
442 (version "3.2.1")
443 (source
444 (origin
445 (method url-fetch)
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")))
450
451 (test-assert "source-file-name: bad checkout"
452 (->bool
453 (string-contains
454 (with-warnings
455 (let ((pkg (dummy-package "x"
456 (version "3.2.1")
457 (source
458 (origin
459 (method git-fetch)
460 (uri (git-reference
461 (url "http://www.example.com/x.git")
462 (commit "0")))
463 (sha256 %null-sha256))))))
464 (check-source-file-name pkg)))
465 "file name should contain the package name")))
466
467 (test-assert "source-file-name: good checkout"
468 (not
469 (->bool
470 (string-contains
471 (with-warnings
472 (let ((pkg (dummy-package "x"
473 (version "3.2.1")
474 (source
475 (origin
476 (method git-fetch)
477 (uri (git-reference
478 (url "http://git.example.com/x.git")
479 (commit "0")))
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"))))
484
485 (test-assert "source-file-name: valid"
486 (not
487 (->bool
488 (string-contains
489 (with-warnings
490 (let ((pkg (dummy-package "x"
491 (version "3.2.1")
492 (source
493 (origin
494 (method url-fetch)
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"))))
499
500 (test-skip (if %http-server-socket 0 1))
501 (test-equal "source: 200"
502 ""
503 (with-warnings
504 (with-http-server 200
505 (let ((pkg (package
506 (inherit (dummy-package "x"))
507 (source (origin
508 (method url-fetch)
509 (uri %local-url)
510 (sha256 %null-sha256))))))
511 (check-source pkg)))))
512
513 (test-skip (if %http-server-socket 0 1))
514 (test-assert "source: 404"
515 (->bool
516 (string-contains
517 (with-warnings
518 (with-http-server 404
519 (let ((pkg (package
520 (inherit (dummy-package "x"))
521 (source (origin
522 (method url-fetch)
523 (uri %local-url)
524 (sha256 %null-sha256))))))
525 (check-source pkg))))
526 "not reachable: 404")))
527
528 (test-assert "cve"
529 (mock ((guix scripts lint) package-vulnerabilities (const '()))
530 (string-null?
531 (with-warnings (check-vulnerabilities (dummy-package "x"))))))
532
533 (test-assert "cve: one vulnerability"
534 (mock ((guix scripts lint) package-vulnerabilities
535 (lambda (package)
536 (list (make-struct (@@ (guix cve) <vulnerability>) 0
537 "CVE-2015-1234"
538 (list (cons (package-name package)
539 (package-version package)))))))
540 (string-contains
541 (with-warnings
542 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))
543 "vulnerable to CVE-2015-1234")))
544
545 (test-assert "cve: one patched vulnerability"
546 (mock ((guix scripts lint) package-vulnerabilities
547 (lambda (package)
548 (list (make-struct (@@ (guix cve) <vulnerability>) 0
549 "CVE-2015-1234"
550 (list (cons (package-name package)
551 (package-version package)))))))
552 (string-null?
553 (with-warnings
554 (check-vulnerabilities
555 (dummy-package "pi"
556 (version "3.14")
557 (source
558 (dummy-origin
559 (patches
560 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
561
562 (test-assert "cve: patched vulnerability in replacement"
563 (mock ((guix scripts lint) package-vulnerabilities
564 (lambda (package)
565 (list (make-struct (@@ (guix cve) <vulnerability>) 0
566 "CVE-2015-1234"
567 (list (cons (package-name package)
568 (package-version package)))))))
569 (string-null?
570 (with-warnings
571 (check-vulnerabilities
572 (dummy-package
573 "pi" (version "3.14") (source (dummy-origin))
574 (replacement (dummy-package
575 "pi" (version "3.14")
576 (source
577 (dummy-origin
578 (patches
579 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))))
580
581 (test-assert "formatting: lonely parentheses"
582 (string-contains
583 (with-warnings
584 (check-formatting
585 (
586 dummy-package "ugly as hell!"
587 )
588 ))
589 "lonely"))
590
591 (test-assert "formatting: tabulation"
592 (string-contains
593 (with-warnings
594 (check-formatting (dummy-package "leave the tab here: ")))
595 "tabulation"))
596
597 (test-assert "formatting: trailing white space"
598 (string-contains
599 (with-warnings
600 ;; Leave the trailing white space on the next line!
601 (check-formatting (dummy-package "x")))
602 "trailing white space"))
603
604 (test-assert "formatting: long line"
605 (string-contains
606 (with-warnings
607 (check-formatting
608 (dummy-package "x" ;here is a stupid comment just to make a long line
609 )))
610 "too long"))
611
612 (test-assert "formatting: alright"
613 (string-null?
614 (with-warnings
615 (check-formatting (dummy-package "x")))))
616
617 (test-end "lint")
618
619 ;; Local Variables:
620 ;; eval: (put 'with-http-server 'scheme-indent-function 1)
621 ;; eval: (put 'with-warnings 'scheme-indent-function 0)
622 ;; End: