Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix PathDistribution._normalized_name implementation #377

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,10 @@ def _normalized_name(self):
normalized name from the file system path.
"""
stem = os.path.basename(str(self._path))
return self._name_from_stem(stem) or super()._normalized_name
return (
pass_none(Prepared.normalize)(self._name_from_stem(stem))
or super()._normalized_name
)

@staticmethod
def _name_from_stem(stem):
Expand Down
43 changes: 35 additions & 8 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
EntryPoint,
MetadataPathFinder,
PackageNotFoundError,
distribution,
distributions,
entry_points,
metadata,
Expand Down Expand Up @@ -87,15 +88,17 @@ def pkg_with_dashes(site_dir):
metadata = metadata_dir / 'METADATA'
with metadata.open('w', encoding='utf-8') as strm:
strm.write('Version: 1.0\n')
return 'my-pkg'
return 'my-pkg', 'my_pkg'

def test_dashes_in_dist_name_found_as_underscores(self):
"""
For a package with a dash in the name, the dist-info metadata
uses underscores in the name. Ensure the metadata loads.
"""
pkg_name = self.pkg_with_dashes(self.site_dir)
assert version(pkg_name) == '1.0'
pkg_name, norm_pkg_name = self.pkg_with_dashes(self.site_dir)
dist = distribution(pkg_name)
assert dist._normalized_name == norm_pkg_name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of testing the internal implementation details, I'd rather have a test that captures the failed expectation - namely that entry points of two packages whose file system names vary only by normalization won't deduplicate.

assert dist.version == '1.0'

@staticmethod
def pkg_with_mixed_case(site_dir):
Expand All @@ -108,16 +111,40 @@ def pkg_with_mixed_case(site_dir):
metadata = metadata_dir / 'METADATA'
with metadata.open('w', encoding='utf-8') as strm:
strm.write('Version: 1.0\n')
return 'CherryPy'
return 'CheRRypY', 'cherrypy'

def test_dist_name_found_as_any_case(self):
"""
Ensure the metadata loads when queried with any case.
"""
pkg_name = self.pkg_with_mixed_case(self.site_dir)
assert version(pkg_name) == '1.0'
assert version(pkg_name.lower()) == '1.0'
assert version(pkg_name.upper()) == '1.0'
pkg_name, norm_pkg_name = self.pkg_with_mixed_case(self.site_dir)
for name_variant in (pkg_name, pkg_name.lower(), pkg_name.upper()):
dist = distribution(name_variant)
assert dist._normalized_name == norm_pkg_name
assert dist.version == '1.0'

@staticmethod
def pkg_with_replaced_chars(site_dir):
"""
Create minimal metadata for a package with some
characters replaced by PEP 503 normalization
in the name.
"""
metadata_dir = site_dir / 'zope..inter_face-4.2.dist-info'
metadata_dir.mkdir()
metadata = metadata_dir / 'METADATA'
with metadata.open('w', encoding='utf-8') as strm:
strm.write('Version: 4.2\n')
return 'zope-inter._FACE', 'zope_inter_face'

def test_dist_name_pep503_normalization(self):
"""
Ensure the distribution name is properly normalized.
"""
pkg_name, norm_pkg_name = self.pkg_with_replaced_chars(self.site_dir)
dist = distribution(pkg_name)
assert dist._normalized_name == norm_pkg_name
assert dist.version == '4.2'


class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
Expand Down