Skip to content

Commit b0541e9

Browse files
committed
Correctly restore sys.path in test and remove dead code in test_pytester
The code in test_pytester has been refactored into a class right above the dead code, and the code has been left there by mistake apparently.
1 parent f872fcb commit b0541e9

File tree

2 files changed

+9
-56
lines changed

2 files changed

+9
-56
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_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)