From c8d52b633b6a7423633042c0eec3c67483ac13a3 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 21 Nov 2017 22:49:46 -0200 Subject: [PATCH] Fix assertion rewrite to match module names correctly Fix #2939 --- _pytest/assertion/rewrite.py | 2 +- changelog/2939.bugfix | 1 + testing/test_assertion.py | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 changelog/2939.bugfix diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index d48b6648f..27026b0fe 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -168,7 +168,7 @@ class AssertionRewritingHook(object): 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 diff --git a/changelog/2939.bugfix b/changelog/2939.bugfix new file mode 100644 index 000000000..8aeedad01 --- /dev/null +++ b/changelog/2939.bugfix @@ -0,0 +1 @@ +Fix issue in assertion rewriting which could lead it to rewrite modules which should not be rewritten. diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 4cd050d8c..244b47516 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -129,6 +129,24 @@ class TestImportHookInstallation(object): 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):