Skip to content

Commit

Permalink
Avoid plugin activation with random ansible_ prefixed fixtures
Browse files Browse the repository at this point in the history
Fixes a bug where a randomly named fixture caused plugin activation
even if the project would not make any use of pytest-ansible, causing
a runtime error due to the assertion of missing arguments.
  • Loading branch information
ssbarnea committed Oct 27, 2023
1 parent d84d0fc commit 467f5bc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 40 deletions.
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

0 comments on commit 467f5bc

Please sign in to comment.