X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=wvtest.py;h=8b5ab647e47e270caccbd67c31789211b32f6ce8;hb=aab82669cb0efd0a275e0ccd14ee779e894455ea;hp=561cc8794ba8e93fc43be65fbd5a50ba53922bb0;hpb=af1e9c1078fa2392ae416e70addb41c6fcf4dc8d;p=bup.git diff --git a/wvtest.py b/wvtest.py index 561cc87..8b5ab64 100755 --- a/wvtest.py +++ b/wvtest.py @@ -4,6 +4,7 @@ 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. @@ -11,6 +12,8 @@ exec "$bup_python" "$0" ${1+"$@"} # 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 @@ -18,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 @@ -63,8 +68,26 @@ 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: @@ -73,14 +96,16 @@ if __name__ != '__main__': # we're imported as a module _code_rx = re.compile(r'^\w+\((.*)\)(\s*#.*)?$') def _code(): - (filename, line, func, text) = traceback.extract_stack()[-3] + 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. ''' @@ -130,6 +155,18 @@ 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