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

Verify hooks after collection completes #2292

Merged
merged 5 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 9 additions & 3 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@ Changes
* Change exception raised by ``capture.DontReadFromInput.fileno()`` from ``ValueError``
to ``io.UnsupportedOperation``. Thanks `@vlad-dragos`_ for the PR.

* fix `#2013`_: turn RecordedWarning into namedtupe,
to give it a comprehensible repr while preventing unwarranted modification
* fix `#2013`_: turn RecordedWarning into ``namedtuple``,
to give it a comprehensible repr while preventing unwarranted modification.

* fix `#2208`_: ensure a iteration limit for _pytest.compat.get_real_func.
Thanks `@RonnyPfannschmidt`_ for the Report and PR
Thanks `@RonnyPfannschmidt`_ for the report and PR.

* Hooks are now verified after collection is complete, rather than right after loading installed plugins. This
makes it easy to write hooks for plugins which will be loaded during collection, for example using the
``pytest_plugins`` special variable (`#1821`_).
Thanks `@nicoddemus`_ for the PR.

* Modify ``pytest_make_parametrize_id()`` hook to accept ``argname`` as an
additional parameter.
Expand Down Expand Up @@ -96,6 +101,7 @@ Bug Fixes

.. _#1407: https://github.com/pytest-dev/pytest/issues/1407
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
.. _#1821: https://github.com/pytest-dev/pytest/issues/1821
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874
.. _#1952: https://github.com/pytest-dev/pytest/pull/1952
.. _#2007: https://github.com/pytest-dev/pytest/issues/2007
Expand Down
1 change: 0 additions & 1 deletion _pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def main(args=None, plugins=None):
return 4
else:
try:
config.pluginmanager.check_pending()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we remove this check just because we added a new one at a different time in execution?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, that was the main motivation of this PR (at least for me): to delay hook checking until we have loaded all plugins, included those delayed by the pytest_plugins mechanism; this avoid having to write [deferred installation](https://docs.pytest.org/en/latest/writing_plugins.html#optionally-using-hooks-from-3rd-party-plugins mechanisms) if the hooks declared by other plugins are required by yours.

Also see #1821 for a detailed example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicoddemus oh right, thanks for the reference, my understanding had been wrong

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries!

Do you or anybody else have any pointers on how to fix the tests leaking FDs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately not, does it hapen locally as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, but I don't have a Linux box with me to check this now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is unrelated to this PR (the PR actually fixed the plugin and it is working again), should we create an issue for it and disable the plugin internally?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

return config.hook.pytest_cmdline_main(config=config)
finally:
config._ensure_unconfigure()
Expand Down
1 change: 1 addition & 0 deletions _pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ def perform_collect(self, args=None, genitems=True):
hook = self.config.hook
try:
items = self._perform_collect(args, genitems)
self.config.pluginmanager.check_pending()
hook.pytest_collection_modifyitems(session=self,
config=self.config, items=items)
finally:
Expand Down
2 changes: 1 addition & 1 deletion _pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def matching_platform(self):
return True

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_item(self, item):
def pytest_runtest_protocol(self, item):
lines1 = self.get_open_files()
yield
if hasattr(sys, "pypy_version_info"):
Expand Down
28 changes: 28 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,3 +807,31 @@ def test(): pass
""")
r = testdir.runpytest()
assert r.ret == 0


def test_deferred_hook_checking(testdir):
"""
Check hooks as late as possible (#1821).
"""
testdir.syspathinsert()
testdir.makepyfile(**{
'plugin.py': """
class Hooks:
def pytest_my_hook(self, config):
pass

def pytest_configure(config):
config.pluginmanager.add_hookspecs(Hooks)
""",
'conftest.py': """
pytest_plugins = ['plugin']
def pytest_my_hook(config):
return 40
""",
'test_foo.py': """
def test(request):
assert request.config.hook.pytest_my_hook(config=request.config) == [40]
"""
})
result = testdir.runpytest()
result.stdout.fnmatch_lines(['* 1 passed *'])
2 changes: 1 addition & 1 deletion testing/test_helpconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def pytest_hello(xyz):
""")
result = testdir.runpytest()
assert result.ret != 0
result.stderr.fnmatch_lines([
result.stdout.fnmatch_lines([
'*unknown hook*pytest_hello*'
])

Expand Down