Skip to content

Commit

Permalink
Merge pull request #2940 from nicoddemus/rewrite-bug-2939
Browse files Browse the repository at this point in the history
Fix assertion rewrite to match module names correctly
  • Loading branch information
The-Compiler committed Nov 22, 2017
2 parents 77bd0aa + c8d52b6 commit 8df7ed1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion _pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _should_rewrite(self, name, fn_pypath, state):
return True

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

Expand Down
1 change: 1 addition & 0 deletions changelog/2939.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix issue in assertion rewriting which could lead it to rewrite modules which should not be rewritten.
18 changes: 18 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ def test_foo(pytestconfig):
result = testdir.runpytest_subprocess('--assert=rewrite')
assert result.ret == 0

def test_pytest_plugins_rewrite_module_names_correctly(self, testdir):
"""Test that we match files correctly when they are marked for rewriting (#2939)."""
contents = {
'conftest.py': """
pytest_plugins = "ham"
""",
'ham.py': "",
'hamster.py': "",
'test_foo.py': """
def test_foo(pytestconfig):
assert pytestconfig.pluginmanager.rewrite_hook.find_module('ham') is not None
assert pytestconfig.pluginmanager.rewrite_hook.find_module('hamster') is None
""",
}
testdir.makepyfile(**contents)
result = testdir.runpytest_subprocess('--assert=rewrite')
assert result.ret == 0

@pytest.mark.parametrize('mode', ['plain', 'rewrite'])
@pytest.mark.parametrize('plugin_state', ['development', 'installed'])
def test_installed_plugin_rewrite(self, testdir, mode, plugin_state):
Expand Down

0 comments on commit 8df7ed1

Please sign in to comment.