packages: Add "aarch64-linux" to '%hydra-supported-systems'.
[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>
83f18e06 3;;; Copyright © 2014, 2015, 2016 Eric Bavier <bavier@member.fsf.org>
571f6e7f 4;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
052d53df 5;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
891a843d 6;;; Copyright © 2016 Hartmut Goebel <h.goebel@crazy-compilers.com>
689db38e 7;;; Copyright © 2017 Alex Kost <alezost@gmail.com>
f4007b25 8;;; Copyright © 2017 Efraim Flashner <efraim@flashner.co.il>
40fa21c2 9;;; Copyright © 2018, 2019 Arun Isaac <arunisaac@systemreboot.net>
b4f5e0e8
CR
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
c74f0cb2
LC
26;; Avoid interference.
27(unsetenv "http_proxy")
28
4e7b6b48 29(define-module (test-lint)
8b385969 30 #:use-module (guix tests)
17ab08bc 31 #:use-module (guix tests http)
754e5be2 32 #:use-module (guix download)
50f5c46d 33 #:use-module (guix git-download)
b4f5e0e8
CR
34 #:use-module (guix build-system gnu)
35 #:use-module (guix packages)
f363c836 36 #:use-module (guix lint)
b4f5e0e8 37 #:use-module (guix ui)
55549c7b 38 #:use-module (guix swh)
b4f5e0e8 39 #:use-module (gnu packages)
99fe215c 40 #:use-module (gnu packages glib)
b4f5e0e8 41 #:use-module (gnu packages pkg-config)
3b98522b 42 #:use-module (gnu packages python-xyz)
61f28fe7 43 #:use-module (web uri)
907c98ac
LC
44 #:use-module (web server)
45 #:use-module (web server http)
46 #:use-module (web response)
9bee2bd1 47 #:use-module (ice-9 match)
50fc2384
CB
48 #:use-module (ice-9 regex)
49 #:use-module (ice-9 getopt-long)
50 #:use-module (ice-9 pretty-print)
55549c7b 51 #:use-module (rnrs bytevectors)
50fc2384 52 #:use-module (srfi srfi-1)
907c98ac 53 #:use-module (srfi srfi-9 gnu)
50fc2384 54 #:use-module (srfi srfi-26)
b4f5e0e8
CR
55 #:use-module (srfi srfi-64))
56
57;; Test the linter.
58
17ab08bc
LC
59;; Avoid collisions with other tests.
60(%http-server-port 9999)
907c98ac 61
950d2ea4
LC
62(define %null-sha256
63 ;; SHA256 of the empty string.
64 (base32
65 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
66
bfcb3d76
LC
67(define %long-string
68 (make-string 2000 #\a))
907c98ac 69
50fc2384
CB
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
37592014
LC
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
b4f5e0e8
CR
85\f
86(test-begin "lint")
87
50fc2384
CB
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
891a843d 286 "inputs: python-setuptools should not be an input at all (input)"
50fc2384
CB
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
891a843d 295 "inputs: python-setuptools should not be an input at all (native-input)"
50fc2384
CB
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
891a843d 305 "inputs: python-setuptools should not be an input at all (propagated-input)"
50fc2384
CB
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 "license: invalid license"
357 "invalid license field"
358 (single-lint-warning-message
359 (check-license (dummy-package "x" (license #f)))))
360
361(test-equal "home-page: wrong home-page"
362 "invalid value for home page"
363 (let ((pkg (package
364 (inherit (dummy-package "x"))
365 (home-page #f))))
366 (single-lint-warning-message
367 (check-home-page pkg))))
368
369(test-equal "home-page: invalid URI"
370 "invalid home page URL: \"foobar\""
371 (let ((pkg (package
372 (inherit (dummy-package "x"))
373 (home-page "foobar"))))
374 (single-lint-warning-message
375 (check-home-page pkg))))
376
37592014 377(test-assert "home-page: host not found"
50fc2384
CB
378 (let ((pkg (package
379 (inherit (dummy-package "x"))
380 (home-page "http://does-not-exist"))))
37592014 381 (warning-contains? "domain not found" (check-home-page pkg))))
907c98ac 382
6ea10db9 383(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
384(test-equal "home-page: Connection refused"
385 "URI http://localhost:9999/foo/bar unreachable: Connection refused"
386 (let ((pkg (package
387 (inherit (dummy-package "x"))
388 (home-page (%local-url)))))
389 (single-lint-warning-message
390 (check-home-page pkg))))
907c98ac 391
6ea10db9 392(test-skip (if (http-server-can-listen?) 0 1))
907c98ac 393(test-equal "home-page: 200"
50fc2384 394 '()
9323ab55 395 (with-http-server `((200 ,%long-string))
50fc2384
CB
396 (let ((pkg (package
397 (inherit (dummy-package "x"))
398 (home-page (%local-url)))))
399 (check-home-page pkg))))
907c98ac 400
6ea10db9 401(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
402(test-equal "home-page: 200 but short length"
403 "URI http://localhost:9999/foo/bar returned suspiciously small file (18 bytes)"
9323ab55 404 (with-http-server `((200 "This is too small."))
50fc2384
CB
405 (let ((pkg (package
406 (inherit (dummy-package "x"))
407 (home-page (%local-url)))))
408
409 (single-lint-warning-message
410 (check-home-page pkg)))))
bfcb3d76 411
6ea10db9 412(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
413(test-equal "home-page: 404"
414 "URI http://localhost:9999/foo/bar not reachable: 404 (\"Such is life\")"
9323ab55 415 (with-http-server `((404 ,%long-string))
50fc2384
CB
416 (let ((pkg (package
417 (inherit (dummy-package "x"))
418 (home-page (%local-url)))))
419 (single-lint-warning-message
420 (check-home-page pkg)))))
b4f5e0e8 421
61f28fe7 422(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
423(test-equal "home-page: 301, invalid"
424 "invalid permanent redirect from http://localhost:9999/foo/bar"
9323ab55 425 (with-http-server `((301 ,%long-string))
50fc2384
CB
426 (let ((pkg (package
427 (inherit (dummy-package "x"))
428 (home-page (%local-url)))))
429 (single-lint-warning-message
430 (check-home-page pkg)))))
61f28fe7
LC
431
432(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
433(test-equal "home-page: 301 -> 200"
434 "permanent redirect from http://localhost:10000/foo/bar to http://localhost:9999/foo/bar"
9323ab55
LC
435 (with-http-server `((200 ,%long-string))
436 (let* ((initial-url (%local-url))
437 (redirect (build-response #:code 301
438 #:headers
439 `((location
440 . ,(string->uri initial-url))))))
50fc2384 441 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
9323ab55 442 (with-http-server `((,redirect ""))
50fc2384
CB
443 (let ((pkg (package
444 (inherit (dummy-package "x"))
445 (home-page (%local-url)))))
446 (single-lint-warning-message
447 (check-home-page pkg))))))))
61f28fe7
LC
448
449(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
450(test-equal "home-page: 301 -> 404"
451 "URI http://localhost:10000/foo/bar not reachable: 404 (\"Such is life\")"
9323ab55
LC
452 (with-http-server '((404 "booh!"))
453 (let* ((initial-url (%local-url))
454 (redirect (build-response #:code 301
455 #:headers
456 `((location
457 . ,(string->uri initial-url))))))
50fc2384 458 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
9323ab55 459 (with-http-server `((,redirect ""))
50fc2384
CB
460 (let ((pkg (package
461 (inherit (dummy-package "x"))
462 (home-page (%local-url)))))
463 (single-lint-warning-message
464 (check-home-page pkg))))))))
465
466
467(test-equal "source-file-name"
468 "the source file name should contain the package name"
469 (let ((pkg (dummy-package "x"
470 (version "3.2.1")
471 (source
472 (origin
473 (method url-fetch)
474 (uri "http://www.example.com/3.2.1.tar.gz")
475 (sha256 %null-sha256))))))
476 (single-lint-warning-message
477 (check-source-file-name pkg))))
478
479(test-equal "source-file-name: v prefix"
480 "the source file name should contain the package name"
481 (let ((pkg (dummy-package "x"
482 (version "3.2.1")
483 (source
484 (origin
485 (method url-fetch)
486 (uri "http://www.example.com/v3.2.1.tar.gz")
487 (sha256 %null-sha256))))))
488 (single-lint-warning-message
489 (check-source-file-name pkg))))
490
491(test-equal "source-file-name: bad checkout"
492 "the source file name should contain the package name"
493 (let ((pkg (dummy-package "x"
494 (version "3.2.1")
495 (source
496 (origin
497 (method git-fetch)
498 (uri (git-reference
499 (url "http://www.example.com/x.git")
500 (commit "0")))
501 (sha256 %null-sha256))))))
502 (single-lint-warning-message
503 (check-source-file-name pkg))))
504
505(test-equal "source-file-name: good checkout"
506 '()
507 (let ((pkg (dummy-package "x"
508 (version "3.2.1")
509 (source
510 (origin
511 (method git-fetch)
512 (uri (git-reference
513 (url "http://git.example.com/x.git")
514 (commit "0")))
515 (file-name (string-append "x-" version))
516 (sha256 %null-sha256))))))
517 (check-source-file-name pkg)))
518
519(test-equal "source-file-name: valid"
520 '()
521 (let ((pkg (dummy-package "x"
522 (version "3.2.1")
523 (source
524 (origin
525 (method url-fetch)
526 (uri "http://www.example.com/x-3.2.1.tar.gz")
527 (sha256 %null-sha256))))))
528 (check-source-file-name pkg)))
c180017b 529
50fc2384
CB
530(test-equal "source-unstable-tarball"
531 "the source URI should not be an autogenerated tarball"
532 (let ((pkg (dummy-package "x"
533 (source
534 (origin
535 (method url-fetch)
536 (uri "https://github.com/example/example/archive/v0.0.tar.gz")
537 (sha256 %null-sha256))))))
538 (single-lint-warning-message
539 (check-source-unstable-tarball pkg))))
540
541(test-equal "source-unstable-tarball: source #f"
542 '()
543 (let ((pkg (dummy-package "x"
544 (source #f))))
545 (check-source-unstable-tarball pkg)))
546
547(test-equal "source-unstable-tarball: valid"
548 '()
549 (let ((pkg (dummy-package "x"
550 (source
551 (origin
552 (method url-fetch)
553 (uri "https://github.com/example/example/releases/download/x-0.0/x-0.0.tar.gz")
554 (sha256 %null-sha256))))))
555 (check-source-unstable-tarball pkg)))
950d2ea4 556
50fc2384
CB
557(test-equal "source-unstable-tarball: package named archive"
558 '()
559 (let ((pkg (dummy-package "x"
560 (source
561 (origin
bfcb3d76 562 (method url-fetch)
50fc2384 563 (uri "https://github.com/example/archive/releases/download/x-0.0/x-0.0.tar.gz")
bfcb3d76 564 (sha256 %null-sha256))))))
50fc2384 565 (check-source-unstable-tarball pkg)))
bfcb3d76 566
50fc2384
CB
567(test-equal "source-unstable-tarball: not-github"
568 '()
569 (let ((pkg (dummy-package "x"
570 (source
571 (origin
950d2ea4 572 (method url-fetch)
50fc2384 573 (uri "https://bitbucket.org/archive/example/download/x-0.0.tar.gz")
950d2ea4 574 (sha256 %null-sha256))))))
50fc2384
CB
575 (check-source-unstable-tarball pkg)))
576
577(test-equal "source-unstable-tarball: git-fetch"
578 '()
579 (let ((pkg (dummy-package "x"
580 (source
581 (origin
582 (method git-fetch)
583 (uri (git-reference
584 (url "https://github.com/archive/example.git")
585 (commit "0")))
586 (sha256 %null-sha256))))))
587 (check-source-unstable-tarball pkg)))
588
589(test-skip (if (http-server-can-listen?) 0 1))
590(test-equal "source: 200"
591 '()
9323ab55 592 (with-http-server `((200 ,%long-string))
50fc2384
CB
593 (let ((pkg (package
594 (inherit (dummy-package "x"))
595 (source (origin
596 (method url-fetch)
597 (uri (%local-url))
598 (sha256 %null-sha256))))))
599 (check-source pkg))))
600
601(test-skip (if (http-server-can-listen?) 0 1))
602(test-equal "source: 200 but short length"
603 "URI http://localhost:9999/foo/bar returned suspiciously small file (18 bytes)"
9323ab55 604 (with-http-server '((200 "This is too small."))
50fc2384
CB
605 (let ((pkg (package
606 (inherit (dummy-package "x"))
607 (source (origin
608 (method url-fetch)
609 (uri (%local-url))
610 (sha256 %null-sha256))))))
611 (match (check-source pkg)
612 ((first-warning ; All source URIs are unreachable
613 (and (? lint-warning?) second-warning))
614 (lint-warning-message second-warning))))))
615
616(test-skip (if (http-server-can-listen?) 0 1))
617(test-equal "source: 404"
618 "URI http://localhost:9999/foo/bar not reachable: 404 (\"Such is life\")"
9323ab55 619 (with-http-server `((404 ,%long-string))
50fc2384
CB
620 (let ((pkg (package
621 (inherit (dummy-package "x"))
622 (source (origin
623 (method url-fetch)
624 (uri (%local-url))
625 (sha256 %null-sha256))))))
626 (match (check-source pkg)
627 ((first-warning ; All source URIs are unreachable
628 (and (? lint-warning?) second-warning))
629 (lint-warning-message second-warning))))))
950d2ea4 630
99b20428
LC
631(test-skip (if (http-server-can-listen?) 0 1))
632(test-equal "source: 404 and 200"
633 '()
9323ab55 634 (with-http-server `((404 ,%long-string))
99b20428
LC
635 (let ((bad-url (%local-url)))
636 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
9323ab55 637 (with-http-server `((200 ,%long-string))
99b20428
LC
638 (let ((pkg (package
639 (inherit (dummy-package "x"))
640 (source (origin
641 (method url-fetch)
642 (uri (list bad-url (%local-url)))
643 (sha256 %null-sha256))))))
644 ;; Since one of the two URLs is good, this should return the empty
645 ;; list.
646 (check-source pkg)))))))
647
61f28fe7
LC
648(test-skip (if (http-server-can-listen?) 0 1))
649(test-equal "source: 301 -> 200"
50fc2384 650 "permanent redirect from http://localhost:10000/foo/bar to http://localhost:9999/foo/bar"
9323ab55
LC
651 (with-http-server `((200 ,%long-string))
652 (let* ((initial-url (%local-url))
653 (redirect (build-response #:code 301
654 #:headers
655 `((location
656 . ,(string->uri initial-url))))))
50fc2384 657 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
9323ab55 658 (with-http-server `((,redirect ""))
50fc2384
CB
659 (let ((pkg (package
660 (inherit (dummy-package "x"))
661 (source (origin
662 (method url-fetch)
663 (uri (%local-url))
664 (sha256 %null-sha256))))))
665 (match (check-source pkg)
666 ((first-warning ; All source URIs are unreachable
667 (and (? lint-warning?) second-warning))
668 (lint-warning-message second-warning)))))))))
61f28fe7
LC
669
670(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
671(test-equal "source: 301 -> 404"
672 "URI http://localhost:10000/foo/bar not reachable: 404 (\"Such is life\")"
9323ab55
LC
673 (with-http-server '((404 "booh!"))
674 (let* ((initial-url (%local-url))
675 (redirect (build-response #:code 301
676 #:headers
677 `((location
678 . ,(string->uri initial-url))))))
50fc2384 679 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
9323ab55 680 (with-http-server `((,redirect ""))
50fc2384
CB
681 (let ((pkg (package
682 (inherit (dummy-package "x"))
683 (source (origin
684 (method url-fetch)
685 (uri (%local-url))
686 (sha256 %null-sha256))))))
687 (match (check-source pkg)
688 ((first-warning ; The first warning says that all URI's are
689 ; unreachable
690 (and (? lint-warning?) second-warning))
691 (lint-warning-message second-warning)))))))))
692
693(test-equal "mirror-url"
694 '()
695 (let ((source (origin
696 (method url-fetch)
697 (uri "http://example.org/foo/bar.tar.gz")
698 (sha256 %null-sha256))))
699 (check-mirror-url (dummy-package "x" (source source)))))
700
701(test-equal "mirror-url: one suggestion"
702 "URL should be 'mirror://gnu/foo/foo.tar.gz'"
703 (let ((source (origin
704 (method url-fetch)
705 (uri "http://ftp.gnu.org/pub/gnu/foo/foo.tar.gz")
706 (sha256 %null-sha256))))
707 (single-lint-warning-message
708 (check-mirror-url (dummy-package "x" (source source))))))
709
710(test-equal "github-url"
711 '()
9323ab55 712 (with-http-server `((200 ,%long-string))
50fc2384
CB
713 (check-github-url
714 (dummy-package "x" (source
715 (origin
716 (method url-fetch)
717 (uri (%local-url))
718 (sha256 %null-sha256)))))))
0865d8a8
AI
719
720(let ((github-url "https://github.com/foo/bar/bar-1.0.tar.gz"))
50fc2384
CB
721 (test-equal "github-url: one suggestion"
722 (string-append
723 "URL should be '" github-url "'")
9323ab55
LC
724 (let ((redirect (build-response #:code 301
725 #:headers
726 `((location
727 . ,(string->uri github-url))))))
728 (with-http-server `((,redirect ""))
729 (let* ((initial-url (%local-url))
730 (redirect (build-response #:code 302
731 #:headers
732 `((location
733 . ,(string->uri initial-url))))))
734 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
735 (with-http-server `((,redirect ""))
736 (single-lint-warning-message
737 (check-github-url
738 (dummy-package "x" (source
739 (origin
740 (method url-fetch)
741 (uri (%local-url))
742 (sha256 %null-sha256))))))))))))
50fc2384
CB
743 (test-equal "github-url: already the correct github url"
744 '()
745 (check-github-url
746 (dummy-package "x" (source
747 (origin
748 (method url-fetch)
749 (uri github-url)
750 (sha256 %null-sha256)))))))
751
752(test-equal "cve"
753 '()
571f6e7f 754 (mock ((guix lint) package-vulnerabilities (const '()))
50fc2384 755 (check-vulnerabilities (dummy-package "x"))))
5432734b 756
50fc2384
CB
757(test-equal "cve: one vulnerability"
758 "probably vulnerable to CVE-2015-1234"
571f6e7f 759 (mock ((guix lint) package-vulnerabilities
5432734b
LC
760 (lambda (package)
761 (list (make-struct (@@ (guix cve) <vulnerability>) 0
762 "CVE-2015-1234"
763 (list (cons (package-name package)
764 (package-version package)))))))
50fc2384
CB
765 (single-lint-warning-message
766 (check-vulnerabilities (dummy-package "pi" (version "3.14"))))))
5432734b 767
50fc2384
CB
768(test-equal "cve: one patched vulnerability"
769 '()
571f6e7f 770 (mock ((guix lint) package-vulnerabilities
4e70fe4d
LC
771 (lambda (package)
772 (list (make-struct (@@ (guix cve) <vulnerability>) 0
773 "CVE-2015-1234"
774 (list (cons (package-name package)
775 (package-version package)))))))
50fc2384
CB
776 (check-vulnerabilities
777 (dummy-package "pi"
778 (version "3.14")
779 (source
780 (dummy-origin
781 (patches
782 (list "/a/b/pi-CVE-2015-1234.patch"))))))))
783
784(test-equal "cve: known safe from vulnerability"
785 '()
571f6e7f 786 (mock ((guix lint) package-vulnerabilities
f4007b25
EF
787 (lambda (package)
788 (list (make-struct (@@ (guix cve) <vulnerability>) 0
789 "CVE-2015-1234"
790 (list (cons (package-name package)
791 (package-version package)))))))
50fc2384
CB
792 (check-vulnerabilities
793 (dummy-package "pi"
794 (version "3.14")
795 (properties `((lint-hidden-cve . ("CVE-2015-1234"))))))))
796
797(test-equal "cve: vulnerability fixed in replacement version"
798 '()
571f6e7f 799 (mock ((guix lint) package-vulnerabilities
9bee2bd1
LC
800 (lambda (package)
801 (match (package-version package)
802 ("0"
803 (list (make-struct (@@ (guix cve) <vulnerability>) 0
804 "CVE-2015-1234"
805 (list (cons (package-name package)
806 (package-version package))))))
807 ("1"
808 '()))))
50fc2384
CB
809 (check-vulnerabilities
810 (dummy-package
811 "foo" (version "0")
812 (replacement (dummy-package "foo" (version "1")))))))
813
814(test-equal "cve: patched vulnerability in replacement"
815 '()
571f6e7f 816 (mock ((guix lint) package-vulnerabilities
5c6a062d
LC
817 (lambda (package)
818 (list (make-struct (@@ (guix cve) <vulnerability>) 0
819 "CVE-2015-1234"
820 (list (cons (package-name package)
821 (package-version package)))))))
50fc2384
CB
822 (check-vulnerabilities
823 (dummy-package
824 "pi" (version "3.14") (source (dummy-origin))
825 (replacement (dummy-package
826 "pi" (version "3.14")
827 (source
828 (dummy-origin
829 (patches
830 (list "/a/b/pi-CVE-2015-1234.patch"))))))))))
831
832(test-equal "formatting: lonely parentheses"
833 "parentheses feel lonely, move to the previous or next line"
834 (single-lint-warning-message
835 (check-formatting
836 (dummy-package "ugly as hell!"
837 )
838 )))
e0566f12 839
40a7d4e5 840(test-assert "formatting: tabulation"
50fc2384
CB
841 (string-match-or-error
842 "tabulation on line [0-9]+, column [0-9]+"
843 (single-lint-warning-message
844 (check-formatting (dummy-package "leave the tab here: ")))))
40a7d4e5
LC
845
846(test-assert "formatting: trailing white space"
50fc2384
CB
847 (string-match-or-error
848 "trailing white space .*"
849 ;; Leave the trailing white space on the next line!
850 (single-lint-warning-message
851 (check-formatting (dummy-package "x")))))
40a7d4e5
LC
852
853(test-assert "formatting: long line"
50fc2384
CB
854 (string-match-or-error
855 "line [0-9]+ is way too long \\([0-9]+ characters\\)"
856 (single-lint-warning-message (check-formatting
857 (dummy-package "x")) ;here is a stupid comment just to make a long line
858 )))
859
860(test-equal "formatting: alright"
861 '()
862 (check-formatting (dummy-package "x")))
40a7d4e5 863
55549c7b
LC
864(test-assert "archival: missing content"
865 (let* ((origin (origin
866 (method url-fetch)
867 (uri "http://example.org/foo.tgz")
868 (sha256 (make-bytevector 32))))
869 (warnings (with-http-server '((404 "Not archived."))
870 (parameterize ((%swh-base-url (%local-url)))
871 (check-archival (dummy-package "x"
872 (source origin)))))))
873 (warning-contains? "not archived" warnings)))
874
875(test-equal "archival: content available"
876 '()
877 (let* ((origin (origin
878 (method url-fetch)
879 (uri "http://example.org/foo.tgz")
880 (sha256 (make-bytevector 32))))
881 ;; https://archive.softwareheritage.org/api/1/content/
882 (content "{ \"checksums\": {}, \"data_url\": \"xyz\",
883 \"length\": 42 }"))
884 (with-http-server `((200 ,content))
885 (parameterize ((%swh-base-url (%local-url)))
886 (check-archival (dummy-package "x" (source origin)))))))
887
888(test-assert "archival: missing revision"
889 (let* ((origin (origin
890 (method git-fetch)
891 (uri (git-reference
892 (url "http://example.org/foo.git")
893 (commit "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")))
894 (sha256 (make-bytevector 32))))
895 ;; https://archive.softwareheritage.org/api/1/origin/save/
896 (save "{ \"origin_url\": \"http://example.org/foo.git\",
897 \"save_request_date\": \"2014-11-17T22:09:38+01:00\",
898 \"save_request_status\": \"accepted\",
899 \"save_task_status\": \"scheduled\" }")
900 (warnings (with-http-server `((404 "No revision.") ;lookup-revision
901 (404 "No origin.") ;lookup-origin
902 (200 ,save)) ;save-origin
903 (parameterize ((%swh-base-url (%local-url)))
904 (check-archival (dummy-package "x" (source origin)))))))
905 (warning-contains? "scheduled" warnings)))
906
907(test-equal "archival: revision available"
908 '()
909 (let* ((origin (origin
910 (method git-fetch)
911 (uri (git-reference
912 (url "http://example.org/foo.git")
913 (commit "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")))
914 (sha256 (make-bytevector 32))))
915 ;; https://archive.softwareheritage.org/api/1/revision/
916 (revision "{ \"author\": {}, \"parents\": [],
917 \"date\": \"2014-11-17T22:09:38+01:00\" }"))
918 (with-http-server `((200 ,revision))
919 (parameterize ((%swh-base-url (%local-url)))
920 (check-archival (dummy-package "x" (source origin)))))))
921
922(test-assert "archival: rate limit reached"
923 ;; We should get a single warning stating that the rate limit was reached,
924 ;; and nothing more, in particular no other HTTP requests.
925 (let* ((origin (origin
926 (method url-fetch)
927 (uri "http://example.org/foo.tgz")
928 (sha256 (make-bytevector 32))))
929 (too-many (build-response
930 #:code 429
931 #:reason-phrase "Too many requests"
932 #:headers '((x-ratelimit-remaining . "0")
933 (x-ratelimit-reset . "3000000000"))))
934 (warnings (with-http-server `((,too-many "Rate limit reached."))
935 (parameterize ((%swh-base-url (%local-url)))
936 (append-map (lambda (name)
937 (check-archival
938 (dummy-package name (source origin))))
939 '("x" "y" "z"))))))
940 (string-contains (single-lint-warning-message warnings)
941 "rate limit reached")))
942
b4f5e0e8
CR
943(test-end "lint")
944
907c98ac 945;; Local Variables:
9323ab55 946;; eval: (put 'with-http-server 'scheme-indent-function 1)
4fbf4ca5 947;; eval: (put 'with-warnings 'scheme-indent-function 0)
907c98ac 948;; End: