test_packageindex.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. from __future__ import absolute_import
  2. import sys
  3. import os
  4. import distutils.errors
  5. from setuptools.extern import six
  6. from setuptools.extern.six.moves import urllib, http_client
  7. from .textwrap import DALS
  8. import pkg_resources
  9. import setuptools.package_index
  10. from setuptools.tests.server import IndexServer
  11. class TestPackageIndex:
  12. def test_bad_url_bad_port(self):
  13. index = setuptools.package_index.PackageIndex()
  14. url = 'http://127.0.0.1:0/nonesuch/test_package_index'
  15. try:
  16. v = index.open_url(url)
  17. except Exception as v:
  18. assert url in str(v)
  19. else:
  20. assert isinstance(v, urllib.error.HTTPError)
  21. def test_bad_url_typo(self):
  22. # issue 16
  23. # easy_install inquant.contentmirror.plone breaks because of a typo
  24. # in its home URL
  25. index = setuptools.package_index.PackageIndex(
  26. hosts=('www.example.com',)
  27. )
  28. url = 'url:%20https://svn.plone.org/svn/collective/inquant.contentmirror.plone/trunk'
  29. try:
  30. v = index.open_url(url)
  31. except Exception as v:
  32. assert url in str(v)
  33. else:
  34. assert isinstance(v, urllib.error.HTTPError)
  35. def test_bad_url_bad_status_line(self):
  36. index = setuptools.package_index.PackageIndex(
  37. hosts=('www.example.com',)
  38. )
  39. def _urlopen(*args):
  40. raise http_client.BadStatusLine('line')
  41. index.opener = _urlopen
  42. url = 'http://example.com'
  43. try:
  44. v = index.open_url(url)
  45. except Exception as v:
  46. assert 'line' in str(v)
  47. else:
  48. raise AssertionError('Should have raise here!')
  49. def test_bad_url_double_scheme(self):
  50. """
  51. A bad URL with a double scheme should raise a DistutilsError.
  52. """
  53. index = setuptools.package_index.PackageIndex(
  54. hosts=('www.example.com',)
  55. )
  56. # issue 20
  57. url = 'http://http://svn.pythonpaste.org/Paste/wphp/trunk'
  58. try:
  59. index.open_url(url)
  60. except distutils.errors.DistutilsError as error:
  61. msg = six.text_type(error)
  62. assert 'nonnumeric port' in msg or 'getaddrinfo failed' in msg or 'Name or service not known' in msg
  63. return
  64. raise RuntimeError("Did not raise")
  65. def test_bad_url_screwy_href(self):
  66. index = setuptools.package_index.PackageIndex(
  67. hosts=('www.example.com',)
  68. )
  69. # issue #160
  70. if sys.version_info[0] == 2 and sys.version_info[1] == 7:
  71. # this should not fail
  72. url = 'http://example.com'
  73. page = ('<a href="http://www.famfamfam.com]('
  74. 'http://www.famfamfam.com/">')
  75. index.process_index(url, page)
  76. def test_url_ok(self):
  77. index = setuptools.package_index.PackageIndex(
  78. hosts=('www.example.com',)
  79. )
  80. url = 'file:///tmp/test_package_index'
  81. assert index.url_ok(url, True)
  82. def test_links_priority(self):
  83. """
  84. Download links from the pypi simple index should be used before
  85. external download links.
  86. https://bitbucket.org/tarek/distribute/issue/163
  87. Usecase :
  88. - someone uploads a package on pypi, a md5 is generated
  89. - someone manually copies this link (with the md5 in the url) onto an
  90. external page accessible from the package page.
  91. - someone reuploads the package (with a different md5)
  92. - while easy_installing, an MD5 error occurs because the external link
  93. is used
  94. -> Setuptools should use the link from pypi, not the external one.
  95. """
  96. if sys.platform.startswith('java'):
  97. # Skip this test on jython because binding to :0 fails
  98. return
  99. # start an index server
  100. server = IndexServer()
  101. server.start()
  102. index_url = server.base_url() + 'test_links_priority/simple/'
  103. # scan a test index
  104. pi = setuptools.package_index.PackageIndex(index_url)
  105. requirement = pkg_resources.Requirement.parse('foobar')
  106. pi.find_packages(requirement)
  107. server.stop()
  108. # the distribution has been found
  109. assert 'foobar' in pi
  110. # we have only one link, because links are compared without md5
  111. assert len(pi['foobar'])==1
  112. # the link should be from the index
  113. assert 'correct_md5' in pi['foobar'][0].location
  114. def test_parse_bdist_wininst(self):
  115. parse = setuptools.package_index.parse_bdist_wininst
  116. actual = parse('reportlab-2.5.win32-py2.4.exe')
  117. expected = 'reportlab-2.5', '2.4', 'win32'
  118. assert actual == expected
  119. actual = parse('reportlab-2.5.win32.exe')
  120. expected = 'reportlab-2.5', None, 'win32'
  121. assert actual == expected
  122. actual = parse('reportlab-2.5.win-amd64-py2.7.exe')
  123. expected = 'reportlab-2.5', '2.7', 'win-amd64'
  124. assert actual == expected
  125. actual = parse('reportlab-2.5.win-amd64.exe')
  126. expected = 'reportlab-2.5', None, 'win-amd64'
  127. assert actual == expected
  128. def test__vcs_split_rev_from_url(self):
  129. """
  130. Test the basic usage of _vcs_split_rev_from_url
  131. """
  132. vsrfu = setuptools.package_index.PackageIndex._vcs_split_rev_from_url
  133. url, rev = vsrfu('https://example.com/bar@2995')
  134. assert url == 'https://example.com/bar'
  135. assert rev == '2995'
  136. def test_local_index(self, tmpdir):
  137. """
  138. local_open should be able to read an index from the file system.
  139. """
  140. index_file = tmpdir / 'index.html'
  141. with index_file.open('w') as f:
  142. f.write('<div>content</div>')
  143. url = 'file:' + urllib.request.pathname2url(str(tmpdir)) + '/'
  144. res = setuptools.package_index.local_open(url)
  145. assert 'content' in res.read()
  146. class TestContentCheckers:
  147. def test_md5(self):
  148. checker = setuptools.package_index.HashChecker.from_url(
  149. 'http://foo/bar#md5=f12895fdffbd45007040d2e44df98478')
  150. checker.feed('You should probably not be using MD5'.encode('ascii'))
  151. assert checker.hash.hexdigest() == 'f12895fdffbd45007040d2e44df98478'
  152. assert checker.is_valid()
  153. def test_other_fragment(self):
  154. "Content checks should succeed silently if no hash is present"
  155. checker = setuptools.package_index.HashChecker.from_url(
  156. 'http://foo/bar#something%20completely%20different')
  157. checker.feed('anything'.encode('ascii'))
  158. assert checker.is_valid()
  159. def test_blank_md5(self):
  160. "Content checks should succeed if a hash is empty"
  161. checker = setuptools.package_index.HashChecker.from_url(
  162. 'http://foo/bar#md5=')
  163. checker.feed('anything'.encode('ascii'))
  164. assert checker.is_valid()
  165. def test_get_hash_name_md5(self):
  166. checker = setuptools.package_index.HashChecker.from_url(
  167. 'http://foo/bar#md5=f12895fdffbd45007040d2e44df98478')
  168. assert checker.hash_name == 'md5'
  169. def test_report(self):
  170. checker = setuptools.package_index.HashChecker.from_url(
  171. 'http://foo/bar#md5=f12895fdffbd45007040d2e44df98478')
  172. rep = checker.report(lambda x: x, 'My message about %s')
  173. assert rep == 'My message about md5'
  174. class TestPyPIConfig:
  175. def test_percent_in_password(self, tmpdir, monkeypatch):
  176. monkeypatch.setitem(os.environ, 'HOME', str(tmpdir))
  177. pypirc = tmpdir / '.pypirc'
  178. with pypirc.open('w') as strm:
  179. strm.write(DALS("""
  180. [pypi]
  181. repository=https://pypi.python.org
  182. username=jaraco
  183. password=pity%
  184. """))
  185. cfg = setuptools.package_index.PyPIConfig()
  186. cred = cfg.creds_by_repository['https://pypi.python.org']
  187. assert cred.username == 'jaraco'
  188. assert cred.password == 'pity%'