• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import sys
2import unittest
3
4from contextlib import ExitStack
5from importlib.metadata import (
6    distribution, entry_points, files, PackageNotFoundError,
7    version, distributions,
8)
9from importlib import resources
10
11from test.support import requires_zlib
12
13
14@requires_zlib()
15class TestZip(unittest.TestCase):
16    root = 'test.test_importlib.data'
17
18    def _fixture_on_path(self, filename):
19        pkg_file = resources.files(self.root).joinpath(filename)
20        file = self.resources.enter_context(resources.as_file(pkg_file))
21        assert file.name.startswith('example-'), file.name
22        sys.path.insert(0, str(file))
23        self.resources.callback(sys.path.pop, 0)
24
25    def setUp(self):
26        # Find the path to the example-*.whl so we can add it to the front of
27        # sys.path, where we'll then try to find the metadata thereof.
28        self.resources = ExitStack()
29        self.addCleanup(self.resources.close)
30        self._fixture_on_path('example-21.12-py3-none-any.whl')
31
32    def test_zip_version(self):
33        self.assertEqual(version('example'), '21.12')
34
35    def test_zip_version_does_not_match(self):
36        with self.assertRaises(PackageNotFoundError):
37            version('definitely-not-installed')
38
39    def test_zip_entry_points(self):
40        scripts = dict(entry_points()['console_scripts'])
41        entry_point = scripts['example']
42        self.assertEqual(entry_point.value, 'example:main')
43        entry_point = scripts['Example']
44        self.assertEqual(entry_point.value, 'example:main')
45
46    def test_missing_metadata(self):
47        self.assertIsNone(distribution('example').read_text('does not exist'))
48
49    def test_case_insensitive(self):
50        self.assertEqual(version('Example'), '21.12')
51
52    def test_files(self):
53        for file in files('example'):
54            path = str(file.dist.locate_file(file))
55            assert '.whl/' in path, path
56
57    def test_one_distribution(self):
58        dists = list(distributions(path=sys.path[:1]))
59        assert len(dists) == 1
60
61
62@requires_zlib()
63class TestEgg(TestZip):
64    def setUp(self):
65        # Find the path to the example-*.egg so we can add it to the front of
66        # sys.path, where we'll then try to find the metadata thereof.
67        self.resources = ExitStack()
68        self.addCleanup(self.resources.close)
69        self._fixture_on_path('example-21.12-py3.6.egg')
70
71    def test_files(self):
72        for file in files('example'):
73            path = str(file.dist.locate_file(file))
74            assert '.egg/' in path, path
75