lint: 'inputs-should-be-native' checks for intltool, itstool and glib:bin.
[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 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 "synopsis: not a string"
207 (->bool
208 (string-contains (with-warnings
209 (let ((pkg (dummy-package "x"
210 (synopsis #f))))
211 (check-synopsis-style pkg)))
212 "invalid synopsis")))
213
214 (test-assert "synopsis: not empty"
215 (->bool
216 (string-contains (with-warnings
217 (let ((pkg (dummy-package "x"
218 (synopsis ""))))
219 (check-synopsis-style pkg)))
220 "synopsis should not be empty")))
221
222 (test-assert "synopsis: does not start with an upper-case letter"
223 (->bool
224 (string-contains (with-warnings
225 (let ((pkg (dummy-package "x"
226 (synopsis "bad synopsis."))))
227 (check-synopsis-style pkg)))
228 "synopsis should start with an upper-case letter")))
229
230 (test-assert "synopsis: may start with a digit"
231 (string-null?
232 (with-warnings
233 (let ((pkg (dummy-package "x"
234 (synopsis "5-dimensional frobnicator"))))
235 (check-synopsis-style pkg)))))
236
237 (test-assert "synopsis: ends with a period"
238 (->bool
239 (string-contains (with-warnings
240 (let ((pkg (dummy-package "x"
241 (synopsis "Bad synopsis."))))
242 (check-synopsis-style pkg)))
243 "no period allowed at the end of the synopsis")))
244
245 (test-assert "synopsis: ends with 'etc.'"
246 (string-null? (with-warnings
247 (let ((pkg (dummy-package "x"
248 (synopsis "Foo, bar, etc."))))
249 (check-synopsis-style pkg)))))
250
251 (test-assert "synopsis: starts with 'A'"
252 (->bool
253 (string-contains (with-warnings
254 (let ((pkg (dummy-package "x"
255 (synopsis "A bad synopŝis"))))
256 (check-synopsis-style pkg)))
257 "no article allowed at the beginning of the synopsis")))
258
259 (test-assert "synopsis: starts with 'An'"
260 (->bool
261 (string-contains (with-warnings
262 (let ((pkg (dummy-package "x"
263 (synopsis "An awful synopsis"))))
264 (check-synopsis-style pkg)))
265 "no article allowed at the beginning of the synopsis")))
266
267 (test-assert "synopsis: starts with 'a'"
268 (->bool
269 (string-contains (with-warnings
270 (let ((pkg (dummy-package "x"
271 (synopsis "a bad synopsis"))))
272 (check-synopsis-style pkg)))
273 "no article allowed at the beginning of the synopsis")))
274
275 (test-assert "synopsis: starts with 'an'"
276 (->bool
277 (string-contains (with-warnings
278 (let ((pkg (dummy-package "x"
279 (synopsis "an awful synopsis"))))
280 (check-synopsis-style pkg)))
281 "no article allowed at the beginning of the synopsis")))
282
283 (test-assert "synopsis: too long"
284 (->bool
285 (string-contains (with-warnings
286 (let ((pkg (dummy-package "x"
287 (synopsis (make-string 80 #\x)))))
288 (check-synopsis-style pkg)))
289 "synopsis should be less than 80 characters long")))
290
291 (test-assert "synopsis: start with package name"
292 (->bool
293 (string-contains (with-warnings
294 (let ((pkg (dummy-package "x"
295 (name "foo")
296 (synopsis "foo, a nice package"))))
297 (check-synopsis-style pkg)))
298 "synopsis should not start with the package name")))
299
300 (test-assert "synopsis: start with package name prefix"
301 (string-null?
302 (with-warnings
303 (let ((pkg (dummy-package "arb"
304 (synopsis "Arbitrary precision"))))
305 (check-synopsis-style pkg)))))
306
307 (test-assert "synopsis: start with abbreviation"
308 (string-null?
309 (with-warnings
310 (let ((pkg (dummy-package "uucp"
311 ;; Same problem with "APL interpreter", etc.
312 (synopsis "UUCP implementation")
313 (description "Imagine this is Taylor UUCP."))))
314 (check-synopsis-style pkg)))))
315
316 (test-assert "inputs: pkg-config is probably a native input"
317 (->bool
318 (string-contains
319 (with-warnings
320 (let ((pkg (dummy-package "x"
321 (inputs `(("pkg-config" ,pkg-config))))))
322 (check-inputs-should-be-native pkg)))
323 "'pkg-config' should probably be a native input")))
324
325 (test-assert "inputs: glib:bin is probably a native input"
326 (->bool
327 (string-contains
328 (with-warnings
329 (let ((pkg (dummy-package "x"
330 (inputs `(("glib" ,glib "bin"))))))
331 (check-inputs-should-be-native pkg)))
332 "'glib:bin' should probably be a native input")))
333
334 (test-assert "patches: file names"
335 (->bool
336 (string-contains
337 (with-warnings
338 (let ((pkg (dummy-package "x"
339 (source
340 (dummy-origin
341 (patches (list "/path/to/y.patch")))))))
342 (check-patch-file-names pkg)))
343 "file names of patches should start with the package name")))
344
345 (test-assert "patches: not found"
346 (->bool
347 (string-contains
348 (with-warnings
349 (let ((pkg (dummy-package "x"
350 (source
351 (dummy-origin
352 (patches
353 (list (search-patch "this-patch-does-not-exist!"))))))))
354 (check-patch-file-names pkg)))
355 "patch not found")))
356
357 (test-assert "derivation: invalid arguments"
358 (->bool
359 (string-contains
360 (with-warnings
361 (let ((pkg (dummy-package "x"
362 (arguments
363 '(#:imported-modules (invalid-module))))))
364 (check-derivation pkg)))
365 "failed to create derivation")))
366
367 (test-assert "license: invalid license"
368 (string-contains
369 (with-warnings
370 (check-license (dummy-package "x" (license #f))))
371 "invalid license"))
372
373 (test-assert "home-page: wrong home-page"
374 (->bool
375 (string-contains
376 (with-warnings
377 (let ((pkg (package
378 (inherit (dummy-package "x"))
379 (home-page #f))))
380 (check-home-page pkg)))
381 "invalid")))
382
383 (test-assert "home-page: invalid URI"
384 (->bool
385 (string-contains
386 (with-warnings
387 (let ((pkg (package
388 (inherit (dummy-package "x"))
389 (home-page "foobar"))))
390 (check-home-page pkg)))
391 "invalid home page URL")))
392
393 (test-assert "home-page: host not found"
394 (->bool
395 (string-contains
396 (with-warnings
397 (let ((pkg (package
398 (inherit (dummy-package "x"))
399 (home-page "http://does-not-exist"))))
400 (check-home-page pkg)))
401 "domain not found")))
402
403 (test-skip (if %http-server-socket 0 1))
404 (test-assert "home-page: Connection refused"
405 (->bool
406 (string-contains
407 (with-warnings
408 (let ((pkg (package
409 (inherit (dummy-package "x"))
410 (home-page %local-url))))
411 (check-home-page pkg)))
412 "Connection refused")))
413
414 (test-skip (if %http-server-socket 0 1))
415 (test-equal "home-page: 200"
416 ""
417 (with-warnings
418 (with-http-server 200 %long-string
419 (let ((pkg (package
420 (inherit (dummy-package "x"))
421 (home-page %local-url))))
422 (check-home-page pkg)))))
423
424 (test-skip (if %http-server-socket 0 1))
425 (test-assert "home-page: 200 but short length"
426 (->bool
427 (string-contains
428 (with-warnings
429 (with-http-server 200 "This is too small."
430 (let ((pkg (package
431 (inherit (dummy-package "x"))
432 (home-page %local-url))))
433 (check-home-page pkg))))
434 "suspiciously small")))
435
436 (test-skip (if %http-server-socket 0 1))
437 (test-assert "home-page: 404"
438 (->bool
439 (string-contains
440 (with-warnings
441 (with-http-server 404 %long-string
442 (let ((pkg (package
443 (inherit (dummy-package "x"))
444 (home-page %local-url))))
445 (check-home-page pkg))))
446 "not reachable: 404")))
447
448 (test-assert "source-file-name"
449 (->bool
450 (string-contains
451 (with-warnings
452 (let ((pkg (dummy-package "x"
453 (version "3.2.1")
454 (source
455 (origin
456 (method url-fetch)
457 (uri "http://www.example.com/3.2.1.tar.gz")
458 (sha256 %null-sha256))))))
459 (check-source-file-name pkg)))
460 "file name should contain the package name")))
461
462 (test-assert "source-file-name: v prefix"
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/v3.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: bad checkout"
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 git-fetch)
485 (uri (git-reference
486 (url "http://www.example.com/x.git")
487 (commit "0")))
488 (sha256 %null-sha256))))))
489 (check-source-file-name pkg)))
490 "file name should contain the package name")))
491
492 (test-assert "source-file-name: good checkout"
493 (not
494 (->bool
495 (string-contains
496 (with-warnings
497 (let ((pkg (dummy-package "x"
498 (version "3.2.1")
499 (source
500 (origin
501 (method git-fetch)
502 (uri (git-reference
503 (url "http://git.example.com/x.git")
504 (commit "0")))
505 (file-name (string-append "x-" version))
506 (sha256 %null-sha256))))))
507 (check-source-file-name pkg)))
508 "file name should contain the package name"))))
509
510 (test-assert "source-file-name: valid"
511 (not
512 (->bool
513 (string-contains
514 (with-warnings
515 (let ((pkg (dummy-package "x"
516 (version "3.2.1")
517 (source
518 (origin
519 (method url-fetch)
520 (uri "http://www.example.com/x-3.2.1.tar.gz")
521 (sha256 %null-sha256))))))
522 (check-source-file-name pkg)))
523 "file name should contain the package name"))))
524
525 (test-skip (if %http-server-socket 0 1))
526 (test-equal "source: 200"
527 ""
528 (with-warnings
529 (with-http-server 200 %long-string
530 (let ((pkg (package
531 (inherit (dummy-package "x"))
532 (source (origin
533 (method url-fetch)
534 (uri %local-url)
535 (sha256 %null-sha256))))))
536 (check-source pkg)))))
537
538 (test-skip (if %http-server-socket 0 1))
539 (test-assert "source: 200 but short length"
540 (->bool
541 (string-contains
542 (with-warnings
543 (with-http-server 200 "This is too small."
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 "suspiciously small")))
552
553 (test-skip (if %http-server-socket 0 1))
554 (test-assert "source: 404"
555 (->bool
556 (string-contains
557 (with-warnings
558 (with-http-server 404 %long-string
559 (let ((pkg (package
560 (inherit (dummy-package "x"))
561 (source (origin
562 (method url-fetch)
563 (uri %local-url)
564 (sha256 %null-sha256))))))
565 (check-source pkg))))
566 "not reachable: 404")))
567
568 (test-assert "cve"
569 (mock ((guix scripts lint) package-vulnerabilities (const '()))
570 (string-null?
571 (with-warnings (check-vulnerabilities (dummy-package "x"))))))
572
573 (test-assert "cve: one vulnerability"
574 (mock ((guix scripts lint) package-vulnerabilities
575 (lambda (package)
576 (list (make-struct (@@ (guix cve) <vulnerability>) 0
577 "CVE-2015-1234"
578 (list (cons (package-name package)
579 (package-version package)))))))
580 (string-contains
581 (with-warnings
582 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))
583 "vulnerable to CVE-2015-1234")))
584
585 (test-assert "cve: one patched vulnerability"
586 (mock ((guix scripts lint) package-vulnerabilities
587 (lambda (package)
588 (list (make-struct (@@ (guix cve) <vulnerability>) 0
589 "CVE-2015-1234"
590 (list (cons (package-name package)
591 (package-version package)))))))
592 (string-null?
593 (with-warnings
594 (check-vulnerabilities
595 (dummy-package "pi"
596 (version "3.14")
597 (source
598 (dummy-origin
599 (patches
600 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
601
602 (test-assert "cve: patched vulnerability in replacement"
603 (mock ((guix scripts lint) package-vulnerabilities
604 (lambda (package)
605 (list (make-struct (@@ (guix cve) <vulnerability>) 0
606 "CVE-2015-1234"
607 (list (cons (package-name package)
608 (package-version package)))))))
609 (string-null?
610 (with-warnings
611 (check-vulnerabilities
612 (dummy-package
613 "pi" (version "3.14") (source (dummy-origin))
614 (replacement (dummy-package
615 "pi" (version "3.14")
616 (source
617 (dummy-origin
618 (patches
619 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))))
620
621 (test-assert "formatting: lonely parentheses"
622 (string-contains
623 (with-warnings
624 (check-formatting
625 (
626 dummy-package "ugly as hell!"
627 )
628 ))
629 "lonely"))
630
631 (test-assert "formatting: tabulation"
632 (string-contains
633 (with-warnings
634 (check-formatting (dummy-package "leave the tab here: ")))
635 "tabulation"))
636
637 (test-assert "formatting: trailing white space"
638 (string-contains
639 (with-warnings
640 ;; Leave the trailing white space on the next line!
641 (check-formatting (dummy-package "x")))
642 "trailing white space"))
643
644 (test-assert "formatting: long line"
645 (string-contains
646 (with-warnings
647 (check-formatting
648 (dummy-package "x" ;here is a stupid comment just to make a long line
649 )))
650 "too long"))
651
652 (test-assert "formatting: alright"
653 (string-null?
654 (with-warnings
655 (check-formatting (dummy-package "x")))))
656
657 (test-end "lint")
658
659 ;; Local Variables:
660 ;; eval: (put 'with-http-server 'scheme-indent-function 2)
661 ;; eval: (put 'with-warnings 'scheme-indent-function 0)
662 ;; End: