summaryrefslogtreecommitdiff
path: root/git/test/lib/helper.py
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2011-07-07 23:37:19 +0200
committerSebastian Thiel <byronimo@gmail.com>2011-07-07 23:37:19 +0200
commit023dc1244c02d415bb964eeb0b51b257523897df (patch)
tree2ebffe75f484d48a8fdc2c282a457cf77a86c3cf /git/test/lib/helper.py
parent2baf8a493618463d2bb41b8e96c8304bf48e2c8a (diff)
parentf4f330f8588dacd43af6513e1e1e1a50237da1e7 (diff)
downloadgitpython-023dc1244c02d415bb964eeb0b51b257523897df.tar.gz
Merge branch 'dulwich'
Diffstat (limited to 'git/test/lib/helper.py')
-rw-r--r--git/test/lib/helper.py76
1 files changed, 73 insertions, 3 deletions
diff --git a/git/test/lib/helper.py b/git/test/lib/helper.py
index d9a92a52..2045f9d3 100644
--- a/git/test/lib/helper.py
+++ b/git/test/lib/helper.py
@@ -12,6 +12,9 @@ import tempfile
import shutil
import cStringIO
+import warnings
+from nose import SkipTest
+
from base import (
maketemp,
rorepo_dir
@@ -19,8 +22,8 @@ from base import (
__all__ = (
- 'StringProcessAdapter', 'GlobalsItemDeletorMetaCls',
- 'with_rw_repo', 'with_rw_and_rw_remote_repo', 'TestBase', 'TestCase',
+ 'StringProcessAdapter', 'GlobalsItemDeletorMetaCls', 'InheritedTestMethodsOverrideWrapperMetaClsAutoMixin',
+ 'with_rw_repo', 'with_rw_and_rw_remote_repo', 'TestBase', 'TestCase', 'needs_module_or_skip'
)
@@ -191,6 +194,27 @@ def with_rw_and_rw_remote_repo(working_tree_ref):
return argument_passer
+def needs_module_or_skip(module):
+ """Decorator to be used for test cases only.
+ Print a warning if the given module could not be imported, and skip the test.
+ Otherwise run the test as usual
+ :param module: the name of the module to skip"""
+ def argpasser(func):
+ def wrapper(self, *args, **kwargs):
+ try:
+ __import__(module)
+ except ImportError:
+ msg = "Module %r is required to run this test - skipping" % module
+ warnings.warn(msg)
+ raise SkipTest(msg)
+ #END check import
+ return func(self, *args, **kwargs)
+ #END wrapper
+ wrapper.__name__ = func.__name__
+ return wrapper
+ #END argpasser
+ return argpasser
+
#} END decorators
#{ Meta Classes
@@ -207,10 +231,56 @@ class GlobalsItemDeletorMetaCls(type):
new_type = super(GlobalsItemDeletorMetaCls, metacls).__new__(metacls, name, bases, clsdict)
if name != metacls.ModuleToDelete:
mod = __import__(new_type.__module__, globals(), locals(), new_type.__module__)
- delattr(mod, metacls.ModuleToDelete)
+ try:
+ delattr(mod, metacls.ModuleToDelete)
+ except AttributeError:
+ pass
+ #END skip case that people import our base without actually using it
#END handle deletion
return new_type
+
+class InheritedTestMethodsOverrideWrapperMetaClsAutoMixin(object):
+ """Automatically picks up the actual metaclass of the the type to be created,
+ that is the one inherited by one of the bases, and patch up its __new__ to use
+ the InheritedTestMethodsOverrideWrapperInstanceDecorator with our configured decorator"""
+
+ #{ Configuration
+ # decorator function to use when wrapping the inherited methods. Put it into a list as first member
+ # to hide it from being created as class method
+ decorator = []
+ #}END configuration
+
+ @classmethod
+ def _find_metacls(metacls, bases):
+ """emulate pythons lookup"""
+ mcls_attr = '__metaclass__'
+ for base in bases:
+ if hasattr(base, mcls_attr):
+ return getattr(base, mcls_attr)
+ return metacls._find_metacls(base.__bases__)
+ #END for each base
+ raise AssertionError("base class had not metaclass attached")
+
+ @classmethod
+ def _patch_methods_recursive(metacls, bases, clsdict):
+ """depth-first patching of methods"""
+ for base in bases:
+ metacls._patch_methods_recursive(base.__bases__, clsdict)
+ for name, item in base.__dict__.iteritems():
+ if not name.startswith('test_'):
+ continue
+ #END skip non-tests
+ clsdict[name] = metacls.decorator[0](item)
+ #END for each item
+ #END for each base
+
+ def __new__(metacls, name, bases, clsdict):
+ assert metacls.decorator, "'decorator' member needs to be set in subclass"
+ base_metacls = metacls._find_metacls(bases)
+ metacls._patch_methods_recursive(bases, clsdict)
+ return base_metacls.__new__(base_metacls, name, bases, clsdict)
+
#} END meta classes
class TestBase(TestCase):