lint.scm 45 KB

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