test_zip.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import sys
  2. import unittest
  3. from .. import distribution, entry_points, files, version
  4. try:
  5. from importlib.resources import path
  6. except ImportError:
  7. from importlib_resources import path
  8. try:
  9. from contextlib import ExitStack
  10. except ImportError:
  11. from contextlib2 import ExitStack
  12. class TestZip(unittest.TestCase):
  13. root = 'importlib_metadata.tests.data'
  14. def setUp(self):
  15. # Find the path to the example-*.whl so we can add it to the front of
  16. # sys.path, where we'll then try to find the metadata thereof.
  17. self.resources = ExitStack()
  18. self.addCleanup(self.resources.close)
  19. wheel = self.resources.enter_context(
  20. path(self.root, 'example-21.12-py3-none-any.whl'))
  21. sys.path.insert(0, str(wheel))
  22. self.resources.callback(sys.path.pop, 0)
  23. def test_zip_version(self):
  24. self.assertEqual(version('example'), '21.12')
  25. def test_zip_entry_points(self):
  26. scripts = dict(entry_points()['console_scripts'])
  27. entry_point = scripts['example']
  28. self.assertEqual(entry_point.value, 'example:main')
  29. entry_point = scripts['Example']
  30. self.assertEqual(entry_point.value, 'example:main')
  31. def test_missing_metadata(self):
  32. self.assertIsNone(distribution('example').read_text('does not exist'))
  33. def test_case_insensitive(self):
  34. self.assertEqual(version('Example'), '21.12')
  35. def test_files(self):
  36. for file in files('example'):
  37. path = str(file.dist.locate_file(file))
  38. assert '.whl/' in path, path
  39. class TestEgg(TestZip):
  40. def setUp(self):
  41. # Find the path to the example-*.egg so we can add it to the front of
  42. # sys.path, where we'll then try to find the metadata thereof.
  43. self.resources = ExitStack()
  44. self.addCleanup(self.resources.close)
  45. egg = self.resources.enter_context(
  46. path(self.root, 'example-21.12-py3.6.egg'))
  47. sys.path.insert(0, str(egg))
  48. self.resources.callback(sys.path.pop, 0)
  49. def test_files(self):
  50. for file in files('example'):
  51. path = str(file.dist.locate_file(file))
  52. assert '.egg/' in path, path