pypi.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (test-pypi)
  21. #:use-module (guix import pypi)
  22. #:use-module (guix base32)
  23. #:use-module (guix memoization)
  24. #:use-module (gcrypt hash)
  25. #:use-module (guix tests)
  26. #:use-module (guix build-system python)
  27. #:use-module ((guix build utils) #:select (delete-file-recursively which mkdir-p))
  28. #:use-module (srfi srfi-64)
  29. #:use-module (ice-9 match))
  30. (define test-json
  31. "{
  32. \"info\": {
  33. \"version\": \"1.0.0\",
  34. \"name\": \"foo\",
  35. \"license\": \"GNU LGPL\",
  36. \"summary\": \"summary\",
  37. \"home_page\": \"http://example.com\",
  38. \"classifiers\": [],
  39. \"download_url\": \"\"
  40. },
  41. \"urls\": [],
  42. \"releases\": {
  43. \"1.0.0\": [
  44. {
  45. \"url\": \"https://example.com/foo-1.0.0.egg\",
  46. \"packagetype\": \"bdist_egg\"
  47. }, {
  48. \"url\": \"https://example.com/foo-1.0.0.tar.gz\",
  49. \"packagetype\": \"sdist\"
  50. }, {
  51. \"url\": \"https://example.com/foo-1.0.0-py2.py3-none-any.whl\",
  52. \"packagetype\": \"bdist_wheel\"
  53. }
  54. ]
  55. }
  56. }")
  57. (define test-source-hash
  58. "")
  59. (define test-specifications
  60. '("Fizzy [foo, bar]"
  61. "PickyThing<1.6,>1.9,!=1.9.6,<2.0a0,==2.4c1"
  62. "SomethingWithMarker[foo]>1.0;python_version<\"2.7\""
  63. "requests [security,tests] >= 2.8.1, == 2.8.* ; python_version < \"2.7\""
  64. "pip @ https://github.com/pypa/pip/archive/1.3.1.zip#\
  65. sha1=da9234ee9982d4bbb3c72346a6de940a148ea686"))
  66. (define test-requires.txt "\
  67. # A comment
  68. # A comment after a space
  69. foo ~= 3
  70. bar != 2
  71. [test]
  72. pytest (>=2.5.0)
  73. ")
  74. ;; Beaker contains only optional dependencies.
  75. (define test-requires.txt-beaker "\
  76. [crypto]
  77. pycryptopp>=0.5.12
  78. [cryptography]
  79. cryptography
  80. [testsuite]
  81. Mock
  82. coverage
  83. ")
  84. (define test-metadata "\
  85. Classifier: Programming Language :: Python :: 3.7
  86. Requires-Dist: baz ~= 3
  87. Requires-Dist: bar != 2
  88. Provides-Extra: test
  89. Requires-Dist: pytest (>=2.5.0) ; extra == 'test'
  90. ")
  91. (define test-metadata-with-extras "
  92. Classifier: Programming Language :: Python :: 3.7
  93. Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
  94. Requires-Dist: wrapt (<2,>=1)
  95. Requires-Dist: bar
  96. Provides-Extra: dev
  97. Requires-Dist: tox ; extra == 'dev'
  98. Requires-Dist: bumpversion (<1) ; extra == 'dev'
  99. ")
  100. ;;; Provides-Extra can appear before Requires-Dist.
  101. (define test-metadata-with-extras-jedi "\
  102. Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
  103. Provides-Extra: testing
  104. Requires-Dist: parso (>=0.3.0)
  105. Provides-Extra: testing
  106. Requires-Dist: pytest (>=3.1.0); extra == 'testing'
  107. ")
  108. (test-begin "pypi")
  109. (test-equal "guix-package->pypi-name, old URL style"
  110. "psutil"
  111. (guix-package->pypi-name
  112. (dummy-package "foo"
  113. (source (dummy-origin
  114. (uri
  115. "https://pypi.org/packages/source/p/psutil/psutil-4.3.0.tar.gz"))))))
  116. (test-equal "guix-package->pypi-name, new URL style"
  117. "certbot"
  118. (guix-package->pypi-name
  119. (dummy-package "foo"
  120. (source (dummy-origin
  121. (uri
  122. "https://pypi.org/packages/a2/3b/4756e6a0ceb14e084042a2a65c615d68d25621c6fd446d0fc10d14c4ce7d/certbot-0.8.1.tar.gz"))))))
  123. (test-equal "guix-package->pypi-name, several URLs"
  124. "cram"
  125. (guix-package->pypi-name
  126. (dummy-package "foo"
  127. (source
  128. (dummy-origin
  129. (uri (list "https://bitheap.org/cram/cram-0.7.tar.gz"
  130. (pypi-uri "cram" "0.7"))))))))
  131. (test-equal "specification->requirement-name"
  132. '("Fizzy" "PickyThing" "SomethingWithMarker" "requests" "pip")
  133. (map specification->requirement-name test-specifications))
  134. (test-equal "parse-requires.txt"
  135. (list '("foo" "bar") '("pytest"))
  136. (mock ((ice-9 ports) call-with-input-file
  137. call-with-input-string)
  138. (parse-requires.txt test-requires.txt)))
  139. (test-equal "parse-requires.txt - Beaker"
  140. (list '() '("Mock" "coverage"))
  141. (mock ((ice-9 ports) call-with-input-file
  142. call-with-input-string)
  143. (parse-requires.txt test-requires.txt-beaker)))
  144. (test-equal "parse-wheel-metadata, with extras"
  145. (list '("wrapt" "bar") '("tox" "bumpversion"))
  146. (mock ((ice-9 ports) call-with-input-file
  147. call-with-input-string)
  148. (parse-wheel-metadata test-metadata-with-extras)))
  149. (test-equal "parse-wheel-metadata, with extras - Jedi"
  150. (list '("parso") '("pytest"))
  151. (mock ((ice-9 ports) call-with-input-file
  152. call-with-input-string)
  153. (parse-wheel-metadata test-metadata-with-extras-jedi)))
  154. (test-assert "pypi->guix-package, no wheel"
  155. ;; Replace network resources with sample data.
  156. (mock ((guix import utils) url-fetch
  157. (lambda (url file-name)
  158. (match url
  159. ("https://example.com/foo-1.0.0.tar.gz"
  160. (begin
  161. ;; Unusual requires.txt location should still be found.
  162. (mkdir-p "foo-1.0.0/src/bizarre.egg-info")
  163. (with-output-to-file "foo-1.0.0/src/bizarre.egg-info/requires.txt"
  164. (lambda ()
  165. (display test-requires.txt)))
  166. (parameterize ((current-output-port (%make-void-port "rw+")))
  167. (system* "tar" "czvf" file-name "foo-1.0.0/"))
  168. (delete-file-recursively "foo-1.0.0")
  169. (set! test-source-hash
  170. (call-with-input-file file-name port-sha256))))
  171. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
  172. (_ (error "Unexpected URL: " url)))))
  173. (mock ((guix http-client) http-fetch
  174. (lambda (url . rest)
  175. (match url
  176. ("https://pypi.org/pypi/foo/json"
  177. (values (open-input-string test-json)
  178. (string-length test-json)))
  179. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
  180. (_ (error "Unexpected URL: " url)))))
  181. (match (pypi->guix-package "foo")
  182. (('package
  183. ('name "python-foo")
  184. ('version "1.0.0")
  185. ('source ('origin
  186. ('method 'url-fetch)
  187. ('uri ('pypi-uri "foo" 'version))
  188. ('sha256
  189. ('base32
  190. (? string? hash)))))
  191. ('build-system 'python-build-system)
  192. ('propagated-inputs ('list 'python-bar 'python-foo))
  193. ('native-inputs ('list 'python-pytest))
  194. ('home-page "http://example.com")
  195. ('synopsis "summary")
  196. ('description "summary")
  197. ('license 'license:lgpl2.0))
  198. (string=? (bytevector->nix-base32-string
  199. test-source-hash)
  200. hash))
  201. (x
  202. (pk 'fail x #f))))))
  203. (test-skip (if (which "zip") 0 1))
  204. (test-assert "pypi->guix-package, wheels"
  205. ;; Replace network resources with sample data.
  206. (mock ((guix import utils) url-fetch
  207. (lambda (url file-name)
  208. (match url
  209. ("https://example.com/foo-1.0.0.tar.gz"
  210. (begin
  211. (mkdir-p "foo-1.0.0/foo.egg-info/")
  212. (with-output-to-file "foo-1.0.0/foo.egg-info/requires.txt"
  213. (lambda ()
  214. (display "wrong data to make sure we're testing wheels ")))
  215. (parameterize ((current-output-port (%make-void-port "rw+")))
  216. (system* "tar" "czvf" file-name "foo-1.0.0/"))
  217. (delete-file-recursively "foo-1.0.0")
  218. (set! test-source-hash
  219. (call-with-input-file file-name port-sha256))))
  220. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl"
  221. (begin
  222. (mkdir "foo-1.0.0.dist-info")
  223. (with-output-to-file "foo-1.0.0.dist-info/METADATA"
  224. (lambda ()
  225. (display test-metadata)))
  226. (let ((zip-file (string-append file-name ".zip")))
  227. ;; zip always adds a "zip" extension to the file it creates,
  228. ;; so we need to rename it.
  229. (system* "zip" "-q" zip-file "foo-1.0.0.dist-info/METADATA")
  230. (rename-file zip-file file-name))
  231. (delete-file-recursively "foo-1.0.0.dist-info")))
  232. (_ (error "Unexpected URL: " url)))))
  233. (mock ((guix http-client) http-fetch
  234. (lambda (url . rest)
  235. (match url
  236. ("https://pypi.org/pypi/foo/json"
  237. (values (open-input-string test-json)
  238. (string-length test-json)))
  239. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
  240. (_ (error "Unexpected URL: " url)))))
  241. ;; Not clearing the memoization cache here would mean returning the value
  242. ;; computed in the previous test.
  243. (invalidate-memoization! pypi->guix-package)
  244. (match (pypi->guix-package "foo")
  245. (('package
  246. ('name "python-foo")
  247. ('version "1.0.0")
  248. ('source ('origin
  249. ('method 'url-fetch)
  250. ('uri ('pypi-uri "foo" 'version))
  251. ('sha256
  252. ('base32
  253. (? string? hash)))))
  254. ('build-system 'python-build-system)
  255. ('propagated-inputs ('list 'python-bar 'python-baz))
  256. ('native-inputs ('list 'python-pytest))
  257. ('home-page "http://example.com")
  258. ('synopsis "summary")
  259. ('description "summary")
  260. ('license 'license:lgpl2.0))
  261. (string=? (bytevector->nix-base32-string
  262. test-source-hash)
  263. hash))
  264. (x
  265. (pk 'fail x #f))))))
  266. (test-assert "pypi->guix-package, no usable requirement file."
  267. ;; Replace network resources with sample data.
  268. (mock ((guix import utils) url-fetch
  269. (lambda (url file-name)
  270. (match url
  271. ("https://example.com/foo-1.0.0.tar.gz"
  272. (mkdir-p "foo-1.0.0/foo.egg-info/")
  273. (parameterize ((current-output-port (%make-void-port "rw+")))
  274. (system* "tar" "czvf" file-name "foo-1.0.0/"))
  275. (delete-file-recursively "foo-1.0.0")
  276. (set! test-source-hash
  277. (call-with-input-file file-name port-sha256)))
  278. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
  279. (_ (error "Unexpected URL: " url)))))
  280. (mock ((guix http-client) http-fetch
  281. (lambda (url . rest)
  282. (match url
  283. ("https://pypi.org/pypi/foo/json"
  284. (values (open-input-string test-json)
  285. (string-length test-json)))
  286. ("https://example.com/foo-1.0.0-py2.py3-none-any.whl" #f)
  287. (_ (error "Unexpected URL: " url)))))
  288. ;; Not clearing the memoization cache here would mean returning the value
  289. ;; computed in the previous test.
  290. (invalidate-memoization! pypi->guix-package)
  291. (match (pypi->guix-package "foo")
  292. (('package
  293. ('name "python-foo")
  294. ('version "1.0.0")
  295. ('source ('origin
  296. ('method 'url-fetch)
  297. ('uri ('pypi-uri "foo" 'version))
  298. ('sha256
  299. ('base32
  300. (? string? hash)))))
  301. ('build-system 'python-build-system)
  302. ('home-page "http://example.com")
  303. ('synopsis "summary")
  304. ('description "summary")
  305. ('license 'license:lgpl2.0))
  306. (string=? (bytevector->nix-base32-string
  307. test-source-hash)
  308. hash))
  309. (x
  310. (pk 'fail x #f))))))
  311. (test-end "pypi")