11import re
22import json
33import pickle
4- import textwrap
54import unittest
65import warnings
76import importlib .metadata
1615 Distribution ,
1716 EntryPoint ,
1817 PackageNotFoundError ,
18+ _unique ,
1919 distributions ,
2020 entry_points ,
2121 metadata ,
@@ -51,6 +51,14 @@ def test_package_not_found_mentions_metadata(self):
5151 def test_new_style_classes (self ):
5252 self .assertIsInstance (Distribution , type )
5353
54+ @fixtures .parameterize (
55+ dict (name = None ),
56+ dict (name = '' ),
57+ )
58+ def test_invalid_inputs_to_from_name (self , name ):
59+ with self .assertRaises (Exception ):
60+ Distribution .from_name (name )
61+
5462
5563class ImportTests (fixtures .DistInfoPkg , unittest .TestCase ):
5664 def test_import_nonexistent_module (self ):
@@ -78,48 +86,50 @@ def test_resolve_without_attr(self):
7886
7987class NameNormalizationTests (fixtures .OnSysPath , fixtures .SiteDir , unittest .TestCase ):
8088 @staticmethod
81- def pkg_with_dashes ( site_dir ):
89+ def make_pkg ( name ):
8290 """
83- Create minimal metadata for a package with dashes
84- in the name (and thus underscores in the filename) .
91+ Create minimal metadata for a dist-info package with
92+ the indicated name on the file system .
8593 """
86- metadata_dir = site_dir / 'my_pkg.dist-info'
87- metadata_dir .mkdir ()
88- metadata = metadata_dir / 'METADATA'
89- with metadata .open ('w' , encoding = 'utf-8' ) as strm :
90- strm .write ('Version: 1.0\n ' )
91- return 'my-pkg'
94+ return {
95+ f'{ name } .dist-info' : {
96+ 'METADATA' : 'VERSION: 1.0\n ' ,
97+ },
98+ }
9299
93100 def test_dashes_in_dist_name_found_as_underscores (self ):
94101 """
95102 For a package with a dash in the name, the dist-info metadata
96103 uses underscores in the name. Ensure the metadata loads.
97104 """
98- pkg_name = self .pkg_with_dashes (self .site_dir )
99- assert version (pkg_name ) == '1.0'
100-
101- @staticmethod
102- def pkg_with_mixed_case (site_dir ):
103- """
104- Create minimal metadata for a package with mixed case
105- in the name.
106- """
107- metadata_dir = site_dir / 'CherryPy.dist-info'
108- metadata_dir .mkdir ()
109- metadata = metadata_dir / 'METADATA'
110- with metadata .open ('w' , encoding = 'utf-8' ) as strm :
111- strm .write ('Version: 1.0\n ' )
112- return 'CherryPy'
105+ fixtures .build_files (self .make_pkg ('my_pkg' ), self .site_dir )
106+ assert version ('my-pkg' ) == '1.0'
113107
114108 def test_dist_name_found_as_any_case (self ):
115109 """
116110 Ensure the metadata loads when queried with any case.
117111 """
118- pkg_name = self .pkg_with_mixed_case (self .site_dir )
112+ pkg_name = 'CherryPy'
113+ fixtures .build_files (self .make_pkg (pkg_name ), self .site_dir )
119114 assert version (pkg_name ) == '1.0'
120115 assert version (pkg_name .lower ()) == '1.0'
121116 assert version (pkg_name .upper ()) == '1.0'
122117
118+ def test_unique_distributions (self ):
119+ """
120+ Two distributions varying only by non-normalized name on
121+ the file system should resolve as the same.
122+ """
123+ fixtures .build_files (self .make_pkg ('abc' ), self .site_dir )
124+ before = list (_unique (distributions ()))
125+
126+ alt_site_dir = self .fixtures .enter_context (fixtures .tempdir ())
127+ self .fixtures .enter_context (self .add_sys_path (alt_site_dir ))
128+ fixtures .build_files (self .make_pkg ('ABC' ), alt_site_dir )
129+ after = list (_unique (distributions ()))
130+
131+ assert len (after ) == len (before )
132+
123133
124134class NonASCIITests (fixtures .OnSysPath , fixtures .SiteDir , unittest .TestCase ):
125135 @staticmethod
@@ -128,11 +138,12 @@ def pkg_with_non_ascii_description(site_dir):
128138 Create minimal metadata for a package with non-ASCII in
129139 the description.
130140 """
131- metadata_dir = site_dir / 'portend.dist-info'
132- metadata_dir .mkdir ()
133- metadata = metadata_dir / 'METADATA'
134- with metadata .open ('w' , encoding = 'utf-8' ) as fp :
135- fp .write ('Description: pôrˈtend' )
141+ contents = {
142+ 'portend.dist-info' : {
143+ 'METADATA' : 'Description: pôrˈtend' ,
144+ },
145+ }
146+ fixtures .build_files (contents , site_dir )
136147 return 'portend'
137148
138149 @staticmethod
@@ -141,19 +152,15 @@ def pkg_with_non_ascii_description_egg_info(site_dir):
141152 Create minimal metadata for an egg-info package with
142153 non-ASCII in the description.
143154 """
144- metadata_dir = site_dir / 'portend.dist-info'
145- metadata_dir .mkdir ()
146- metadata = metadata_dir / 'METADATA'
147- with metadata .open ('w' , encoding = 'utf-8' ) as fp :
148- fp .write (
149- textwrap .dedent (
150- """
155+ contents = {
156+ 'portend.dist-info' : {
157+ 'METADATA' : """
151158 Name: portend
152159
153- pôrˈtend
154- """
155- ). strip ()
156- )
160+ pôrˈtend""" ,
161+ },
162+ }
163+ fixtures . build_files ( contents , site_dir )
157164 return 'portend'
158165
159166 def test_metadata_loads (self ):
0 commit comments