Merge branch 'gtk-im-modules'
[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, 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>
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 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))
42
43 ;; Test the linter.
44
45 (define %http-server-port
46 ;; TCP port to use for the stub HTTP server.
47 9999)
48
49 (define %local-url
50 ;; URL to use for 'home-page' tests.
51 (string-append "http://localhost:" (number->string %http-server-port)
52 "/foo/bar"))
53
54 (define %null-sha256
55 ;; SHA256 of the empty string.
56 (base32
57 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
58
59 (define %http-server-socket
60 ;; Socket used by the Web server.
61 (catch 'system-error
62 (lambda ()
63 (let ((sock (socket PF_INET SOCK_STREAM 0)))
64 (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
65 (bind sock
66 (make-socket-address AF_INET INADDR_LOOPBACK
67 %http-server-port))
68 sock))
69 (lambda args
70 (let ((err (system-error-errno args)))
71 (format (current-error-port)
72 "warning: cannot run Web server for tests: ~a~%"
73 (strerror err))
74 #f))))
75
76 (define (http-write server client response body)
77 "Write RESPONSE."
78 (let* ((response (write-response response client))
79 (port (response-port response)))
80 (cond
81 ((not body)) ;pass
82 (else
83 (write-response-body response body)))
84 (close-port port)
85 (quit #t) ;exit the server thread
86 (values)))
87
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))
91
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)
97 result)))
98
99 (define-server-impl stub-http-server
100 ;; Stripped-down version of Guile's built-in HTTP server.
101 http-open
102 (@@ (web server http) http-read)
103 http-write
104 (@@ (web server http) http-close))
105
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")
113 data))
114
115 (catch 'quit
116 (lambda ()
117 (run-server handle stub-http-server
118 `(#:socket ,%http-server-socket)))
119 (const #t)))
120
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.
125 (thunk))))
126
127 (define-syntax-rule (with-http-server code data body ...)
128 (call-with-http-server code data (lambda () body ...)))
129
130 (define %long-string
131 (make-string 2000 #\a))
132
133 \f
134 (test-begin "lint")
135
136 (define (call-with-warnings thunk)
137 (let ((port (open-output-string)))
138 (parameterize ((guix-warning-port port))
139 (thunk))
140 (get-output-string port)))
141
142 (define-syntax-rule (with-warnings body ...)
143 (call-with-warnings (lambda () body ...)))
144
145 (test-assert "description: not a string"
146 (->bool
147 (string-contains (with-warnings
148 (let ((pkg (dummy-package "x"
149 (description 'foobar))))
150 (check-description-style pkg)))
151 "invalid description")))
152
153 (test-assert "description: not empty"
154 (->bool
155 (string-contains (with-warnings
156 (let ((pkg (dummy-package "x"
157 (description ""))))
158 (check-description-style pkg)))
159 "description should not be empty")))
160
161 (test-assert "description: valid Texinfo markup"
162 (->bool
163 (string-contains
164 (with-warnings
165 (check-description-style (dummy-package "x" (description "f{oo}b@r"))))
166 "Texinfo markup in description is invalid")))
167
168 (test-assert "description: does not start with an upper-case letter"
169 (->bool
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")))
175
176 (test-assert "description: may start with a digit"
177 (string-null?
178 (with-warnings
179 (let ((pkg (dummy-package "x"
180 (description "2-component library."))))
181 (check-description-style pkg)))))
182
183 (test-assert "description: may start with lower-case package name"
184 (string-null?
185 (with-warnings
186 (let ((pkg (dummy-package "x"
187 (description "x is a dummy package."))))
188 (check-description-style pkg)))))
189
190 (test-assert "description: two spaces after end of sentence"
191 (->bool
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")))
197
198 (test-assert "description: end-of-sentence detection with abbreviations"
199 (string-null?
200 (with-warnings
201 (let ((pkg (dummy-package "x"
202 (description
203 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
204 (check-description-style pkg)))))
205
206 (test-assert "description: may not contain trademark signs"
207 (and (->bool
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"))
213 (->bool
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"))))
219
220 (test-assert "synopsis: not a string"
221 (->bool
222 (string-contains (with-warnings
223 (let ((pkg (dummy-package "x"
224 (synopsis #f))))
225 (check-synopsis-style pkg)))
226 "invalid synopsis")))
227
228 (test-assert "synopsis: not empty"
229 (->bool
230 (string-contains (with-warnings
231 (let ((pkg (dummy-package "x"
232 (synopsis ""))))
233 (check-synopsis-style pkg)))
234 "synopsis should not be empty")))
235
236 (test-assert "synopsis: does not start with an upper-case letter"
237 (->bool
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")))
243
244 (test-assert "synopsis: may start with a digit"
245 (string-null?
246 (with-warnings
247 (let ((pkg (dummy-package "x"
248 (synopsis "5-dimensional frobnicator"))))
249 (check-synopsis-style pkg)))))
250
251 (test-assert "synopsis: ends with a period"
252 (->bool
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")))
258
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)))))
264
265 (test-assert "synopsis: starts with 'A'"
266 (->bool
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")))
272
273 (test-assert "synopsis: starts with 'An'"
274 (->bool
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")))
280
281 (test-assert "synopsis: starts with 'a'"
282 (->bool
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")))
288
289 (test-assert "synopsis: starts with 'an'"
290 (->bool
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")))
296
297 (test-assert "synopsis: too long"
298 (->bool
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")))
304
305 (test-assert "synopsis: start with package name"
306 (->bool
307 (string-contains (with-warnings
308 (let ((pkg (dummy-package "x"
309 (name "foo")
310 (synopsis "foo, a nice package"))))
311 (check-synopsis-style pkg)))
312 "synopsis should not start with the package name")))
313
314 (test-assert "synopsis: start with package name prefix"
315 (string-null?
316 (with-warnings
317 (let ((pkg (dummy-package "arb"
318 (synopsis "Arbitrary precision"))))
319 (check-synopsis-style pkg)))))
320
321 (test-assert "synopsis: start with abbreviation"
322 (string-null?
323 (with-warnings
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)))))
329
330 (test-assert "inputs: pkg-config is probably a native input"
331 (->bool
332 (string-contains
333 (with-warnings
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")))
338
339 (test-assert "inputs: glib:bin is probably a native input"
340 (->bool
341 (string-contains
342 (with-warnings
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")))
347
348 (test-assert "patches: file names"
349 (->bool
350 (string-contains
351 (with-warnings
352 (let ((pkg (dummy-package "x"
353 (source
354 (dummy-origin
355 (patches (list "/path/to/y.patch")))))))
356 (check-patch-file-names pkg)))
357 "file names of patches should start with the package name")))
358
359 (test-assert "patches: not found"
360 (->bool
361 (string-contains
362 (with-warnings
363 (let ((pkg (dummy-package "x"
364 (source
365 (dummy-origin
366 (patches
367 (list (search-patch "this-patch-does-not-exist!"))))))))
368 (check-patch-file-names pkg)))
369 "patch not found")))
370
371 (test-assert "derivation: invalid arguments"
372 (->bool
373 (string-contains
374 (with-warnings
375 (let ((pkg (dummy-package "x"
376 (arguments
377 '(#:imported-modules (invalid-module))))))
378 (check-derivation pkg)))
379 "failed to create derivation")))
380
381 (test-assert "license: invalid license"
382 (string-contains
383 (with-warnings
384 (check-license (dummy-package "x" (license #f))))
385 "invalid license"))
386
387 (test-assert "home-page: wrong home-page"
388 (->bool
389 (string-contains
390 (with-warnings
391 (let ((pkg (package
392 (inherit (dummy-package "x"))
393 (home-page #f))))
394 (check-home-page pkg)))
395 "invalid")))
396
397 (test-assert "home-page: invalid URI"
398 (->bool
399 (string-contains
400 (with-warnings
401 (let ((pkg (package
402 (inherit (dummy-package "x"))
403 (home-page "foobar"))))
404 (check-home-page pkg)))
405 "invalid home page URL")))
406
407 (test-assert "home-page: host not found"
408 (->bool
409 (string-contains
410 (with-warnings
411 (let ((pkg (package
412 (inherit (dummy-package "x"))
413 (home-page "http://does-not-exist"))))
414 (check-home-page pkg)))
415 "domain not found")))
416
417 (test-skip (if %http-server-socket 0 1))
418 (test-assert "home-page: Connection refused"
419 (->bool
420 (string-contains
421 (with-warnings
422 (let ((pkg (package
423 (inherit (dummy-package "x"))
424 (home-page %local-url))))
425 (check-home-page pkg)))
426 "Connection refused")))
427
428 (test-skip (if %http-server-socket 0 1))
429 (test-equal "home-page: 200"
430 ""
431 (with-warnings
432 (with-http-server 200 %long-string
433 (let ((pkg (package
434 (inherit (dummy-package "x"))
435 (home-page %local-url))))
436 (check-home-page pkg)))))
437
438 (test-skip (if %http-server-socket 0 1))
439 (test-assert "home-page: 200 but short length"
440 (->bool
441 (string-contains
442 (with-warnings
443 (with-http-server 200 "This is too small."
444 (let ((pkg (package
445 (inherit (dummy-package "x"))
446 (home-page %local-url))))
447 (check-home-page pkg))))
448 "suspiciously small")))
449
450 (test-skip (if %http-server-socket 0 1))
451 (test-assert "home-page: 404"
452 (->bool
453 (string-contains
454 (with-warnings
455 (with-http-server 404 %long-string
456 (let ((pkg (package
457 (inherit (dummy-package "x"))
458 (home-page %local-url))))
459 (check-home-page pkg))))
460 "not reachable: 404")))
461
462 (test-assert "source-file-name"
463 (->bool
464 (string-contains
465 (with-warnings
466 (let ((pkg (dummy-package "x"
467 (version "3.2.1")
468 (source
469 (origin
470 (method url-fetch)
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")))
475
476 (test-assert "source-file-name: v prefix"
477 (->bool
478 (string-contains
479 (with-warnings
480 (let ((pkg (dummy-package "x"
481 (version "3.2.1")
482 (source
483 (origin
484 (method url-fetch)
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")))
489
490 (test-assert "source-file-name: bad checkout"
491 (->bool
492 (string-contains
493 (with-warnings
494 (let ((pkg (dummy-package "x"
495 (version "3.2.1")
496 (source
497 (origin
498 (method git-fetch)
499 (uri (git-reference
500 (url "http://www.example.com/x.git")
501 (commit "0")))
502 (sha256 %null-sha256))))))
503 (check-source-file-name pkg)))
504 "file name should contain the package name")))
505
506 (test-assert "source-file-name: good checkout"
507 (not
508 (->bool
509 (string-contains
510 (with-warnings
511 (let ((pkg (dummy-package "x"
512 (version "3.2.1")
513 (source
514 (origin
515 (method git-fetch)
516 (uri (git-reference
517 (url "http://git.example.com/x.git")
518 (commit "0")))
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"))))
523
524 (test-assert "source-file-name: valid"
525 (not
526 (->bool
527 (string-contains
528 (with-warnings
529 (let ((pkg (dummy-package "x"
530 (version "3.2.1")
531 (source
532 (origin
533 (method url-fetch)
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"))))
538
539 (test-skip (if %http-server-socket 0 1))
540 (test-equal "source: 200"
541 ""
542 (with-warnings
543 (with-http-server 200 %long-string
544 (let ((pkg (package
545 (inherit (dummy-package "x"))
546 (source (origin
547 (method url-fetch)
548 (uri %local-url)
549 (sha256 %null-sha256))))))
550 (check-source pkg)))))
551
552 (test-skip (if %http-server-socket 0 1))
553 (test-assert "source: 200 but short length"
554 (->bool
555 (string-contains
556 (with-warnings
557 (with-http-server 200 "This is too small."
558 (let ((pkg (package
559 (inherit (dummy-package "x"))
560 (source (origin
561 (method url-fetch)
562 (uri %local-url)
563 (sha256 %null-sha256))))))
564 (check-source pkg))))
565 "suspiciously small")))
566
567 (test-skip (if %http-server-socket 0 1))
568 (test-assert "source: 404"
569 (->bool
570 (string-contains
571 (with-warnings
572 (with-http-server 404 %long-string
573 (let ((pkg (package
574 (inherit (dummy-package "x"))
575 (source (origin
576 (method url-fetch)
577 (uri %local-url)
578 (sha256 %null-sha256))))))
579 (check-source pkg))))
580 "not reachable: 404")))
581
582 (test-assert "cve"
583 (mock ((guix scripts lint) package-vulnerabilities (const '()))
584 (string-null?
585 (with-warnings (check-vulnerabilities (dummy-package "x"))))))
586
587 (test-assert "cve: one vulnerability"
588 (mock ((guix scripts lint) package-vulnerabilities
589 (lambda (package)
590 (list (make-struct (@@ (guix cve) <vulnerability>) 0
591 "CVE-2015-1234"
592 (list (cons (package-name package)
593 (package-version package)))))))
594 (string-contains
595 (with-warnings
596 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))
597 "vulnerable to CVE-2015-1234")))
598
599 (test-assert "cve: one patched vulnerability"
600 (mock ((guix scripts lint) package-vulnerabilities
601 (lambda (package)
602 (list (make-struct (@@ (guix cve) <vulnerability>) 0
603 "CVE-2015-1234"
604 (list (cons (package-name package)
605 (package-version package)))))))
606 (string-null?
607 (with-warnings
608 (check-vulnerabilities
609 (dummy-package "pi"
610 (version "3.14")
611 (source
612 (dummy-origin
613 (patches
614 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
615
616 (test-assert "cve: patched vulnerability in replacement"
617 (mock ((guix scripts lint) package-vulnerabilities
618 (lambda (package)
619 (list (make-struct (@@ (guix cve) <vulnerability>) 0
620 "CVE-2015-1234"
621 (list (cons (package-name package)
622 (package-version package)))))))
623 (string-null?
624 (with-warnings
625 (check-vulnerabilities
626 (dummy-package
627 "pi" (version "3.14") (source (dummy-origin))
628 (replacement (dummy-package
629 "pi" (version "3.14")
630 (source
631 (dummy-origin
632 (patches
633 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))))
634
635 (test-assert "formatting: lonely parentheses"
636 (string-contains
637 (with-warnings
638 (check-formatting
639 (
640 dummy-package "ugly as hell!"
641 )
642 ))
643 "lonely"))
644
645 (test-assert "formatting: tabulation"
646 (string-contains
647 (with-warnings
648 (check-formatting (dummy-package "leave the tab here: ")))
649 "tabulation"))
650
651 (test-assert "formatting: trailing white space"
652 (string-contains
653 (with-warnings
654 ;; Leave the trailing white space on the next line!
655 (check-formatting (dummy-package "x")))
656 "trailing white space"))
657
658 (test-assert "formatting: long line"
659 (string-contains
660 (with-warnings
661 (check-formatting
662 (dummy-package "x" ;here is a stupid comment just to make a long line
663 )))
664 "too long"))
665
666 (test-assert "formatting: alright"
667 (string-null?
668 (with-warnings
669 (check-formatting (dummy-package "x")))))
670
671 (test-end "lint")
672
673 ;; Local Variables:
674 ;; eval: (put 'with-http-server 'scheme-indent-function 2)
675 ;; eval: (put 'with-warnings 'scheme-indent-function 0)
676 ;; End: