1import sys 2import unittest 3 4from contextlib import ExitStack 5from importlib.metadata import ( 6 distribution, entry_points, files, PackageNotFoundError, version, 7) 8from importlib.resources import path 9 10 11class TestZip(unittest.TestCase): 12 root = 'test.test_importlib.data' 13 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 24 def test_zip_version(self): 25 self.assertEqual(version('example'), '21.12') 26 27 def test_zip_version_does_not_match(self): 28 with self.assertRaises(PackageNotFoundError): 29 version('definitely-not-installed') 30 31 def test_zip_entry_points(self): 32 scripts = dict(entry_points()['console_scripts']) 33 entry_point = scripts['example'] 34 self.assertEqual(entry_point.value, 'example:main') 35 entry_point = scripts['Example'] 36 self.assertEqual(entry_point.value, 'example:main') 37 38 def test_missing_metadata(self): 39 self.assertIsNone(distribution('example').read_text('does not exist')) 40 41 def test_case_insensitive(self): 42 self.assertEqual(version('Example'), '21.12') 43 44 def test_files(self): 45 for file in files('example'): 46 path = str(file.dist.locate_file(file)) 47 assert '.whl/' in path, path 48 49 50class TestEgg(TestZip): 51 def setUp(self): 52 # Find the path to the example-*.egg so we can add it to the front of 53 # sys.path, where we'll then try to find the metadata thereof. 54 self.resources = ExitStack() 55 self.addCleanup(self.resources.close) 56 egg = self.resources.enter_context( 57 path(self.root, 'example-21.12-py3.6.egg')) 58 sys.path.insert(0, str(egg)) 59 self.resources.callback(sys.path.pop, 0) 60 61 def test_files(self): 62 for file in files('example'): 63 path = str(file.dist.locate_file(file)) 64 assert '.egg/' in path, path 65