1414 EntryPoint ,
1515 MetadataPathFinder ,
1616 PackageNotFoundError ,
17+ distribution ,
1718 distributions ,
1819 entry_points ,
1920 metadata ,
@@ -87,15 +88,17 @@ def pkg_with_dashes(site_dir):
8788 metadata = metadata_dir / 'METADATA'
8889 with metadata .open ('w' , encoding = 'utf-8' ) as strm :
8990 strm .write ('Version: 1.0\n ' )
90- return 'my-pkg'
91+ return 'my-pkg' , 'my_pkg'
9192
9293 def test_dashes_in_dist_name_found_as_underscores (self ):
9394 """
9495 For a package with a dash in the name, the dist-info metadata
9596 uses underscores in the name. Ensure the metadata loads.
9697 """
97- pkg_name = self .pkg_with_dashes (self .site_dir )
98- assert version (pkg_name ) == '1.0'
98+ pkg_name , norm_pkg_name = self .pkg_with_dashes (self .site_dir )
99+ dist = distribution (pkg_name )
100+ assert dist ._normalized_name == norm_pkg_name
101+ assert dist .version == '1.0'
99102
100103 @staticmethod
101104 def pkg_with_mixed_case (site_dir ):
@@ -108,16 +111,40 @@ def pkg_with_mixed_case(site_dir):
108111 metadata = metadata_dir / 'METADATA'
109112 with metadata .open ('w' , encoding = 'utf-8' ) as strm :
110113 strm .write ('Version: 1.0\n ' )
111- return 'CherryPy '
114+ return 'CheRRypY' , 'cherrypy '
112115
113116 def test_dist_name_found_as_any_case (self ):
114117 """
115118 Ensure the metadata loads when queried with any case.
116119 """
117- pkg_name = self .pkg_with_mixed_case (self .site_dir )
118- assert version (pkg_name ) == '1.0'
119- assert version (pkg_name .lower ()) == '1.0'
120- assert version (pkg_name .upper ()) == '1.0'
120+ pkg_name , norm_pkg_name = self .pkg_with_mixed_case (self .site_dir )
121+ for name_variant in (pkg_name , pkg_name .lower (), pkg_name .upper ()):
122+ dist = distribution (name_variant )
123+ assert dist ._normalized_name == norm_pkg_name
124+ assert dist .version == '1.0'
125+
126+ @staticmethod
127+ def pkg_with_replaced_chars (site_dir ):
128+ """
129+ Create minimal metadata for a package with some
130+ characters replaced by PEP 503 normalization
131+ in the name.
132+ """
133+ metadata_dir = site_dir / 'zope..inter_face-4.2.dist-info'
134+ metadata_dir .mkdir ()
135+ metadata = metadata_dir / 'METADATA'
136+ with metadata .open ('w' , encoding = 'utf-8' ) as strm :
137+ strm .write ('Version: 4.2\n ' )
138+ return 'zope-inter._FACE' , 'zope_inter_face'
139+
140+ def test_dist_name_pep503_normalization (self ):
141+ """
142+ Ensure the distribution name is properly normalized.
143+ """
144+ pkg_name , norm_pkg_name = self .pkg_with_replaced_chars (self .site_dir )
145+ dist = distribution (pkg_name )
146+ assert dist ._normalized_name == norm_pkg_name
147+ assert dist .version == '4.2'
121148
122149
123150class NonASCIITests (fixtures .OnSysPath , fixtures .SiteDir , unittest .TestCase ):
0 commit comments