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