33
44from contextlib import ExitStack
55from importlib .metadata import (
6- distribution , entry_points , files , PackageNotFoundError , version ,
6+ distribution , entry_points , files , PackageNotFoundError ,
7+ version , distributions ,
78)
8- from importlib . resources import path
9+ from importlib import resources
910
1011from test .support import requires_zlib
1112
1415class TestZip (unittest .TestCase ):
1516 root = 'test.test_importlib.data'
1617
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+
1725 def setUp (self ):
1826 # Find the path to the example-*.whl so we can add it to the front of
1927 # sys.path, where we'll then try to find the metadata thereof.
2028 self .resources = ExitStack ()
2129 self .addCleanup (self .resources .close )
22- wheel = self .resources .enter_context (
23- path (self .root , 'example-21.12-py3-none-any.whl' ))
24- sys .path .insert (0 , str (wheel ))
25- self .resources .callback (sys .path .pop , 0 )
30+ self ._fixture_on_path ('example-21.12-py3-none-any.whl' )
2631
2732 def test_zip_version (self ):
2833 self .assertEqual (version ('example' ), '21.12' )
@@ -49,6 +54,10 @@ def test_files(self):
4954 path = str (file .dist .locate_file (file ))
5055 assert '.whl/' in path , path
5156
57+ def test_one_distribution (self ):
58+ dists = list (distributions (path = sys .path [:1 ]))
59+ assert len (dists ) == 1
60+
5261
5362@requires_zlib ()
5463class TestEgg (TestZip ):
@@ -57,10 +66,7 @@ def setUp(self):
5766 # sys.path, where we'll then try to find the metadata thereof.
5867 self .resources = ExitStack ()
5968 self .addCleanup (self .resources .close )
60- egg = self .resources .enter_context (
61- path (self .root , 'example-21.12-py3.6.egg' ))
62- sys .path .insert (0 , str (egg ))
63- self .resources .callback (sys .path .pop , 0 )
69+ self ._fixture_on_path ('example-21.12-py3.6.egg' )
6470
6571 def test_files (self ):
6672 for file in files ('example' ):
0 commit comments