summaryrefslogtreecommitdiff
path: root/testing/test_monkeypatch.py
diff options
context:
space:
mode:
authorRalf Schmitt <ralf@systemexit.de>2012-01-04 12:40:57 +0100
committerRalf Schmitt <ralf@systemexit.de>2012-01-04 12:40:57 +0100
commit4131923c0f6e636010f8a1ad6730ba42b7bac6af (patch)
tree57ad9c309ab0a81e77ae16e0bd0dd567b35cffeb /testing/test_monkeypatch.py
parent7b95af240048e13ef482250528d9fec26662c053 (diff)
downloadpytest-4131923c0f6e636010f8a1ad6730ba42b7bac6af.tar.gz
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.
Diffstat (limited to 'testing/test_monkeypatch.py')
-rw-r--r--testing/test_monkeypatch.py42
1 files changed, 27 insertions, 15 deletions
diff --git a/testing/test_monkeypatch.py b/testing/test_monkeypatch.py
index c1abe0743..8ec7e1b23 100644
--- a/testing/test_monkeypatch.py
+++ b/testing/test_monkeypatch.py
@@ -2,6 +2,17 @@ import os, sys
import pytest
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():
class A:
x = 1
@@ -144,19 +155,20 @@ def test_monkeypatch_plugin(testdir):
res = reprec.countoutcomes()
assert tuple(res) == (1, 0, 0), res
-def test_syspath_prepend():
+def test_syspath_prepend(mp):
old = list(sys.path)
- try:
- monkeypatch = MonkeyPatch()
- monkeypatch.syspath_prepend('world')
- monkeypatch.syspath_prepend('hello')
- assert sys.path[0] == "hello"
- assert sys.path[1] == "world"
- monkeypatch.undo()
- assert sys.path == old
- monkeypatch.undo()
- assert sys.path == old
- finally:
- sys.path[:] = old
-
-
+ mp.syspath_prepend('world')
+ mp.syspath_prepend('hello')
+ assert sys.path[0] == "hello"
+ assert sys.path[1] == "world"
+ mp.undo()
+ assert sys.path == old
+ mp.undo()
+ assert 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'