Skip to content

Commit c8d52b6

Browse files
committed
Fix assertion rewrite to match module names correctly
Fix pytest-dev#2939
1 parent 77bd0aa commit c8d52b6

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

_pytest/assertion/rewrite.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def _should_rewrite(self, name, fn_pypath, state):
168168
return True
169169

170170
for marked in self._must_rewrite:
171-
if name.startswith(marked):
171+
if name == marked or name.startswith(marked + '.'):
172172
state.trace("matched marked file %r (from %r)" % (name, marked))
173173
return True
174174

changelog/2939.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix issue in assertion rewriting which could lead it to rewrite modules which should not be rewritten.

testing/test_assertion.py

+18
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,24 @@ def test_foo(pytestconfig):
129129
result = testdir.runpytest_subprocess('--assert=rewrite')
130130
assert result.ret == 0
131131

132+
def test_pytest_plugins_rewrite_module_names_correctly(self, testdir):
133+
"""Test that we match files correctly when they are marked for rewriting (#2939)."""
134+
contents = {
135+
'conftest.py': """
136+
pytest_plugins = "ham"
137+
""",
138+
'ham.py': "",
139+
'hamster.py': "",
140+
'test_foo.py': """
141+
def test_foo(pytestconfig):
142+
assert pytestconfig.pluginmanager.rewrite_hook.find_module('ham') is not None
143+
assert pytestconfig.pluginmanager.rewrite_hook.find_module('hamster') is None
144+
""",
145+
}
146+
testdir.makepyfile(**contents)
147+
result = testdir.runpytest_subprocess('--assert=rewrite')
148+
assert result.ret == 0
149+
132150
@pytest.mark.parametrize('mode', ['plain', 'rewrite'])
133151
@pytest.mark.parametrize('plugin_state', ['development', 'installed'])
134152
def test_installed_plugin_rewrite(self, testdir, mode, plugin_state):

0 commit comments

Comments
 (0)