]> arthur.barton.de Git - bup.git/blobdiff - wvtest.py
Update to latest wvtest.py, wvtest.sh, and wvtestrun from wvtest project.
[bup.git] / wvtest.py
index 7edd91a0d28aca4ddd596d7b34bc79d6a6c82332..fd6994eeae5ee616d314ceb52e06d05ed8f399c2 100755 (executable)
--- a/wvtest.py
+++ b/wvtest.py
@@ -8,7 +8,7 @@ if __name__ != "__main__":   # we're imported as a module
     _registered = []
     _tests = 0
     _fails = 0
-    
+
     def wvtest(func):
         """ Use this decorator (@wvtest) in front of any function you want to run
             as part of the unit test suite.  Then run:
@@ -17,8 +17,8 @@ if __name__ != "__main__":   # we're imported as a module
         """
         _registered.append(func)
         return func
-    
-    
+
+
     def _result(msg, tb, code):
         global _tests, _fails
         _tests += 1
@@ -31,8 +31,8 @@ if __name__ != "__main__":   # we're imported as a module
         print '! %-70s %s' % ('%s:%-4d %s' % (filename, line, msg),
                               code)
         sys.stdout.flush()
-    
-    
+
+
     def _check(cond, msg = 'unknown', tb = None):
         if tb == None: tb = traceback.extract_stack()[-3]
         if cond:
@@ -40,49 +40,65 @@ if __name__ != "__main__":   # we're imported as a module
         else:
             _result(msg, tb, 'FAILED')
         return cond
-    
-    
+
+
     def _code():
         (filename, line, func, text) = traceback.extract_stack()[-3]
-        text = re.sub(r'^\w+\((.*)\)$', r'\1', unicode(text));
+        text = re.sub(r'^\w+\((.*)\)(\s*#.*)?$', r'\1', text);
         return text
-    
-    
+
+
     def WVPASS(cond = True):
-        ''' Throws an exception unless cond is true. '''
+        ''' Counts a test failure unless cond is true. '''
         return _check(cond, _code())
-    
+
     def WVFAIL(cond = True):
-        ''' Throws an exception unless cond is false. '''
+        ''' Counts a test failure  unless cond is false. '''
         return _check(not cond, 'NOT(%s)' % _code())
-    
+
     def WVPASSEQ(a, b):
-        ''' Throws an exception unless a == b. '''
+        ''' Counts a test failure unless a == b. '''
         return _check(a == b, '%s == %s' % (repr(a), repr(b)))
-    
+
     def WVPASSNE(a, b):
-        ''' Throws an exception unless a != b. '''
+        ''' Counts a test failure unless a != b. '''
         return _check(a != b, '%s != %s' % (repr(a), repr(b)))
-    
+
     def WVPASSLT(a, b):
-        ''' Throws an exception unless a < b. '''
+        ''' Counts a test failure unless a < b. '''
         return _check(a < b, '%s < %s' % (repr(a), repr(b)))
-    
+
     def WVPASSLE(a, b):
-        ''' Throws an exception unless a <= b. '''
+        ''' Counts a test failure unless a <= b. '''
         return _check(a <= b, '%s <= %s' % (repr(a), repr(b)))
-    
+
     def WVPASSGT(a, b):
-        ''' Throws an exception unless a > b. '''
+        ''' Counts a test failure unless a > b. '''
         return _check(a > b, '%s > %s' % (repr(a), repr(b)))
-    
+
     def WVPASSGE(a, b):
-        ''' Throws an exception unless a >= b. '''
+        ''' Counts a test failure unless a >= b. '''
         return _check(a >= b, '%s >= %s' % (repr(a), repr(b)))
 
+    def WVEXCEPT(etype, func, *args, **kwargs):
+        ''' Counts a test failure unless func throws an 'etype' exception.
+            You have to spell out the function name and arguments, rather than
+            calling the function yourself, so that WVEXCEPT can run before
+            your test code throws an exception.
+        '''
+        try:
+            func(*args, **kwargs)
+        except etype, e:
+            return _check(True, 'EXCEPT(%s)' % _code())
+        except:
+            _check(False, 'EXCEPT(%s)' % _code())
+            raise
+        else:
+            return _check(False, 'EXCEPT(%s)' % _code())
+
 else:  # we're the main program
     # NOTE
-    # Why do we do this in such  convoluted way?  Because if you run
+    # Why do we do this in such a convoluted way?  Because if you run
     # wvtest.py as a main program and it imports your test files, then
     # those test files will try to import the wvtest module recursively.
     # That actually *works* fine, because we don't run this main program
@@ -98,7 +114,7 @@ else:  # we're the main program
     # All this is done just so that wvtest.py can be a single file that's
     # easy to import into your own applications.
     import wvtest
-    
+
     def _runtest(modname, fname, f):
         print
         print 'Testing "%s" in %s.py:' % (fname, modname)
@@ -109,9 +125,9 @@ else:  # we're the main program
             print
             print traceback.format_exc()
             tb = sys.exc_info()[2]
-            wvtest._result(e, traceback.extract_tb(tb)[-1],
+            wvtest._result(e, traceback.extract_tb(tb)[1],
                            'EXCEPTION')
-            
+
     # main code
     for modname in sys.argv[1:]:
         if not os.path.exists(modname):
@@ -121,11 +137,20 @@ else:  # we're the main program
             modname = modname[:-3]
         print 'Importing: %s' % modname
         wvtest._registered = []
-        mod = __import__(modname.replace('/', '.'), None, None, [])
-
-        for t in wvtest._registered:
-            _runtest(modname, t.func_name, t)
-            print
+        oldwd = os.getcwd()
+        oldpath = sys.path
+        try:
+            modpath = os.path.abspath(modname).split('/')[:-1]
+            os.chdir('/'.join(modpath))
+            sys.path += ['/'.join(modpath),
+                         '/'.join(modpath[:-1])]
+            mod = __import__(modname.replace('/', '.'), None, None, [])
+            for t in wvtest._registered:
+                _runtest(modname, t.func_name, t)
+                print
+        finally:
+            os.chdir(oldwd)
+            sys.path = oldpath
 
     print
     print 'WvTest: %d tests, %d failures.' % (wvtest._tests, wvtest._fails)