]> arthur.barton.de Git - bup.git/blobdiff - wvtest.py
PackWriter: rename write() to just_write()
[bup.git] / wvtest.py
index 1c2981e9be31975fe58aa2bd7d85fa2a581ea0b6..61245b29bc4fc976634624b88f1d48e7f409fa37 100755 (executable)
--- a/wvtest.py
+++ b/wvtest.py
@@ -18,6 +18,8 @@ import re
 import sys
 import traceback
 
+_start_dir = os.getcwd()
+
 # NOTE
 # Why do we do we need the "!= main" check?  Because if you run
 # wvtest.py as a main program and it imports your test files, then
@@ -63,24 +65,32 @@ if __name__ != '__main__':   # we're imported as a module
         sys.stdout.flush()
 
 
+    def _caller_stack():
+        # Without the chdir, the source text lookup may fail
+        orig = os.getcwd()
+        os.chdir(_start_dir)
+        try:
+            return traceback.extract_stack()[-4]
+        finally:
+            os.chdir(orig)
+
+
     def _check(cond, msg = 'unknown', tb = None):
-        if tb == None: tb = traceback.extract_stack()[-3]
+        if tb == None: tb = _caller_stack()
         if cond:
             _result(msg, tb, 'ok')
         else:
             _result(msg, tb, 'FAILED')
         return cond
 
-
+    _code_rx = re.compile(r'^\w+\((.*)\)(\s*#.*)?$')
     def _code():
-        (filename, line, func, text) = traceback.extract_stack()[-3]
-        text = re.sub(r'^\w+\((.*)\)(\s*#.*)?$', r'\1', text);
-        return text
-
+        text = _caller_stack()[3]
+        return _code_rx.sub(r'\1', text)
 
     def WVMSG(message):
         ''' Issues a notification. '''
-        return _result(message, traceback.extract_stack()[-3], 'ok')
+        return _result(message, _caller_stack(), 'ok')
 
     def WVPASS(cond = True):
         ''' Counts a test failure unless cond is true. '''
@@ -122,7 +132,7 @@ if __name__ != '__main__':   # we're imported as a module
         '''
         try:
             func(*args, **kwargs)
-        except etype, e:
+        except etype as e:
             return _check(True, 'EXCEPT(%s)' % _code())
         except:
             _check(False, 'EXCEPT(%s)' % _code())
@@ -186,7 +196,7 @@ def _runtest(fname, f):
     sys.stdout.flush()
     try:
         _run_in_chdir(os.path.split(mod.__file__)[0], f)
-    except Exception, e:
+    except Exception as e:
         print
         print traceback.format_exc()
         tb = sys.exc_info()[2]
@@ -201,7 +211,7 @@ def _run_registered_tests():
         print
 
 
-def wvtest_main(extra_testfiles=[]):
+def wvtest_main(extra_testfiles=tuple()):
     import wvtest as _wvtestmod
     _run_registered_tests()
     for modname in extra_testfiles:
@@ -226,4 +236,5 @@ if __name__ == '__main__':
     import wvtest as _wvtestmod
     sys.modules['wvtest'] = _wvtestmod
     sys.modules['wvtest.wvtest'] = _wvtestmod
+    wvtest = _wvtestmod
     wvtest_main(sys.argv[1:])