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

Avoid accidental plugin activation #202

Merged
merged 1 commit into from
Oct 27, 2023
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
72 changes: 37 additions & 35 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
Barroso
Laska
NOTESTSCOLLECTED
Rominger
TESTSFAILED
USAGEERROR
VIRSH
addini
addinivalue_line
addoption
argparsing
autouse
caplog
cidrblock
codecov
errlines
filterwarnings
pathsep
pytest
setenv
setuptools
startpath
metafunc
testpaths
udring
webservers
addoption
Rominger
Barroso
Laska
runas
runtask
pluginmanager
modifyitems
fixturedefs
fixtureinfo
fixturenames
fspath
funcargs
getfixturevalue
getplugin
pytestmark
autouse
rootpath
VIRSH
pytester
getgroup
addini
addinivalue_line
getplugin
lineinfile
makepyfile
metafunc
modifyitems
pandoc
funcargs
parseoutcomes
pathsep
pluginmanager
pytest
pytester
pytestmark
pytrace
reportinfo
fspath
NOTESTSCOLLECTED
TESTSFAILED
USAGEERROR
makepyfile
runpytest_subprocess
parseoutcomes
rootpath
runas
runpytest
errlines
codecov
runpytest_subprocess
runtask
setenv
setuptools
startpath
testpaths
udring
webservers
19 changes: 16 additions & 3 deletions src/pytest_ansible/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
3: logging.INFO,
4: logging.DEBUG,
}
OUR_FIXTURES = ("ansible_adhoc", "ansible_module", "ansible_facts")


def pytest_addoption(parser):
Expand Down Expand Up @@ -333,12 +334,24 @@ def pytest_collection_modifyitems(self, session, config, items):
for item in items:
if not hasattr(item, "fixturenames"):
continue
if any(fixture.startswith("ansible_") for fixture in item.fixturenames):
marker = item.get_closest_marker("ansible")
if marker is None:

for fixture_name in item.fixturenames:
if fixture_name in OUR_FIXTURES:
uses_ansible_fixtures = True
break

# ignore any normal fixtures that have definitions to avoid miss activations
if (
hasattr(item, "_fixtureinfo")
and hasattr(item._fixtureinfo, "name2fixturedefs")
and fixture_name in item._fixtureinfo.name2fixturedefs
):
continue
logger.error(
"Found %s fixture which seem to have no definition.",
fixture_name,
)

if uses_ansible_fixtures:
# assert required --ansible-* parameters were used
self.assert_required_ansible_parameters(config)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_pytest_collection_modifyitems_without_marker():
mock_config.setoption("ansible_inventory", "some_inventory")

plugin = PyTestAnsiblePlugin(mock_config)
items = [MockItem(fixturenames=["ansible_fixture"])]
items = [MockItem(fixturenames=["ansible_adhoc"])]

# Without the marker, ensure that assert_required_ansible_parameters is called
with mock.patch.object(plugin, "assert_required_ansible_parameters") as mock_assert:
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ description =
commands_pre =
sh -c "rm -f .tox/.coverage.* 2>/dev/null || true"
commands =
coverage run --parallel --source pytest_ansible -m pytest -v --doctest-glob='*.md' {posargs}
coverage run --parallel --source pytest_ansible -m pytest --doctest-glob='*.md' {posargs}
sh -c "coverage combine -a -q --data-file=.coverage .tox/.coverage.*"

passenv =
Expand Down