lint.scm 44 KB

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