gnu: Add bsnes.
[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>
fcb2318e 4;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 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
993023a2
LC
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
50fc2384
CB
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
37592014 407(test-assert "home-page: host not found"
50fc2384
CB
408 (let ((pkg (package
409 (inherit (dummy-package "x"))
410 (home-page "http://does-not-exist"))))
37592014 411 (warning-contains? "domain not found" (check-home-page pkg))))
907c98ac 412
6ea10db9 413(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
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))))
907c98ac 421
6ea10db9 422(test-skip (if (http-server-can-listen?) 0 1))
907c98ac 423(test-equal "home-page: 200"
50fc2384 424 '()
9323ab55 425 (with-http-server `((200 ,%long-string))
50fc2384
CB
426 (let ((pkg (package
427 (inherit (dummy-package "x"))
428 (home-page (%local-url)))))
429 (check-home-page pkg))))
907c98ac 430
6ea10db9 431(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
432(test-equal "home-page: 200 but short length"
433 "URI http://localhost:9999/foo/bar returned suspiciously small file (18 bytes)"
9323ab55 434 (with-http-server `((200 "This is too small."))
50fc2384
CB
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)))))
bfcb3d76 441
6ea10db9 442(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
443(test-equal "home-page: 404"
444 "URI http://localhost:9999/foo/bar not reachable: 404 (\"Such is life\")"
9323ab55 445 (with-http-server `((404 ,%long-string))
50fc2384
CB
446 (let ((pkg (package
447 (inherit (dummy-package "x"))
448 (home-page (%local-url)))))
449 (single-lint-warning-message
450 (check-home-page pkg)))))
b4f5e0e8 451
61f28fe7 452(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
453(test-equal "home-page: 301, invalid"
454 "invalid permanent redirect from http://localhost:9999/foo/bar"
9323ab55 455 (with-http-server `((301 ,%long-string))
50fc2384
CB
456 (let ((pkg (package
457 (inherit (dummy-package "x"))
458 (home-page (%local-url)))))
459 (single-lint-warning-message
460 (check-home-page pkg)))))
61f28fe7
LC
461
462(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
463(test-equal "home-page: 301 -> 200"
464 "permanent redirect from http://localhost:10000/foo/bar to http://localhost:9999/foo/bar"
9323ab55
LC
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))))))
50fc2384 471 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
9323ab55 472 (with-http-server `((,redirect ""))
50fc2384
CB
473 (let ((pkg (package
474 (inherit (dummy-package "x"))
475 (home-page (%local-url)))))
476 (single-lint-warning-message
477 (check-home-page pkg))))))))
61f28fe7
LC
478
479(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
480(test-equal "home-page: 301 -> 404"
481 "URI http://localhost:10000/foo/bar not reachable: 404 (\"Such is life\")"
9323ab55
LC
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))))))
50fc2384 488 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
9323ab55 489 (with-http-server `((,redirect ""))
50fc2384
CB
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)))
c180017b 559
50fc2384
CB
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)))
950d2ea4 586
50fc2384
CB
587(test-equal "source-unstable-tarball: package named archive"
588 '()
589 (let ((pkg (dummy-package "x"
590 (source
591 (origin
bfcb3d76 592 (method url-fetch)
50fc2384 593 (uri "https://github.com/example/archive/releases/download/x-0.0/x-0.0.tar.gz")
bfcb3d76 594 (sha256 %null-sha256))))))
50fc2384 595 (check-source-unstable-tarball pkg)))
bfcb3d76 596
50fc2384
CB
597(test-equal "source-unstable-tarball: not-github"
598 '()
599 (let ((pkg (dummy-package "x"
600 (source
601 (origin
950d2ea4 602 (method url-fetch)
50fc2384 603 (uri "https://bitbucket.org/archive/example/download/x-0.0.tar.gz")
950d2ea4 604 (sha256 %null-sha256))))))
50fc2384
CB
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 '()
9323ab55 622 (with-http-server `((200 ,%long-string))
50fc2384
CB
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)"
9323ab55 634 (with-http-server '((200 "This is too small."))
50fc2384
CB
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\")"
9323ab55 649 (with-http-server `((404 ,%long-string))
50fc2384
CB
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))))))
950d2ea4 660
99b20428
LC
661(test-skip (if (http-server-can-listen?) 0 1))
662(test-equal "source: 404 and 200"
663 '()
9323ab55 664 (with-http-server `((404 ,%long-string))
99b20428
LC
665 (let ((bad-url (%local-url)))
666 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
9323ab55 667 (with-http-server `((200 ,%long-string))
99b20428
LC
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
61f28fe7
LC
678(test-skip (if (http-server-can-listen?) 0 1))
679(test-equal "source: 301 -> 200"
50fc2384 680 "permanent redirect from http://localhost:10000/foo/bar to http://localhost:9999/foo/bar"
9323ab55
LC
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))))))
50fc2384 687 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
9323ab55 688 (with-http-server `((,redirect ""))
50fc2384
CB
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)))))))))
61f28fe7
LC
699
700(test-skip (if (http-server-can-listen?) 0 1))
50fc2384
CB
701(test-equal "source: 301 -> 404"
702 "URI http://localhost:10000/foo/bar not reachable: 404 (\"Such is life\")"
9323ab55
LC
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))))))
50fc2384 709 (parameterize ((%http-server-port (+ 1 (%http-server-port))))
9323ab55 710 (with-http-server `((,redirect ""))
50fc2384
CB
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 '()
9323ab55 742 (with-http-server `((200 ,%long-string))
50fc2384
CB
743 (check-github-url
744 (dummy-package "x" (source
745 (origin
746 (method url-fetch)
747 (uri (%local-url))
748 (sha256 %null-sha256)))))))
0865d8a8
AI
749
750(let ((github-url "https://github.com/foo/bar/bar-1.0.tar.gz"))
50fc2384
CB
751 (test-equal "github-url: one suggestion"
752 (string-append
753 "URL should be '" github-url "'")
9323ab55
LC
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))))))))))))
50fc2384
CB
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 '()
571f6e7f 784 (mock ((guix lint) package-vulnerabilities (const '()))
50fc2384 785 (check-vulnerabilities (dummy-package "x"))))
5432734b 786
50fc2384
CB
787(test-equal "cve: one vulnerability"
788 "probably vulnerable to CVE-2015-1234"
fcb2318e 789 (let ((dummy-vulnerabilities
5432734b 790 (lambda (package)
fcb2318e
LC
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))))
5432734b 799
50fc2384
CB
800(test-equal "cve: one patched vulnerability"
801 '()
571f6e7f 802 (mock ((guix lint) package-vulnerabilities
4e70fe4d 803 (lambda (package)
79c03e55
LC
804 (list (make-struct/no-tail (@@ (guix cve) <vulnerability>)
805 "CVE-2015-1234"
806 (list (cons (package-name package)
807 (package-version package)))))))
50fc2384
CB
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 '()
571f6e7f 818 (mock ((guix lint) package-vulnerabilities
f4007b25 819 (lambda (package)
79c03e55
LC
820 (list (make-struct/no-tail (@@ (guix cve) <vulnerability>)
821 "CVE-2015-1234"
822 (list (cons (package-name package)
823 (package-version package)))))))
50fc2384
CB
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 '()
571f6e7f 831 (mock ((guix lint) package-vulnerabilities
9bee2bd1
LC
832 (lambda (package)
833 (match (package-version package)
834 ("0"
79c03e55
LC
835 (list (make-struct/no-tail (@@ (guix cve) <vulnerability>)
836 "CVE-2015-1234"
837 (list (cons (package-name package)
838 (package-version package))))))
9bee2bd1
LC
839 ("1"
840 '()))))
50fc2384
CB
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 '()
571f6e7f 848 (mock ((guix lint) package-vulnerabilities
5c6a062d 849 (lambda (package)
79c03e55
LC
850 (list (make-struct/no-tail (@@ (guix cve) <vulnerability>)
851 "CVE-2015-1234"
852 (list (cons (package-name package)
853 (package-version package)))))))
50fc2384
CB
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 )))
e0566f12 871
40a7d4e5 872(test-assert "formatting: tabulation"
50fc2384
CB
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: ")))))
40a7d4e5
LC
877
878(test-assert "formatting: trailing white space"
50fc2384
CB
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")))))
40a7d4e5
LC
884
885(test-assert "formatting: long line"
50fc2384
CB
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")))
40a7d4e5 895
55549c7b
LC
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
b4f5e0e8
CR
975(test-end "lint")
976
907c98ac 977;; Local Variables:
9323ab55 978;; eval: (put 'with-http-server 'scheme-indent-function 1)
4fbf4ca5 979;; eval: (put 'with-warnings 'scheme-indent-function 0)
907c98ac 980;; End: