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