]> arthur.barton.de Git - bup.git/blobdiff - wvtest.py
Rename chain_ex to add_ex_ctx to reflect upstream terminology
[bup.git] / wvtest.py
index 4c4b4b99d0469e668c8241f8007230ac41a6a3f5..8b5ab647e47e270caccbd67c31789211b32f6ce8 100755 (executable)
--- a/wvtest.py
+++ b/wvtest.py
@@ -1,4 +1,10 @@
-#!/usr/bin/env python
+#!/bin/sh
+"""": # -*-python-*-
+bup_python="$(dirname "$0")/cmd/bup-python"
+exec "$bup_python" "$0" ${1+"$@"}
+"""
+# end of bup preamble
+
 #
 # WvTest:
 #   Copyright (C)2007-2012 Versabanq Innovations Inc. and contributors.
@@ -6,6 +12,8 @@
 #       See the included file named LICENSE for license information.
 #       You can get wvtest from: http://github.com/apenwarr/wvtest
 #
+
+from __future__ import absolute_import
 import atexit
 import inspect
 import os
@@ -13,6 +21,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
@@ -58,24 +68,44 @@ if __name__ != '__main__':   # we're imported as a module
         sys.stdout.flush()
 
 
+    def _caller_stack(wv_call_depth):
+        # Without the chdir, the source text lookup may fail
+        orig = os.getcwd()
+        os.chdir(_start_dir)
+        try:
+            return traceback.extract_stack()[-(wv_call_depth + 2)]
+        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(2)
         if cond:
             _result(msg, tb, 'ok')
         else:
             _result(msg, tb, 'FAILED')
         return cond
 
+    def wvcheck(cond, msg, tb = None):
+        if tb == None: tb = _caller_stack(2)
+        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(2)[3]
+        return _code_rx.sub(r'\1', text)
 
+    def WVSTART(message):
+        filename = _caller_stack(1)[0]
+        sys.stderr.write('Testing \"' + message + '\" in ' + filename + ':\n')
 
     def WVMSG(message):
         ''' Issues a notification. '''
-        return _result(message, traceback.extract_stack()[-3], 'ok')
+        return _result(message, _caller_stack(1), 'ok')
 
     def WVPASS(cond = True):
         ''' Counts a test failure unless cond is true. '''
@@ -117,7 +147,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())
@@ -125,6 +155,20 @@ if __name__ != '__main__':   # we're imported as a module
         else:
             return _check(False, 'EXCEPT(%s)' % _code())
 
+    wvstart = WVSTART
+    wvmsg = WVMSG
+    wvpass = WVPASS
+    wvfail = WVFAIL
+    wvpasseq = WVPASSEQ
+    wvpassne = WVPASSNE
+    wvpaslt = WVPASSLT
+    wvpassle = WVPASSLE
+    wvpassgt = WVPASSGT
+    wvpassge = WVPASSGE
+    wvexcept = WVEXCEPT
+
+    def wvfailure_count():
+        return _fails
 
     def _check_unfinished():
         if _registered:
@@ -149,15 +193,37 @@ def _run_in_chdir(path, func, *args, **kwargs):
         sys.path = oldpath
 
 
+if sys.version_info >= (2,6,0):
+    _relpath = os.path.relpath;
+else:
+    # Implementation for Python 2.5, taken from CPython (tag v2.6,
+    # file Lib/posixpath.py, hg-commit 95fff5a6a276).  Update
+    # ./LICENSE When this code is eventually removed.
+    def _relpath(path, start=os.path.curdir):
+        if not path:
+            raise ValueError("no path specified")
+
+        start_list = os.path.abspath(start).split(os.path.sep)
+        path_list = os.path.abspath(path).split(os.path.sep)
+
+        # Work out how much of the filepath is shared by start and path.
+        i = len(os.path.commonprefix([start_list, path_list]))
+
+        rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
+        if not rel_list:
+            return curdir
+        return os.path.join(*rel_list)
+
+
 def _runtest(fname, f):
     mod = inspect.getmodule(f)
-    relpath = os.path.relpath(mod.__file__, os.getcwd()).replace('.pyc', '.py')
+    relpath = _relpath(mod.__file__, os.getcwd()).replace('.pyc', '.py')
     print
     print 'Testing "%s" in %s:' % (fname, relpath)
     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]
@@ -172,7 +238,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:
@@ -197,4 +263,5 @@ if __name__ == '__main__':
     import wvtest as _wvtestmod
     sys.modules['wvtest'] = _wvtestmod
     sys.modules['wvtest.wvtest'] = _wvtestmod
+    wvtest = _wvtestmod
     wvtest_main(sys.argv[1:])