lint: Report lonely parentheses.
[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 Ludovic Courtès <ludo@gnu.org>
5 ;;; Copyright © 2015 Mathieu Lirzin <mthl@openmailbox.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 (define-module (test-lint)
23 #:use-module (guix tests)
24 #:use-module (guix download)
25 #:use-module (guix git-download)
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)
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)
37 #:use-module (srfi srfi-64))
38
39 ;; Test the linter.
40
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
50 (define %null-sha256
51 ;; SHA256 of the empty string.
52 (base32
53 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
54
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
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
95 (define-server-impl stub-http-server
96 ;; Stripped-down version of Guile's built-in HTTP server.
97 http-open
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
104 requests."
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
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))))
122
123 (define-syntax-rule (with-http-server code body ...)
124 (call-with-http-server code (lambda () body ...)))
125
126 \f
127 (test-begin "lint")
128
129 (define (call-with-warnings thunk)
130 (let ((port (open-output-string)))
131 (parameterize ((guix-warning-port port))
132 (thunk))
133 (get-output-string port)))
134
135 (define-syntax-rule (with-warnings body ...)
136 (call-with-warnings (lambda () body ...)))
137
138 (test-assert "description: not empty"
139 (->bool
140 (string-contains (with-warnings
141 (let ((pkg (dummy-package "x"
142 (description ""))))
143 (check-description-style pkg)))
144 "description should not be empty")))
145
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
153 (test-assert "description: does not start with an upper-case letter"
154 (->bool
155 (string-contains (with-warnings
156 (let ((pkg (dummy-package "x"
157 (description "bad description."))))
158 (check-description-style pkg)))
159 "description should start with an upper-case letter")))
160
161 (test-assert "description: may start with a digit"
162 (string-null?
163 (with-warnings
164 (let ((pkg (dummy-package "x"
165 (description "2-component library."))))
166 (check-description-style pkg)))))
167
168 (test-assert "description: may start with lower-case package name"
169 (string-null?
170 (with-warnings
171 (let ((pkg (dummy-package "x"
172 (description "x is a dummy package."))))
173 (check-description-style pkg)))))
174
175 (test-assert "description: two spaces after end of sentence"
176 (->bool
177 (string-contains (with-warnings
178 (let ((pkg (dummy-package "x"
179 (description "Bad. Quite bad."))))
180 (check-description-style pkg)))
181 "sentences in description should be followed by two spaces")))
182
183 (test-assert "description: end-of-sentence detection with abbreviations"
184 (string-null?
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)))))
190
191 (test-assert "synopsis: not empty"
192 (->bool
193 (string-contains (with-warnings
194 (let ((pkg (dummy-package "x"
195 (synopsis ""))))
196 (check-synopsis-style pkg)))
197 "synopsis should not be empty")))
198
199 (test-assert "synopsis: does not start with an upper-case letter"
200 (->bool
201 (string-contains (with-warnings
202 (let ((pkg (dummy-package "x"
203 (synopsis "bad synopsis."))))
204 (check-synopsis-style pkg)))
205 "synopsis should start with an upper-case letter")))
206
207 (test-assert "synopsis: may start with a digit"
208 (string-null?
209 (with-warnings
210 (let ((pkg (dummy-package "x"
211 (synopsis "5-dimensional frobnicator"))))
212 (check-synopsis-style pkg)))))
213
214 (test-assert "synopsis: ends with a period"
215 (->bool
216 (string-contains (with-warnings
217 (let ((pkg (dummy-package "x"
218 (synopsis "Bad synopsis."))))
219 (check-synopsis-style pkg)))
220 "no period allowed at the end of the synopsis")))
221
222 (test-assert "synopsis: ends with 'etc.'"
223 (string-null? (with-warnings
224 (let ((pkg (dummy-package "x"
225 (synopsis "Foo, bar, etc."))))
226 (check-synopsis-style pkg)))))
227
228 (test-assert "synopsis: starts with 'A'"
229 (->bool
230 (string-contains (with-warnings
231 (let ((pkg (dummy-package "x"
232 (synopsis "A bad synopŝis"))))
233 (check-synopsis-style pkg)))
234 "no article allowed at the beginning of the synopsis")))
235
236 (test-assert "synopsis: starts with 'An'"
237 (->bool
238 (string-contains (with-warnings
239 (let ((pkg (dummy-package "x"
240 (synopsis "An awful synopsis"))))
241 (check-synopsis-style pkg)))
242 "no article allowed at the beginning of the synopsis")))
243
244 (test-assert "synopsis: starts with 'a'"
245 (->bool
246 (string-contains (with-warnings
247 (let ((pkg (dummy-package "x"
248 (synopsis "a bad synopsis"))))
249 (check-synopsis-style pkg)))
250 "no article allowed at the beginning of the synopsis")))
251
252 (test-assert "synopsis: starts with 'an'"
253 (->bool
254 (string-contains (with-warnings
255 (let ((pkg (dummy-package "x"
256 (synopsis "an awful synopsis"))))
257 (check-synopsis-style pkg)))
258 "no article allowed at the beginning of the synopsis")))
259
260 (test-assert "synopsis: too long"
261 (->bool
262 (string-contains (with-warnings
263 (let ((pkg (dummy-package "x"
264 (synopsis (make-string 80 #\x)))))
265 (check-synopsis-style pkg)))
266 "synopsis should be less than 80 characters long")))
267
268 (test-assert "synopsis: start with package name"
269 (->bool
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)))
275 "synopsis should not start with the package name")))
276
277 (test-assert "synopsis: start with package name prefix"
278 (string-null?
279 (with-warnings
280 (let ((pkg (dummy-package "arb"
281 (synopsis "Arbitrary precision"))))
282 (check-synopsis-style pkg)))))
283
284 (test-assert "synopsis: start with abbreviation"
285 (string-null?
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)))))
292
293 (test-assert "inputs: pkg-config is probably a native input"
294 (->bool
295 (string-contains
296 (with-warnings
297 (let ((pkg (dummy-package "x"
298 (inputs `(("pkg-config" ,pkg-config))))))
299 (check-inputs-should-be-native pkg)))
300 "pkg-config should probably be a native input")))
301
302 (test-assert "patches: file names"
303 (->bool
304 (string-contains
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")))))))
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 (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
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
341 (test-assert "license: invalid license"
342 (string-contains
343 (with-warnings
344 (check-license (dummy-package "x" (license #f))))
345 "invalid license"))
346
347 (test-assert "home-page: wrong home-page"
348 (->bool
349 (string-contains
350 (with-warnings
351 (let ((pkg (package
352 (inherit (dummy-package "x"))
353 (home-page #f))))
354 (check-home-page pkg)))
355 "invalid")))
356
357 (test-assert "home-page: invalid URI"
358 (->bool
359 (string-contains
360 (with-warnings
361 (let ((pkg (package
362 (inherit (dummy-package "x"))
363 (home-page "foobar"))))
364 (check-home-page pkg)))
365 "invalid home page URL")))
366
367 (test-assert "home-page: host not found"
368 (->bool
369 (string-contains
370 (with-warnings
371 (let ((pkg (package
372 (inherit (dummy-package "x"))
373 (home-page "http://does-not-exist"))))
374 (check-home-page pkg)))
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
381 (with-warnings
382 (let ((pkg (package
383 (inherit (dummy-package "x"))
384 (home-page %local-url))))
385 (check-home-page pkg)))
386 "Connection refused")))
387
388 (test-skip (if %http-server-socket 0 1))
389 (test-equal "home-page: 200"
390 ""
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)))))
397
398 (test-skip (if %http-server-socket 0 1))
399 (test-assert "home-page: 404"
400 (->bool
401 (string-contains
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))))
408 "not reachable: 404")))
409
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
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
515 (test-assert "formatting: lonely parentheses"
516 (string-contains
517 (with-warnings
518 (check-formatting
519 (
520 dummy-package "ugly as hell!"
521 )
522 ))
523 "lonely"))
524
525 (test-assert "formatting: tabulation"
526 (string-contains
527 (with-warnings
528 (check-formatting (dummy-package "leave the tab here: ")))
529 "tabulation"))
530
531 (test-assert "formatting: trailing white space"
532 (string-contains
533 (with-warnings
534 ;; Leave the trailing white space on the next line!
535 (check-formatting (dummy-package "x")))
536 "trailing white space"))
537
538 (test-assert "formatting: long line"
539 (string-contains
540 (with-warnings
541 (check-formatting
542 (dummy-package "x" ;here is a stupid comment just to make a long line
543 )))
544 "too long"))
545
546 (test-assert "formatting: alright"
547 (string-null?
548 (with-warnings
549 (check-formatting (dummy-package "x")))))
550
551 (test-end "lint")
552
553 \f
554 (exit (= (test-runner-fail-count (test-runner-current)) 0))
555
556 ;; Local Variables:
557 ;; eval: (put 'with-http-server 'scheme-indent-function 1)
558 ;; eval: (put 'with-warnings 'scheme-indent-function 0)
559 ;; End: