tests: Move HTTP server to (guix tests http).
[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 tests http)
28 #:use-module (guix download)
29 #:use-module (guix git-download)
30 #:use-module (guix build-system gnu)
31 #:use-module (guix packages)
32 #:use-module (guix scripts lint)
33 #:use-module (guix ui)
34 #:use-module (gnu packages)
35 #:use-module (gnu packages glib)
36 #:use-module (gnu packages pkg-config)
37 #:use-module (ice-9 match)
38 #:use-module (srfi srfi-9 gnu)
39 #:use-module (srfi srfi-64))
40
41 ;; Test the linter.
42
43 ;; Avoid collisions with other tests.
44 (%http-server-port 9999)
45
46 (define %null-sha256
47 ;; SHA256 of the empty string.
48 (base32
49 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
50
51 (define %long-string
52 (make-string 2000 #\a))
53
54 \f
55 (test-begin "lint")
56
57 (define (call-with-warnings thunk)
58 (let ((port (open-output-string)))
59 (parameterize ((guix-warning-port port))
60 (thunk))
61 (get-output-string port)))
62
63 (define-syntax-rule (with-warnings body ...)
64 (call-with-warnings (lambda () body ...)))
65
66 (test-assert "description: not a string"
67 (->bool
68 (string-contains (with-warnings
69 (let ((pkg (dummy-package "x"
70 (description 'foobar))))
71 (check-description-style pkg)))
72 "invalid description")))
73
74 (test-assert "description: not empty"
75 (->bool
76 (string-contains (with-warnings
77 (let ((pkg (dummy-package "x"
78 (description ""))))
79 (check-description-style pkg)))
80 "description should not be empty")))
81
82 (test-assert "description: valid Texinfo markup"
83 (->bool
84 (string-contains
85 (with-warnings
86 (check-description-style (dummy-package "x" (description "f{oo}b@r"))))
87 "Texinfo markup in description is invalid")))
88
89 (test-assert "description: does not start with an upper-case letter"
90 (->bool
91 (string-contains (with-warnings
92 (let ((pkg (dummy-package "x"
93 (description "bad description."))))
94 (check-description-style pkg)))
95 "description should start with an upper-case letter")))
96
97 (test-assert "description: may start with a digit"
98 (string-null?
99 (with-warnings
100 (let ((pkg (dummy-package "x"
101 (description "2-component library."))))
102 (check-description-style pkg)))))
103
104 (test-assert "description: may start with lower-case package name"
105 (string-null?
106 (with-warnings
107 (let ((pkg (dummy-package "x"
108 (description "x is a dummy package."))))
109 (check-description-style pkg)))))
110
111 (test-assert "description: two spaces after end of sentence"
112 (->bool
113 (string-contains (with-warnings
114 (let ((pkg (dummy-package "x"
115 (description "Bad. Quite bad."))))
116 (check-description-style pkg)))
117 "sentences in description should be followed by two spaces")))
118
119 (test-assert "description: end-of-sentence detection with abbreviations"
120 (string-null?
121 (with-warnings
122 (let ((pkg (dummy-package "x"
123 (description
124 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
125 (check-description-style pkg)))))
126
127 (test-assert "description: may not contain trademark signs"
128 (and (->bool
129 (string-contains (with-warnings
130 (let ((pkg (dummy-package "x"
131 (description "Does The Right Thing™"))))
132 (check-description-style pkg)))
133 "should not contain trademark sign"))
134 (->bool
135 (string-contains (with-warnings
136 (let ((pkg (dummy-package "x"
137 (description "Works with Format®"))))
138 (check-description-style pkg)))
139 "should not contain trademark sign"))))
140
141 (test-assert "description: suggest ornament instead of quotes"
142 (->bool
143 (string-contains (with-warnings
144 (let ((pkg (dummy-package "x"
145 (description "This is a 'quoted' thing."))))
146 (check-description-style pkg)))
147 "use @code")))
148
149 (test-assert "synopsis: not a string"
150 (->bool
151 (string-contains (with-warnings
152 (let ((pkg (dummy-package "x"
153 (synopsis #f))))
154 (check-synopsis-style pkg)))
155 "invalid synopsis")))
156
157 (test-assert "synopsis: not empty"
158 (->bool
159 (string-contains (with-warnings
160 (let ((pkg (dummy-package "x"
161 (synopsis ""))))
162 (check-synopsis-style pkg)))
163 "synopsis should not be empty")))
164
165 (test-assert "synopsis: does not start with an upper-case letter"
166 (->bool
167 (string-contains (with-warnings
168 (let ((pkg (dummy-package "x"
169 (synopsis "bad synopsis."))))
170 (check-synopsis-style pkg)))
171 "synopsis should start with an upper-case letter")))
172
173 (test-assert "synopsis: may start with a digit"
174 (string-null?
175 (with-warnings
176 (let ((pkg (dummy-package "x"
177 (synopsis "5-dimensional frobnicator"))))
178 (check-synopsis-style pkg)))))
179
180 (test-assert "synopsis: ends with a period"
181 (->bool
182 (string-contains (with-warnings
183 (let ((pkg (dummy-package "x"
184 (synopsis "Bad synopsis."))))
185 (check-synopsis-style pkg)))
186 "no period allowed at the end of the synopsis")))
187
188 (test-assert "synopsis: ends with 'etc.'"
189 (string-null? (with-warnings
190 (let ((pkg (dummy-package "x"
191 (synopsis "Foo, bar, etc."))))
192 (check-synopsis-style pkg)))))
193
194 (test-assert "synopsis: starts with 'A'"
195 (->bool
196 (string-contains (with-warnings
197 (let ((pkg (dummy-package "x"
198 (synopsis "A bad synopŝis"))))
199 (check-synopsis-style pkg)))
200 "no article allowed at the beginning of the synopsis")))
201
202 (test-assert "synopsis: starts with 'An'"
203 (->bool
204 (string-contains (with-warnings
205 (let ((pkg (dummy-package "x"
206 (synopsis "An awful synopsis"))))
207 (check-synopsis-style pkg)))
208 "no article allowed at the beginning of the synopsis")))
209
210 (test-assert "synopsis: starts with 'a'"
211 (->bool
212 (string-contains (with-warnings
213 (let ((pkg (dummy-package "x"
214 (synopsis "a bad synopsis"))))
215 (check-synopsis-style pkg)))
216 "no article allowed at the beginning of the synopsis")))
217
218 (test-assert "synopsis: starts with 'an'"
219 (->bool
220 (string-contains (with-warnings
221 (let ((pkg (dummy-package "x"
222 (synopsis "an awful synopsis"))))
223 (check-synopsis-style pkg)))
224 "no article allowed at the beginning of the synopsis")))
225
226 (test-assert "synopsis: too long"
227 (->bool
228 (string-contains (with-warnings
229 (let ((pkg (dummy-package "x"
230 (synopsis (make-string 80 #\x)))))
231 (check-synopsis-style pkg)))
232 "synopsis should be less than 80 characters long")))
233
234 (test-assert "synopsis: start with package name"
235 (->bool
236 (string-contains (with-warnings
237 (let ((pkg (dummy-package "x"
238 (name "foo")
239 (synopsis "foo, a nice package"))))
240 (check-synopsis-style pkg)))
241 "synopsis should not start with the package name")))
242
243 (test-assert "synopsis: start with package name prefix"
244 (string-null?
245 (with-warnings
246 (let ((pkg (dummy-package "arb"
247 (synopsis "Arbitrary precision"))))
248 (check-synopsis-style pkg)))))
249
250 (test-assert "synopsis: start with abbreviation"
251 (string-null?
252 (with-warnings
253 (let ((pkg (dummy-package "uucp"
254 ;; Same problem with "APL interpreter", etc.
255 (synopsis "UUCP implementation")
256 (description "Imagine this is Taylor UUCP."))))
257 (check-synopsis-style pkg)))))
258
259 (test-assert "inputs: pkg-config is probably a native input"
260 (->bool
261 (string-contains
262 (with-warnings
263 (let ((pkg (dummy-package "x"
264 (inputs `(("pkg-config" ,pkg-config))))))
265 (check-inputs-should-be-native pkg)))
266 "'pkg-config' should probably be a native input")))
267
268 (test-assert "inputs: glib:bin is probably a native input"
269 (->bool
270 (string-contains
271 (with-warnings
272 (let ((pkg (dummy-package "x"
273 (inputs `(("glib" ,glib "bin"))))))
274 (check-inputs-should-be-native pkg)))
275 "'glib:bin' should probably be a native input")))
276
277 (test-assert "patches: file names"
278 (->bool
279 (string-contains
280 (with-warnings
281 (let ((pkg (dummy-package "x"
282 (source
283 (dummy-origin
284 (patches (list "/path/to/y.patch")))))))
285 (check-patch-file-names pkg)))
286 "file names of patches should start with the package name")))
287
288 (test-assert "patches: not found"
289 (->bool
290 (string-contains
291 (with-warnings
292 (let ((pkg (dummy-package "x"
293 (source
294 (dummy-origin
295 (patches
296 (list (search-patch "this-patch-does-not-exist!"))))))))
297 (check-patch-file-names pkg)))
298 "patch not found")))
299
300 (test-assert "derivation: invalid arguments"
301 (->bool
302 (string-contains
303 (with-warnings
304 (let ((pkg (dummy-package "x"
305 (arguments
306 '(#:imported-modules (invalid-module))))))
307 (check-derivation pkg)))
308 "failed to create derivation")))
309
310 (test-assert "license: invalid license"
311 (string-contains
312 (with-warnings
313 (check-license (dummy-package "x" (license #f))))
314 "invalid license"))
315
316 (test-assert "home-page: wrong home-page"
317 (->bool
318 (string-contains
319 (with-warnings
320 (let ((pkg (package
321 (inherit (dummy-package "x"))
322 (home-page #f))))
323 (check-home-page pkg)))
324 "invalid")))
325
326 (test-assert "home-page: invalid URI"
327 (->bool
328 (string-contains
329 (with-warnings
330 (let ((pkg (package
331 (inherit (dummy-package "x"))
332 (home-page "foobar"))))
333 (check-home-page pkg)))
334 "invalid home page URL")))
335
336 (test-assert "home-page: host not found"
337 (->bool
338 (string-contains
339 (with-warnings
340 (let ((pkg (package
341 (inherit (dummy-package "x"))
342 (home-page "http://does-not-exist"))))
343 (check-home-page pkg)))
344 "domain not found")))
345
346 (test-skip (if (force %http-server-socket) 0 1))
347 (test-assert "home-page: Connection refused"
348 (->bool
349 (string-contains
350 (with-warnings
351 (let ((pkg (package
352 (inherit (dummy-package "x"))
353 (home-page (%local-url)))))
354 (check-home-page pkg)))
355 "Connection refused")))
356
357 (test-skip (if (force %http-server-socket) 0 1))
358 (test-equal "home-page: 200"
359 ""
360 (with-warnings
361 (with-http-server 200 %long-string
362 (let ((pkg (package
363 (inherit (dummy-package "x"))
364 (home-page (%local-url)))))
365 (check-home-page pkg)))))
366
367 (test-skip (if (force %http-server-socket) 0 1))
368 (test-assert "home-page: 200 but short length"
369 (->bool
370 (string-contains
371 (with-warnings
372 (with-http-server 200 "This is too small."
373 (let ((pkg (package
374 (inherit (dummy-package "x"))
375 (home-page (%local-url)))))
376 (check-home-page pkg))))
377 "suspiciously small")))
378
379 (test-skip (if (force %http-server-socket) 0 1))
380 (test-assert "home-page: 404"
381 (->bool
382 (string-contains
383 (with-warnings
384 (with-http-server 404 %long-string
385 (let ((pkg (package
386 (inherit (dummy-package "x"))
387 (home-page (%local-url)))))
388 (check-home-page pkg))))
389 "not reachable: 404")))
390
391 (test-assert "source-file-name"
392 (->bool
393 (string-contains
394 (with-warnings
395 (let ((pkg (dummy-package "x"
396 (version "3.2.1")
397 (source
398 (origin
399 (method url-fetch)
400 (uri "http://www.example.com/3.2.1.tar.gz")
401 (sha256 %null-sha256))))))
402 (check-source-file-name pkg)))
403 "file name should contain the package name")))
404
405 (test-assert "source-file-name: v prefix"
406 (->bool
407 (string-contains
408 (with-warnings
409 (let ((pkg (dummy-package "x"
410 (version "3.2.1")
411 (source
412 (origin
413 (method url-fetch)
414 (uri "http://www.example.com/v3.2.1.tar.gz")
415 (sha256 %null-sha256))))))
416 (check-source-file-name pkg)))
417 "file name should contain the package name")))
418
419 (test-assert "source-file-name: bad checkout"
420 (->bool
421 (string-contains
422 (with-warnings
423 (let ((pkg (dummy-package "x"
424 (version "3.2.1")
425 (source
426 (origin
427 (method git-fetch)
428 (uri (git-reference
429 (url "http://www.example.com/x.git")
430 (commit "0")))
431 (sha256 %null-sha256))))))
432 (check-source-file-name pkg)))
433 "file name should contain the package name")))
434
435 (test-assert "source-file-name: good checkout"
436 (not
437 (->bool
438 (string-contains
439 (with-warnings
440 (let ((pkg (dummy-package "x"
441 (version "3.2.1")
442 (source
443 (origin
444 (method git-fetch)
445 (uri (git-reference
446 (url "http://git.example.com/x.git")
447 (commit "0")))
448 (file-name (string-append "x-" version))
449 (sha256 %null-sha256))))))
450 (check-source-file-name pkg)))
451 "file name should contain the package name"))))
452
453 (test-assert "source-file-name: valid"
454 (not
455 (->bool
456 (string-contains
457 (with-warnings
458 (let ((pkg (dummy-package "x"
459 (version "3.2.1")
460 (source
461 (origin
462 (method url-fetch)
463 (uri "http://www.example.com/x-3.2.1.tar.gz")
464 (sha256 %null-sha256))))))
465 (check-source-file-name pkg)))
466 "file name should contain the package name"))))
467
468 (test-skip (if (force %http-server-socket) 0 1))
469 (test-equal "source: 200"
470 ""
471 (with-warnings
472 (with-http-server 200 %long-string
473 (let ((pkg (package
474 (inherit (dummy-package "x"))
475 (source (origin
476 (method url-fetch)
477 (uri (%local-url))
478 (sha256 %null-sha256))))))
479 (check-source pkg)))))
480
481 (test-skip (if (force %http-server-socket) 0 1))
482 (test-assert "source: 200 but short length"
483 (->bool
484 (string-contains
485 (with-warnings
486 (with-http-server 200 "This is too small."
487 (let ((pkg (package
488 (inherit (dummy-package "x"))
489 (source (origin
490 (method url-fetch)
491 (uri (%local-url))
492 (sha256 %null-sha256))))))
493 (check-source pkg))))
494 "suspiciously small")))
495
496 (test-skip (if (force %http-server-socket) 0 1))
497 (test-assert "source: 404"
498 (->bool
499 (string-contains
500 (with-warnings
501 (with-http-server 404 %long-string
502 (let ((pkg (package
503 (inherit (dummy-package "x"))
504 (source (origin
505 (method url-fetch)
506 (uri (%local-url))
507 (sha256 %null-sha256))))))
508 (check-source pkg))))
509 "not reachable: 404")))
510
511 (test-assert "cve"
512 (mock ((guix scripts lint) package-vulnerabilities (const '()))
513 (string-null?
514 (with-warnings (check-vulnerabilities (dummy-package "x"))))))
515
516 (test-assert "cve: one vulnerability"
517 (mock ((guix scripts lint) package-vulnerabilities
518 (lambda (package)
519 (list (make-struct (@@ (guix cve) <vulnerability>) 0
520 "CVE-2015-1234"
521 (list (cons (package-name package)
522 (package-version package)))))))
523 (string-contains
524 (with-warnings
525 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))
526 "vulnerable to CVE-2015-1234")))
527
528 (test-assert "cve: one patched vulnerability"
529 (mock ((guix scripts lint) package-vulnerabilities
530 (lambda (package)
531 (list (make-struct (@@ (guix cve) <vulnerability>) 0
532 "CVE-2015-1234"
533 (list (cons (package-name package)
534 (package-version package)))))))
535 (string-null?
536 (with-warnings
537 (check-vulnerabilities
538 (dummy-package "pi"
539 (version "3.14")
540 (source
541 (dummy-origin
542 (patches
543 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
544
545 (test-assert "cve: vulnerability fixed in replacement version"
546 (mock ((guix scripts lint) package-vulnerabilities
547 (lambda (package)
548 (match (package-version package)
549 ("0"
550 (list (make-struct (@@ (guix cve) <vulnerability>) 0
551 "CVE-2015-1234"
552 (list (cons (package-name package)
553 (package-version package))))))
554 ("1"
555 '()))))
556 (and (not (string-null?
557 (with-warnings
558 (check-vulnerabilities
559 (dummy-package "foo" (version "0"))))))
560 (string-null?
561 (with-warnings
562 (check-vulnerabilities
563 (dummy-package
564 "foo" (version "0")
565 (replacement (dummy-package "foo" (version "1"))))))))))
566
567 (test-assert "cve: patched vulnerability in replacement"
568 (mock ((guix scripts lint) package-vulnerabilities
569 (lambda (package)
570 (list (make-struct (@@ (guix cve) <vulnerability>) 0
571 "CVE-2015-1234"
572 (list (cons (package-name package)
573 (package-version package)))))))
574 (string-null?
575 (with-warnings
576 (check-vulnerabilities
577 (dummy-package
578 "pi" (version "3.14") (source (dummy-origin))
579 (replacement (dummy-package
580 "pi" (version "3.14")
581 (source
582 (dummy-origin
583 (patches
584 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))))
585
586 (test-assert "formatting: lonely parentheses"
587 (string-contains
588 (with-warnings
589 (check-formatting
590 (
591 dummy-package "ugly as hell!"
592 )
593 ))
594 "lonely"))
595
596 (test-assert "formatting: tabulation"
597 (string-contains
598 (with-warnings
599 (check-formatting (dummy-package "leave the tab here: ")))
600 "tabulation"))
601
602 (test-assert "formatting: trailing white space"
603 (string-contains
604 (with-warnings
605 ;; Leave the trailing white space on the next line!
606 (check-formatting (dummy-package "x")))
607 "trailing white space"))
608
609 (test-assert "formatting: long line"
610 (string-contains
611 (with-warnings
612 (check-formatting
613 (dummy-package "x" ;here is a stupid comment just to make a long line
614 )))
615 "too long"))
616
617 (test-assert "formatting: alright"
618 (string-null?
619 (with-warnings
620 (check-formatting (dummy-package "x")))))
621
622 (test-end "lint")
623
624 ;; Local Variables:
625 ;; eval: (put 'with-http-server 'scheme-indent-function 2)
626 ;; eval: (put 'with-warnings 'scheme-indent-function 0)
627 ;; End: