Merge pull request #1935 from nicoddemus/assert-rewrite-dev-plugins

Fixed assertion rewriting for plugins in development mode
This commit is contained in:
Ronny Pfannschmidt 2016-09-15 08:45:23 +02:00 committed by GitHub
commit 40ec35767f
3 changed files with 33 additions and 15 deletions

View File

@ -7,7 +7,9 @@
* *
* * Assertions are now being rewritten for plugins in development mode
(``pip install -e``) (`#1934`_).
Thanks `@nicoddemus`_ for the PR.
* *
@ -17,6 +19,7 @@
.. _@philpep: https://github.com/philpep .. _@philpep: https://github.com/philpep
.. _#1905: https://github.com/pytest-dev/pytest/issues/1905 .. _#1905: https://github.com/pytest-dev/pytest/issues/1905
.. _#1934: https://github.com/pytest-dev/pytest/issues/1934
3.0.2 3.0.2

View File

@ -954,16 +954,21 @@ class Config(object):
else: else:
self.pluginmanager.rewrite_hook = hook self.pluginmanager.rewrite_hook = hook
for entrypoint in pkg_resources.iter_entry_points('pytest11'): for entrypoint in pkg_resources.iter_entry_points('pytest11'):
for entry in entrypoint.dist._get_metadata('RECORD'): # 'RECORD' available for plugins installed normally (pip install)
fn = entry.split(',')[0] # 'SOURCES.txt' available for plugins installed in dev mode (pip install -e)
is_simple_module = os.sep not in fn and fn.endswith('.py') # for installed plugins 'SOURCES.txt' returns an empty list, and vice-versa
is_package = fn.count(os.sep) == 1 and fn.endswith('__init__.py') # so it shouldn't be an issue
if is_simple_module: for metadata in ('RECORD', 'SOURCES.txt'):
module_name, ext = os.path.splitext(fn) for entry in entrypoint.dist._get_metadata(metadata):
hook.mark_rewrite(module_name) fn = entry.split(',')[0]
elif is_package: is_simple_module = os.sep not in fn and fn.endswith('.py')
package_name = os.path.dirname(fn) is_package = fn.count(os.sep) == 1 and fn.endswith('__init__.py')
hook.mark_rewrite(package_name) if is_simple_module:
module_name, ext = os.path.splitext(fn)
hook.mark_rewrite(module_name)
elif is_package:
package_name = os.path.dirname(fn)
hook.mark_rewrite(package_name)
self._warn_about_missing_assertion(mode) self._warn_about_missing_assertion(mode)
def _warn_about_missing_assertion(self, mode): def _warn_about_missing_assertion(self, mode):

View File

@ -108,7 +108,8 @@ class TestImportHookInstallation:
assert result.ret == 0 assert result.ret == 0
@pytest.mark.parametrize('mode', ['plain', 'rewrite']) @pytest.mark.parametrize('mode', ['plain', 'rewrite'])
def test_installed_plugin_rewrite(self, testdir, mode): @pytest.mark.parametrize('plugin_state', ['development', 'installed'])
def test_installed_plugin_rewrite(self, testdir, mode, plugin_state):
# Make sure the hook is installed early enough so that plugins # Make sure the hook is installed early enough so that plugins
# installed via setuptools are re-written. # installed via setuptools are re-written.
testdir.tmpdir.join('hampkg').ensure(dir=1) testdir.tmpdir.join('hampkg').ensure(dir=1)
@ -135,13 +136,22 @@ class TestImportHookInstallation:
'mainwrapper.py': """ 'mainwrapper.py': """
import pytest, pkg_resources import pytest, pkg_resources
plugin_state = "{plugin_state}"
class DummyDistInfo: class DummyDistInfo:
project_name = 'spam' project_name = 'spam'
version = '1.0' version = '1.0'
def _get_metadata(self, name): def _get_metadata(self, name):
return ['spamplugin.py,sha256=abc,123', # 'RECORD' meta-data only available in installed plugins
'hampkg/__init__.py,sha256=abc,123'] if name == 'RECORD' and plugin_state == "installed":
return ['spamplugin.py,sha256=abc,123',
'hampkg/__init__.py,sha256=abc,123']
# 'SOURCES.txt' meta-data only available for plugins in development mode
elif name == 'SOURCES.txt' and plugin_state == "development":
return ['spamplugin.py',
'hampkg/__init__.py']
return []
class DummyEntryPoint: class DummyEntryPoint:
name = 'spam' name = 'spam'
@ -159,7 +169,7 @@ class TestImportHookInstallation:
pkg_resources.iter_entry_points = iter_entry_points pkg_resources.iter_entry_points = iter_entry_points
pytest.main() pytest.main()
""", """.format(plugin_state=plugin_state),
'test_foo.py': """ 'test_foo.py': """
def test(check_first): def test(check_first):
check_first([10, 30], 30) check_first([10, 30], 30)