blob: ea63d3e4f15a59c194289340ed7b72e9a5927b7b [file] [log] [blame]
Tarek Ziade1231a4e2011-05-19 13:07:25 +02001import os
2import io
3import csv
4import imp
5import sys
6import shutil
7import zipfile
8import tempfile
9from os.path import relpath # separate import for backport concerns
10from hashlib import md5
11
12from packaging.errors import PackagingError
13from packaging.metadata import Metadata
14from packaging.tests import unittest, run_unittest, support, TESTFN
Ezio Melotticad648c2011-05-19 21:25:10 +030015from packaging.tests.support import requires_zlib
Tarek Ziade1231a4e2011-05-19 13:07:25 +020016
17from packaging.database import (
18 Distribution, EggInfoDistribution, get_distribution, get_distributions,
19 provides_distribution, obsoletes_distribution, get_file_users,
20 enable_cache, disable_cache, distinfo_dirname, _yield_distributions)
21
22# TODO Add a test for getting a distribution provided by another distribution
23# TODO Add a test for absolute pathed RECORD items (e.g. /etc/myapp/config.ini)
24# TODO Add tests from the former pep376 project (zipped site-packages, etc.)
25
26
27def get_hexdigest(filename):
28 with open(filename, 'rb') as file:
29 checksum = md5(file.read())
30 return checksum.hexdigest()
31
32
33def record_pieces(file):
34 path = relpath(file, sys.prefix)
35 digest = get_hexdigest(file)
36 size = os.path.getsize(file)
37 return [path, digest, size]
38
39
40class CommonDistributionTests:
41 """Mixin used to test the interface common to both Distribution classes.
42
43 Derived classes define cls, sample_dist, dirs and records. These
44 attributes are used in test methods. See source code for details.
45 """
46
47 def setUp(self):
48 super(CommonDistributionTests, self).setUp()
49 self.addCleanup(enable_cache)
50 disable_cache()
51 self.fake_dists_path = os.path.abspath(
52 os.path.join(os.path.dirname(__file__), 'fake_dists'))
53
54 def test_instantiation(self):
55 # check that useful attributes are here
56 name, version, distdir = self.sample_dist
57 here = os.path.abspath(os.path.dirname(__file__))
58 dist_path = os.path.join(here, 'fake_dists', distdir)
59
60 dist = self.dist = self.cls(dist_path)
61 self.assertEqual(dist.path, dist_path)
62 self.assertEqual(dist.name, name)
63 self.assertEqual(dist.metadata['Name'], name)
64 self.assertIsInstance(dist.metadata, Metadata)
65 self.assertEqual(dist.version, version)
66 self.assertEqual(dist.metadata['Version'], version)
67
Ezio Melotticad648c2011-05-19 21:25:10 +030068 @requires_zlib
Tarek Ziade1231a4e2011-05-19 13:07:25 +020069 def test_repr(self):
70 dist = self.cls(self.dirs[0])
71 # just check that the class name is in the repr
72 self.assertIn(self.cls.__name__, repr(dist))
73
Ezio Melotticad648c2011-05-19 21:25:10 +030074 @requires_zlib
Tarek Ziade1231a4e2011-05-19 13:07:25 +020075 def test_comparison(self):
76 # tests for __eq__ and __hash__
77 dist = self.cls(self.dirs[0])
78 dist2 = self.cls(self.dirs[0])
79 dist3 = self.cls(self.dirs[1])
80 self.assertIn(dist, {dist: True})
81 self.assertEqual(dist, dist)
82
83 self.assertIsNot(dist, dist2)
84 self.assertEqual(dist, dist2)
85 self.assertNotEqual(dist, dist3)
86 self.assertNotEqual(dist, ())
87
88 def test_list_installed_files(self):
89 for dir_ in self.dirs:
90 dist = self.cls(dir_)
91 for path, md5_, size in dist.list_installed_files():
92 record_data = self.records[dist.path]
93 self.assertIn(path, record_data)
94 self.assertEqual(md5_, record_data[path][0])
95 self.assertEqual(size, record_data[path][1])
96
97
98class TestDistribution(CommonDistributionTests, unittest.TestCase):
99
100 cls = Distribution
101 sample_dist = 'choxie', '2.0.0.9', 'choxie-2.0.0.9.dist-info'
102
103 def setUp(self):
104 super(TestDistribution, self).setUp()
105 self.dirs = [os.path.join(self.fake_dists_path, f)
106 for f in os.listdir(self.fake_dists_path)
107 if f.endswith('.dist-info')]
108
109 self.records = {}
110 for distinfo_dir in self.dirs:
111 record_file = os.path.join(distinfo_dir, 'RECORD')
112 with open(record_file, 'w') as file:
113 record_writer = csv.writer(
Tarek Ziadebe20be12011-05-21 19:45:48 +0200114 file, delimiter=',', quoting=csv.QUOTE_NONE,
115 lineterminator='\n')
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200116
117 dist_location = distinfo_dir.replace('.dist-info', '')
118
119 for path, dirs, files in os.walk(dist_location):
120 for f in files:
121 record_writer.writerow(record_pieces(
122 os.path.join(path, f)))
123 for file in ('INSTALLER', 'METADATA', 'REQUESTED'):
124 record_writer.writerow(record_pieces(
125 os.path.join(distinfo_dir, file)))
126 record_writer.writerow([relpath(record_file, sys.prefix)])
127
128 with open(record_file) as file:
Tarek Ziadebe20be12011-05-21 19:45:48 +0200129 record_reader = csv.reader(file, lineterminator='\n')
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200130 record_data = {}
131 for row in record_reader:
Tarek Ziadebe20be12011-05-21 19:45:48 +0200132 if row == []:
133 continue
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200134 path, md5_, size = (row[:] +
135 [None for i in range(len(row), 3)])
136 record_data[path] = md5_, size
137 self.records[distinfo_dir] = record_data
138
139 def tearDown(self):
140 for distinfo_dir in self.dirs:
141 record_file = os.path.join(distinfo_dir, 'RECORD')
Victor Stinner4c9706b2011-05-19 15:52:59 +0200142 open(record_file, 'wb').close()
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200143 super(TestDistribution, self).tearDown()
144
145 def test_instantiation(self):
146 super(TestDistribution, self).test_instantiation()
147 self.assertIsInstance(self.dist.requested, bool)
148
149 def test_uses(self):
150 # Test to determine if a distribution uses a specified file.
151 # Criteria to test against
152 distinfo_name = 'grammar-1.0a4'
153 distinfo_dir = os.path.join(self.fake_dists_path,
154 distinfo_name + '.dist-info')
155 true_path = [self.fake_dists_path, distinfo_name,
156 'grammar', 'utils.py']
157 true_path = relpath(os.path.join(*true_path), sys.prefix)
158 false_path = [self.fake_dists_path, 'towel_stuff-0.1', 'towel_stuff',
159 '__init__.py']
160 false_path = relpath(os.path.join(*false_path), sys.prefix)
161
162 # Test if the distribution uses the file in question
163 dist = Distribution(distinfo_dir)
164 self.assertTrue(dist.uses(true_path))
165 self.assertFalse(dist.uses(false_path))
166
167 def test_get_distinfo_file(self):
168 # Test the retrieval of dist-info file objects.
169 distinfo_name = 'choxie-2.0.0.9'
170 other_distinfo_name = 'grammar-1.0a4'
171 distinfo_dir = os.path.join(self.fake_dists_path,
172 distinfo_name + '.dist-info')
173 dist = Distribution(distinfo_dir)
174 # Test for known good file matches
175 distinfo_files = [
176 # Relative paths
177 'INSTALLER', 'METADATA',
178 # Absolute paths
179 os.path.join(distinfo_dir, 'RECORD'),
180 os.path.join(distinfo_dir, 'REQUESTED'),
181 ]
182
183 for distfile in distinfo_files:
184 with dist.get_distinfo_file(distfile) as value:
185 self.assertIsInstance(value, io.TextIOWrapper)
186 # Is it the correct file?
187 self.assertEqual(value.name,
188 os.path.join(distinfo_dir, distfile))
189
190 # Test an absolute path that is part of another distributions dist-info
191 other_distinfo_file = os.path.join(
192 self.fake_dists_path, other_distinfo_name + '.dist-info',
193 'REQUESTED')
194 self.assertRaises(PackagingError, dist.get_distinfo_file,
195 other_distinfo_file)
196 # Test for a file that should not exist
197 self.assertRaises(PackagingError, dist.get_distinfo_file,
198 'MAGICFILE')
199
200 def test_list_distinfo_files(self):
201 # Test for the iteration of RECORD path entries.
202 distinfo_name = 'towel_stuff-0.1'
203 distinfo_dir = os.path.join(self.fake_dists_path,
204 distinfo_name + '.dist-info')
205 dist = Distribution(distinfo_dir)
206 # Test for the iteration of the raw path
207 distinfo_record_paths = self.records[distinfo_dir].keys()
208 found = dist.list_distinfo_files()
209 self.assertEqual(sorted(found), sorted(distinfo_record_paths))
210 # Test for the iteration of local absolute paths
211 distinfo_record_paths = [os.path.join(sys.prefix, path)
212 for path in self.records[distinfo_dir]]
213 found = dist.list_distinfo_files(local=True)
214 self.assertEqual(sorted(found), sorted(distinfo_record_paths))
215
216 def test_get_resources_path(self):
217 distinfo_name = 'babar-0.1'
218 distinfo_dir = os.path.join(self.fake_dists_path,
219 distinfo_name + '.dist-info')
220 dist = Distribution(distinfo_dir)
221 resource_path = dist.get_resource_path('babar.png')
222 self.assertEqual(resource_path, 'babar.png')
223 self.assertRaises(KeyError, dist.get_resource_path, 'notexist')
224
225
226class TestEggInfoDistribution(CommonDistributionTests,
227 support.LoggingCatcher,
228 unittest.TestCase):
229
230 cls = EggInfoDistribution
231 sample_dist = 'bacon', '0.1', 'bacon-0.1.egg-info'
232
233 def setUp(self):
234 super(TestEggInfoDistribution, self).setUp()
235
236 self.dirs = [os.path.join(self.fake_dists_path, f)
237 for f in os.listdir(self.fake_dists_path)
238 if f.endswith('.egg') or f.endswith('.egg-info')]
239
240 self.records = {}
241
242 @unittest.skip('not implemented yet')
243 def test_list_installed_files(self):
244 # EggInfoDistribution defines list_installed_files but there is no
245 # test for it yet; someone with setuptools expertise needs to add a
246 # file with the list of installed files for one of the egg fake dists
247 # and write the support code to populate self.records (and then delete
248 # this method)
249 pass
250
251
252class TestDatabase(support.LoggingCatcher,
253 unittest.TestCase):
254
255 def setUp(self):
256 super(TestDatabase, self).setUp()
257 disable_cache()
258 # Setup the path environment with our fake distributions
259 current_path = os.path.abspath(os.path.dirname(__file__))
260 self.sys_path = sys.path[:]
261 self.fake_dists_path = os.path.join(current_path, 'fake_dists')
262 sys.path.insert(0, self.fake_dists_path)
263
264 def tearDown(self):
265 sys.path[:] = self.sys_path
266 enable_cache()
267 super(TestDatabase, self).tearDown()
268
269 def test_distinfo_dirname(self):
270 # Given a name and a version, we expect the distinfo_dirname function
271 # to return a standard distribution information directory name.
272
273 items = [
274 # (name, version, standard_dirname)
275 # Test for a very simple single word name and decimal version
276 # number
277 ('docutils', '0.5', 'docutils-0.5.dist-info'),
278 # Test for another except this time with a '-' in the name, which
279 # needs to be transformed during the name lookup
280 ('python-ldap', '2.5', 'python_ldap-2.5.dist-info'),
281 # Test for both '-' in the name and a funky version number
282 ('python-ldap', '2.5 a---5', 'python_ldap-2.5 a---5.dist-info'),
283 ]
284
285 # Loop through the items to validate the results
286 for name, version, standard_dirname in items:
287 dirname = distinfo_dirname(name, version)
288 self.assertEqual(dirname, standard_dirname)
289
Ezio Melotticad648c2011-05-19 21:25:10 +0300290 @requires_zlib
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200291 def test_get_distributions(self):
292 # Lookup all distributions found in the ``sys.path``.
293 # This test could potentially pick up other installed distributions
294 fake_dists = [('grammar', '1.0a4'), ('choxie', '2.0.0.9'),
295 ('towel-stuff', '0.1'), ('babar', '0.1')]
296 found_dists = []
297
298 # Verify the fake dists have been found.
299 dists = [dist for dist in get_distributions()]
300 for dist in dists:
301 self.assertIsInstance(dist, Distribution)
302 if (dist.name in dict(fake_dists) and
303 dist.path.startswith(self.fake_dists_path)):
304 found_dists.append((dist.name, dist.metadata['version'], ))
305 else:
306 # check that it doesn't find anything more than this
307 self.assertFalse(dist.path.startswith(self.fake_dists_path))
308 # otherwise we don't care what other distributions are found
309
310 # Finally, test that we found all that we were looking for
311 self.assertEqual(sorted(found_dists), sorted(fake_dists))
312
313 # Now, test if the egg-info distributions are found correctly as well
314 fake_dists += [('bacon', '0.1'), ('cheese', '2.0.2'),
315 ('coconuts-aster', '10.3'),
316 ('banana', '0.4'), ('strawberry', '0.6'),
317 ('truffles', '5.0'), ('nut', 'funkyversion')]
318 found_dists = []
319
320 dists = [dist for dist in get_distributions(use_egg_info=True)]
321 for dist in dists:
322 self.assertIsInstance(dist, (Distribution, EggInfoDistribution))
323 if (dist.name in dict(fake_dists) and
324 dist.path.startswith(self.fake_dists_path)):
325 found_dists.append((dist.name, dist.metadata['version']))
326 else:
327 self.assertFalse(dist.path.startswith(self.fake_dists_path))
328
329 self.assertEqual(sorted(fake_dists), sorted(found_dists))
330
Ezio Melotticad648c2011-05-19 21:25:10 +0300331 @requires_zlib
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200332 def test_get_distribution(self):
333 # Test for looking up a distribution by name.
334 # Test the lookup of the towel-stuff distribution
335 name = 'towel-stuff' # Note: This is different from the directory name
336
337 # Lookup the distribution
338 dist = get_distribution(name)
339 self.assertIsInstance(dist, Distribution)
340 self.assertEqual(dist.name, name)
341
342 # Verify that an unknown distribution returns None
343 self.assertIsNone(get_distribution('bogus'))
344
345 # Verify partial name matching doesn't work
346 self.assertIsNone(get_distribution('towel'))
347
348 # Verify that it does not find egg-info distributions, when not
349 # instructed to
350 self.assertIsNone(get_distribution('bacon'))
351 self.assertIsNone(get_distribution('cheese'))
352 self.assertIsNone(get_distribution('strawberry'))
353 self.assertIsNone(get_distribution('banana'))
354
355 # Now check that it works well in both situations, when egg-info
356 # is a file and directory respectively.
357 dist = get_distribution('cheese', use_egg_info=True)
358 self.assertIsInstance(dist, EggInfoDistribution)
359 self.assertEqual(dist.name, 'cheese')
360
361 dist = get_distribution('bacon', use_egg_info=True)
362 self.assertIsInstance(dist, EggInfoDistribution)
363 self.assertEqual(dist.name, 'bacon')
364
365 dist = get_distribution('banana', use_egg_info=True)
366 self.assertIsInstance(dist, EggInfoDistribution)
367 self.assertEqual(dist.name, 'banana')
368
369 dist = get_distribution('strawberry', use_egg_info=True)
370 self.assertIsInstance(dist, EggInfoDistribution)
371 self.assertEqual(dist.name, 'strawberry')
372
373 def test_get_file_users(self):
374 # Test the iteration of distributions that use a file.
375 name = 'towel_stuff-0.1'
376 path = os.path.join(self.fake_dists_path, name,
377 'towel_stuff', '__init__.py')
378 for dist in get_file_users(path):
379 self.assertIsInstance(dist, Distribution)
380 self.assertEqual(dist.name, name)
381
Ezio Melotticad648c2011-05-19 21:25:10 +0300382 @requires_zlib
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200383 def test_provides(self):
384 # Test for looking up distributions by what they provide
385 checkLists = lambda x, y: self.assertEqual(sorted(x), sorted(y))
386
387 l = [dist.name for dist in provides_distribution('truffles')]
388 checkLists(l, ['choxie', 'towel-stuff'])
389
390 l = [dist.name for dist in provides_distribution('truffles', '1.0')]
391 checkLists(l, ['choxie'])
392
393 l = [dist.name for dist in provides_distribution('truffles', '1.0',
394 use_egg_info=True)]
395 checkLists(l, ['choxie', 'cheese'])
396
397 l = [dist.name for dist in provides_distribution('truffles', '1.1.2')]
398 checkLists(l, ['towel-stuff'])
399
400 l = [dist.name for dist in provides_distribution('truffles', '1.1')]
401 checkLists(l, ['towel-stuff'])
402
403 l = [dist.name for dist in provides_distribution('truffles',
404 '!=1.1,<=2.0')]
405 checkLists(l, ['choxie'])
406
407 l = [dist.name for dist in provides_distribution('truffles',
408 '!=1.1,<=2.0',
409 use_egg_info=True)]
410 checkLists(l, ['choxie', 'bacon', 'cheese'])
411
412 l = [dist.name for dist in provides_distribution('truffles', '>1.0')]
413 checkLists(l, ['towel-stuff'])
414
415 l = [dist.name for dist in provides_distribution('truffles', '>1.5')]
416 checkLists(l, [])
417
418 l = [dist.name for dist in provides_distribution('truffles', '>1.5',
419 use_egg_info=True)]
420 checkLists(l, ['bacon'])
421
422 l = [dist.name for dist in provides_distribution('truffles', '>=1.0')]
423 checkLists(l, ['choxie', 'towel-stuff'])
424
425 l = [dist.name for dist in provides_distribution('strawberry', '0.6',
426 use_egg_info=True)]
427 checkLists(l, ['coconuts-aster'])
428
429 l = [dist.name for dist in provides_distribution('strawberry', '>=0.5',
430 use_egg_info=True)]
431 checkLists(l, ['coconuts-aster'])
432
433 l = [dist.name for dist in provides_distribution('strawberry', '>0.6',
434 use_egg_info=True)]
435 checkLists(l, [])
436
437 l = [dist.name for dist in provides_distribution('banana', '0.4',
438 use_egg_info=True)]
439 checkLists(l, ['coconuts-aster'])
440
441 l = [dist.name for dist in provides_distribution('banana', '>=0.3',
442 use_egg_info=True)]
443 checkLists(l, ['coconuts-aster'])
444
445 l = [dist.name for dist in provides_distribution('banana', '!=0.4',
446 use_egg_info=True)]
447 checkLists(l, [])
448
Ezio Melotticad648c2011-05-19 21:25:10 +0300449 @requires_zlib
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200450 def test_obsoletes(self):
451 # Test looking for distributions based on what they obsolete
452 checkLists = lambda x, y: self.assertEqual(sorted(x), sorted(y))
453
454 l = [dist.name for dist in obsoletes_distribution('truffles', '1.0')]
455 checkLists(l, [])
456
457 l = [dist.name for dist in obsoletes_distribution('truffles', '1.0',
458 use_egg_info=True)]
459 checkLists(l, ['cheese', 'bacon'])
460
461 l = [dist.name for dist in obsoletes_distribution('truffles', '0.8')]
462 checkLists(l, ['choxie'])
463
464 l = [dist.name for dist in obsoletes_distribution('truffles', '0.8',
465 use_egg_info=True)]
466 checkLists(l, ['choxie', 'cheese'])
467
468 l = [dist.name for dist in obsoletes_distribution('truffles', '0.9.6')]
469 checkLists(l, ['choxie', 'towel-stuff'])
470
471 l = [dist.name for dist in obsoletes_distribution('truffles',
472 '0.5.2.3')]
473 checkLists(l, ['choxie', 'towel-stuff'])
474
475 l = [dist.name for dist in obsoletes_distribution('truffles', '0.2')]
476 checkLists(l, ['towel-stuff'])
477
Ezio Melotticad648c2011-05-19 21:25:10 +0300478 @requires_zlib
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200479 def test_yield_distribution(self):
480 # tests the internal function _yield_distributions
481 checkLists = lambda x, y: self.assertEqual(sorted(x), sorted(y))
482
483 eggs = [('bacon', '0.1'), ('banana', '0.4'), ('strawberry', '0.6'),
484 ('truffles', '5.0'), ('cheese', '2.0.2'),
485 ('coconuts-aster', '10.3'), ('nut', 'funkyversion')]
486 dists = [('choxie', '2.0.0.9'), ('grammar', '1.0a4'),
487 ('towel-stuff', '0.1'), ('babar', '0.1')]
488
489 checkLists([], _yield_distributions(False, False))
490
491 found = [(dist.name, dist.metadata['Version'])
492 for dist in _yield_distributions(False, True)
493 if dist.path.startswith(self.fake_dists_path)]
494 checkLists(eggs, found)
495
496 found = [(dist.name, dist.metadata['Version'])
497 for dist in _yield_distributions(True, False)
498 if dist.path.startswith(self.fake_dists_path)]
499 checkLists(dists, found)
500
501 found = [(dist.name, dist.metadata['Version'])
502 for dist in _yield_distributions(True, True)
503 if dist.path.startswith(self.fake_dists_path)]
504 checkLists(dists + eggs, found)
505
506
507def test_suite():
508 suite = unittest.TestSuite()
509 load = unittest.defaultTestLoader.loadTestsFromTestCase
510 suite.addTest(load(TestDistribution))
511 suite.addTest(load(TestEggInfoDistribution))
512 suite.addTest(load(TestDatabase))
513 return suite
514
515
516if __name__ == "__main__":
517 unittest.main(defaultTest='test_suite')