reformat monkeypatch core plugin/its tests
This commit is contained in:
parent
c8caa87759
commit
9e6bb74d71
|
@ -5,7 +5,6 @@ import re
|
||||||
|
|
||||||
from py.builtin import _basestring
|
from py.builtin import _basestring
|
||||||
|
|
||||||
|
|
||||||
RE_IMPORT_ERROR_NAME = re.compile("^No module named (.*)$")
|
RE_IMPORT_ERROR_NAME = re.compile("^No module named (.*)$")
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,7 +31,6 @@ def pytest_funcarg__monkeypatch(request):
|
||||||
return mpatch
|
return mpatch
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def derive_importpath(import_path, raising):
|
def derive_importpath(import_path, raising):
|
||||||
import pytest
|
import pytest
|
||||||
if not isinstance(import_path, _basestring) or "." not in import_path:
|
if not isinstance(import_path, _basestring) or "." not in import_path:
|
||||||
|
@ -79,15 +77,17 @@ def derive_importpath(import_path, raising):
|
||||||
return attr, obj
|
return attr, obj
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Notset:
|
class Notset:
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<notset>"
|
return "<notset>"
|
||||||
|
|
||||||
|
|
||||||
notset = Notset()
|
notset = Notset()
|
||||||
|
|
||||||
|
|
||||||
class monkeypatch:
|
class monkeypatch:
|
||||||
""" Object keeping a record of setattr/item/env/syspath changes. """
|
""" Object keeping a record of setattr/item/env/syspath changes. """
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._setattr = []
|
self._setattr = []
|
||||||
self._setitem = []
|
self._setitem = []
|
||||||
|
@ -114,14 +114,14 @@ class monkeypatch:
|
||||||
if value is notset:
|
if value is notset:
|
||||||
if not isinstance(target, _basestring):
|
if not isinstance(target, _basestring):
|
||||||
raise TypeError("use setattr(target, name, value) or "
|
raise TypeError("use setattr(target, name, value) or "
|
||||||
"setattr(target, value) with target being a dotted "
|
"setattr(target, value) with target being a dotted "
|
||||||
"import string")
|
"import string")
|
||||||
value = name
|
value = name
|
||||||
name, target = derive_importpath(target, raising)
|
name, target = derive_importpath(target, raising)
|
||||||
|
|
||||||
oldval = getattr(target, name, notset)
|
oldval = getattr(target, name, notset)
|
||||||
if raising and oldval is notset:
|
if raising and oldval is notset:
|
||||||
raise AttributeError("%r has no attribute %r" %(target, name))
|
raise AttributeError("%r has no attribute %r" % (target, name))
|
||||||
|
|
||||||
# avoid class descriptors like staticmethod/classmethod
|
# avoid class descriptors like staticmethod/classmethod
|
||||||
if inspect.isclass(target):
|
if inspect.isclass(target):
|
||||||
|
@ -233,7 +233,7 @@ class monkeypatch:
|
||||||
try:
|
try:
|
||||||
del dictionary[name]
|
del dictionary[name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass # was already deleted, so we have the desired state
|
pass # was already deleted, so we have the desired state
|
||||||
else:
|
else:
|
||||||
dictionary[name] = value
|
dictionary[name] = value
|
||||||
self._setitem[:] = []
|
self._setitem[:] = []
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
import os, sys
|
import os
|
||||||
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from _pytest.monkeypatch import monkeypatch as MonkeyPatch
|
from _pytest.monkeypatch import monkeypatch as MonkeyPatch
|
||||||
|
|
||||||
|
|
||||||
def pytest_funcarg__mp(request):
|
def pytest_funcarg__mp(request):
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
sys_path = list(sys.path)
|
sys_path = list(sys.path)
|
||||||
|
@ -15,9 +17,11 @@ def pytest_funcarg__mp(request):
|
||||||
request.addfinalizer(cleanup)
|
request.addfinalizer(cleanup)
|
||||||
return MonkeyPatch()
|
return MonkeyPatch()
|
||||||
|
|
||||||
|
|
||||||
def test_setattr():
|
def test_setattr():
|
||||||
class A:
|
class A:
|
||||||
x = 1
|
x = 1
|
||||||
|
|
||||||
monkeypatch = MonkeyPatch()
|
monkeypatch = MonkeyPatch()
|
||||||
pytest.raises(AttributeError, "monkeypatch.setattr(A, 'notexists', 2)")
|
pytest.raises(AttributeError, "monkeypatch.setattr(A, 'notexists', 2)")
|
||||||
monkeypatch.setattr(A, 'y', 2, raising=False)
|
monkeypatch.setattr(A, 'y', 2, raising=False)
|
||||||
|
@ -34,9 +38,10 @@ def test_setattr():
|
||||||
assert A.x == 1
|
assert A.x == 1
|
||||||
|
|
||||||
A.x = 5
|
A.x = 5
|
||||||
monkeypatch.undo() # double-undo makes no modification
|
monkeypatch.undo() # double-undo makes no modification
|
||||||
assert A.x == 5
|
assert A.x == 5
|
||||||
|
|
||||||
|
|
||||||
class TestSetattrWithImportPath:
|
class TestSetattrWithImportPath:
|
||||||
def test_string_expression(self, monkeypatch):
|
def test_string_expression(self, monkeypatch):
|
||||||
monkeypatch.setattr("os.path.abspath", lambda x: "hello2")
|
monkeypatch.setattr("os.path.abspath", lambda x: "hello2")
|
||||||
|
@ -75,9 +80,11 @@ class TestSetattrWithImportPath:
|
||||||
monkeypatch.undo()
|
monkeypatch.undo()
|
||||||
assert os.path.abspath
|
assert os.path.abspath
|
||||||
|
|
||||||
|
|
||||||
def test_delattr():
|
def test_delattr():
|
||||||
class A:
|
class A:
|
||||||
x = 1
|
x = 1
|
||||||
|
|
||||||
monkeypatch = MonkeyPatch()
|
monkeypatch = MonkeyPatch()
|
||||||
monkeypatch.delattr(A, 'x')
|
monkeypatch.delattr(A, 'x')
|
||||||
assert not hasattr(A, 'x')
|
assert not hasattr(A, 'x')
|
||||||
|
@ -93,6 +100,7 @@ def test_delattr():
|
||||||
monkeypatch.undo()
|
monkeypatch.undo()
|
||||||
assert A.x == 1
|
assert A.x == 1
|
||||||
|
|
||||||
|
|
||||||
def test_setitem():
|
def test_setitem():
|
||||||
d = {'x': 1}
|
d = {'x': 1}
|
||||||
monkeypatch = MonkeyPatch()
|
monkeypatch = MonkeyPatch()
|
||||||
|
@ -110,6 +118,7 @@ def test_setitem():
|
||||||
monkeypatch.undo()
|
monkeypatch.undo()
|
||||||
assert d['x'] == 5
|
assert d['x'] == 5
|
||||||
|
|
||||||
|
|
||||||
def test_setitem_deleted_meanwhile():
|
def test_setitem_deleted_meanwhile():
|
||||||
d = {}
|
d = {}
|
||||||
monkeypatch = MonkeyPatch()
|
monkeypatch = MonkeyPatch()
|
||||||
|
@ -118,6 +127,7 @@ def test_setitem_deleted_meanwhile():
|
||||||
monkeypatch.undo()
|
monkeypatch.undo()
|
||||||
assert not d
|
assert not d
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("before", [True, False])
|
@pytest.mark.parametrize("before", [True, False])
|
||||||
def test_setenv_deleted_meanwhile(before):
|
def test_setenv_deleted_meanwhile(before):
|
||||||
key = "qwpeoip123"
|
key = "qwpeoip123"
|
||||||
|
@ -133,6 +143,7 @@ def test_setenv_deleted_meanwhile(before):
|
||||||
else:
|
else:
|
||||||
assert key not in os.environ
|
assert key not in os.environ
|
||||||
|
|
||||||
|
|
||||||
def test_delitem():
|
def test_delitem():
|
||||||
d = {'x': 1}
|
d = {'x': 1}
|
||||||
monkeypatch = MonkeyPatch()
|
monkeypatch = MonkeyPatch()
|
||||||
|
@ -149,6 +160,7 @@ def test_delitem():
|
||||||
monkeypatch.undo()
|
monkeypatch.undo()
|
||||||
assert d == {'hello': 'world', 'x': 1}
|
assert d == {'hello': 'world', 'x': 1}
|
||||||
|
|
||||||
|
|
||||||
def test_setenv():
|
def test_setenv():
|
||||||
monkeypatch = MonkeyPatch()
|
monkeypatch = MonkeyPatch()
|
||||||
monkeypatch.setenv('XYZ123', 2)
|
monkeypatch.setenv('XYZ123', 2)
|
||||||
|
@ -157,6 +169,7 @@ def test_setenv():
|
||||||
monkeypatch.undo()
|
monkeypatch.undo()
|
||||||
assert 'XYZ123' not in os.environ
|
assert 'XYZ123' not in os.environ
|
||||||
|
|
||||||
|
|
||||||
def test_delenv():
|
def test_delenv():
|
||||||
name = 'xyz1234'
|
name = 'xyz1234'
|
||||||
assert name not in os.environ
|
assert name not in os.environ
|
||||||
|
@ -177,6 +190,7 @@ def test_delenv():
|
||||||
if name in os.environ:
|
if name in os.environ:
|
||||||
del os.environ[name]
|
del os.environ[name]
|
||||||
|
|
||||||
|
|
||||||
def test_setenv_prepend():
|
def test_setenv_prepend():
|
||||||
import os
|
import os
|
||||||
monkeypatch = MonkeyPatch()
|
monkeypatch = MonkeyPatch()
|
||||||
|
@ -187,6 +201,7 @@ def test_setenv_prepend():
|
||||||
monkeypatch.undo()
|
monkeypatch.undo()
|
||||||
assert 'XYZ123' not in os.environ
|
assert 'XYZ123' not in os.environ
|
||||||
|
|
||||||
|
|
||||||
def test_monkeypatch_plugin(testdir):
|
def test_monkeypatch_plugin(testdir):
|
||||||
reprec = testdir.inline_runsource("""
|
reprec = testdir.inline_runsource("""
|
||||||
def test_method(monkeypatch):
|
def test_method(monkeypatch):
|
||||||
|
@ -195,6 +210,7 @@ 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(mp):
|
def test_syspath_prepend(mp):
|
||||||
old = list(sys.path)
|
old = list(sys.path)
|
||||||
mp.syspath_prepend('world')
|
mp.syspath_prepend('world')
|
||||||
|
@ -206,6 +222,7 @@ def test_syspath_prepend(mp):
|
||||||
mp.undo()
|
mp.undo()
|
||||||
assert sys.path == old
|
assert sys.path == old
|
||||||
|
|
||||||
|
|
||||||
def test_syspath_prepend_double_undo(mp):
|
def test_syspath_prepend_double_undo(mp):
|
||||||
mp.syspath_prepend('hello world')
|
mp.syspath_prepend('hello world')
|
||||||
mp.undo()
|
mp.undo()
|
||||||
|
@ -213,20 +230,24 @@ def test_syspath_prepend_double_undo(mp):
|
||||||
mp.undo()
|
mp.undo()
|
||||||
assert sys.path[-1] == 'more hello world'
|
assert sys.path[-1] == 'more hello world'
|
||||||
|
|
||||||
|
|
||||||
def test_chdir_with_path_local(mp, tmpdir):
|
def test_chdir_with_path_local(mp, tmpdir):
|
||||||
mp.chdir(tmpdir)
|
mp.chdir(tmpdir)
|
||||||
assert os.getcwd() == tmpdir.strpath
|
assert os.getcwd() == tmpdir.strpath
|
||||||
|
|
||||||
|
|
||||||
def test_chdir_with_str(mp, tmpdir):
|
def test_chdir_with_str(mp, tmpdir):
|
||||||
mp.chdir(tmpdir.strpath)
|
mp.chdir(tmpdir.strpath)
|
||||||
assert os.getcwd() == tmpdir.strpath
|
assert os.getcwd() == tmpdir.strpath
|
||||||
|
|
||||||
|
|
||||||
def test_chdir_undo(mp, tmpdir):
|
def test_chdir_undo(mp, tmpdir):
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
mp.chdir(tmpdir)
|
mp.chdir(tmpdir)
|
||||||
mp.undo()
|
mp.undo()
|
||||||
assert os.getcwd() == cwd
|
assert os.getcwd() == cwd
|
||||||
|
|
||||||
|
|
||||||
def test_chdir_double_undo(mp, tmpdir):
|
def test_chdir_double_undo(mp, tmpdir):
|
||||||
mp.chdir(tmpdir.strpath)
|
mp.chdir(tmpdir.strpath)
|
||||||
mp.undo()
|
mp.undo()
|
||||||
|
@ -234,6 +255,7 @@ def test_chdir_double_undo(mp, tmpdir):
|
||||||
mp.undo()
|
mp.undo()
|
||||||
assert os.getcwd() == tmpdir.strpath
|
assert os.getcwd() == tmpdir.strpath
|
||||||
|
|
||||||
|
|
||||||
def test_issue185_time_breaks(testdir):
|
def test_issue185_time_breaks(testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import time
|
import time
|
||||||
|
@ -247,6 +269,7 @@ def test_issue185_time_breaks(testdir):
|
||||||
*1 passed*
|
*1 passed*
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
|
||||||
def test_importerror(testdir):
|
def test_importerror(testdir):
|
||||||
p = testdir.mkpydir("package")
|
p = testdir.mkpydir("package")
|
||||||
p.join("a.py").write(textwrap.dedent("""\
|
p.join("a.py").write(textwrap.dedent("""\
|
||||||
|
@ -275,11 +298,12 @@ class SampleNewInherit(SampleNew):
|
||||||
|
|
||||||
|
|
||||||
class SampleOld:
|
class SampleOld:
|
||||||
#oldstyle on python2
|
# oldstyle on python2
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def hello():
|
def hello():
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class SampleOldInherit(SampleOld):
|
class SampleOldInherit(SampleOld):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -296,5 +320,3 @@ def test_issue156_undo_staticmethod(Sample):
|
||||||
|
|
||||||
monkeypatch.undo()
|
monkeypatch.undo()
|
||||||
assert Sample.hello()
|
assert Sample.hello()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue