summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_importlib
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2021-03-13 16:31:45 (GMT)
committerGitHub <noreply@github.com>2021-03-13 16:31:45 (GMT)
commitf917efccf8d5aa2b8315d2a832a520339e668187 (patch)
tree25319b76b7c107939278df8ebaaeed52619462bb /Lib/test/test_importlib
parent2256a2876b5214a5a7492bf78bd86cf8beb690bf (diff)
downloadcpython-f917efccf8d5aa2b8315d2a832a520339e668187.zip
cpython-f917efccf8d5aa2b8315d2a832a520339e668187.tar.gz
cpython-f917efccf8d5aa2b8315d2a832a520339e668187.tar.bz2
bpo-43428: Sync with importlib_metadata 3.7. (GH-24782)
* bpo-43428: Sync with importlib_metadata 3.7.2 (67234b6) * Add blurb * Reformat blurb to create separate paragraphs for each change included.
Diffstat (limited to 'Lib/test/test_importlib')
-rw-r--r--Lib/test/test_importlib/fixtures.py5
-rw-r--r--Lib/test/test_importlib/test_main.py10
-rw-r--r--Lib/test/test_importlib/test_metadata_api.py84
-rw-r--r--Lib/test/test_importlib/test_zip.py2
4 files changed, 88 insertions, 13 deletions
diff --git a/Lib/test/test_importlib/fixtures.py b/Lib/test/test_importlib/fixtures.py
index acf6bc8..429313e 100644
--- a/Lib/test/test_importlib/fixtures.py
+++ b/Lib/test/test_importlib/fixtures.py
@@ -5,7 +5,6 @@ import pathlib
import tempfile
import textwrap
import contextlib
-import unittest
from test.support.os_helper import FS_NONASCII
from typing import Dict, Union
@@ -221,7 +220,6 @@ class LocalPackage:
build_files(self.files)
-
def build_files(file_defs, prefix=pathlib.Path()):
"""Build a set of files/directories, as described by the
@@ -260,9 +258,6 @@ class FileBuilder:
def unicode_filename(self):
return FS_NONASCII or self.skip("File system does not support non-ascii.")
- def skip(self, reason):
- raise unittest.SkipTest(reason)
-
def DALS(str):
"Dedent and left-strip"
diff --git a/Lib/test/test_importlib/test_main.py b/Lib/test/test_importlib/test_main.py
index c937361..02e8a57 100644
--- a/Lib/test/test_importlib/test_main.py
+++ b/Lib/test/test_importlib/test_main.py
@@ -3,6 +3,7 @@ import json
import pickle
import textwrap
import unittest
+import warnings
import importlib.metadata
try:
@@ -58,13 +59,11 @@ class ImportTests(fixtures.DistInfoPkg, unittest.TestCase):
importlib.import_module('does_not_exist')
def test_resolve(self):
- entries = dict(entry_points()['entries'])
- ep = entries['main']
+ ep = entry_points(group='entries')['main']
self.assertEqual(ep.load().__name__, "main")
def test_entrypoint_with_colon_in_name(self):
- entries = dict(entry_points()['entries'])
- ep = entries['ns:sub']
+ ep = entry_points(group='entries')['ns:sub']
self.assertEqual(ep.value, 'mod:main')
def test_resolve_without_attr(self):
@@ -250,7 +249,8 @@ class TestEntryPoints(unittest.TestCase):
json should not expect to be able to dump an EntryPoint
"""
with self.assertRaises(Exception):
- json.dumps(self.ep)
+ with warnings.catch_warnings(record=True):
+ json.dumps(self.ep)
def test_module(self):
assert self.ep.module == 'value'
diff --git a/Lib/test/test_importlib/test_metadata_api.py b/Lib/test/test_importlib/test_metadata_api.py
index df00ae9..a0f9d51 100644
--- a/Lib/test/test_importlib/test_metadata_api.py
+++ b/Lib/test/test_importlib/test_metadata_api.py
@@ -1,6 +1,7 @@
import re
import textwrap
import unittest
+import warnings
from . import fixtures
from importlib.metadata import (
@@ -64,18 +65,97 @@ class APITests(
self.assertEqual(top_level.read_text(), 'mod\n')
def test_entry_points(self):
- entries = dict(entry_points()['entries'])
+ eps = entry_points()
+ assert 'entries' in eps.groups
+ entries = eps.select(group='entries')
+ assert 'main' in entries.names
ep = entries['main']
self.assertEqual(ep.value, 'mod:main')
self.assertEqual(ep.extras, [])
def test_entry_points_distribution(self):
- entries = dict(entry_points()['entries'])
+ entries = entry_points(group='entries')
for entry in ("main", "ns:sub"):
ep = entries[entry]
self.assertIn(ep.dist.name, ('distinfo-pkg', 'egginfo-pkg'))
self.assertEqual(ep.dist.version, "1.0.0")
+ def test_entry_points_unique_packages(self):
+ """
+ Entry points should only be exposed for the first package
+ on sys.path with a given name.
+ """
+ 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": {
+ "METADATA": """
+ Name: distinfo-pkg
+ Version: 1.1.0
+ """,
+ "entry_points.txt": """
+ [entries]
+ main = mod:altmain
+ """,
+ },
+ }
+ fixtures.build_files(alt_pkg, alt_site_dir)
+ entries = entry_points(group='entries')
+ assert not any(
+ ep.dist.name == 'distinfo-pkg' and ep.dist.version == '1.0.0'
+ for ep in entries
+ )
+ # ns:sub doesn't exist in alt_pkg
+ assert 'ns:sub' not in entries
+
+ def test_entry_points_missing_name(self):
+ with self.assertRaises(KeyError):
+ entry_points(group='entries')['missing']
+
+ def test_entry_points_missing_group(self):
+ assert entry_points(group='missing') == ()
+
+ def test_entry_points_dict_construction(self):
+ """
+ Prior versions of entry_points() returned simple lists and
+ allowed casting those lists into maps by name using ``dict()``.
+ Capture this now deprecated use-case.
+ """
+ with warnings.catch_warnings(record=True) as caught:
+ warnings.filterwarnings("default", category=DeprecationWarning)
+ eps = dict(entry_points(group='entries'))
+
+ assert 'main' in eps
+ assert eps['main'] == entry_points(group='entries')['main']
+
+ # check warning
+ expected = next(iter(caught))
+ assert expected.category is DeprecationWarning
+ assert "Construction of dict of EntryPoints is deprecated" in str(expected)
+
+ def test_entry_points_groups_getitem(self):
+ """
+ Prior versions of entry_points() returned a dict. Ensure
+ that callers using '.__getitem__()' are supported but warned to
+ migrate.
+ """
+ with warnings.catch_warnings(record=True):
+ entry_points()['entries'] == entry_points(group='entries')
+
+ with self.assertRaises(KeyError):
+ entry_points()['missing']
+
+ def test_entry_points_groups_get(self):
+ """
+ Prior versions of entry_points() returned a dict. Ensure
+ that callers using '.get()' are supported but warned to
+ migrate.
+ """
+ with warnings.catch_warnings(record=True):
+ entry_points().get('missing', 'default') == 'default'
+ entry_points().get('entries', 'default') == entry_points()['entries']
+ entry_points().get('missing', ()) == ()
+
def test_metadata_for_this_package(self):
md = metadata('egginfo-pkg')
assert md['author'] == 'Steven Ma'
diff --git a/Lib/test/test_importlib/test_zip.py b/Lib/test/test_importlib/test_zip.py
index 74783fc..83e0413 100644
--- a/Lib/test/test_importlib/test_zip.py
+++ b/Lib/test/test_importlib/test_zip.py
@@ -41,7 +41,7 @@ class TestZip(unittest.TestCase):
version('definitely-not-installed')
def test_zip_entry_points(self):
- scripts = dict(entry_points()['console_scripts'])
+ scripts = entry_points(group='console_scripts')
entry_point = scripts['example']
self.assertEqual(entry_point.value, 'example:main')
entry_point = scripts['Example']