2010-10-11 05:45:45 +08:00
|
|
|
""" monkeypatching and mocking functionality. """
|
2009-07-21 00:54:08 +08:00
|
|
|
|
2010-09-26 22:23:44 +08:00
|
|
|
import os, sys
|
2009-03-17 17:18:38 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_funcarg__monkeypatch(request):
|
2010-07-27 03:15:15 +08:00
|
|
|
"""The returned ``monkeypatch`` funcarg provides these
|
2009-07-21 00:54:08 +08:00
|
|
|
helper methods to modify objects, dictionaries or os.environ::
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
monkeypatch.setattr(obj, name, value, raising=True)
|
2009-08-22 18:45:58 +08:00
|
|
|
monkeypatch.delattr(obj, name, raising=True)
|
2010-07-27 03:15:15 +08:00
|
|
|
monkeypatch.setitem(mapping, name, value)
|
2009-08-22 18:45:58 +08:00
|
|
|
monkeypatch.delitem(obj, name, raising=True)
|
2010-07-27 03:15:15 +08:00
|
|
|
monkeypatch.setenv(name, value, prepend=False)
|
2009-08-22 18:45:58 +08:00
|
|
|
monkeypatch.delenv(name, value, raising=True)
|
2009-09-06 05:21:58 +08:00
|
|
|
monkeypatch.syspath_prepend(path)
|
2009-08-22 18:45:58 +08:00
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
All modifications will be undone after the requesting
|
|
|
|
test function has finished. The ``raising``
|
2010-07-27 03:15:15 +08:00
|
|
|
parameter determines if a KeyError or AttributeError
|
|
|
|
will be raised if the set/deletion operation has no target.
|
2009-07-21 00:54:08 +08:00
|
|
|
"""
|
2010-10-11 05:45:45 +08:00
|
|
|
mpatch = monkeypatch()
|
|
|
|
request.addfinalizer(mpatch.undo)
|
|
|
|
return mpatch
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
notset = object()
|
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
class monkeypatch:
|
|
|
|
""" object keeping a record of setattr/item/env/syspath changes. """
|
2009-02-27 18:18:27 +08:00
|
|
|
def __init__(self):
|
|
|
|
self._setattr = []
|
|
|
|
self._setitem = []
|
2012-01-04 19:43:19 +08:00
|
|
|
self._cwd = None
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-09-07 23:53:50 +08:00
|
|
|
def setattr(self, obj, name, value, raising=True):
|
2010-10-11 05:45:45 +08:00
|
|
|
""" set attribute ``name`` on ``obj`` to ``value``, by default
|
|
|
|
raise AttributeEror if the attribute did not exist. """
|
2009-09-07 23:53:50 +08:00
|
|
|
oldval = getattr(obj, name, notset)
|
|
|
|
if raising and oldval is notset:
|
|
|
|
raise AttributeError("%r has no attribute %r" %(obj, name))
|
|
|
|
self._setattr.insert(0, (obj, name, oldval))
|
2009-02-27 18:18:27 +08:00
|
|
|
setattr(obj, name, value)
|
|
|
|
|
2009-08-22 18:45:58 +08:00
|
|
|
def delattr(self, obj, name, raising=True):
|
2010-10-11 05:45:45 +08:00
|
|
|
""" delete attribute ``name`` from ``obj``, by default raise
|
|
|
|
AttributeError it the attribute did not previously exist. """
|
2009-08-22 18:45:58 +08:00
|
|
|
if not hasattr(obj, name):
|
|
|
|
if raising:
|
2010-07-27 03:15:15 +08:00
|
|
|
raise AttributeError(name)
|
2009-08-22 18:45:58 +08:00
|
|
|
else:
|
|
|
|
self._setattr.insert(0, (obj, name, getattr(obj, name, notset)))
|
|
|
|
delattr(obj, name)
|
|
|
|
|
|
|
|
def setitem(self, dic, name, value):
|
2010-10-11 05:45:45 +08:00
|
|
|
""" set dictionary entry ``name`` to value. """
|
2009-08-22 18:45:58 +08:00
|
|
|
self._setitem.insert(0, (dic, name, dic.get(name, notset)))
|
|
|
|
dic[name] = value
|
|
|
|
|
|
|
|
def delitem(self, dic, name, raising=True):
|
2010-10-11 05:45:45 +08:00
|
|
|
""" delete ``name`` from dict, raise KeyError if it doesn't exist."""
|
2009-08-22 18:45:58 +08:00
|
|
|
if name not in dic:
|
|
|
|
if raising:
|
2010-07-27 03:15:15 +08:00
|
|
|
raise KeyError(name)
|
|
|
|
else:
|
2009-08-22 18:45:58 +08:00
|
|
|
self._setitem.insert(0, (dic, name, dic.get(name, notset)))
|
|
|
|
del dic[name]
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-08-14 02:10:12 +08:00
|
|
|
def setenv(self, name, value, prepend=None):
|
2010-10-11 05:45:45 +08:00
|
|
|
""" set environment variable ``name`` to ``value``. if ``prepend``
|
2010-11-06 18:38:53 +08:00
|
|
|
is a character, read the current environment variable value
|
2010-10-11 05:45:45 +08:00
|
|
|
and prepend the ``value`` adjoined with the ``prepend`` character."""
|
2009-08-14 02:10:12 +08:00
|
|
|
value = str(value)
|
|
|
|
if prepend and name in os.environ:
|
|
|
|
value = value + prepend + os.environ[name]
|
|
|
|
self.setitem(os.environ, name, value)
|
2009-03-17 17:18:38 +08:00
|
|
|
|
2009-08-22 18:45:58 +08:00
|
|
|
def delenv(self, name, raising=True):
|
2010-10-11 05:45:45 +08:00
|
|
|
""" delete ``name`` from environment, raise KeyError it not exists."""
|
2009-08-22 18:45:58 +08:00
|
|
|
self.delitem(os.environ, name, raising=raising)
|
|
|
|
|
2009-09-06 05:21:58 +08:00
|
|
|
def syspath_prepend(self, path):
|
2010-10-11 05:45:45 +08:00
|
|
|
""" prepend ``path`` to ``sys.path`` list of import locations. """
|
2009-09-06 05:21:58 +08:00
|
|
|
if not hasattr(self, '_savesyspath'):
|
|
|
|
self._savesyspath = sys.path[:]
|
|
|
|
sys.path.insert(0, str(path))
|
|
|
|
|
2012-01-04 19:43:19 +08:00
|
|
|
def chdir(self, path):
|
|
|
|
if self._cwd is None:
|
|
|
|
self._cwd = os.getcwd()
|
|
|
|
if hasattr(path, "chdir"):
|
|
|
|
path.chdir()
|
|
|
|
else:
|
|
|
|
os.chdir(path)
|
|
|
|
|
2009-09-05 22:54:52 +08:00
|
|
|
def undo(self):
|
2010-10-11 05:45:45 +08:00
|
|
|
""" undo previous changes. This call consumes the
|
|
|
|
undo stack. Calling it a second time has no effect unless
|
|
|
|
you do more monkeypatching after the undo call."""
|
2009-02-27 18:18:27 +08:00
|
|
|
for obj, name, value in self._setattr:
|
|
|
|
if value is not notset:
|
|
|
|
setattr(obj, name, value)
|
2009-03-04 01:42:32 +08:00
|
|
|
else:
|
|
|
|
delattr(obj, name)
|
2009-09-05 22:54:52 +08:00
|
|
|
self._setattr[:] = []
|
2009-02-27 18:18:27 +08:00
|
|
|
for dictionary, name, value in self._setitem:
|
|
|
|
if value is notset:
|
2011-12-28 23:49:13 +08:00
|
|
|
try:
|
|
|
|
del dictionary[name]
|
|
|
|
except KeyError:
|
|
|
|
pass # was already deleted, so we have the desired state
|
2009-02-27 18:18:27 +08:00
|
|
|
else:
|
|
|
|
dictionary[name] = value
|
2009-09-05 22:54:52 +08:00
|
|
|
self._setitem[:] = []
|
2009-09-06 05:21:58 +08:00
|
|
|
if hasattr(self, '_savesyspath'):
|
|
|
|
sys.path[:] = self._savesyspath
|
2012-01-04 19:42:23 +08:00
|
|
|
del self._savesyspath
|
2012-01-04 19:43:19 +08:00
|
|
|
|
|
|
|
if self._cwd is not None:
|
|
|
|
os.chdir(self._cwd)
|
|
|
|
self._cwd = None
|