2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2016-01-24 01:08:18 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2015-12-28 05:42:42 +08:00
|
|
|
import textwrap
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2016-07-12 09:03:53 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
2017-01-04 21:31:44 +08:00
|
|
|
def mp():
|
2012-01-04 19:40:57 +08:00
|
|
|
cwd = os.getcwd()
|
|
|
|
sys_path = list(sys.path)
|
2017-01-04 21:31:44 +08:00
|
|
|
yield MonkeyPatch()
|
|
|
|
sys.path[:] = sys_path
|
|
|
|
os.chdir(cwd)
|
2012-01-04 19:40:57 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_setattr():
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2009-09-06 22:59:39 +08:00
|
|
|
x = 1
|
2016-01-24 01:08:18 +08:00
|
|
|
|
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
|
2016-01-24 01:08:18 +08:00
|
|
|
monkeypatch.undo() # double-undo makes no modification
|
2009-09-06 22:59:39 +08:00
|
|
|
assert A.x == 5
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestSetattrWithImportPath(object):
|
2013-08-07 21:35:27 +08:00
|
|
|
def test_string_expression(self, monkeypatch):
|
2013-09-27 18:33:06 +08:00
|
|
|
monkeypatch.setattr("os.path.abspath", lambda x: "hello2")
|
2013-08-07 21:35:27 +08:00
|
|
|
assert os.path.abspath("123") == "hello2"
|
|
|
|
|
|
|
|
def test_string_expression_class(self, monkeypatch):
|
2013-09-27 18:33:06 +08:00
|
|
|
monkeypatch.setattr("_pytest.config.Config", 42)
|
2013-08-07 21:35:27 +08:00
|
|
|
import _pytest
|
|
|
|
assert _pytest.config.Config == 42
|
|
|
|
|
2013-10-21 20:01:02 +08:00
|
|
|
def test_unicode_string(self, monkeypatch):
|
2013-11-12 20:48:17 +08:00
|
|
|
monkeypatch.setattr("_pytest.config.Config", 42)
|
2013-10-21 20:01:02 +08:00
|
|
|
import _pytest
|
|
|
|
assert _pytest.config.Config == 42
|
2013-11-12 20:48:17 +08:00
|
|
|
monkeypatch.delattr("_pytest.config.Config")
|
2013-10-21 20:01:02 +08:00
|
|
|
|
2013-08-07 22:49:29 +08:00
|
|
|
def test_wrong_target(self, monkeypatch):
|
2013-09-27 18:33:06 +08:00
|
|
|
pytest.raises(TypeError, lambda: monkeypatch.setattr(None, None))
|
2013-08-07 22:49:29 +08:00
|
|
|
|
|
|
|
def test_unknown_import(self, monkeypatch):
|
2016-01-24 02:31:17 +08:00
|
|
|
pytest.raises(ImportError,
|
2013-09-27 18:33:06 +08:00
|
|
|
lambda: monkeypatch.setattr("unkn123.classx", None))
|
2013-08-07 22:49:29 +08:00
|
|
|
|
|
|
|
def test_unknown_attr(self, monkeypatch):
|
2016-01-24 02:31:17 +08:00
|
|
|
pytest.raises(AttributeError,
|
2013-09-27 18:33:06 +08:00
|
|
|
lambda: monkeypatch.setattr("os.path.qweqwe", None))
|
|
|
|
|
2015-05-20 04:59:49 +08:00
|
|
|
def test_unknown_attr_non_raising(self, monkeypatch):
|
2015-06-16 05:28:31 +08:00
|
|
|
# https://github.com/pytest-dev/pytest/issues/746
|
2015-05-20 04:59:49 +08:00
|
|
|
monkeypatch.setattr('os.path.qweqwe', 42, raising=False)
|
|
|
|
assert os.path.qweqwe == 42
|
|
|
|
|
2013-09-27 18:33:06 +08:00
|
|
|
def test_delattr(self, monkeypatch):
|
|
|
|
monkeypatch.delattr("os.path.abspath")
|
|
|
|
assert not hasattr(os.path, "abspath")
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert os.path.abspath
|
2013-08-07 22:49:29 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_delattr():
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2009-09-06 22:59:39 +08:00
|
|
|
x = 1
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
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
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
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
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
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
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
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
|
|
|
|
2016-01-24 01:08:18 +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}
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_setenv():
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.setenv('XYZ123', 2)
|
|
|
|
import os
|
|
|
|
assert os.environ['XYZ123'] == "2"
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert 'XYZ123' not in os.environ
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
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]
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
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
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_monkeypatch_plugin(testdir):
|
|
|
|
reprec = testdir.inline_runsource("""
|
|
|
|
def test_method(monkeypatch):
|
2016-07-12 09:03:53 +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
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2012-01-04 19:40:57 +08:00
|
|
|
def test_syspath_prepend(mp):
|
2009-09-06 22:59:39 +08:00
|
|
|
old = list(sys.path)
|
2012-01-04 19:40:57 +08:00
|
|
|
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
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2012-01-04 19:40:57 +08:00
|
|
|
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'
|
2012-01-04 19:43:19 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2012-01-04 19:43:19 +08:00
|
|
|
def test_chdir_with_path_local(mp, tmpdir):
|
|
|
|
mp.chdir(tmpdir)
|
|
|
|
assert os.getcwd() == tmpdir.strpath
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2012-01-04 19:43:19 +08:00
|
|
|
def test_chdir_with_str(mp, tmpdir):
|
|
|
|
mp.chdir(tmpdir.strpath)
|
|
|
|
assert os.getcwd() == tmpdir.strpath
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2012-01-04 19:43:19 +08:00
|
|
|
def test_chdir_undo(mp, tmpdir):
|
|
|
|
cwd = os.getcwd()
|
|
|
|
mp.chdir(tmpdir)
|
|
|
|
mp.undo()
|
|
|
|
assert os.getcwd() == cwd
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2012-01-04 19:43:19 +08:00
|
|
|
def test_chdir_double_undo(mp, tmpdir):
|
|
|
|
mp.chdir(tmpdir.strpath)
|
|
|
|
mp.undo()
|
|
|
|
tmpdir.chdir()
|
|
|
|
mp.undo()
|
|
|
|
assert os.getcwd() == tmpdir.strpath
|
2012-09-01 15:58:10 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2012-09-01 15:58:10 +08:00
|
|
|
def test_issue185_time_breaks(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import time
|
|
|
|
def test_m(monkeypatch):
|
|
|
|
def f():
|
|
|
|
raise Exception
|
|
|
|
monkeypatch.setattr(time, "time", f)
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*1 passed*
|
|
|
|
""")
|
2012-09-26 00:15:13 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2015-12-28 05:42:42 +08:00
|
|
|
def test_importerror(testdir):
|
|
|
|
p = testdir.mkpydir("package")
|
|
|
|
p.join("a.py").write(textwrap.dedent("""\
|
|
|
|
import doesnotexist
|
|
|
|
|
|
|
|
x = 1
|
|
|
|
"""))
|
|
|
|
testdir.tmpdir.join("test_importerror.py").write(textwrap.dedent("""\
|
|
|
|
def test_importerror(monkeypatch):
|
|
|
|
monkeypatch.setattr('package.a.x', 2)
|
|
|
|
"""))
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines("""
|
2016-01-24 02:31:17 +08:00
|
|
|
*import error in package.a: No module named {0}doesnotexist{0}*
|
2015-12-28 05:42:42 +08:00
|
|
|
""".format("'" if sys.version_info > (3, 0) else ""))
|
2012-09-26 00:15:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SampleNew(object):
|
|
|
|
@staticmethod
|
|
|
|
def hello():
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class SampleNewInherit(SampleNew):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class SampleOld(object):
|
2016-01-24 01:08:18 +08:00
|
|
|
# oldstyle on python2
|
2012-09-26 00:15:13 +08:00
|
|
|
@staticmethod
|
|
|
|
def hello():
|
|
|
|
return True
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2012-09-26 00:15:13 +08:00
|
|
|
class SampleOldInherit(SampleOld):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('Sample', [
|
|
|
|
SampleNew, SampleNewInherit,
|
|
|
|
SampleOld, SampleOldInherit,
|
|
|
|
], ids=['new', 'new-inherit', 'old', 'old-inherit'])
|
|
|
|
def test_issue156_undo_staticmethod(Sample):
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
|
|
|
|
monkeypatch.setattr(Sample, 'hello', None)
|
|
|
|
assert Sample.hello is None
|
|
|
|
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert Sample.hello()
|
2016-01-24 02:12:10 +08:00
|
|
|
|
|
|
|
def test_issue1338_name_resolving():
|
|
|
|
pytest.importorskip('requests')
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
try:
|
|
|
|
monkeypatch.delattr('requests.sessions.Session.request')
|
|
|
|
finally:
|
2016-07-21 09:05:49 +08:00
|
|
|
monkeypatch.undo()
|