lint.scm 40 KB

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