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