| 1 | n/a | import os |
|---|
| 2 | n/a | import io |
|---|
| 3 | n/a | import csv |
|---|
| 4 | n/a | import sys |
|---|
| 5 | n/a | import shutil |
|---|
| 6 | n/a | import tempfile |
|---|
| 7 | n/a | from hashlib import md5 |
|---|
| 8 | n/a | from textwrap import dedent |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | from packaging.tests.test_util import GlobTestCaseBase |
|---|
| 11 | n/a | from packaging.tests.support import requires_zlib |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | import packaging.database |
|---|
| 14 | n/a | from packaging.config import get_resources_dests |
|---|
| 15 | n/a | from packaging.errors import PackagingError |
|---|
| 16 | n/a | from packaging.metadata import Metadata |
|---|
| 17 | n/a | from packaging.tests import unittest, support |
|---|
| 18 | n/a | from packaging.database import ( |
|---|
| 19 | n/a | Distribution, EggInfoDistribution, get_distribution, get_distributions, |
|---|
| 20 | n/a | provides_distribution, obsoletes_distribution, get_file_users, |
|---|
| 21 | n/a | enable_cache, disable_cache, distinfo_dirname, _yield_distributions, |
|---|
| 22 | n/a | get_file, get_file_path) |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | # TODO Add a test for getting a distribution provided by another distribution |
|---|
| 25 | n/a | # TODO Add a test for absolute pathed RECORD items (e.g. /etc/myapp/config.ini) |
|---|
| 26 | n/a | # TODO Add tests from the former pep376 project (zipped site-packages, etc.) |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | def get_hexdigest(filename): |
|---|
| 30 | n/a | with open(filename, 'rb') as file: |
|---|
| 31 | n/a | checksum = md5(file.read()) |
|---|
| 32 | n/a | return checksum.hexdigest() |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | def record_pieces(path): |
|---|
| 36 | n/a | path = os.path.join(*path) |
|---|
| 37 | n/a | digest = get_hexdigest(path) |
|---|
| 38 | n/a | size = os.path.getsize(path) |
|---|
| 39 | n/a | return path, digest, size |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | class FakeDistsMixin: |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | def setUp(self): |
|---|
| 45 | n/a | super(FakeDistsMixin, self).setUp() |
|---|
| 46 | n/a | self.addCleanup(enable_cache) |
|---|
| 47 | n/a | disable_cache() |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | # make a copy that we can write into for our fake installed |
|---|
| 50 | n/a | # distributions |
|---|
| 51 | n/a | tmpdir = tempfile.mkdtemp() |
|---|
| 52 | n/a | self.addCleanup(shutil.rmtree, tmpdir) |
|---|
| 53 | n/a | self.fake_dists_path = os.path.realpath( |
|---|
| 54 | n/a | os.path.join(tmpdir, 'fake_dists')) |
|---|
| 55 | n/a | fake_dists_src = os.path.abspath( |
|---|
| 56 | n/a | os.path.join(os.path.dirname(__file__), 'fake_dists')) |
|---|
| 57 | n/a | shutil.copytree(fake_dists_src, self.fake_dists_path) |
|---|
| 58 | n/a | # XXX ugly workaround: revert copystat calls done by shutil behind our |
|---|
| 59 | n/a | # back (to avoid getting a read-only copy of a read-only file). we |
|---|
| 60 | n/a | # could pass a custom copy_function to change the mode of files, but |
|---|
| 61 | n/a | # shutil gives no control over the mode of directories :( |
|---|
| 62 | n/a | # see http://bugs.python.org/issue1666318 |
|---|
| 63 | n/a | for root, dirs, files in os.walk(self.fake_dists_path): |
|---|
| 64 | n/a | os.chmod(root, 0o755) |
|---|
| 65 | n/a | for f in files: |
|---|
| 66 | n/a | os.chmod(os.path.join(root, f), 0o644) |
|---|
| 67 | n/a | for d in dirs: |
|---|
| 68 | n/a | os.chmod(os.path.join(root, d), 0o755) |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | class CommonDistributionTests(FakeDistsMixin): |
|---|
| 72 | n/a | """Mixin used to test the interface common to both Distribution classes. |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | Derived classes define cls, sample_dist, dirs and records. These |
|---|
| 75 | n/a | attributes are used in test methods. See source code for details. |
|---|
| 76 | n/a | """ |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | def test_instantiation(self): |
|---|
| 79 | n/a | # check that useful attributes are here |
|---|
| 80 | n/a | name, version, distdir = self.sample_dist |
|---|
| 81 | n/a | here = os.path.abspath(os.path.dirname(__file__)) |
|---|
| 82 | n/a | dist_path = os.path.join(here, 'fake_dists', distdir) |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | dist = self.dist = self.cls(dist_path) |
|---|
| 85 | n/a | self.assertEqual(dist.path, dist_path) |
|---|
| 86 | n/a | self.assertEqual(dist.name, name) |
|---|
| 87 | n/a | self.assertEqual(dist.metadata['Name'], name) |
|---|
| 88 | n/a | self.assertIsInstance(dist.metadata, Metadata) |
|---|
| 89 | n/a | self.assertEqual(dist.version, version) |
|---|
| 90 | n/a | self.assertEqual(dist.metadata['Version'], version) |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | @requires_zlib |
|---|
| 93 | n/a | def test_repr(self): |
|---|
| 94 | n/a | dist = self.cls(self.dirs[0]) |
|---|
| 95 | n/a | # just check that the class name is in the repr |
|---|
| 96 | n/a | self.assertIn(self.cls.__name__, repr(dist)) |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | @requires_zlib |
|---|
| 99 | n/a | def test_comparison(self): |
|---|
| 100 | n/a | # tests for __eq__ and __hash__ |
|---|
| 101 | n/a | dist = self.cls(self.dirs[0]) |
|---|
| 102 | n/a | dist2 = self.cls(self.dirs[0]) |
|---|
| 103 | n/a | dist3 = self.cls(self.dirs[1]) |
|---|
| 104 | n/a | self.assertIn(dist, {dist: True}) |
|---|
| 105 | n/a | self.assertEqual(dist, dist) |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | self.assertIsNot(dist, dist2) |
|---|
| 108 | n/a | self.assertEqual(dist, dist2) |
|---|
| 109 | n/a | self.assertNotEqual(dist, dist3) |
|---|
| 110 | n/a | self.assertNotEqual(dist, ()) |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | def test_list_installed_files(self): |
|---|
| 113 | n/a | for dir_ in self.dirs: |
|---|
| 114 | n/a | dist = self.cls(dir_) |
|---|
| 115 | n/a | for path, md5_, size in dist.list_installed_files(): |
|---|
| 116 | n/a | record_data = self.records[dist.path] |
|---|
| 117 | n/a | self.assertIn(path, record_data) |
|---|
| 118 | n/a | self.assertEqual(md5_, record_data[path][0]) |
|---|
| 119 | n/a | self.assertEqual(size, record_data[path][1]) |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | class TestDistribution(CommonDistributionTests, unittest.TestCase): |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | cls = Distribution |
|---|
| 125 | n/a | sample_dist = 'choxie', '2.0.0.9', 'choxie-2.0.0.9.dist-info' |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | def setUp(self): |
|---|
| 128 | n/a | super(TestDistribution, self).setUp() |
|---|
| 129 | n/a | self.dirs = [os.path.join(self.fake_dists_path, f) |
|---|
| 130 | n/a | for f in os.listdir(self.fake_dists_path) |
|---|
| 131 | n/a | if f.endswith('.dist-info')] |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | self.records = {} |
|---|
| 134 | n/a | for distinfo_dir in self.dirs: |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | record_file = os.path.join(distinfo_dir, 'RECORD') |
|---|
| 137 | n/a | with open(record_file, 'w') as file: |
|---|
| 138 | n/a | record_writer = csv.writer( |
|---|
| 139 | n/a | file, delimiter=',', quoting=csv.QUOTE_NONE, |
|---|
| 140 | n/a | lineterminator='\n') |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | dist_location = distinfo_dir.replace('.dist-info', '') |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | for path, dirs, files in os.walk(dist_location): |
|---|
| 145 | n/a | for f in files: |
|---|
| 146 | n/a | record_writer.writerow(record_pieces((path, f))) |
|---|
| 147 | n/a | for file in ('INSTALLER', 'METADATA', 'REQUESTED'): |
|---|
| 148 | n/a | record_writer.writerow(record_pieces((distinfo_dir, file))) |
|---|
| 149 | n/a | record_writer.writerow([record_file]) |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | with open(record_file) as file: |
|---|
| 152 | n/a | record_reader = csv.reader(file, lineterminator='\n') |
|---|
| 153 | n/a | record_data = {} |
|---|
| 154 | n/a | for row in record_reader: |
|---|
| 155 | n/a | if row == []: |
|---|
| 156 | n/a | continue |
|---|
| 157 | n/a | path, md5_, size = (row[:] + |
|---|
| 158 | n/a | [None for i in range(len(row), 3)]) |
|---|
| 159 | n/a | record_data[path] = md5_, size |
|---|
| 160 | n/a | self.records[distinfo_dir] = record_data |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | def test_instantiation(self): |
|---|
| 163 | n/a | super(TestDistribution, self).test_instantiation() |
|---|
| 164 | n/a | self.assertIsInstance(self.dist.requested, bool) |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | def test_uses(self): |
|---|
| 167 | n/a | # Test to determine if a distribution uses a specified file. |
|---|
| 168 | n/a | # Criteria to test against |
|---|
| 169 | n/a | distinfo_name = 'grammar-1.0a4' |
|---|
| 170 | n/a | distinfo_dir = os.path.join(self.fake_dists_path, |
|---|
| 171 | n/a | distinfo_name + '.dist-info') |
|---|
| 172 | n/a | true_path = [self.fake_dists_path, distinfo_name, |
|---|
| 173 | n/a | 'grammar', 'utils.py'] |
|---|
| 174 | n/a | true_path = os.path.join(*true_path) |
|---|
| 175 | n/a | false_path = [self.fake_dists_path, 'towel_stuff-0.1', 'towel_stuff', |
|---|
| 176 | n/a | '__init__.py'] |
|---|
| 177 | n/a | false_path = os.path.join(*false_path) |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | # Test if the distribution uses the file in question |
|---|
| 180 | n/a | dist = Distribution(distinfo_dir) |
|---|
| 181 | n/a | self.assertTrue(dist.uses(true_path), 'dist %r is supposed to use %r' % |
|---|
| 182 | n/a | (dist, true_path)) |
|---|
| 183 | n/a | self.assertFalse(dist.uses(false_path), 'dist %r is not supposed to ' |
|---|
| 184 | n/a | 'use %r' % (dist, true_path)) |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | def test_get_distinfo_file(self): |
|---|
| 187 | n/a | # Test the retrieval of dist-info file objects. |
|---|
| 188 | n/a | distinfo_name = 'choxie-2.0.0.9' |
|---|
| 189 | n/a | other_distinfo_name = 'grammar-1.0a4' |
|---|
| 190 | n/a | distinfo_dir = os.path.join(self.fake_dists_path, |
|---|
| 191 | n/a | distinfo_name + '.dist-info') |
|---|
| 192 | n/a | dist = Distribution(distinfo_dir) |
|---|
| 193 | n/a | # Test for known good file matches |
|---|
| 194 | n/a | distinfo_files = [ |
|---|
| 195 | n/a | # Relative paths |
|---|
| 196 | n/a | 'INSTALLER', 'METADATA', |
|---|
| 197 | n/a | # Absolute paths |
|---|
| 198 | n/a | os.path.join(distinfo_dir, 'RECORD'), |
|---|
| 199 | n/a | os.path.join(distinfo_dir, 'REQUESTED'), |
|---|
| 200 | n/a | ] |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | for distfile in distinfo_files: |
|---|
| 203 | n/a | with dist.get_distinfo_file(distfile) as value: |
|---|
| 204 | n/a | self.assertIsInstance(value, io.TextIOWrapper) |
|---|
| 205 | n/a | # Is it the correct file? |
|---|
| 206 | n/a | self.assertEqual(value.name, |
|---|
| 207 | n/a | os.path.join(distinfo_dir, distfile)) |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | # Test an absolute path that is part of another distributions dist-info |
|---|
| 210 | n/a | other_distinfo_file = os.path.join( |
|---|
| 211 | n/a | self.fake_dists_path, other_distinfo_name + '.dist-info', |
|---|
| 212 | n/a | 'REQUESTED') |
|---|
| 213 | n/a | self.assertRaises(PackagingError, dist.get_distinfo_file, |
|---|
| 214 | n/a | other_distinfo_file) |
|---|
| 215 | n/a | # Test for a file that should not exist |
|---|
| 216 | n/a | self.assertRaises(PackagingError, dist.get_distinfo_file, |
|---|
| 217 | n/a | 'MAGICFILE') |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | def test_list_distinfo_files(self): |
|---|
| 220 | n/a | distinfo_name = 'towel_stuff-0.1' |
|---|
| 221 | n/a | distinfo_dir = os.path.join(self.fake_dists_path, |
|---|
| 222 | n/a | distinfo_name + '.dist-info') |
|---|
| 223 | n/a | dist = Distribution(distinfo_dir) |
|---|
| 224 | n/a | # Test for the iteration of the raw path |
|---|
| 225 | n/a | distinfo_files = [os.path.join(distinfo_dir, filename) for filename in |
|---|
| 226 | n/a | os.listdir(distinfo_dir)] |
|---|
| 227 | n/a | found = dist.list_distinfo_files() |
|---|
| 228 | n/a | self.assertEqual(sorted(found), sorted(distinfo_files)) |
|---|
| 229 | n/a | # Test for the iteration of local absolute paths |
|---|
| 230 | n/a | distinfo_files = [os.path.join(sys.prefix, distinfo_dir, path) for |
|---|
| 231 | n/a | path in distinfo_files] |
|---|
| 232 | n/a | found = sorted(dist.list_distinfo_files(local=True)) |
|---|
| 233 | n/a | if os.sep != '/': |
|---|
| 234 | n/a | self.assertNotIn('/', found[0]) |
|---|
| 235 | n/a | self.assertIn(os.sep, found[0]) |
|---|
| 236 | n/a | self.assertEqual(found, sorted(distinfo_files)) |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | def test_get_resources_path(self): |
|---|
| 239 | n/a | distinfo_name = 'babar-0.1' |
|---|
| 240 | n/a | distinfo_dir = os.path.join(self.fake_dists_path, |
|---|
| 241 | n/a | distinfo_name + '.dist-info') |
|---|
| 242 | n/a | dist = Distribution(distinfo_dir) |
|---|
| 243 | n/a | resource_path = dist.get_resource_path('babar.png') |
|---|
| 244 | n/a | self.assertEqual(resource_path, 'babar.png') |
|---|
| 245 | n/a | self.assertRaises(KeyError, dist.get_resource_path, 'notexist') |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | class TestEggInfoDistribution(CommonDistributionTests, |
|---|
| 249 | n/a | support.LoggingCatcher, |
|---|
| 250 | n/a | unittest.TestCase): |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | cls = EggInfoDistribution |
|---|
| 253 | n/a | sample_dist = 'bacon', '0.1', 'bacon-0.1.egg-info' |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | def setUp(self): |
|---|
| 256 | n/a | super(TestEggInfoDistribution, self).setUp() |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | self.dirs = [os.path.join(self.fake_dists_path, f) |
|---|
| 259 | n/a | for f in os.listdir(self.fake_dists_path) |
|---|
| 260 | n/a | if f.endswith('.egg') or f.endswith('.egg-info')] |
|---|
| 261 | n/a | |
|---|
| 262 | n/a | self.records = {} |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | @unittest.skip('not implemented yet') |
|---|
| 265 | n/a | def test_list_installed_files(self): |
|---|
| 266 | n/a | # EggInfoDistribution defines list_installed_files but there is no |
|---|
| 267 | n/a | # test for it yet; someone with setuptools expertise needs to add a |
|---|
| 268 | n/a | # file with the list of installed files for one of the egg fake dists |
|---|
| 269 | n/a | # and write the support code to populate self.records (and then delete |
|---|
| 270 | n/a | # this method) |
|---|
| 271 | n/a | pass |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | class TestDatabase(support.LoggingCatcher, |
|---|
| 275 | n/a | FakeDistsMixin, |
|---|
| 276 | n/a | unittest.TestCase): |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | def setUp(self): |
|---|
| 279 | n/a | super(TestDatabase, self).setUp() |
|---|
| 280 | n/a | sys.path.insert(0, self.fake_dists_path) |
|---|
| 281 | n/a | self.addCleanup(sys.path.remove, self.fake_dists_path) |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | def test_caches(self): |
|---|
| 284 | n/a | # sanity check for internal caches |
|---|
| 285 | n/a | for name in ('_cache_name', '_cache_name_egg', |
|---|
| 286 | n/a | '_cache_path', '_cache_path_egg'): |
|---|
| 287 | n/a | self.assertEqual(getattr(packaging.database, name), {}) |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | def test_distinfo_dirname(self): |
|---|
| 290 | n/a | # Given a name and a version, we expect the distinfo_dirname function |
|---|
| 291 | n/a | # to return a standard distribution information directory name. |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | items = [ |
|---|
| 294 | n/a | # (name, version, standard_dirname) |
|---|
| 295 | n/a | # Test for a very simple single word name and decimal version |
|---|
| 296 | n/a | # number |
|---|
| 297 | n/a | ('docutils', '0.5', 'docutils-0.5.dist-info'), |
|---|
| 298 | n/a | # Test for another except this time with a '-' in the name, which |
|---|
| 299 | n/a | # needs to be transformed during the name lookup |
|---|
| 300 | n/a | ('python-ldap', '2.5', 'python_ldap-2.5.dist-info'), |
|---|
| 301 | n/a | # Test for both '-' in the name and a funky version number |
|---|
| 302 | n/a | ('python-ldap', '2.5 a---5', 'python_ldap-2.5 a---5.dist-info'), |
|---|
| 303 | n/a | ] |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | # Loop through the items to validate the results |
|---|
| 306 | n/a | for name, version, standard_dirname in items: |
|---|
| 307 | n/a | dirname = distinfo_dirname(name, version) |
|---|
| 308 | n/a | self.assertEqual(dirname, standard_dirname) |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | @requires_zlib |
|---|
| 311 | n/a | def test_get_distributions(self): |
|---|
| 312 | n/a | # Lookup all distributions found in the ``sys.path``. |
|---|
| 313 | n/a | # This test could potentially pick up other installed distributions |
|---|
| 314 | n/a | fake_dists = [('grammar', '1.0a4'), ('choxie', '2.0.0.9'), |
|---|
| 315 | n/a | ('towel-stuff', '0.1'), ('babar', '0.1')] |
|---|
| 316 | n/a | found_dists = [] |
|---|
| 317 | n/a | |
|---|
| 318 | n/a | # Verify the fake dists have been found. |
|---|
| 319 | n/a | dists = [dist for dist in get_distributions()] |
|---|
| 320 | n/a | for dist in dists: |
|---|
| 321 | n/a | self.assertIsInstance(dist, Distribution) |
|---|
| 322 | n/a | if (dist.name in dict(fake_dists) and |
|---|
| 323 | n/a | dist.path.startswith(self.fake_dists_path)): |
|---|
| 324 | n/a | found_dists.append((dist.name, dist.version)) |
|---|
| 325 | n/a | else: |
|---|
| 326 | n/a | # check that it doesn't find anything more than this |
|---|
| 327 | n/a | self.assertFalse(dist.path.startswith(self.fake_dists_path)) |
|---|
| 328 | n/a | # otherwise we don't care what other distributions are found |
|---|
| 329 | n/a | |
|---|
| 330 | n/a | # Finally, test that we found all that we were looking for |
|---|
| 331 | n/a | self.assertEqual(sorted(found_dists), sorted(fake_dists)) |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | # Now, test if the egg-info distributions are found correctly as well |
|---|
| 334 | n/a | fake_dists += [('bacon', '0.1'), ('cheese', '2.0.2'), |
|---|
| 335 | n/a | ('coconuts-aster', '10.3'), |
|---|
| 336 | n/a | ('banana', '0.4'), ('strawberry', '0.6'), |
|---|
| 337 | n/a | ('truffles', '5.0'), ('nut', 'funkyversion')] |
|---|
| 338 | n/a | found_dists = [] |
|---|
| 339 | n/a | |
|---|
| 340 | n/a | dists = [dist for dist in get_distributions(use_egg_info=True)] |
|---|
| 341 | n/a | for dist in dists: |
|---|
| 342 | n/a | self.assertIsInstance(dist, (Distribution, EggInfoDistribution)) |
|---|
| 343 | n/a | if (dist.name in dict(fake_dists) and |
|---|
| 344 | n/a | dist.path.startswith(self.fake_dists_path)): |
|---|
| 345 | n/a | found_dists.append((dist.name, dist.version)) |
|---|
| 346 | n/a | else: |
|---|
| 347 | n/a | self.assertFalse(dist.path.startswith(self.fake_dists_path)) |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | self.assertEqual(sorted(fake_dists), sorted(found_dists)) |
|---|
| 350 | n/a | |
|---|
| 351 | n/a | @requires_zlib |
|---|
| 352 | n/a | def test_get_distribution(self): |
|---|
| 353 | n/a | # Test for looking up a distribution by name. |
|---|
| 354 | n/a | # Test the lookup of the towel-stuff distribution |
|---|
| 355 | n/a | name = 'towel-stuff' # Note: This is different from the directory name |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | # Lookup the distribution |
|---|
| 358 | n/a | dist = get_distribution(name) |
|---|
| 359 | n/a | self.assertIsInstance(dist, Distribution) |
|---|
| 360 | n/a | self.assertEqual(dist.name, name) |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | # Verify that an unknown distribution returns None |
|---|
| 363 | n/a | self.assertIsNone(get_distribution('bogus')) |
|---|
| 364 | n/a | |
|---|
| 365 | n/a | # Verify partial name matching doesn't work |
|---|
| 366 | n/a | self.assertIsNone(get_distribution('towel')) |
|---|
| 367 | n/a | |
|---|
| 368 | n/a | # Verify that it does not find egg-info distributions, when not |
|---|
| 369 | n/a | # instructed to |
|---|
| 370 | n/a | self.assertIsNone(get_distribution('bacon')) |
|---|
| 371 | n/a | self.assertIsNone(get_distribution('cheese')) |
|---|
| 372 | n/a | self.assertIsNone(get_distribution('strawberry')) |
|---|
| 373 | n/a | self.assertIsNone(get_distribution('banana')) |
|---|
| 374 | n/a | |
|---|
| 375 | n/a | # Now check that it works well in both situations, when egg-info |
|---|
| 376 | n/a | # is a file and directory respectively. |
|---|
| 377 | n/a | dist = get_distribution('cheese', use_egg_info=True) |
|---|
| 378 | n/a | self.assertIsInstance(dist, EggInfoDistribution) |
|---|
| 379 | n/a | self.assertEqual(dist.name, 'cheese') |
|---|
| 380 | n/a | |
|---|
| 381 | n/a | dist = get_distribution('bacon', use_egg_info=True) |
|---|
| 382 | n/a | self.assertIsInstance(dist, EggInfoDistribution) |
|---|
| 383 | n/a | self.assertEqual(dist.name, 'bacon') |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | dist = get_distribution('banana', use_egg_info=True) |
|---|
| 386 | n/a | self.assertIsInstance(dist, EggInfoDistribution) |
|---|
| 387 | n/a | self.assertEqual(dist.name, 'banana') |
|---|
| 388 | n/a | |
|---|
| 389 | n/a | dist = get_distribution('strawberry', use_egg_info=True) |
|---|
| 390 | n/a | self.assertIsInstance(dist, EggInfoDistribution) |
|---|
| 391 | n/a | self.assertEqual(dist.name, 'strawberry') |
|---|
| 392 | n/a | |
|---|
| 393 | n/a | def test_get_file_users(self): |
|---|
| 394 | n/a | # Test the iteration of distributions that use a file. |
|---|
| 395 | n/a | name = 'towel_stuff-0.1' |
|---|
| 396 | n/a | path = os.path.join(self.fake_dists_path, name, |
|---|
| 397 | n/a | 'towel_stuff', '__init__.py') |
|---|
| 398 | n/a | for dist in get_file_users(path): |
|---|
| 399 | n/a | self.assertIsInstance(dist, Distribution) |
|---|
| 400 | n/a | self.assertEqual(dist.name, name) |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | @requires_zlib |
|---|
| 403 | n/a | def test_provides(self): |
|---|
| 404 | n/a | # Test for looking up distributions by what they provide |
|---|
| 405 | n/a | checkLists = lambda x, y: self.assertEqual(sorted(x), sorted(y)) |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | l = [dist.name for dist in provides_distribution('truffles')] |
|---|
| 408 | n/a | checkLists(l, ['choxie', 'towel-stuff']) |
|---|
| 409 | n/a | |
|---|
| 410 | n/a | l = [dist.name for dist in provides_distribution('truffles', '1.0')] |
|---|
| 411 | n/a | checkLists(l, ['choxie']) |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | l = [dist.name for dist in provides_distribution('truffles', '1.0', |
|---|
| 414 | n/a | use_egg_info=True)] |
|---|
| 415 | n/a | checkLists(l, ['choxie', 'cheese']) |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | l = [dist.name for dist in provides_distribution('truffles', '1.1.2')] |
|---|
| 418 | n/a | checkLists(l, ['towel-stuff']) |
|---|
| 419 | n/a | |
|---|
| 420 | n/a | l = [dist.name for dist in provides_distribution('truffles', '1.1')] |
|---|
| 421 | n/a | checkLists(l, ['towel-stuff']) |
|---|
| 422 | n/a | |
|---|
| 423 | n/a | l = [dist.name for dist in provides_distribution('truffles', |
|---|
| 424 | n/a | '!=1.1,<=2.0')] |
|---|
| 425 | n/a | checkLists(l, ['choxie']) |
|---|
| 426 | n/a | |
|---|
| 427 | n/a | l = [dist.name for dist in provides_distribution('truffles', |
|---|
| 428 | n/a | '!=1.1,<=2.0', |
|---|
| 429 | n/a | use_egg_info=True)] |
|---|
| 430 | n/a | checkLists(l, ['choxie', 'bacon', 'cheese']) |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | l = [dist.name for dist in provides_distribution('truffles', '>1.0')] |
|---|
| 433 | n/a | checkLists(l, ['towel-stuff']) |
|---|
| 434 | n/a | |
|---|
| 435 | n/a | l = [dist.name for dist in provides_distribution('truffles', '>1.5')] |
|---|
| 436 | n/a | checkLists(l, []) |
|---|
| 437 | n/a | |
|---|
| 438 | n/a | l = [dist.name for dist in provides_distribution('truffles', '>1.5', |
|---|
| 439 | n/a | use_egg_info=True)] |
|---|
| 440 | n/a | checkLists(l, ['bacon']) |
|---|
| 441 | n/a | |
|---|
| 442 | n/a | l = [dist.name for dist in provides_distribution('truffles', '>=1.0')] |
|---|
| 443 | n/a | checkLists(l, ['choxie', 'towel-stuff']) |
|---|
| 444 | n/a | |
|---|
| 445 | n/a | l = [dist.name for dist in provides_distribution('strawberry', '0.6', |
|---|
| 446 | n/a | use_egg_info=True)] |
|---|
| 447 | n/a | checkLists(l, ['coconuts-aster']) |
|---|
| 448 | n/a | |
|---|
| 449 | n/a | l = [dist.name for dist in provides_distribution('strawberry', '>=0.5', |
|---|
| 450 | n/a | use_egg_info=True)] |
|---|
| 451 | n/a | checkLists(l, ['coconuts-aster']) |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | l = [dist.name for dist in provides_distribution('strawberry', '>0.6', |
|---|
| 454 | n/a | use_egg_info=True)] |
|---|
| 455 | n/a | checkLists(l, []) |
|---|
| 456 | n/a | |
|---|
| 457 | n/a | l = [dist.name for dist in provides_distribution('banana', '0.4', |
|---|
| 458 | n/a | use_egg_info=True)] |
|---|
| 459 | n/a | checkLists(l, ['coconuts-aster']) |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | l = [dist.name for dist in provides_distribution('banana', '>=0.3', |
|---|
| 462 | n/a | use_egg_info=True)] |
|---|
| 463 | n/a | checkLists(l, ['coconuts-aster']) |
|---|
| 464 | n/a | |
|---|
| 465 | n/a | l = [dist.name for dist in provides_distribution('banana', '!=0.4', |
|---|
| 466 | n/a | use_egg_info=True)] |
|---|
| 467 | n/a | checkLists(l, []) |
|---|
| 468 | n/a | |
|---|
| 469 | n/a | @requires_zlib |
|---|
| 470 | n/a | def test_obsoletes(self): |
|---|
| 471 | n/a | # Test looking for distributions based on what they obsolete |
|---|
| 472 | n/a | checkLists = lambda x, y: self.assertEqual(sorted(x), sorted(y)) |
|---|
| 473 | n/a | |
|---|
| 474 | n/a | l = [dist.name for dist in obsoletes_distribution('truffles', '1.0')] |
|---|
| 475 | n/a | checkLists(l, []) |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | l = [dist.name for dist in obsoletes_distribution('truffles', '1.0', |
|---|
| 478 | n/a | use_egg_info=True)] |
|---|
| 479 | n/a | checkLists(l, ['cheese', 'bacon']) |
|---|
| 480 | n/a | |
|---|
| 481 | n/a | l = [dist.name for dist in obsoletes_distribution('truffles', '0.8')] |
|---|
| 482 | n/a | checkLists(l, ['choxie']) |
|---|
| 483 | n/a | |
|---|
| 484 | n/a | l = [dist.name for dist in obsoletes_distribution('truffles', '0.8', |
|---|
| 485 | n/a | use_egg_info=True)] |
|---|
| 486 | n/a | checkLists(l, ['choxie', 'cheese']) |
|---|
| 487 | n/a | |
|---|
| 488 | n/a | l = [dist.name for dist in obsoletes_distribution('truffles', '0.9.6')] |
|---|
| 489 | n/a | checkLists(l, ['choxie', 'towel-stuff']) |
|---|
| 490 | n/a | |
|---|
| 491 | n/a | l = [dist.name for dist in obsoletes_distribution('truffles', |
|---|
| 492 | n/a | '0.5.2.3')] |
|---|
| 493 | n/a | checkLists(l, ['choxie', 'towel-stuff']) |
|---|
| 494 | n/a | |
|---|
| 495 | n/a | l = [dist.name for dist in obsoletes_distribution('truffles', '0.2')] |
|---|
| 496 | n/a | checkLists(l, ['towel-stuff']) |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | @requires_zlib |
|---|
| 499 | n/a | def test_yield_distribution(self): |
|---|
| 500 | n/a | # tests the internal function _yield_distributions |
|---|
| 501 | n/a | checkLists = lambda x, y: self.assertEqual(sorted(x), sorted(y)) |
|---|
| 502 | n/a | |
|---|
| 503 | n/a | eggs = [('bacon', '0.1'), ('banana', '0.4'), ('strawberry', '0.6'), |
|---|
| 504 | n/a | ('truffles', '5.0'), ('cheese', '2.0.2'), |
|---|
| 505 | n/a | ('coconuts-aster', '10.3'), ('nut', 'funkyversion')] |
|---|
| 506 | n/a | dists = [('choxie', '2.0.0.9'), ('grammar', '1.0a4'), |
|---|
| 507 | n/a | ('towel-stuff', '0.1'), ('babar', '0.1')] |
|---|
| 508 | n/a | |
|---|
| 509 | n/a | checkLists([], _yield_distributions(False, False, sys.path)) |
|---|
| 510 | n/a | |
|---|
| 511 | n/a | found = [(dist.name, dist.version) |
|---|
| 512 | n/a | for dist in _yield_distributions(False, True, sys.path) |
|---|
| 513 | n/a | if dist.path.startswith(self.fake_dists_path)] |
|---|
| 514 | n/a | checkLists(eggs, found) |
|---|
| 515 | n/a | |
|---|
| 516 | n/a | found = [(dist.name, dist.version) |
|---|
| 517 | n/a | for dist in _yield_distributions(True, False, sys.path) |
|---|
| 518 | n/a | if dist.path.startswith(self.fake_dists_path)] |
|---|
| 519 | n/a | checkLists(dists, found) |
|---|
| 520 | n/a | |
|---|
| 521 | n/a | found = [(dist.name, dist.version) |
|---|
| 522 | n/a | for dist in _yield_distributions(True, True, sys.path) |
|---|
| 523 | n/a | if dist.path.startswith(self.fake_dists_path)] |
|---|
| 524 | n/a | checkLists(dists + eggs, found) |
|---|
| 525 | n/a | |
|---|
| 526 | n/a | |
|---|
| 527 | n/a | class DataFilesTestCase(GlobTestCaseBase): |
|---|
| 528 | n/a | |
|---|
| 529 | n/a | def assertRulesMatch(self, rules, spec): |
|---|
| 530 | n/a | tempdir = self.build_files_tree(spec) |
|---|
| 531 | n/a | expected = self.clean_tree(spec) |
|---|
| 532 | n/a | result = get_resources_dests(tempdir, rules) |
|---|
| 533 | n/a | self.assertEqual(expected, result) |
|---|
| 534 | n/a | |
|---|
| 535 | n/a | def clean_tree(self, spec): |
|---|
| 536 | n/a | files = {} |
|---|
| 537 | n/a | for path, value in spec.items(): |
|---|
| 538 | n/a | if value is not None: |
|---|
| 539 | n/a | files[path] = value |
|---|
| 540 | n/a | return files |
|---|
| 541 | n/a | |
|---|
| 542 | n/a | def test_simple_glob(self): |
|---|
| 543 | n/a | rules = [('', '*.tpl', '{data}')] |
|---|
| 544 | n/a | spec = {'coucou.tpl': '{data}/coucou.tpl', |
|---|
| 545 | n/a | 'Donotwant': None} |
|---|
| 546 | n/a | self.assertRulesMatch(rules, spec) |
|---|
| 547 | n/a | |
|---|
| 548 | n/a | def test_multiple_match(self): |
|---|
| 549 | n/a | rules = [('scripts', '*.bin', '{appdata}'), |
|---|
| 550 | n/a | ('scripts', '*', '{appscript}')] |
|---|
| 551 | n/a | spec = {'scripts/script.bin': '{appscript}/script.bin', |
|---|
| 552 | n/a | 'Babarlikestrawberry': None} |
|---|
| 553 | n/a | self.assertRulesMatch(rules, spec) |
|---|
| 554 | n/a | |
|---|
| 555 | n/a | def test_set_match(self): |
|---|
| 556 | n/a | rules = [('scripts', '*.{bin,sh}', '{appscript}')] |
|---|
| 557 | n/a | spec = {'scripts/script.bin': '{appscript}/script.bin', |
|---|
| 558 | n/a | 'scripts/babar.sh': '{appscript}/babar.sh', |
|---|
| 559 | n/a | 'Babarlikestrawberry': None} |
|---|
| 560 | n/a | self.assertRulesMatch(rules, spec) |
|---|
| 561 | n/a | |
|---|
| 562 | n/a | def test_set_match_multiple(self): |
|---|
| 563 | n/a | rules = [('scripts', 'script{s,}.{bin,sh}', '{appscript}')] |
|---|
| 564 | n/a | spec = {'scripts/scripts.bin': '{appscript}/scripts.bin', |
|---|
| 565 | n/a | 'scripts/script.sh': '{appscript}/script.sh', |
|---|
| 566 | n/a | 'Babarlikestrawberry': None} |
|---|
| 567 | n/a | self.assertRulesMatch(rules, spec) |
|---|
| 568 | n/a | |
|---|
| 569 | n/a | def test_set_match_exclude(self): |
|---|
| 570 | n/a | rules = [('scripts', '*', '{appscript}'), |
|---|
| 571 | n/a | ('', os.path.join('**', '*.sh'), None)] |
|---|
| 572 | n/a | spec = {'scripts/scripts.bin': '{appscript}/scripts.bin', |
|---|
| 573 | n/a | 'scripts/script.sh': None, |
|---|
| 574 | n/a | 'Babarlikestrawberry': None} |
|---|
| 575 | n/a | self.assertRulesMatch(rules, spec) |
|---|
| 576 | n/a | |
|---|
| 577 | n/a | def test_glob_in_base(self): |
|---|
| 578 | n/a | rules = [('scrip*', '*.bin', '{appscript}')] |
|---|
| 579 | n/a | spec = {'scripts/scripts.bin': '{appscript}/scripts.bin', |
|---|
| 580 | n/a | 'scripouille/babar.bin': '{appscript}/babar.bin', |
|---|
| 581 | n/a | 'scriptortu/lotus.bin': '{appscript}/lotus.bin', |
|---|
| 582 | n/a | 'Babarlikestrawberry': None} |
|---|
| 583 | n/a | self.assertRulesMatch(rules, spec) |
|---|
| 584 | n/a | |
|---|
| 585 | n/a | def test_recursive_glob(self): |
|---|
| 586 | n/a | rules = [('', os.path.join('**', '*.bin'), '{binary}')] |
|---|
| 587 | n/a | spec = {'binary0.bin': '{binary}/binary0.bin', |
|---|
| 588 | n/a | 'scripts/binary1.bin': '{binary}/scripts/binary1.bin', |
|---|
| 589 | n/a | 'scripts/bin/binary2.bin': '{binary}/scripts/bin/binary2.bin', |
|---|
| 590 | n/a | 'you/kill/pandabear.guy': None} |
|---|
| 591 | n/a | self.assertRulesMatch(rules, spec) |
|---|
| 592 | n/a | |
|---|
| 593 | n/a | def test_final_exemple_glob(self): |
|---|
| 594 | n/a | rules = [ |
|---|
| 595 | n/a | ('mailman/database/schemas/', '*', '{appdata}/schemas'), |
|---|
| 596 | n/a | ('', os.path.join('**', '*.tpl'), '{appdata}/templates'), |
|---|
| 597 | n/a | ('', os.path.join('developer-docs', '**', '*.txt'), '{doc}'), |
|---|
| 598 | n/a | ('', 'README', '{doc}'), |
|---|
| 599 | n/a | ('mailman/etc/', '*', '{config}'), |
|---|
| 600 | n/a | ('mailman/foo/', os.path.join('**', 'bar', '*.cfg'), |
|---|
| 601 | n/a | '{config}/baz'), |
|---|
| 602 | n/a | ('mailman/foo/', os.path.join('**', '*.cfg'), '{config}/hmm'), |
|---|
| 603 | n/a | ('', 'some-new-semantic.sns', '{funky-crazy-category}'), |
|---|
| 604 | n/a | ] |
|---|
| 605 | n/a | spec = { |
|---|
| 606 | n/a | 'README': '{doc}/README', |
|---|
| 607 | n/a | 'some.tpl': '{appdata}/templates/some.tpl', |
|---|
| 608 | n/a | 'some-new-semantic.sns': |
|---|
| 609 | n/a | '{funky-crazy-category}/some-new-semantic.sns', |
|---|
| 610 | n/a | 'mailman/database/mailman.db': None, |
|---|
| 611 | n/a | 'mailman/database/schemas/blah.schema': |
|---|
| 612 | n/a | '{appdata}/schemas/blah.schema', |
|---|
| 613 | n/a | 'mailman/etc/my.cnf': '{config}/my.cnf', |
|---|
| 614 | n/a | 'mailman/foo/some/path/bar/my.cfg': |
|---|
| 615 | n/a | '{config}/hmm/some/path/bar/my.cfg', |
|---|
| 616 | n/a | 'mailman/foo/some/path/other.cfg': |
|---|
| 617 | n/a | '{config}/hmm/some/path/other.cfg', |
|---|
| 618 | n/a | 'developer-docs/index.txt': '{doc}/developer-docs/index.txt', |
|---|
| 619 | n/a | 'developer-docs/api/toc.txt': '{doc}/developer-docs/api/toc.txt', |
|---|
| 620 | n/a | } |
|---|
| 621 | n/a | self.maxDiff = None |
|---|
| 622 | n/a | self.assertRulesMatch(rules, spec) |
|---|
| 623 | n/a | |
|---|
| 624 | n/a | def test_get_file(self): |
|---|
| 625 | n/a | # Create a fake dist |
|---|
| 626 | n/a | temp_site_packages = tempfile.mkdtemp() |
|---|
| 627 | n/a | self.addCleanup(shutil.rmtree, temp_site_packages) |
|---|
| 628 | n/a | |
|---|
| 629 | n/a | dist_name = 'test' |
|---|
| 630 | n/a | dist_info = os.path.join(temp_site_packages, 'test-0.1.dist-info') |
|---|
| 631 | n/a | os.mkdir(dist_info) |
|---|
| 632 | n/a | |
|---|
| 633 | n/a | metadata_path = os.path.join(dist_info, 'METADATA') |
|---|
| 634 | n/a | resources_path = os.path.join(dist_info, 'RESOURCES') |
|---|
| 635 | n/a | |
|---|
| 636 | n/a | with open(metadata_path, 'w') as fp: |
|---|
| 637 | n/a | fp.write(dedent("""\ |
|---|
| 638 | n/a | Metadata-Version: 1.2 |
|---|
| 639 | n/a | Name: test |
|---|
| 640 | n/a | Version: 0.1 |
|---|
| 641 | n/a | Summary: test |
|---|
| 642 | n/a | Author: me |
|---|
| 643 | n/a | """)) |
|---|
| 644 | n/a | |
|---|
| 645 | n/a | test_path = 'test.cfg' |
|---|
| 646 | n/a | |
|---|
| 647 | n/a | fd, test_resource_path = tempfile.mkstemp() |
|---|
| 648 | n/a | os.close(fd) |
|---|
| 649 | n/a | self.addCleanup(os.remove, test_resource_path) |
|---|
| 650 | n/a | |
|---|
| 651 | n/a | with open(test_resource_path, 'w') as fp: |
|---|
| 652 | n/a | fp.write('Config') |
|---|
| 653 | n/a | |
|---|
| 654 | n/a | with open(resources_path, 'w') as fp: |
|---|
| 655 | n/a | fp.write('%s,%s' % (test_path, test_resource_path)) |
|---|
| 656 | n/a | |
|---|
| 657 | n/a | # Add fake site-packages to sys.path to retrieve fake dist |
|---|
| 658 | n/a | self.addCleanup(sys.path.remove, temp_site_packages) |
|---|
| 659 | n/a | sys.path.insert(0, temp_site_packages) |
|---|
| 660 | n/a | |
|---|
| 661 | n/a | # Force packaging.database to rescan the sys.path |
|---|
| 662 | n/a | self.addCleanup(enable_cache) |
|---|
| 663 | n/a | disable_cache() |
|---|
| 664 | n/a | |
|---|
| 665 | n/a | # Try to retrieve resources paths and files |
|---|
| 666 | n/a | self.assertEqual(get_file_path(dist_name, test_path), |
|---|
| 667 | n/a | test_resource_path) |
|---|
| 668 | n/a | self.assertRaises(KeyError, get_file_path, dist_name, 'i-dont-exist') |
|---|
| 669 | n/a | |
|---|
| 670 | n/a | with get_file(dist_name, test_path) as fp: |
|---|
| 671 | n/a | self.assertEqual(fp.read(), 'Config') |
|---|
| 672 | n/a | self.assertRaises(KeyError, get_file, dist_name, 'i-dont-exist') |
|---|
| 673 | n/a | |
|---|
| 674 | n/a | |
|---|
| 675 | n/a | def test_suite(): |
|---|
| 676 | n/a | suite = unittest.TestSuite() |
|---|
| 677 | n/a | load = unittest.defaultTestLoader.loadTestsFromTestCase |
|---|
| 678 | n/a | suite.addTest(load(TestDistribution)) |
|---|
| 679 | n/a | suite.addTest(load(TestEggInfoDistribution)) |
|---|
| 680 | n/a | suite.addTest(load(TestDatabase)) |
|---|
| 681 | n/a | suite.addTest(load(DataFilesTestCase)) |
|---|
| 682 | n/a | return suite |
|---|
| 683 | n/a | |
|---|
| 684 | n/a | |
|---|
| 685 | n/a | if __name__ == "__main__": |
|---|
| 686 | n/a | unittest.main(defaultTest='test_suite') |
|---|