Commit cb78e6b2 authored by Julien Muchembled's avatar Julien Muchembled

qa: add a basic assertion in Patch to detect when patched code changes

parent 43fdd059
...@@ -409,10 +409,14 @@ class Patch(object): ...@@ -409,10 +409,14 @@ class Patch(object):
Usage: Usage:
with Patch(someObject, attrToPatch=newValue) as patch: with Patch(someObject, [new,] attrToPatch=newValue) as patch:
[... code that runs with patches ...] [... code that runs with patches ...]
[... code that runs without patch ...] [... code that runs without patch ...]
The 'new' positional parameter defaults to False and it must be equal to
not hasattr(someObject, 'attrToPatch')
It is an assertion to detect when a Patch is obsolete.
' as patch' is optional: 'patch.revert()' can be used to revert patches ' as patch' is optional: 'patch.revert()' can be used to revert patches
in the middle of the 'with' clause. in the middle of the 'with' clause.
...@@ -424,7 +428,7 @@ class Patch(object): ...@@ -424,7 +428,7 @@ class Patch(object):
In this case, patches are automatically reverted when 'patch' is deleted. In this case, patches are automatically reverted when 'patch' is deleted.
For patched callables, the new one receives the original value as first For patched callables, the new one receives the original value as first
argument. argument if 'new' is True.
Alternative usage: Alternative usage:
...@@ -448,16 +452,22 @@ class Patch(object): ...@@ -448,16 +452,22 @@ class Patch(object):
return self return self
return patch return patch
def __init__(self, patched, **patch): def __init__(self, patched, *args, **patch):
new, = args or (0,)
(name, patch), = patch.iteritems() (name, patch), = patch.iteritems()
self._patched = patched self._patched = patched
self._name = name self._name = name
if callable(patch): try:
wrapped = getattr(patched, name, None) wrapped = getattr(patched, name)
func = patch except AttributeError:
patch = lambda *args, **kw: func(wrapped, *args, **kw) assert new, (patched, name)
if callable(wrapped): else:
patch = wraps(wrapped)(patch) assert not new, (patched, name)
if callable(patch):
func = patch
patch = lambda *args, **kw: func(wrapped, *args, **kw)
if callable(wrapped):
patch = wraps(wrapped)(patch)
self._patch = patch self._patch = patch
try: try:
orig = patched.__dict__[name] orig = patched.__dict__[name]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment