author | jonas |
Mon, 15 Sep 2008 15:58:55 +0000 | |
changeset 2989 | 4e65295b3eef |
parent 2987 | 711910106e3a |
child 2990 | 151cac03036a |
permissions | -rw-r--r-- |
2963 | 1 |
#!/usr/bin/env python |
2966 | 2 |
#Copyright ReportLab Europe Ltd. 2000-2008 |
2963 | 3 |
#see license.txt for license details |
4 |
"""Runs all test files in all subfolders. |
|
5 |
""" |
|
2966 | 6 |
__version__=''' $Id$ ''' |
7 |
import os, glob, sys, string, traceback, unittest |
|
2978 | 8 |
|
9 |
#we need to ensure 'tests' is on the path. It will be if you |
|
10 |
#run 'setup.py tests', but won't be if you CD into the tests |
|
11 |
#directory and run this directly |
|
2987 | 12 |
if __name__=='__main__': |
13 |
P=[] |
|
14 |
try: |
|
15 |
from reportlab.lib.testutils import setOutDir |
|
16 |
except ImportError: |
|
17 |
if __name__=='__main__': |
|
18 |
topDir = os.path.dirname(sys.argv[0]) |
|
19 |
if not topDir: topDir = os.getcwd() |
|
20 |
else: |
|
21 |
topDir = os.path.dirname(__file__) |
|
22 |
topDir = os.path.dirname(os.path.abspath(topDir)) |
|
23 |
if not os.path.isdir(os.path.join(topDir,'reportlab')): |
|
2989
4e65295b3eef
Tests: Fixes and changes to run rlextra tests if available.
jonas
parents:
2987
diff
changeset
|
24 |
topDir=os.path.join(topDir,'src') |
4e65295b3eef
Tests: Fixes and changes to run rlextra tests if available.
jonas
parents:
2987
diff
changeset
|
25 |
assert os.path.isdir(os.path.join(topDir,'reportlab')), "Cannot find reportlab" |
2987 | 26 |
sys.path.insert(0, topDir) |
27 |
P.append(topDir) |
|
28 |
del topDir |
|
29 |
from reportlab.lib.testutils import setOutDir |
|
30 |
||
31 |
setOutDir(__name__) |
|
32 |
from reportlab.lib.testutils import testsFolder as topDir |
|
33 |
if topDir: |
|
34 |
topDir = os.path.dirname(topDir) |
|
35 |
if topDir not in sys.path: |
|
36 |
sys.path.insert(0,topDir) |
|
37 |
P.append(topDir) |
|
38 |
del topDir |
|
39 |
from reportlab.lib.testutils import GlobDirectoryWalker, outputfile, printLocation |
|
40 |
pp = os.environ.get('PYTHONPATH','') |
|
41 |
if pp: P.append(pp) |
|
42 |
del pp |
|
43 |
os.environ['PYTHONPATH']=os.pathsep.join(P) |
|
44 |
del P |
|
2963 | 45 |
|
46 |
def makeSuite(folder, exclude=[],nonImportable=[],pattern='test_*.py'): |
|
47 |
"Build a test suite of all available test files." |
|
48 |
allTests = unittest.TestSuite() |
|
49 |
||
50 |
if os.path.isdir(folder): sys.path.insert(0, folder) |
|
51 |
for filename in GlobDirectoryWalker(folder, pattern): |
|
52 |
modname = os.path.splitext(os.path.basename(filename))[0] |
|
53 |
if modname not in exclude: |
|
54 |
try: |
|
55 |
exec 'import %s as module' % modname |
|
56 |
allTests.addTest(module.makeSuite()) |
|
57 |
except: |
|
58 |
tt, tv, tb = sys.exc_info()[:] |
|
59 |
nonImportable.append((filename,traceback.format_exception(tt,tv,tb))) |
|
60 |
del tt,tv,tb |
|
61 |
del sys.path[0] |
|
62 |
||
63 |
return allTests |
|
64 |
||
65 |
||
66 |
def main(pattern='test_*.py'): |
|
67 |
try: |
|
68 |
folder = os.path.dirname(__file__) |
|
69 |
assert folder |
|
70 |
except: |
|
71 |
folder = os.path.dirname(sys.argv[0]) or os.getcwd() |
|
72 |
#allow for Benn's "screwball cygwin distro": |
|
2966 | 73 |
if not folder: |
2963 | 74 |
folder = '.' |
75 |
from reportlab.lib.utils import isSourceDistro |
|
76 |
haveSRC = isSourceDistro() |
|
77 |
||
78 |
def cleanup(folder,patterns=('*.pdf', '*.log','*.svg','runAll.txt', 'test_*.txt','_i_am_actually_a_*.*')): |
|
79 |
if not folder: return |
|
80 |
for pat in patterns: |
|
81 |
for filename in GlobDirectoryWalker(folder, pattern=pat): |
|
82 |
try: |
|
83 |
os.remove(filename) |
|
84 |
except: |
|
85 |
pass |
|
86 |
||
2967
ea62529bd1df
reportlab-2.2: first stage changes in on the trunk
rgbecker
parents:
2966
diff
changeset
|
87 |
# special case for tests directory - clean up |
2963 | 88 |
# all PDF & log files before starting run. You don't |
89 |
# want this if reusing runAll anywhere else. |
|
2967
ea62529bd1df
reportlab-2.2: first stage changes in on the trunk
rgbecker
parents:
2966
diff
changeset
|
90 |
if string.find(folder, os.sep+'tests') > -1: cleanup(folder) |
2963 | 91 |
cleanup(outputfile('')) |
92 |
NI = [] |
|
93 |
cleanOnly = '--clean' in sys.argv |
|
94 |
if not cleanOnly: |
|
95 |
testSuite = makeSuite(folder,nonImportable=NI,pattern=pattern+(not haveSRC and 'c' or '')) |
|
96 |
unittest.TextTestRunner().run(testSuite) |
|
2989
4e65295b3eef
Tests: Fixes and changes to run rlextra tests if available.
jonas
parents:
2987
diff
changeset
|
97 |
|
4e65295b3eef
Tests: Fixes and changes to run rlextra tests if available.
jonas
parents:
2987
diff
changeset
|
98 |
try: |
4e65295b3eef
Tests: Fixes and changes to run rlextra tests if available.
jonas
parents:
2987
diff
changeset
|
99 |
from rlextra.testall import makeSuite as makeExtraSuite |
4e65295b3eef
Tests: Fixes and changes to run rlextra tests if available.
jonas
parents:
2987
diff
changeset
|
100 |
except: |
4e65295b3eef
Tests: Fixes and changes to run rlextra tests if available.
jonas
parents:
2987
diff
changeset
|
101 |
sys.stderr.write('\nCould not find rlextra, so not tested.\n') |
4e65295b3eef
Tests: Fixes and changes to run rlextra tests if available.
jonas
parents:
2987
diff
changeset
|
102 |
else: |
4e65295b3eef
Tests: Fixes and changes to run rlextra tests if available.
jonas
parents:
2987
diff
changeset
|
103 |
sys.stdout.write('\nTesting rlextra:\n') |
4e65295b3eef
Tests: Fixes and changes to run rlextra tests if available.
jonas
parents:
2987
diff
changeset
|
104 |
unittest.TextTestRunner().run(makeExtraSuite()) |
4e65295b3eef
Tests: Fixes and changes to run rlextra tests if available.
jonas
parents:
2987
diff
changeset
|
105 |
|
2963 | 106 |
if haveSRC: cleanup(folder,patterns=('*.pyc','*.pyo')) |
107 |
if not cleanOnly: |
|
108 |
if NI: |
|
109 |
sys.stderr.write('\n###################### the following tests could not be imported\n') |
|
110 |
for f,tb in NI: |
|
111 |
print 'file: "%s"\n%s\n' % (f,string.join(tb,'')) |
|
112 |
printLocation() |
|
113 |
||
114 |
def mainEx(): |
|
115 |
'''for use in subprocesses''' |
|
116 |
try: |
|
117 |
main() |
|
118 |
finally: |
|
119 |
sys.stdout.flush() |
|
120 |
sys.stderr.flush() |
|
121 |
sys.stdout.close() |
|
122 |
os.close(sys.stderr.fileno()) |
|
123 |
||
124 |
def runExternally(): |
|
2966 | 125 |
cmd = sys.executable+' -c"from tests import runAll;runAll.mainEx()"' |
2963 | 126 |
i,o,e=os.popen3(cmd) |
127 |
i.close() |
|
128 |
out = o.read() |
|
129 |
err=e.read() |
|
130 |
return '\n'.join((out,err)) |
|
131 |
||
132 |
def checkForFailure(outerr): |
|
133 |
return '\nFAILED' in outerr |
|
134 |
||
135 |
if __name__ == '__main__': #noruntests |
|
136 |
main() |