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