2009-09-06 22:59:39 +08:00
|
|
|
import os, sys
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.monkeypatch import monkeypatch as MonkeyPatch
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_setattr():
|
|
|
|
class A:
|
|
|
|
x = 1
|
2009-09-07 23:53:50 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(AttributeError, "monkeypatch.setattr(A, 'notexists', 2)")
|
2009-09-07 23:53:50 +08:00
|
|
|
monkeypatch.setattr(A, 'y', 2, raising=False)
|
|
|
|
assert A.y == 2
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert not hasattr(A, 'y')
|
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.setattr(A, 'x', 2)
|
|
|
|
assert A.x == 2
|
|
|
|
monkeypatch.setattr(A, 'x', 3)
|
|
|
|
assert A.x == 3
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert A.x == 1
|
|
|
|
|
|
|
|
A.x = 5
|
|
|
|
monkeypatch.undo() # double-undo makes no modification
|
|
|
|
assert A.x == 5
|
|
|
|
|
|
|
|
def test_delattr():
|
|
|
|
class A:
|
|
|
|
x = 1
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.delattr(A, 'x')
|
|
|
|
assert not hasattr(A, 'x')
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert A.x == 1
|
|
|
|
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.delattr(A, 'x')
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(AttributeError, "monkeypatch.delattr(A, 'y')")
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch.delattr(A, 'y', raising=False)
|
2009-09-07 23:53:50 +08:00
|
|
|
monkeypatch.setattr(A, 'x', 5, raising=False)
|
2009-09-06 22:59:39 +08:00
|
|
|
assert A.x == 5
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert A.x == 1
|
|
|
|
|
|
|
|
def test_setitem():
|
|
|
|
d = {'x': 1}
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.setitem(d, 'x', 2)
|
|
|
|
monkeypatch.setitem(d, 'y', 1700)
|
2009-09-07 23:53:50 +08:00
|
|
|
monkeypatch.setitem(d, 'y', 1700)
|
2009-09-06 22:59:39 +08:00
|
|
|
assert d['x'] == 2
|
|
|
|
assert d['y'] == 1700
|
|
|
|
monkeypatch.setitem(d, 'x', 3)
|
|
|
|
assert d['x'] == 3
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert d['x'] == 1
|
|
|
|
assert 'y' not in d
|
|
|
|
d['x'] = 5
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert d['x'] == 5
|
|
|
|
|
2011-12-17 06:41:23 +08:00
|
|
|
def test_setitem_deleted_meanwhile():
|
|
|
|
d = {}
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.setitem(d, 'x', 2)
|
|
|
|
del d['x']
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert not d
|
|
|
|
|
2011-12-28 23:49:13 +08:00
|
|
|
@pytest.mark.parametrize("before", [True, False])
|
|
|
|
def test_setenv_deleted_meanwhile(before):
|
|
|
|
key = "qwpeoip123"
|
|
|
|
if before:
|
|
|
|
os.environ[key] = "world"
|
2011-12-17 06:41:23 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
2011-12-28 23:49:13 +08:00
|
|
|
monkeypatch.setenv(key, 'hello')
|
|
|
|
del os.environ[key]
|
2011-12-17 06:41:23 +08:00
|
|
|
monkeypatch.undo()
|
2011-12-28 23:49:13 +08:00
|
|
|
if before:
|
|
|
|
assert os.environ[key] == "world"
|
|
|
|
del os.environ[key]
|
|
|
|
else:
|
|
|
|
assert key not in os.environ
|
2011-12-17 06:41:23 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_delitem():
|
|
|
|
d = {'x': 1}
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.delitem(d, 'x')
|
|
|
|
assert 'x' not in d
|
|
|
|
monkeypatch.delitem(d, 'y', raising=False)
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(KeyError, "monkeypatch.delitem(d, 'y')")
|
2009-09-06 22:59:39 +08:00
|
|
|
assert not d
|
|
|
|
monkeypatch.setitem(d, 'y', 1700)
|
|
|
|
assert d['y'] == 1700
|
|
|
|
d['hello'] = 'world'
|
|
|
|
monkeypatch.setitem(d, 'x', 1500)
|
|
|
|
assert d['x'] == 1500
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert d == {'hello': 'world', 'x': 1}
|
|
|
|
|
|
|
|
def test_setenv():
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.setenv('XYZ123', 2)
|
|
|
|
import os
|
|
|
|
assert os.environ['XYZ123'] == "2"
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert 'XYZ123' not in os.environ
|
|
|
|
|
|
|
|
def test_delenv():
|
|
|
|
name = 'xyz1234'
|
2010-07-27 03:15:15 +08:00
|
|
|
assert name not in os.environ
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(KeyError, "monkeypatch.delenv(%r, raising=True)" % name)
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch.delenv(name, raising=False)
|
|
|
|
monkeypatch.undo()
|
|
|
|
os.environ[name] = "1"
|
|
|
|
try:
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.delenv(name)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert name not in os.environ
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch.setenv(name, "3")
|
|
|
|
assert os.environ[name] == "3"
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert os.environ[name] == "1"
|
|
|
|
finally:
|
|
|
|
if name in os.environ:
|
|
|
|
del os.environ[name]
|
|
|
|
|
|
|
|
def test_setenv_prepend():
|
|
|
|
import os
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.setenv('XYZ123', 2, prepend="-")
|
|
|
|
assert os.environ['XYZ123'] == "2"
|
|
|
|
monkeypatch.setenv('XYZ123', 3, prepend="-")
|
|
|
|
assert os.environ['XYZ123'] == "3-2"
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert 'XYZ123' not in os.environ
|
|
|
|
|
|
|
|
def test_monkeypatch_plugin(testdir):
|
|
|
|
reprec = testdir.inline_runsource("""
|
|
|
|
def test_method(monkeypatch):
|
2010-10-11 05:45:45 +08:00
|
|
|
assert monkeypatch.__class__.__name__ == "monkeypatch"
|
2009-09-06 22:59:39 +08:00
|
|
|
""")
|
|
|
|
res = reprec.countoutcomes()
|
|
|
|
assert tuple(res) == (1, 0, 0), res
|
|
|
|
|
|
|
|
def test_syspath_prepend():
|
|
|
|
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()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert sys.path == old
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch.undo()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert sys.path == old
|
2009-09-06 22:59:39 +08:00
|
|
|
finally:
|
|
|
|
sys.path[:] = old
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
|