Merge pull request #3873 from nicoddemus/sys-path-fix

Remove dangerous sys.path manipulations in test_pluginmanager
This commit is contained in:
Bruno Oliveira 2018-08-26 08:53:57 -03:00 committed by GitHub
commit 2577a6ce8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 59 deletions

View File

@ -228,11 +228,15 @@ def test_syspath_prepend(mp):
def test_syspath_prepend_double_undo(mp): def test_syspath_prepend_double_undo(mp):
mp.syspath_prepend("hello world") old_syspath = sys.path[:]
mp.undo() try:
sys.path.append("more hello world") mp.syspath_prepend("hello world")
mp.undo() mp.undo()
assert sys.path[-1] == "more hello world" sys.path.append("more hello world")
mp.undo()
assert sys.path[-1] == "more hello world"
finally:
sys.path[:] = old_syspath
def test_chdir_with_path_local(mp, tmpdir): def test_chdir_with_path_local(mp, tmpdir):

View File

@ -25,7 +25,6 @@ class TestPytestPluginInteractions(object):
) )
conf = testdir.makeconftest( conf = testdir.makeconftest(
""" """
import sys ; sys.path.insert(0, '.')
import newhooks import newhooks
def pytest_addhooks(pluginmanager): def pytest_addhooks(pluginmanager):
pluginmanager.addhooks(newhooks) pluginmanager.addhooks(newhooks)
@ -263,8 +262,7 @@ class TestPytestPluginManager(object):
mod.pytest_plugins = "pytest_a" mod.pytest_plugins = "pytest_a"
aplugin = testdir.makepyfile(pytest_a="#") aplugin = testdir.makepyfile(pytest_a="#")
reprec = testdir.make_hook_recorder(pytestpm) reprec = testdir.make_hook_recorder(pytestpm)
# syspath.prepend(aplugin.dirpath()) testdir.syspathinsert(aplugin.dirpath())
sys.path.insert(0, str(aplugin.dirpath()))
pytestpm.consider_module(mod) pytestpm.consider_module(mod)
call = reprec.getcall(pytestpm.hook.pytest_plugin_registered.name) call = reprec.getcall(pytestpm.hook.pytest_plugin_registered.name)
assert call.plugin.__name__ == "pytest_a" assert call.plugin.__name__ == "pytest_a"

View File

@ -215,57 +215,6 @@ class TestInlineRunModulesCleanup(object):
assert imported.data == 42 assert imported.data == 42
def test_inline_run_clean_sys_paths(testdir):
def test_sys_path_change_cleanup(self, testdir):
test_path1 = testdir.tmpdir.join("boink1").strpath
test_path2 = testdir.tmpdir.join("boink2").strpath
test_path3 = testdir.tmpdir.join("boink3").strpath
sys.path.append(test_path1)
sys.meta_path.append(test_path1)
original_path = list(sys.path)
original_meta_path = list(sys.meta_path)
test_mod = testdir.makepyfile(
"""
import sys
sys.path.append({:test_path2})
sys.meta_path.append({:test_path2})
def test_foo():
sys.path.append({:test_path3})
sys.meta_path.append({:test_path3})""".format(
locals()
)
)
testdir.inline_run(str(test_mod))
assert sys.path == original_path
assert sys.meta_path == original_meta_path
def spy_factory(self):
class SysPathsSnapshotSpy(object):
instances = []
def __init__(self):
SysPathsSnapshotSpy.instances.append(self)
self._spy_restore_count = 0
self.__snapshot = SysPathsSnapshot()
def restore(self):
self._spy_restore_count += 1
return self.__snapshot.restore()
return SysPathsSnapshotSpy
def test_inline_run_taking_and_restoring_a_sys_paths_snapshot(
self, testdir, monkeypatch
):
spy_factory = self.spy_factory()
monkeypatch.setattr(pytester, "SysPathsSnapshot", spy_factory)
test_mod = testdir.makepyfile("def test_foo(): pass")
testdir.inline_run(str(test_mod))
assert len(spy_factory.instances) == 1
spy = spy_factory.instances[0]
assert spy._spy_restore_count == 1
def test_assert_outcomes_after_pytest_error(testdir): def test_assert_outcomes_after_pytest_error(testdir):
testdir.makepyfile("def test_foo(): assert True") testdir.makepyfile("def test_foo(): assert True")