Skip to content

Commit 04cf5e1

Browse files
committed
Fixed assertion rewriting for plugins in development mode
Fix #1934
1 parent 51378ab commit 04cf5e1

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
*
99

10-
*
10+
* Assertions are now being rewritten for plugins in development mode
11+
(``pip install -e``) (`#1934`_).
12+
Thanks `@nicoddemus`_ for the PR.
1113

1214
*
1315

@@ -17,6 +19,7 @@
1719
.. _@philpep: https://github.com/philpep
1820

1921
.. _#1905: https://github.com/pytest-dev/pytest/issues/1905
22+
.. _#1934: https://github.com/pytest-dev/pytest/issues/1934
2023

2124

2225
3.0.2

_pytest/config.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -954,16 +954,21 @@ def _consider_importhook(self, args, entrypoint_name):
954954
else:
955955
self.pluginmanager.rewrite_hook = hook
956956
for entrypoint in pkg_resources.iter_entry_points('pytest11'):
957-
for entry in entrypoint.dist._get_metadata('RECORD'):
958-
fn = entry.split(',')[0]
959-
is_simple_module = os.sep not in fn and fn.endswith('.py')
960-
is_package = fn.count(os.sep) == 1 and fn.endswith('__init__.py')
961-
if is_simple_module:
962-
module_name, ext = os.path.splitext(fn)
963-
hook.mark_rewrite(module_name)
964-
elif is_package:
965-
package_name = os.path.dirname(fn)
966-
hook.mark_rewrite(package_name)
957+
# 'RECORD' available for plugins installed normally (pip install)
958+
# 'SOURCES.txt' available for plugins installed in dev mode (pip install -e)
959+
# for installed plugins 'SOURCES.txt' returns an empty list, and vice-versa
960+
# so it shouldn't be an issue
961+
for metadata in ('RECORD', 'SOURCES.txt'):
962+
for entry in entrypoint.dist._get_metadata(metadata):
963+
fn = entry.split(',')[0]
964+
is_simple_module = os.sep not in fn and fn.endswith('.py')
965+
is_package = fn.count(os.sep) == 1 and fn.endswith('__init__.py')
966+
if is_simple_module:
967+
module_name, ext = os.path.splitext(fn)
968+
hook.mark_rewrite(module_name)
969+
elif is_package:
970+
package_name = os.path.dirname(fn)
971+
hook.mark_rewrite(package_name)
967972
self._warn_about_missing_assertion(mode)
968973

969974
def _warn_about_missing_assertion(self, mode):

testing/test_assertion.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ def test_foo(pytestconfig):
108108
assert result.ret == 0
109109

110110
@pytest.mark.parametrize('mode', ['plain', 'rewrite'])
111-
def test_installed_plugin_rewrite(self, testdir, mode):
111+
@pytest.mark.parametrize('plugin_state', ['development', 'installed'])
112+
def test_installed_plugin_rewrite(self, testdir, mode, plugin_state):
112113
# Make sure the hook is installed early enough so that plugins
113114
# installed via setuptools are re-written.
114115
testdir.tmpdir.join('hampkg').ensure(dir=1)
@@ -135,13 +136,22 @@ def check(values, value):
135136
'mainwrapper.py': """
136137
import pytest, pkg_resources
137138
139+
plugin_state = "{plugin_state}"
140+
138141
class DummyDistInfo:
139142
project_name = 'spam'
140143
version = '1.0'
141144
142145
def _get_metadata(self, name):
143-
return ['spamplugin.py,sha256=abc,123',
144-
'hampkg/__init__.py,sha256=abc,123']
146+
# 'RECORD' meta-data only available in installed plugins
147+
if name == 'RECORD' and plugin_state == "installed":
148+
return ['spamplugin.py,sha256=abc,123',
149+
'hampkg/__init__.py,sha256=abc,123']
150+
# 'SOURCES.txt' meta-data only available for plugins in development mode
151+
elif name == 'SOURCES.txt' and plugin_state == "development":
152+
return ['spamplugin.py',
153+
'hampkg/__init__.py']
154+
return []
145155
146156
class DummyEntryPoint:
147157
name = 'spam'
@@ -159,7 +169,7 @@ def iter_entry_points(name):
159169
160170
pkg_resources.iter_entry_points = iter_entry_points
161171
pytest.main()
162-
""",
172+
""".format(plugin_state=plugin_state),
163173
'test_foo.py': """
164174
def test(check_first):
165175
check_first([10, 30], 30)

0 commit comments

Comments
 (0)