Skip to content

Commit d01009b

Browse files
committed
test(refactor): drop pkg_resources
1 parent 3ac3d7f commit d01009b

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

tests/modules/process_test/try_execfile.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929

3030
from typing import Any, List
3131

32-
# sys.path varies by execution environments. Coverage.py uses setuptools to
33-
# make console scripts, which means pkg_resources is imported. pkg_resources
32+
# sys.path varies by execution environments. Some installation libraries
3433
# removes duplicate entries from sys.path. So we do that too, since the extra
3534
# entries don't affect the running of the program.
3635

tests/test_process.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -850,16 +850,19 @@ def test_coverage_custom_script(self) -> None:
850850
platform.python_version().endswith("+"),
851851
reason="setuptools barfs on dev versions: https://github.com/pypa/packaging/issues/678"
852852
# https://github.com/nedbat/coveragepy/issues/1556
853-
# TODO: get rid of pkg_resources
854853
)
855854
def test_bug_862(self) -> None:
856-
# This simulates how pyenv and pyenv-virtualenv end up creating the
857-
# coverage executable.
858-
self.make_file("elsewhere/bin/fake-coverage", """\
859-
#!{executable}
860-
import sys, pkg_resources
861-
sys.exit(pkg_resources.load_entry_point('coverage', 'console_scripts', 'coverage')())
862-
""".format(executable=sys.executable))
855+
# This used to simulate how pyenv and pyenv-virtualenv create the
856+
# coverage executable. Now the code shows how venv does it.
857+
self.make_file("elsewhere/bin/fake-coverage", f"""\
858+
#!{sys.executable}
859+
import re
860+
import sys
861+
from coverage.cmdline import main
862+
if __name__ == '__main__':
863+
sys.argv[0] = re.sub(r'(-script\\.pyw|\\.exe)?$', '', sys.argv[0])
864+
sys.exit(main())
865+
""")
863866
os.chmod("elsewhere/bin/fake-coverage", stat.S_IREAD | stat.S_IEXEC)
864867
os.symlink("elsewhere", "somewhere")
865868
self.make_file("foo.py", "print('inside foo')")

tests/test_venv.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,40 +105,33 @@ def sixth(x):
105105
setup(
106106
name='testcov',
107107
packages=['testcov'],
108-
namespace_packages=['testcov'],
109108
)
110109
""")
110+
# https://packaging.python.org/en/latest/guides/packaging-namespace-packages/#pkgutil-style-namespace-packages
111111
make_file("bug888/app/testcov/__init__.py", """\
112-
try: # pragma: no cover
113-
__import__('pkg_resources').declare_namespace(__name__)
114-
except ImportError: # pragma: no cover
115-
from pkgutil import extend_path
116-
__path__ = extend_path(__path__, __name__)
112+
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
117113
""")
118-
make_file("bug888/app/testcov/main.py", """\
119-
try: # pragma: no cover
120-
entry_points = __import__("pkg_resources").iter_entry_points('plugins')
121-
except ImportError: # pragma: no cover
122-
import importlib.metadata
123-
entry_points = importlib.metadata.entry_points(group="plugins")
124-
for entry_point in entry_points:
114+
if env.PYVERSION < (3, 10):
115+
get_plugins = "entry_points['plugins']"
116+
else:
117+
get_plugins = "entry_points.select(group='plugins')"
118+
make_file("bug888/app/testcov/main.py", f"""\
119+
import importlib.metadata
120+
entry_points = importlib.metadata.entry_points()
121+
for entry_point in {get_plugins}:
125122
entry_point.load()()
126123
""")
127124
make_file("bug888/plugin/setup.py", """\
128125
from setuptools import setup
129126
setup(
130127
name='testcov-plugin',
131128
packages=['testcov'],
132-
namespace_packages=['testcov'],
133129
entry_points={'plugins': ['testp = testcov.plugin:testp']},
134130
)
135131
""")
132+
# https://packaging.python.org/en/latest/guides/packaging-namespace-packages/#pkgutil-style-namespace-packages
136133
make_file("bug888/plugin/testcov/__init__.py", """\
137-
try: # pragma: no cover
138-
__import__('pkg_resources').declare_namespace(__name__)
139-
except ImportError: # pragma: no cover
140-
from pkgutil import extend_path
141-
__path__ = extend_path(__path__, __name__)
134+
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
142135
""")
143136
make_file("bug888/plugin/testcov/plugin.py", """\
144137
def testp():

0 commit comments

Comments
 (0)