2009-05-20 02:00:24 +08:00
|
|
|
"""
|
2009-07-09 01:18:26 +08:00
|
|
|
safely patch object attributes, dicts and environment variables.
|
2009-05-20 02:00:24 +08:00
|
|
|
|
2009-09-07 23:53:50 +08:00
|
|
|
Usage
|
2009-07-21 00:54:08 +08:00
|
|
|
----------------
|
2009-05-20 02:00:24 +08:00
|
|
|
|
2009-09-07 23:53:50 +08:00
|
|
|
Use the `monkeypatch funcarg`_ to tweak your global test environment
|
|
|
|
for running a particular test. You can safely set/del an attribute,
|
|
|
|
dictionary item or environment variable by respective methods
|
|
|
|
on the monkeypatch funcarg. If you want e.g. to set an ENV1 variable
|
|
|
|
and have os.path.expanduser return a particular directory, you can
|
|
|
|
write it down like this:
|
2009-07-09 01:18:26 +08:00
|
|
|
|
2009-07-21 00:54:08 +08:00
|
|
|
.. sourcecode:: python
|
|
|
|
|
|
|
|
def test_mytest(monkeypatch):
|
|
|
|
monkeypatch.setenv('ENV1', 'myval')
|
2009-09-07 23:53:50 +08:00
|
|
|
monkeypatch.setattr(os.path, 'expanduser', lambda x: '/tmp/xyz')
|
|
|
|
... # your test code that uses those patched values implicitely
|
2009-07-21 00:54:08 +08:00
|
|
|
|
2009-09-07 23:53:50 +08:00
|
|
|
After the test function finished all modifications will be undone,
|
|
|
|
because the ``monkeypatch.undo()`` method is registered as a finalizer.
|
2009-08-14 02:10:12 +08:00
|
|
|
|
2009-09-07 23:53:50 +08:00
|
|
|
``monkeypatch.setattr/delattr/delitem/delenv()`` all
|
|
|
|
by default raise an Exception if the target does not exist.
|
|
|
|
Pass ``raising=False`` if you want to skip this check.
|
|
|
|
|
|
|
|
prepending to PATH or other environment variables
|
|
|
|
---------------------------------------------------------
|
|
|
|
|
|
|
|
To prepend a value to an already existing environment parameter:
|
2009-08-14 02:10:12 +08:00
|
|
|
|
|
|
|
.. sourcecode:: python
|
|
|
|
|
|
|
|
def test_mypath_finding(monkeypatch):
|
|
|
|
monkeypatch.setenv('PATH', 'x/y', prepend=":")
|
2009-09-07 23:53:50 +08:00
|
|
|
# in bash language: export PATH=x/y:$PATH
|
2009-07-21 00:54:08 +08:00
|
|
|
|
2009-09-05 22:54:52 +08:00
|
|
|
calling "undo" finalization explicitely
|
|
|
|
-----------------------------------------
|
|
|
|
|
2009-09-07 23:53:50 +08:00
|
|
|
At the end of function execution py.test invokes
|
|
|
|
a teardown hook which undoes all monkeypatch changes.
|
|
|
|
If you do not want to wait that long you can call
|
|
|
|
finalization explicitely::
|
2009-09-05 22:54:52 +08:00
|
|
|
|
|
|
|
monkeypatch.undo()
|
|
|
|
|
|
|
|
This will undo previous changes. This call consumes the
|
2009-09-07 23:53:50 +08:00
|
|
|
undo stack. Calling it a second time has no effect unless
|
|
|
|
you start monkeypatching after the undo call.
|
2009-09-05 22:54:52 +08:00
|
|
|
|
2009-07-21 00:54:08 +08:00
|
|
|
.. _`monkeypatch blog post`: http://tetamap.wordpress.com/2009/03/03/monkeypatching-in-unit-tests-done-right/
|
2009-05-20 02:00:24 +08:00
|
|
|
"""
|
2009-07-21 00:54:08 +08:00
|
|
|
|
2009-09-06 05:21:58 +08:00
|
|
|
import py, os, sys
|
2009-03-17 17:18:38 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_funcarg__monkeypatch(request):
|
2009-08-22 18:45:58 +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::
|
|
|
|
|
2009-09-07 23:53:50 +08:00
|
|
|
monkeypatch.setattr(obj, name, value, raising=True)
|
2009-08-22 18:45:58 +08:00
|
|
|
monkeypatch.delattr(obj, name, raising=True)
|
2009-07-21 00:54:08 +08:00
|
|
|
monkeypatch.setitem(mapping, name, value)
|
2009-08-22 18:45:58 +08:00
|
|
|
monkeypatch.delitem(obj, name, raising=True)
|
|
|
|
monkeypatch.setenv(name, value, prepend=False)
|
|
|
|
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
|
|
|
|
|
|
|
All modifications will be undone when the requesting
|
2010-02-07 08:56:43 +08:00
|
|
|
test function finished its execution. The ``raising``
|
|
|
|
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
|
|
|
"""
|
2009-05-19 05:26:16 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
2009-09-05 22:54:52 +08:00
|
|
|
request.addfinalizer(monkeypatch.undo)
|
2009-05-19 05:26:16 +08:00
|
|
|
return monkeypatch
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
notset = object()
|
|
|
|
|
|
|
|
class MonkeyPatch:
|
|
|
|
def __init__(self):
|
|
|
|
self._setattr = []
|
|
|
|
self._setitem = []
|
|
|
|
|
2009-09-07 23:53:50 +08:00
|
|
|
def setattr(self, obj, name, value, raising=True):
|
|
|
|
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):
|
|
|
|
if not hasattr(obj, name):
|
|
|
|
if raising:
|
|
|
|
raise AttributeError(name)
|
|
|
|
else:
|
|
|
|
self._setattr.insert(0, (obj, name, getattr(obj, name, notset)))
|
|
|
|
delattr(obj, name)
|
|
|
|
|
|
|
|
def setitem(self, dic, name, value):
|
|
|
|
self._setitem.insert(0, (dic, name, dic.get(name, notset)))
|
|
|
|
dic[name] = value
|
|
|
|
|
|
|
|
def delitem(self, dic, name, raising=True):
|
|
|
|
if name not in dic:
|
|
|
|
if raising:
|
|
|
|
raise KeyError(name)
|
|
|
|
else:
|
|
|
|
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):
|
|
|
|
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):
|
|
|
|
self.delitem(os.environ, name, raising=raising)
|
|
|
|
|
2009-09-06 05:21:58 +08:00
|
|
|
def syspath_prepend(self, path):
|
|
|
|
if not hasattr(self, '_savesyspath'):
|
|
|
|
self._savesyspath = sys.path[:]
|
|
|
|
sys.path.insert(0, str(path))
|
|
|
|
|
2009-09-05 22:54:52 +08:00
|
|
|
def undo(self):
|
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:
|
|
|
|
del dictionary[name]
|
|
|
|
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
|