]> arthur.barton.de Git - bup.git/blob - wvtest.py
executable files: don't assume python2.5.
[bup.git] / wvtest.py
1 #!/usr/bin/env python
2 import traceback
3 import os
4 import re
5 import sys
6
7 if __name__ != "__main__":   # we're imported as a module
8     _registered = []
9     _tests = 0
10     _fails = 0
11     
12     def wvtest(func):
13         """ Use this decorator (@wvtest) in front of any function you want to run
14             as part of the unit test suite.  Then run:
15                 python wvtest.py path/to/yourtest.py
16             to run all the @wvtest functions in that file.
17         """
18         _registered.append(func)
19         return func
20     
21     
22     def _result(msg, tb, code):
23         global _tests, _fails
24         _tests += 1
25         if code != 'ok':
26             _fails += 1
27         (filename, line, func, text) = tb
28         filename = os.path.basename(filename)
29         msg = re.sub(r'\s+', ' ', str(msg))
30         sys.stderr.flush()
31         print '! %-70s %s' % ('%s:%-4d %s' % (filename, line, msg),
32                               code)
33         sys.stdout.flush()
34     
35     
36     def _check(cond, msg = 'unknown', tb = None):
37         if tb == None: tb = traceback.extract_stack()[-3]
38         if cond:
39             _result(msg, tb, 'ok')
40         else:
41             _result(msg, tb, 'FAILED')
42         return cond
43     
44     
45     def _code():
46         (filename, line, func, text) = traceback.extract_stack()[-3]
47         text = re.sub(r'^\w+\((.*)\)$', r'\1', unicode(text));
48         return text
49     
50     
51     def WVPASS(cond = True):
52         ''' Throws an exception unless cond is true. '''
53         return _check(cond, _code())
54     
55     def WVFAIL(cond = True):
56         ''' Throws an exception unless cond is false. '''
57         return _check(not cond, 'NOT(%s)' % _code())
58     
59     def WVPASSEQ(a, b):
60         ''' Throws an exception unless a == b. '''
61         return _check(a == b, '%s == %s' % (repr(a), repr(b)))
62     
63     def WVPASSNE(a, b):
64         ''' Throws an exception unless a != b. '''
65         return _check(a != b, '%s != %s' % (repr(a), repr(b)))
66     
67     def WVPASSLT(a, b):
68         ''' Throws an exception unless a < b. '''
69         return _check(a < b, '%s < %s' % (repr(a), repr(b)))
70     
71     def WVPASSLE(a, b):
72         ''' Throws an exception unless a <= b. '''
73         return _check(a <= b, '%s <= %s' % (repr(a), repr(b)))
74     
75     def WVPASSGT(a, b):
76         ''' Throws an exception unless a > b. '''
77         return _check(a > b, '%s > %s' % (repr(a), repr(b)))
78     
79     def WVPASSGE(a, b):
80         ''' Throws an exception unless a >= b. '''
81         return _check(a >= b, '%s >= %s' % (repr(a), repr(b)))
82
83 else:  # we're the main program
84     # NOTE
85     # Why do we do this in such  convoluted way?  Because if you run
86     # wvtest.py as a main program and it imports your test files, then
87     # those test files will try to import the wvtest module recursively.
88     # That actually *works* fine, because we don't run this main program
89     # when we're imported as a module.  But you end up with two separate
90     # wvtest modules, the one that gets imported, and the one that's the
91     # main program.  Each of them would have duplicated global variables
92     # (most importantly, wvtest._registered), and so screwy things could
93     # happen.  Thus, we make the main program module *totally* different
94     # from the imported module.  Then we import wvtest (the module) into
95     # wvtest (the main program) here and make sure to refer to the right
96     # versions of global variables.
97     #
98     # All this is done just so that wvtest.py can be a single file that's
99     # easy to import into your own applications.
100     import wvtest
101     
102     def _runtest(modname, fname, f):
103         print
104         print 'Testing "%s" in %s.py:' % (fname, modname)
105         sys.stdout.flush()
106         try:
107             f()
108         except Exception, e:
109             print
110             print traceback.format_exc()
111             tb = sys.exc_info()[2]
112             wvtest._result(e, traceback.extract_tb(tb)[-1],
113                            'EXCEPTION')
114             
115     # main code
116     for modname in sys.argv[1:]:
117         if not os.path.exists(modname):
118             print 'Skipping: %s' % modname
119             continue
120         if modname.endswith('.py'):
121             modname = modname[:-3]
122         print 'Importing: %s' % modname
123         wvtest._registered = []
124         mod = __import__(modname.replace('/', '.'), None, None, [])
125
126         for t in wvtest._registered:
127             _runtest(modname, t.func_name, t)
128             print
129
130     print
131     print 'WvTest: %d tests, %d failures.' % (wvtest._tests, wvtest._fails)