summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2022-06-26 01:04:28 (GMT)
committerGitHub <noreply@github.com>2022-06-26 01:04:28 (GMT)
commit38612a05b5de33fde82d7960418527b7cfaa2e7c (patch)
treefc311ccb634a3bf6fb025bf5b064ead486e7be25 /Lib/test
parent9af6b75298d066e89646acf8df1704bef183a6f8 (diff)
downloadcpython-38612a05b5de33fde82d7960418527b7cfaa2e7c.zip
cpython-38612a05b5de33fde82d7960418527b7cfaa2e7c.tar.gz
cpython-38612a05b5de33fde82d7960418527b7cfaa2e7c.tar.bz2
gh-93259: Validate arg to ``Distribution.from_name``. (GH-94270)
Syncs with importlib_metadata 4.12.0.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_importlib/fixtures.py16
-rw-r--r--Lib/test/test_importlib/test_main.py91
-rw-r--r--Lib/test/test_importlib/test_metadata_api.py6
3 files changed, 68 insertions, 45 deletions
diff --git a/Lib/test/test_importlib/fixtures.py b/Lib/test/test_importlib/fixtures.py
index 803d373..e7be77b 100644
--- a/Lib/test/test_importlib/fixtures.py
+++ b/Lib/test/test_importlib/fixtures.py
@@ -5,6 +5,7 @@ import shutil
import pathlib
import tempfile
import textwrap
+import functools
import contextlib
from test.support.os_helper import FS_NONASCII
@@ -296,3 +297,18 @@ class ZipFixtures:
# Add self.zip_name to the front of sys.path.
self.resources = contextlib.ExitStack()
self.addCleanup(self.resources.close)
+
+
+def parameterize(*args_set):
+ """Run test method with a series of parameters."""
+
+ def wrapper(func):
+ @functools.wraps(func)
+ def _inner(self):
+ for args in args_set:
+ with self.subTest(**args):
+ func(self, **args)
+
+ return _inner
+
+ return wrapper
diff --git a/Lib/test/test_importlib/test_main.py b/Lib/test/test_importlib/test_main.py
index c80a0e4..d9d067c 100644
--- a/Lib/test/test_importlib/test_main.py
+++ b/Lib/test/test_importlib/test_main.py
@@ -1,7 +1,6 @@
import re
import json
import pickle
-import textwrap
import unittest
import warnings
import importlib.metadata
@@ -16,6 +15,7 @@ from importlib.metadata import (
Distribution,
EntryPoint,
PackageNotFoundError,
+ _unique,
distributions,
entry_points,
metadata,
@@ -51,6 +51,14 @@ class BasicTests(fixtures.DistInfoPkg, unittest.TestCase):
def test_new_style_classes(self):
self.assertIsInstance(Distribution, type)
+ @fixtures.parameterize(
+ dict(name=None),
+ dict(name=''),
+ )
+ def test_invalid_inputs_to_from_name(self, name):
+ with self.assertRaises(Exception):
+ Distribution.from_name(name)
+
class ImportTests(fixtures.DistInfoPkg, unittest.TestCase):
def test_import_nonexistent_module(self):
@@ -78,48 +86,50 @@ class ImportTests(fixtures.DistInfoPkg, unittest.TestCase):
class NameNormalizationTests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
@staticmethod
- def pkg_with_dashes(site_dir):
+ def make_pkg(name):
"""
- Create minimal metadata for a package with dashes
- in the name (and thus underscores in the filename).
+ Create minimal metadata for a dist-info package with
+ the indicated name on the file system.
"""
- metadata_dir = site_dir / 'my_pkg.dist-info'
- metadata_dir.mkdir()
- metadata = metadata_dir / 'METADATA'
- with metadata.open('w', encoding='utf-8') as strm:
- strm.write('Version: 1.0\n')
- return 'my-pkg'
+ return {
+ f'{name}.dist-info': {
+ 'METADATA': 'VERSION: 1.0\n',
+ },
+ }
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'
-
- @staticmethod
- def pkg_with_mixed_case(site_dir):
- """
- Create minimal metadata for a package with mixed case
- in the name.
- """
- metadata_dir = site_dir / 'CherryPy.dist-info'
- metadata_dir.mkdir()
- metadata = metadata_dir / 'METADATA'
- with metadata.open('w', encoding='utf-8') as strm:
- strm.write('Version: 1.0\n')
- return 'CherryPy'
+ fixtures.build_files(self.make_pkg('my_pkg'), self.site_dir)
+ assert version('my-pkg') == '1.0'
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)
+ pkg_name = 'CherryPy'
+ fixtures.build_files(self.make_pkg(pkg_name), self.site_dir)
assert version(pkg_name) == '1.0'
assert version(pkg_name.lower()) == '1.0'
assert version(pkg_name.upper()) == '1.0'
+ def test_unique_distributions(self):
+ """
+ Two distributions varying only by non-normalized name on
+ the file system should resolve as the same.
+ """
+ fixtures.build_files(self.make_pkg('abc'), self.site_dir)
+ before = list(_unique(distributions()))
+
+ alt_site_dir = self.fixtures.enter_context(fixtures.tempdir())
+ self.fixtures.enter_context(self.add_sys_path(alt_site_dir))
+ fixtures.build_files(self.make_pkg('ABC'), alt_site_dir)
+ after = list(_unique(distributions()))
+
+ assert len(after) == len(before)
+
class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
@staticmethod
@@ -128,11 +138,12 @@ class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
Create minimal metadata for a package with non-ASCII in
the description.
"""
- metadata_dir = site_dir / 'portend.dist-info'
- metadata_dir.mkdir()
- metadata = metadata_dir / 'METADATA'
- with metadata.open('w', encoding='utf-8') as fp:
- fp.write('Description: pôrˈtend')
+ contents = {
+ 'portend.dist-info': {
+ 'METADATA': 'Description: pôrˈtend',
+ },
+ }
+ fixtures.build_files(contents, site_dir)
return 'portend'
@staticmethod
@@ -141,19 +152,15 @@ class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
Create minimal metadata for an egg-info package with
non-ASCII in the description.
"""
- metadata_dir = site_dir / 'portend.dist-info'
- metadata_dir.mkdir()
- metadata = metadata_dir / 'METADATA'
- with metadata.open('w', encoding='utf-8') as fp:
- fp.write(
- textwrap.dedent(
- """
+ contents = {
+ 'portend.dist-info': {
+ 'METADATA': """
Name: portend
- pôrˈtend
- """
- ).strip()
- )
+ pôrˈtend""",
+ },
+ }
+ fixtures.build_files(contents, site_dir)
return 'portend'
def test_metadata_loads(self):
diff --git a/Lib/test/test_importlib/test_metadata_api.py b/Lib/test/test_importlib/test_metadata_api.py
index c86bb4d..69c78e9 100644
--- a/Lib/test/test_importlib/test_metadata_api.py
+++ b/Lib/test/test_importlib/test_metadata_api.py
@@ -89,15 +89,15 @@ class APITests(
self.assertIn(ep.dist.name, ('distinfo-pkg', 'egginfo-pkg'))
self.assertEqual(ep.dist.version, "1.0.0")
- def test_entry_points_unique_packages(self):
+ def test_entry_points_unique_packages_normalized(self):
"""
Entry points should only be exposed for the first package
- on sys.path with a given name.
+ on sys.path with a given name (even when normalized).
"""
alt_site_dir = self.fixtures.enter_context(fixtures.tempdir())
self.fixtures.enter_context(self.add_sys_path(alt_site_dir))
alt_pkg = {
- "distinfo_pkg-1.1.0.dist-info": {
+ "DistInfo_pkg-1.1.0.dist-info": {
"METADATA": """
Name: distinfo-pkg
Version: 1.1.0