gnu: Add bsnes.
[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, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
5 ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
6 ;;; Copyright © 2016 Hartmut Goebel <h.goebel@crazy-compilers.com>
7 ;;; Copyright © 2017 Alex Kost <alezost@gmail.com>
8 ;;; Copyright © 2017 Efraim Flashner <efraim@flashner.co.il>
9 ;;; Copyright © 2018, 2019 Arun Isaac <arunisaac@systemreboot.net>
10 ;;;
11 ;;; This file is part of GNU Guix.
12 ;;;
13 ;;; GNU Guix is free software; you can redistribute it and/or modify it
14 ;;; under the terms of the GNU General Public License as published by
15 ;;; the Free Software Foundation; either version 3 of the License, or (at
16 ;;; your option) any later version.
17 ;;;
18 ;;; GNU Guix is distributed in the hope that it will be useful, but
19 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;; GNU General Public License for more details.
22 ;;;
23 ;;; You should have received a copy of the GNU General Public License
24 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25
26 ;; Avoid interference.
27 (unsetenv "http_proxy")
28
29 (define-module (test-lint)
30 #:use-module (guix tests)
31 #:use-module (guix tests http)
32 #:use-module (guix download)
33 #:use-module (guix git-download)
34 #:use-module (guix build-system gnu)
35 #:use-module (guix packages)
36 #:use-module (guix lint)
37 #:use-module (guix ui)
38 #:use-module (guix swh)
39 #:use-module (gnu packages)
40 #:use-module (gnu packages glib)
41 #:use-module (gnu packages pkg-config)
42 #:use-module (gnu packages python-xyz)
43 #:use-module (web uri)
44 #:use-module (web server)
45 #:use-module (web server http)
46 #:use-module (web response)
47 #:use-module (ice-9 match)
48 #:use-module (ice-9 regex)
49 #:use-module (ice-9 getopt-long)
50 #:use-module (ice-9 pretty-print)
51 #:use-module (rnrs bytevectors)
52 #:use-module (srfi srfi-1)
53 #:use-module (srfi srfi-9 gnu)
54 #:use-module (srfi srfi-26)
55 #:use-module (srfi srfi-64))
56
57 ;; Test the linter.
58
59 ;; Avoid collisions with other tests.
60 (%http-server-port 9999)
61
62 (define %null-sha256
63 ;; SHA256 of the empty string.
64 (base32
65 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
66
67 (define %long-string
68 (make-string 2000 #\a))
69
70 (define (string-match-or-error pattern str)
71 (or (string-match pattern str)
72 (error str "did not match" pattern)))
73
74 (define single-lint-warning-message
75 (match-lambda
76 (((and (? lint-warning?) warning))
77 (lint-warning-message warning))))
78
79 (define (warning-contains? str warnings)
80 "Return true if WARNINGS is a singleton with a warning that contains STR."
81 (match warnings
82 (((? lint-warning? warning))
83 (string-contains (lint-warning-message warning) str))))
84
85 \f
86 (test-begin "lint")
87
88 (test-equal "description: not a string"
89 "invalid description: foobar"
90 (single-lint-warning-message
91 (check-description-style
92 (dummy-package "x" (description 'foobar)))))
93
94 (test-equal "description: not empty"
95 "description should not be empty"
96 (single-lint-warning-message
97 (check-description-style
98 (dummy-package "x" (description "")))))
99
100 (test-equal "description: invalid Texinfo markup"
101 "Texinfo markup in description is invalid"
102 (single-lint-warning-message
103 (check-description-style
104 (dummy-package "x" (description "f{oo}b@r")))))
105
106 (test-equal "description: does not start with an upper-case letter"
107 "description should start with an upper-case letter or digit"
108 (single-lint-warning-message
109 (let ((pkg (dummy-package "x"
110 (description "bad description."))))
111 (check-description-style pkg))))
112
113 (test-equal "description: may start with a digit"
114 '()
115 (let ((pkg (dummy-package "x"
116 (description "2-component library."))))
117 (check-description-style pkg)))
118
119 (test-equal "description: may start with lower-case package name"
120 '()
121 (let ((pkg (dummy-package "x"
122 (description "x is a dummy package."))))
123 (check-description-style pkg)))
124
125 (test-equal "description: two spaces after end of sentence"
126 "sentences in description should be followed by two spaces; possible infraction at 3"
127 (single-lint-warning-message
128 (let ((pkg (dummy-package "x"
129 (description "Bad. Quite bad."))))
130 (check-description-style pkg))))
131
132 (test-equal "description: end-of-sentence detection with abbreviations"
133 '()
134 (let ((pkg (dummy-package "x"
135 (description
136 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
137 (check-description-style pkg)))
138
139 (test-equal "description: may not contain trademark signs: ™"
140 "description should not contain trademark sign '™' at 20"
141 (single-lint-warning-message
142 (let ((pkg (dummy-package "x"
143 (description "Does The Right Thing™"))))
144 (check-description-style pkg))))
145
146 (test-equal "description: may not contain trademark signs: ®"
147 "description should not contain trademark sign '®' at 17"
148 (single-lint-warning-message
149 (let ((pkg (dummy-package "x"
150 (description "Works with Format®"))))
151 (check-description-style pkg))))
152
153 (test-equal "description: suggest ornament instead of quotes"
154 "use @code or similar ornament instead of quotes"
155 (single-lint-warning-message
156 (let ((pkg (dummy-package "x"
157 (description "This is a 'quoted' thing."))))
158 (check-description-style pkg))))
159
160 (test-equal "synopsis: not a string"
161 "invalid synopsis: #f"
162 (single-lint-warning-message
163 (let ((pkg (dummy-package "x"
164 (synopsis #f))))
165 (check-synopsis-style pkg))))
166
167 (test-equal "synopsis: not empty"
168 "synopsis should not be empty"
169 (single-lint-warning-message
170 (let ((pkg (dummy-package "x"
171 (synopsis ""))))
172 (check-synopsis-style pkg))))
173
174 (test-equal "synopsis: valid Texinfo markup"
175 "Texinfo markup in synopsis is invalid"
176 (single-lint-warning-message
177 (check-synopsis-style
178 (dummy-package "x" (synopsis "Bad $@ texinfo")))))
179
180 (test-equal "synopsis: does not start with an upper-case letter"
181 "synopsis should start with an upper-case letter or digit"
182 (single-lint-warning-message
183 (let ((pkg (dummy-package "x"
184 (synopsis "bad synopsis"))))
185 (check-synopsis-style pkg))))
186
187 (test-equal "synopsis: may start with a digit"
188 '()
189 (let ((pkg (dummy-package "x"
190 (synopsis "5-dimensional frobnicator"))))
191 (check-synopsis-style pkg)))
192
193 (test-equal "synopsis: ends with a period"
194 "no period allowed at the end of the synopsis"
195 (single-lint-warning-message
196 (let ((pkg (dummy-package "x"
197 (synopsis "Bad synopsis."))))
198 (check-synopsis-style pkg))))
199
200 (test-equal "synopsis: ends with 'etc.'"
201 '()
202 (let ((pkg (dummy-package "x"
203 (synopsis "Foo, bar, etc."))))
204 (check-synopsis-style pkg)))
205
206 (test-equal "synopsis: starts with 'A'"
207 "no article allowed at the beginning of the synopsis"
208 (single-lint-warning-message
209 (let ((pkg (dummy-package "x"
210 (synopsis "A bad synopŝis"))))
211 (check-synopsis-style pkg))))
212
213 (test-equal "synopsis: starts with 'An'"
214 "no article allowed at the beginning of the synopsis"
215 (single-lint-warning-message
216 (let ((pkg (dummy-package "x"
217 (synopsis "An awful synopsis"))))
218 (check-synopsis-style pkg))))
219
220 (test-equal "synopsis: starts with 'a'"
221 '("no article allowed at the beginning of the synopsis"
222 "synopsis should start with an upper-case letter or digit")
223 (sort
224 (map
225 lint-warning-message
226 (let ((pkg (dummy-package "x"
227 (synopsis "a bad synopsis"))))
228 (check-synopsis-style pkg)))
229 string<?))
230
231 (test-equal "synopsis: starts with 'an'"
232 '("no article allowed at the beginning of the synopsis"
233 "synopsis should start with an upper-case letter or digit")
234 (sort
235 (map
236 lint-warning-message
237 (let ((pkg (dummy-package "x"
238 (synopsis "an awful synopsis"))))
239 (check-synopsis-style pkg)))
240 string<?))
241
242 (test-equal "synopsis: too long"
243 "synopsis should be less than 80 characters long"
244 (single-lint-warning-message
245 (let ((pkg (dummy-package "x"
246 (synopsis (make-string 80 #\X)))))
247 (check-synopsis-style pkg))))
248
249 (test-equal "synopsis: start with package name"
250 "synopsis should not start with the package name"
251 (single-lint-warning-message
252 (let ((pkg (dummy-package "x"
253 (name "Foo")
254 (synopsis "Foo, a nice package"))))
255 (check-synopsis-style pkg))))
256
257 (test-equal "synopsis: start with package name prefix"
258 '()
259 (let ((pkg (dummy-package "arb"
260 (synopsis "Arbitrary precision"))))
261 (check-synopsis-style pkg)))
262
263 (test-equal "synopsis: start with abbreviation"
264 '()
265 (let ((pkg (dummy-package "uucp"
266 ;; Same problem with "APL interpreter", etc.
267 (synopsis "UUCP implementation")
268 (description "Imagine this is Taylor UUCP."))))
269 (check-synopsis-style pkg)))
270
271 (test-equal "inputs: pkg-config is probably a native input"
272 "'pkg-config' should probably be a native input"
273 (single-lint-warning-message
274 (let ((pkg (dummy-package "x"
275 (inputs `(("pkg-config" ,pkg-config))))))
276 (check-inputs-should-be-native pkg))))
277
278 (test-equal "inputs: glib:bin is probably a native input"
279 "'glib:bin' should probably be a native input"
280 (single-lint-warning-message
281 (let ((pkg (dummy-package "x"
282 (inputs `(("glib" ,glib "bin"))))))
283 (check-inputs-should-be-native pkg))))
284
285 (test-equal
286 "inputs: python-setuptools should not be an input at all (input)"
287 "'python-setuptools' should probably not be an input at all"
288 (single-lint-warning-message
289 (let ((pkg (dummy-package "x"
290 (inputs `(("python-setuptools"
291 ,python-setuptools))))))
292 (check-inputs-should-not-be-an-input-at-all pkg))))
293
294 (test-equal
295 "inputs: python-setuptools should not be an input at all (native-input)"
296 "'python-setuptools' should probably not be an input at all"
297 (single-lint-warning-message
298 (let ((pkg (dummy-package "x"
299 (native-inputs
300 `(("python-setuptools"
301 ,python-setuptools))))))
302 (check-inputs-should-not-be-an-input-at-all pkg))))
303
304 (test-equal
305 "inputs: python-setuptools should not be an input at all (propagated-input)"
306 "'python-setuptools' should probably not be an input at all"
307 (single-lint-warning-message
308 (let ((pkg (dummy-package "x"
309 (propagated-inputs
310 `(("python-setuptools" ,python-setuptools))))))
311 (check-inputs-should-not-be-an-input-at-all pkg))))
312
313 (test-equal "patches: file names"
314 "file names of patches should start with the package name"
315 (single-lint-warning-message
316 (let ((pkg (dummy-package "x"
317 (source
318 (dummy-origin
319 (patches (list "/path/to/y.patch")))))))
320 (check-patch-file-names pkg))))
321
322 (test-equal "patches: file name too long"
323 (string-append "x-"
324 (make-string 100 #\a)
325 ".patch: file name is too long")
326 (single-lint-warning-message
327 (let ((pkg (dummy-package
328 "x"
329 (source
330 (dummy-origin
331 (patches (list (string-append "x-"
332 (make-string 100 #\a)
333 ".patch"))))))))
334 (check-patch-file-names pkg))))
335
336 (test-equal "patches: not found"
337 "this-patch-does-not-exist!: patch not found"
338 (single-lint-warning-message
339 (let ((pkg (dummy-package
340 "x"
341 (source
342 (dummy-origin
343 (patches
344 (list (search-patch "this-patch-does-not-exist!"))))))))
345 (check-patch-file-names pkg))))
346
347 (test-equal "derivation: invalid arguments"
348 "failed to create x86_64-linux derivation: (wrong-type-arg \"map\" \"Wrong type argument: ~S\" (invalid-module) ())"
349 (match (let ((pkg (dummy-package "x"
350 (arguments
351 '(#:imported-modules (invalid-module))))))
352 (check-derivation pkg))
353 (((and (? lint-warning?) first-warning) others ...)
354 (lint-warning-message first-warning))))
355
356 (test-equal "profile-collisions: no warnings"
357 '()
358 (check-profile-collisions (dummy-package "x")))
359
360 (test-equal "profile-collisions: propagated inputs collide"
361 "propagated inputs p0@1 and p0@2 collide"
362 (let* ((p0 (dummy-package "p0" (version "1")))
363 (p0* (dummy-package "p0" (version "2")))
364 (p1 (dummy-package "p1" (propagated-inputs `(("p0" ,p0)))))
365 (p2 (dummy-package "p2" (propagated-inputs `(("p1" ,p1)))))
366 (p3 (dummy-package "p3" (propagated-inputs `(("p0" ,p0*)))))
367 (p4 (dummy-package "p4" (propagated-inputs
368 `(("p2" ,p2) ("p3", p3))))))
369 (single-lint-warning-message
370 (check-profile-collisions p4))))
371
372 (test-assert "profile-collisions: propagated inputs collide, store items"
373 (string-match-or-error
374 "propagated inputs /[[:graph:]]+-p0-1 and /[[:graph:]]+-p0-1 collide"
375 (let* ((p0 (dummy-package "p0" (version "1")))
376 (p0* (dummy-package "p0" (version "1")
377 (inputs `(("x" ,(dummy-package "x"))))))
378 (p1 (dummy-package "p1" (propagated-inputs `(("p0" ,p0)))))
379 (p2 (dummy-package "p2" (propagated-inputs `(("p1" ,p1)))))
380 (p3 (dummy-package "p3" (propagated-inputs `(("p0" ,p0*)))))
381 (p4 (dummy-package "p4" (propagated-inputs
382 `(("p2" ,p2) ("p3", p3))))))
383 (single-lint-warning-message
384 (check-profile-collisions p4)))))
385
386 (test-equal "license: invalid license"
387 "invalid license field"
388 (single-lint-warning-message
389 (check-license (dummy-package "x" (license #f)))))
390
391 (test-equal "home-page: wrong home-page"
392 "invalid value for home page"
393 (let ((pkg (package
394 (inherit (dummy-package "x"))
395 (home-page #f))))
396 (single-lint-warning-message
397 (check-home-page pkg))))
398
399 (test-equal "home-page: invalid URI"
400 "invalid home page URL: \"foobar\""
401 (let ((pkg (package
402 (inherit (dummy-package "x"))
403 (home-page "foobar"))))
404 (single-lint-warning-message
405 (check-home-page pkg))))
406
407 (test-assert "home-page: host not found"
408 (let ((pkg (package
409 (inherit (dummy-package "x"))
410 (home-page "http://does-not-exist"))))
411 (warning-contains? "domain not found" (check-home-page pkg))))
412
413 (test-skip (if (http-server-can-listen?) 0 1))
414 (test-equal "home-page: Connection refused"
415 "URI http://localhost:9999/foo/bar unreachable: Connection refused"
416 (let ((pkg (package
417 (inherit (dummy-package "x"))
418 (home-page (%local-url)))))
419 (single-lint-warning-message
420 (check-home-page pkg))))
421
422 (test-skip (if (http-server-can-listen?) 0 1))
423 (test-equal "home-page: 200"
424 '()
425 (with-http-server `((200 ,%long-string))
426 (let ((pkg (package
427 (inherit (dummy-package "x"))
428 (home-page (%local-url)))))
429 (check-home-page pkg))))
430
431 (test-skip (if (http-server-can-listen?) 0 1))
432 (test-equal "home-page: 200 but short length"
433 "URI http://localhost:9999/foo/bar returned suspiciously small file (18 bytes)"
434 (with-http-server `((200 "This is too small."))
435 (let ((pkg (package
436 (inherit (dummy-package "x"))
437 (home-page (%local-url)))))
438
439 (single-lint-warning-message
440 (check-home-page pkg)))))
441
442 (test-skip (if (http-server-can-listen?) 0 1))
443 (test-equal "home-page: 404"
444 "URI http://localhost:9999/foo/bar not reachable: 404 (\"Such is life\")"
445 (with-http-server `((404 ,%long-string))
446 (let ((pkg (package
447 (inherit (dummy-package "x"))
448 (home-page (%local-url)))))
449 (single-lint-warning-message
450 (check-home-page pkg)))))
451
452 (test-skip (if (http-server-can-listen?) 0 1))
453 (test-equal "home-page: 301, invalid"
454 "invalid permanent redirect from http://localhost:9999/foo/bar"
455 (with-http-server `((301 ,%long-string))
456 (let ((pkg (package
457 (inherit (dummy-package "x"))
458 (home-page (%local-url)))))
459 (single-lint-warning-message
460 (check-home-page pkg)))))
461
462 (test-skip (if (http-server-can-listen?) 0 1))
463 (test-equal "home-page: 301 -> 200"
464 "permanent redirect from http://localhost:10000/foo/bar to http://localhost:9999/foo/bar"
465 (with-http-server `((200 ,%long-string))
466 (let* ((initial-url (%local-url))
467 (redirect (build-response #:code 301
468 #:headers
469 `((location
470 . ,(string->uri initial-url))))))
471 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
472 (with-http-server `((,redirect ""))
473 (let ((pkg (package
474 (inherit (dummy-package "x"))
475 (home-page (%local-url)))))
476 (single-lint-warning-message
477 (check-home-page pkg))))))))
478
479 (test-skip (if (http-server-can-listen?) 0 1))
480 (test-equal "home-page: 301 -> 404"
481 "URI http://localhost:10000/foo/bar not reachable: 404 (\"Such is life\")"
482 (with-http-server '((404 "booh!"))
483 (let* ((initial-url (%local-url))
484 (redirect (build-response #:code 301
485 #:headers
486 `((location
487 . ,(string->uri initial-url))))))
488 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
489 (with-http-server `((,redirect ""))
490 (let ((pkg (package
491 (inherit (dummy-package "x"))
492 (home-page (%local-url)))))
493 (single-lint-warning-message
494 (check-home-page pkg))))))))
495
496
497 (test-equal "source-file-name"
498 "the source file name should contain the package name"
499 (let ((pkg (dummy-package "x"
500 (version "3.2.1")
501 (source
502 (origin
503 (method url-fetch)
504 (uri "http://www.example.com/3.2.1.tar.gz")
505 (sha256 %null-sha256))))))
506 (single-lint-warning-message
507 (check-source-file-name pkg))))
508
509 (test-equal "source-file-name: v prefix"
510 "the source file name should contain the package name"
511 (let ((pkg (dummy-package "x"
512 (version "3.2.1")
513 (source
514 (origin
515 (method url-fetch)
516 (uri "http://www.example.com/v3.2.1.tar.gz")
517 (sha256 %null-sha256))))))
518 (single-lint-warning-message
519 (check-source-file-name pkg))))
520
521 (test-equal "source-file-name: bad checkout"
522 "the source file name should contain the package name"
523 (let ((pkg (dummy-package "x"
524 (version "3.2.1")
525 (source
526 (origin
527 (method git-fetch)
528 (uri (git-reference
529 (url "http://www.example.com/x.git")
530 (commit "0")))
531 (sha256 %null-sha256))))))
532 (single-lint-warning-message
533 (check-source-file-name pkg))))
534
535 (test-equal "source-file-name: good checkout"
536 '()
537 (let ((pkg (dummy-package "x"
538 (version "3.2.1")
539 (source
540 (origin
541 (method git-fetch)
542 (uri (git-reference
543 (url "http://git.example.com/x.git")
544 (commit "0")))
545 (file-name (string-append "x-" version))
546 (sha256 %null-sha256))))))
547 (check-source-file-name pkg)))
548
549 (test-equal "source-file-name: valid"
550 '()
551 (let ((pkg (dummy-package "x"
552 (version "3.2.1")
553 (source
554 (origin
555 (method url-fetch)
556 (uri "http://www.example.com/x-3.2.1.tar.gz")
557 (sha256 %null-sha256))))))
558 (check-source-file-name pkg)))
559
560 (test-equal "source-unstable-tarball"
561 "the source URI should not be an autogenerated tarball"
562 (let ((pkg (dummy-package "x"
563 (source
564 (origin
565 (method url-fetch)
566 (uri "https://github.com/example/example/archive/v0.0.tar.gz")
567 (sha256 %null-sha256))))))
568 (single-lint-warning-message
569 (check-source-unstable-tarball pkg))))
570
571 (test-equal "source-unstable-tarball: source #f"
572 '()
573 (let ((pkg (dummy-package "x"
574 (source #f))))
575 (check-source-unstable-tarball pkg)))
576
577 (test-equal "source-unstable-tarball: valid"
578 '()
579 (let ((pkg (dummy-package "x"
580 (source
581 (origin
582 (method url-fetch)
583 (uri "https://github.com/example/example/releases/download/x-0.0/x-0.0.tar.gz")
584 (sha256 %null-sha256))))))
585 (check-source-unstable-tarball pkg)))
586
587 (test-equal "source-unstable-tarball: package named archive"
588 '()
589 (let ((pkg (dummy-package "x"
590 (source
591 (origin
592 (method url-fetch)
593 (uri "https://github.com/example/archive/releases/download/x-0.0/x-0.0.tar.gz")
594 (sha256 %null-sha256))))))
595 (check-source-unstable-tarball pkg)))
596
597 (test-equal "source-unstable-tarball: not-github"
598 '()
599 (let ((pkg (dummy-package "x"
600 (source
601 (origin
602 (method url-fetch)
603 (uri "https://bitbucket.org/archive/example/download/x-0.0.tar.gz")
604 (sha256 %null-sha256))))))
605 (check-source-unstable-tarball pkg)))
606
607 (test-equal "source-unstable-tarball: git-fetch"
608 '()
609 (let ((pkg (dummy-package "x"
610 (source
611 (origin
612 (method git-fetch)
613 (uri (git-reference
614 (url "https://github.com/archive/example.git")
615 (commit "0")))
616 (sha256 %null-sha256))))))
617 (check-source-unstable-tarball pkg)))
618
619 (test-skip (if (http-server-can-listen?) 0 1))
620 (test-equal "source: 200"
621 '()
622 (with-http-server `((200 ,%long-string))
623 (let ((pkg (package
624 (inherit (dummy-package "x"))
625 (source (origin
626 (method url-fetch)
627 (uri (%local-url))
628 (sha256 %null-sha256))))))
629 (check-source pkg))))
630
631 (test-skip (if (http-server-can-listen?) 0 1))
632 (test-equal "source: 200 but short length"
633 "URI http://localhost:9999/foo/bar returned suspiciously small file (18 bytes)"
634 (with-http-server '((200 "This is too small."))
635 (let ((pkg (package
636 (inherit (dummy-package "x"))
637 (source (origin
638 (method url-fetch)
639 (uri (%local-url))
640 (sha256 %null-sha256))))))
641 (match (check-source pkg)
642 ((first-warning ; All source URIs are unreachable
643 (and (? lint-warning?) second-warning))
644 (lint-warning-message second-warning))))))
645
646 (test-skip (if (http-server-can-listen?) 0 1))
647 (test-equal "source: 404"
648 "URI http://localhost:9999/foo/bar not reachable: 404 (\"Such is life\")"
649 (with-http-server `((404 ,%long-string))
650 (let ((pkg (package
651 (inherit (dummy-package "x"))
652 (source (origin
653 (method url-fetch)
654 (uri (%local-url))
655 (sha256 %null-sha256))))))
656 (match (check-source pkg)
657 ((first-warning ; All source URIs are unreachable
658 (and (? lint-warning?) second-warning))
659 (lint-warning-message second-warning))))))
660
661 (test-skip (if (http-server-can-listen?) 0 1))
662 (test-equal "source: 404 and 200"
663 '()
664 (with-http-server `((404 ,%long-string))
665 (let ((bad-url (%local-url)))
666 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
667 (with-http-server `((200 ,%long-string))
668 (let ((pkg (package
669 (inherit (dummy-package "x"))
670 (source (origin
671 (method url-fetch)
672 (uri (list bad-url (%local-url)))
673 (sha256 %null-sha256))))))
674 ;; Since one of the two URLs is good, this should return the empty
675 ;; list.
676 (check-source pkg)))))))
677
678 (test-skip (if (http-server-can-listen?) 0 1))
679 (test-equal "source: 301 -> 200"
680 "permanent redirect from http://localhost:10000/foo/bar to http://localhost:9999/foo/bar"
681 (with-http-server `((200 ,%long-string))
682 (let* ((initial-url (%local-url))
683 (redirect (build-response #:code 301
684 #:headers
685 `((location
686 . ,(string->uri initial-url))))))
687 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
688 (with-http-server `((,redirect ""))
689 (let ((pkg (package
690 (inherit (dummy-package "x"))
691 (source (origin
692 (method url-fetch)
693 (uri (%local-url))
694 (sha256 %null-sha256))))))
695 (match (check-source pkg)
696 ((first-warning ; All source URIs are unreachable
697 (and (? lint-warning?) second-warning))
698 (lint-warning-message second-warning)))))))))
699
700 (test-skip (if (http-server-can-listen?) 0 1))
701 (test-equal "source: 301 -> 404"
702 "URI http://localhost:10000/foo/bar not reachable: 404 (\"Such is life\")"
703 (with-http-server '((404 "booh!"))
704 (let* ((initial-url (%local-url))
705 (redirect (build-response #:code 301
706 #:headers
707 `((location
708 . ,(string->uri initial-url))))))
709 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
710 (with-http-server `((,redirect ""))
711 (let ((pkg (package
712 (inherit (dummy-package "x"))
713 (source (origin
714 (method url-fetch)
715 (uri (%local-url))
716 (sha256 %null-sha256))))))
717 (match (check-source pkg)
718 ((first-warning ; The first warning says that all URI's are
719 ; unreachable
720 (and (? lint-warning?) second-warning))
721 (lint-warning-message second-warning)))))))))
722
723 (test-equal "mirror-url"
724 '()
725 (let ((source (origin
726 (method url-fetch)
727 (uri "http://example.org/foo/bar.tar.gz")
728 (sha256 %null-sha256))))
729 (check-mirror-url (dummy-package "x" (source source)))))
730
731 (test-equal "mirror-url: one suggestion"
732 "URL should be 'mirror://gnu/foo/foo.tar.gz'"
733 (let ((source (origin
734 (method url-fetch)
735 (uri "http://ftp.gnu.org/pub/gnu/foo/foo.tar.gz")
736 (sha256 %null-sha256))))
737 (single-lint-warning-message
738 (check-mirror-url (dummy-package "x" (source source))))))
739
740 (test-equal "github-url"
741 '()
742 (with-http-server `((200 ,%long-string))
743 (check-github-url
744 (dummy-package "x" (source
745 (origin
746 (method url-fetch)
747 (uri (%local-url))
748 (sha256 %null-sha256)))))))
749
750 (let ((github-url "https://github.com/foo/bar/bar-1.0.tar.gz"))
751 (test-equal "github-url: one suggestion"
752 (string-append
753 "URL should be '" github-url "'")
754 (let ((redirect (build-response #:code 301
755 #:headers
756 `((location
757 . ,(string->uri github-url))))))
758 (with-http-server `((,redirect ""))
759 (let* ((initial-url (%local-url))
760 (redirect (build-response #:code 302
761 #:headers
762 `((location
763 . ,(string->uri initial-url))))))
764 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
765 (with-http-server `((,redirect ""))
766 (single-lint-warning-message
767 (check-github-url
768 (dummy-package "x" (source
769 (origin
770 (method url-fetch)
771 (uri (%local-url))
772 (sha256 %null-sha256))))))))))))
773 (test-equal "github-url: already the correct github url"
774 '()
775 (check-github-url
776 (dummy-package "x" (source
777 (origin
778 (method url-fetch)
779 (uri github-url)
780 (sha256 %null-sha256)))))))
781
782 (test-equal "cve"
783 '()
784 (mock ((guix lint) package-vulnerabilities (const '()))
785 (check-vulnerabilities (dummy-package "x"))))
786
787 (test-equal "cve: one vulnerability"
788 "probably vulnerable to CVE-2015-1234"
789 (let ((dummy-vulnerabilities
790 (lambda (package)
791 (list (make-struct/no-tail
792 (@@ (guix cve) <vulnerability>)
793 "CVE-2015-1234"
794 (list (cons (package-name package)
795 (package-version package))))))))
796 (single-lint-warning-message
797 (check-vulnerabilities (dummy-package "pi" (version "3.14"))
798 dummy-vulnerabilities))))
799
800 (test-equal "cve: one patched vulnerability"
801 '()
802 (mock ((guix lint) package-vulnerabilities
803 (lambda (package)
804 (list (make-struct/no-tail (@@ (guix cve) <vulnerability>)
805 "CVE-2015-1234"
806 (list (cons (package-name package)
807 (package-version package)))))))
808 (check-vulnerabilities
809 (dummy-package "pi"
810 (version "3.14")
811 (source
812 (dummy-origin
813 (patches
814 (list "/a/b/pi-CVE-2015-1234.patch"))))))))
815
816 (test-equal "cve: known safe from vulnerability"
817 '()
818 (mock ((guix lint) package-vulnerabilities
819 (lambda (package)
820 (list (make-struct/no-tail (@@ (guix cve) <vulnerability>)
821 "CVE-2015-1234"
822 (list (cons (package-name package)
823 (package-version package)))))))
824 (check-vulnerabilities
825 (dummy-package "pi"
826 (version "3.14")
827 (properties `((lint-hidden-cve . ("CVE-2015-1234"))))))))
828
829 (test-equal "cve: vulnerability fixed in replacement version"
830 '()
831 (mock ((guix lint) package-vulnerabilities
832 (lambda (package)
833 (match (package-version package)
834 ("0"
835 (list (make-struct/no-tail (@@ (guix cve) <vulnerability>)
836 "CVE-2015-1234"
837 (list (cons (package-name package)
838 (package-version package))))))
839 ("1"
840 '()))))
841 (check-vulnerabilities
842 (dummy-package
843 "foo" (version "0")
844 (replacement (dummy-package "foo" (version "1")))))))
845
846 (test-equal "cve: patched vulnerability in replacement"
847 '()
848 (mock ((guix lint) package-vulnerabilities
849 (lambda (package)
850 (list (make-struct/no-tail (@@ (guix cve) <vulnerability>)
851 "CVE-2015-1234"
852 (list (cons (package-name package)
853 (package-version package)))))))
854 (check-vulnerabilities
855 (dummy-package
856 "pi" (version "3.14") (source (dummy-origin))
857 (replacement (dummy-package
858 "pi" (version "3.14")
859 (source
860 (dummy-origin
861 (patches
862 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
863
864 (test-equal "formatting: lonely parentheses"
865 "parentheses feel lonely, move to the previous or next line"
866 (single-lint-warning-message
867 (check-formatting
868 (dummy-package "ugly as hell!"
869 )
870 )))
871
872 (test-assert "formatting: tabulation"
873 (string-match-or-error
874 "tabulation on line [0-9]+, column [0-9]+"
875 (single-lint-warning-message
876 (check-formatting (dummy-package "leave the tab here: ")))))
877
878 (test-assert "formatting: trailing white space"
879 (string-match-or-error
880 "trailing white space .*"
881 ;; Leave the trailing white space on the next line!
882 (single-lint-warning-message
883 (check-formatting (dummy-package "x")))))
884
885 (test-assert "formatting: long line"
886 (string-match-or-error
887 "line [0-9]+ is way too long \\([0-9]+ characters\\)"
888 (single-lint-warning-message (check-formatting
889 (dummy-package "x")) ;here is a stupid comment just to make a long line
890 )))
891
892 (test-equal "formatting: alright"
893 '()
894 (check-formatting (dummy-package "x")))
895
896 (test-assert "archival: missing content"
897 (let* ((origin (origin
898 (method url-fetch)
899 (uri "http://example.org/foo.tgz")
900 (sha256 (make-bytevector 32))))
901 (warnings (with-http-server '((404 "Not archived."))
902 (parameterize ((%swh-base-url (%local-url)))
903 (check-archival (dummy-package "x"
904 (source origin)))))))
905 (warning-contains? "not archived" warnings)))
906
907 (test-equal "archival: content available"
908 '()
909 (let* ((origin (origin
910 (method url-fetch)
911 (uri "http://example.org/foo.tgz")
912 (sha256 (make-bytevector 32))))
913 ;; https://archive.softwareheritage.org/api/1/content/
914 (content "{ \"checksums\": {}, \"data_url\": \"xyz\",
915 \"length\": 42 }"))
916 (with-http-server `((200 ,content))
917 (parameterize ((%swh-base-url (%local-url)))
918 (check-archival (dummy-package "x" (source origin)))))))
919
920 (test-assert "archival: missing revision"
921 (let* ((origin (origin
922 (method git-fetch)
923 (uri (git-reference
924 (url "http://example.org/foo.git")
925 (commit "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")))
926 (sha256 (make-bytevector 32))))
927 ;; https://archive.softwareheritage.org/api/1/origin/save/
928 (save "{ \"origin_url\": \"http://example.org/foo.git\",
929 \"save_request_date\": \"2014-11-17T22:09:38+01:00\",
930 \"save_request_status\": \"accepted\",
931 \"save_task_status\": \"scheduled\" }")
932 (warnings (with-http-server `((404 "No revision.") ;lookup-revision
933 (404 "No origin.") ;lookup-origin
934 (200 ,save)) ;save-origin
935 (parameterize ((%swh-base-url (%local-url)))
936 (check-archival (dummy-package "x" (source origin)))))))
937 (warning-contains? "scheduled" warnings)))
938
939 (test-equal "archival: revision available"
940 '()
941 (let* ((origin (origin
942 (method git-fetch)
943 (uri (git-reference
944 (url "http://example.org/foo.git")
945 (commit "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")))
946 (sha256 (make-bytevector 32))))
947 ;; https://archive.softwareheritage.org/api/1/revision/
948 (revision "{ \"author\": {}, \"parents\": [],
949 \"date\": \"2014-11-17T22:09:38+01:00\" }"))
950 (with-http-server `((200 ,revision))
951 (parameterize ((%swh-base-url (%local-url)))
952 (check-archival (dummy-package "x" (source origin)))))))
953
954 (test-assert "archival: rate limit reached"
955 ;; We should get a single warning stating that the rate limit was reached,
956 ;; and nothing more, in particular no other HTTP requests.
957 (let* ((origin (origin
958 (method url-fetch)
959 (uri "http://example.org/foo.tgz")
960 (sha256 (make-bytevector 32))))
961 (too-many (build-response
962 #:code 429
963 #:reason-phrase "Too many requests"
964 #:headers '((x-ratelimit-remaining . "0")
965 (x-ratelimit-reset . "3000000000"))))
966 (warnings (with-http-server `((,too-many "Rate limit reached."))
967 (parameterize ((%swh-base-url (%local-url)))
968 (append-map (lambda (name)
969 (check-archival
970 (dummy-package name (source origin))))
971 '("x" "y" "z"))))))
972 (string-contains (single-lint-warning-message warnings)
973 "rate limit reached")))
974
975 (test-end "lint")
976
977 ;; Local Variables:
978 ;; eval: (put 'with-http-server 'scheme-indent-function 1)
979 ;; eval: (put 'with-warnings 'scheme-indent-function 0)
980 ;; End: