Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/tox-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,20 @@ jobs:
run: pip install tox
- name: Run Tox
run: tox -e py3-bandit
test-py39:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: "3.9"
- name: Install OS packages
run: |
sudo apt-get -y update
sudo apt-get install -y libkrb5-dev
- name: Install Tox
run: pip install tox
- name: Run Tox
run: tox -e py39

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pluggy
setuptools
importlib-metadata; python_version < "3.10"
6 changes: 5 additions & 1 deletion src/pubtools/_impl/pluggy.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import logging
import sys
from contextlib import contextmanager
from importlib.metadata import entry_points

if sys.version_info >= (3, 10):
from importlib.metadata import entry_points
else: # pragma: no cover
# for older python use non-standard compatible module
from importlib_metadata import entry_points
import pluggy

LOG = logging.getLogger("pubtools")
Expand Down
63 changes: 35 additions & 28 deletions tests/hooks/test_entry_points.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
import sys
from importlib import metadata
from importlib.metadata import EntryPoint

if sys.version_info >= (3, 10):
from importlib.metadata import EntryPoint
else:
from importlib_metadata import EntryPoint

from pubtools._impl import pluggy
from pubtools.pluggy import task_context


def test_task_context_loads_entry_points(monkeypatch):
"""task_context eagerly resolves console_scripts and pubtools.hooks entry points."""

target_mod = "pydoc"

# Define the mock entry points EntryPoint(name, value, group)
ep1 = EntryPoint(
name="some-script", value="pubtools.pluggy", group="console_scripts"
)
ep2 = EntryPoint(name="anything", value=target_mod, group="pubtools.hooks")

mock_entry_points = [ep1, ep2]

# Monkeypatch metadata.entry_points globally
# In the new API, mock the function that task_context() will call
def mock_eps(**kwargs):
group = kwargs.get("group")
if group:
return [ep for ep in mock_entry_points if ep.group == group]
return metadata.EntryPoints(mock_entry_points)

monkeypatch.setattr(metadata, "entry_points", mock_eps)

# "un-import" these modules (Ensure they are in sys.modules first so del doesn't fail,
# though usually they are already there from imports above)
for mod in ["pubtools.pluggy", target_mod]:
if mod in sys.modules:
del sys.modules[mod]
# importlib.metadata.entry_points() reads from installed package metadata;
# there is no mutable entry map to patch. Patch entry_points in the pluggy
# module so it returns our custom entry points by group.
console_scripts = [
# This one is pubtools.*, so it should be loaded by resolve_hooks.
EntryPoint("some-script", "pubtools.pluggy", "console_scripts"),
# This should be ignored (module does not start with "pubtools").
EntryPoint("other-script", "something.non.existent", "console_scripts"),
]
pubtools_hooks = [
EntryPoint("anything", "pytest", "pubtools.hooks"),
]

def fake_entry_points(group=None):
if group == "console_scripts":
return console_scripts
if group == "pubtools.hooks":
return pubtools_hooks
return []

monkeypatch.setattr(pluggy, "entry_points", fake_entry_points)

# Effectively "un-import" these modules so we can observe that task_context
# has the side-effect of importing them.
assert "pubtools.pluggy" in sys.modules
assert "pytest" in sys.modules
del sys.modules["pubtools.pluggy"]
del sys.modules["pytest"]

with task_context():
# As soon as we've entered the task context, the modules referenced from
# those entry points should be imported, ensuring we can now use hooks with
# everything registered
assert "pubtools.pluggy" in sys.modules
assert target_mod in sys.modules
assert "pytest" in sys.modules
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ deps=-rtest-requirements.txt
commands=pytest -v {posargs}
whitelist_externals=sh

[testenv:py39]
deps=-rtest-requirements.in

[testenv:cov]
usedevelop=true
commands=
Expand Down