"""Regression testing framework
This module will search for scripts in the same directory named
test_*.py.  Each such script should be a test suite that tests a
module through PyUnit. This script will aggregate all
found test suites into one big test suite and run them all at once.
"""

# Author: Mark Pilgrim
# Modified by Ole Nielsen

import unittest
import os


#List files that should be excluded from the testing process.
#E.g. if they are known to fail and under development
exclude_files = []

exclude_dirs = ['pypar_dist', #Special requirements
                'props', 'wcprops', 'prop-base', 'text-base', '.svn', #Svn
                'tmp']                

def get_test_files(path):

    import sys

    try:
        files = os.listdir(path)
    except:
        return []

    #Check sub directories
    test_files = []
    for file in files:

        #Exclude svn admin dirs
        if file in exclude_dirs: continue
        
        absolute_filename = path + os.sep + file

        if os.path.isdir(absolute_filename):
            sys.path.append(file)            
            print 'Recursing into', file
            test_files += get_test_files(absolute_filename)
        elif file.startswith('test_') and file.endswith('.py'):
            test_files.append(file)
        else:
            pass
    return test_files



def regressionTest():
    import os, unittest
    path = os.getcwd()
    
    files = [x for x in  get_test_files(path) if not x == 'test_all.py']
    
    print 'Testing path %s:' %('...'+path[-50:])
    for file in files:
        print '  ' + file

    if globals().has_key('exclude_files'):
        for file in exclude_files:
            files.remove(file)
            print 'WARNING: File '+ file + ' excluded from testing'


    filenameToModuleName = lambda f: os.path.splitext(f)[0]
    moduleNames = map(filenameToModuleName, files)
    modules = map(__import__, moduleNames)
    load = unittest.defaultTestLoader.loadTestsFromModule
    return unittest.TestSuite(map(load, modules))

if __name__ == '__main__':
    unittest.main(defaultTest='regressionTest')
