test that a second undo doesn't change sys.path
also use a 'mp' funcarg that restores sys.path and the current working directory in preparation for the monkeypatch.chdir method.
This commit is contained in:
parent
7b95af2400
commit
4131923c0f
|
@ -2,6 +2,17 @@ import os, sys
|
||||||
import pytest
|
import pytest
|
||||||
from _pytest.monkeypatch import monkeypatch as MonkeyPatch
|
from _pytest.monkeypatch import monkeypatch as MonkeyPatch
|
||||||
|
|
||||||
|
def pytest_funcarg__mp(request):
|
||||||
|
cwd = os.getcwd()
|
||||||
|
sys_path = list(sys.path)
|
||||||
|
|
||||||
|
def cleanup():
|
||||||
|
sys.path[:] = sys_path
|
||||||
|
os.chdir(cwd)
|
||||||
|
|
||||||
|
request.addfinalizer(cleanup)
|
||||||
|
return MonkeyPatch()
|
||||||
|
|
||||||
def test_setattr():
|
def test_setattr():
|
||||||
class A:
|
class A:
|
||||||
x = 1
|
x = 1
|
||||||
|
@ -144,19 +155,20 @@ def test_monkeypatch_plugin(testdir):
|
||||||
res = reprec.countoutcomes()
|
res = reprec.countoutcomes()
|
||||||
assert tuple(res) == (1, 0, 0), res
|
assert tuple(res) == (1, 0, 0), res
|
||||||
|
|
||||||
def test_syspath_prepend():
|
def test_syspath_prepend(mp):
|
||||||
old = list(sys.path)
|
old = list(sys.path)
|
||||||
try:
|
mp.syspath_prepend('world')
|
||||||
monkeypatch = MonkeyPatch()
|
mp.syspath_prepend('hello')
|
||||||
monkeypatch.syspath_prepend('world')
|
|
||||||
monkeypatch.syspath_prepend('hello')
|
|
||||||
assert sys.path[0] == "hello"
|
assert sys.path[0] == "hello"
|
||||||
assert sys.path[1] == "world"
|
assert sys.path[1] == "world"
|
||||||
monkeypatch.undo()
|
mp.undo()
|
||||||
assert sys.path == old
|
assert sys.path == old
|
||||||
monkeypatch.undo()
|
mp.undo()
|
||||||
assert sys.path == old
|
assert sys.path == old
|
||||||
finally:
|
|
||||||
sys.path[:] = old
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_syspath_prepend_double_undo(mp):
|
||||||
|
mp.syspath_prepend('hello world')
|
||||||
|
mp.undo()
|
||||||
|
sys.path.append('more hello world')
|
||||||
|
mp.undo()
|
||||||
|
assert sys.path[-1] == 'more hello world'
|
||||||
|
|
Loading…
Reference in New Issue