Skip to content

Commit 2577a6c

Browse files
authored
Merge pull request #3873 from nicoddemus/sys-path-fix
Remove dangerous sys.path manipulations in test_pluginmanager
2 parents dd5f5ca + b0541e9 commit 2577a6c

File tree

3 files changed

+10
-59
lines changed

3 files changed

+10
-59
lines changed

testing/test_monkeypatch.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,15 @@ def test_syspath_prepend(mp):
228228

229229

230230
def test_syspath_prepend_double_undo(mp):
231-
mp.syspath_prepend("hello world")
232-
mp.undo()
233-
sys.path.append("more hello world")
234-
mp.undo()
235-
assert sys.path[-1] == "more hello world"
231+
old_syspath = sys.path[:]
232+
try:
233+
mp.syspath_prepend("hello world")
234+
mp.undo()
235+
sys.path.append("more hello world")
236+
mp.undo()
237+
assert sys.path[-1] == "more hello world"
238+
finally:
239+
sys.path[:] = old_syspath
236240

237241

238242
def test_chdir_with_path_local(mp, tmpdir):

testing/test_pluginmanager.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def pytest_myhook(xyz):
2525
)
2626
conf = testdir.makeconftest(
2727
"""
28-
import sys ; sys.path.insert(0, '.')
2928
import newhooks
3029
def pytest_addhooks(pluginmanager):
3130
pluginmanager.addhooks(newhooks)
@@ -263,8 +262,7 @@ def test_consider_module_import_module(self, testdir):
263262
mod.pytest_plugins = "pytest_a"
264263
aplugin = testdir.makepyfile(pytest_a="#")
265264
reprec = testdir.make_hook_recorder(pytestpm)
266-
# syspath.prepend(aplugin.dirpath())
267-
sys.path.insert(0, str(aplugin.dirpath()))
265+
testdir.syspathinsert(aplugin.dirpath())
268266
pytestpm.consider_module(mod)
269267
call = reprec.getcall(pytestpm.hook.pytest_plugin_registered.name)
270268
assert call.plugin.__name__ == "pytest_a"

testing/test_pytester.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -215,57 +215,6 @@ def test_foo():
215215
assert imported.data == 42
216216

217217

218-
def test_inline_run_clean_sys_paths(testdir):
219-
def test_sys_path_change_cleanup(self, testdir):
220-
test_path1 = testdir.tmpdir.join("boink1").strpath
221-
test_path2 = testdir.tmpdir.join("boink2").strpath
222-
test_path3 = testdir.tmpdir.join("boink3").strpath
223-
sys.path.append(test_path1)
224-
sys.meta_path.append(test_path1)
225-
original_path = list(sys.path)
226-
original_meta_path = list(sys.meta_path)
227-
test_mod = testdir.makepyfile(
228-
"""
229-
import sys
230-
sys.path.append({:test_path2})
231-
sys.meta_path.append({:test_path2})
232-
def test_foo():
233-
sys.path.append({:test_path3})
234-
sys.meta_path.append({:test_path3})""".format(
235-
locals()
236-
)
237-
)
238-
testdir.inline_run(str(test_mod))
239-
assert sys.path == original_path
240-
assert sys.meta_path == original_meta_path
241-
242-
def spy_factory(self):
243-
class SysPathsSnapshotSpy(object):
244-
instances = []
245-
246-
def __init__(self):
247-
SysPathsSnapshotSpy.instances.append(self)
248-
self._spy_restore_count = 0
249-
self.__snapshot = SysPathsSnapshot()
250-
251-
def restore(self):
252-
self._spy_restore_count += 1
253-
return self.__snapshot.restore()
254-
255-
return SysPathsSnapshotSpy
256-
257-
def test_inline_run_taking_and_restoring_a_sys_paths_snapshot(
258-
self, testdir, monkeypatch
259-
):
260-
spy_factory = self.spy_factory()
261-
monkeypatch.setattr(pytester, "SysPathsSnapshot", spy_factory)
262-
test_mod = testdir.makepyfile("def test_foo(): pass")
263-
testdir.inline_run(str(test_mod))
264-
assert len(spy_factory.instances) == 1
265-
spy = spy_factory.instances[0]
266-
assert spy._spy_restore_count == 1
267-
268-
269218
def test_assert_outcomes_after_pytest_error(testdir):
270219
testdir.makepyfile("def test_foo(): assert True")
271220

0 commit comments

Comments
 (0)