#!/usr/bin/env python
# -*- coding: utf-8 -*-
# $Id: runtests 1634 2013-04-12 15:36:36Z amelung $
#
# Copyright (c) 2007-2011 Otto-von-Guericke-Universität Magdeburg
#
# This file is part of ECSpooler.

"""
Runs a simgle test suite given as command line parameter.
"""

import os
import sys
import getopt
import unittest

# add parent directory to the system path
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir))

from lib.util import settings

# change dir to test dir
os.chdir(os.path.join(os.path.abspath(os.path.dirname(__file__))))

def usage():
    """
    """
    print  >> sys.stdout, 'Usage: runtests TESTFILE.py'

def main():    
    """
    """
    try:
        opts, args = getopt.getopt(sys.argv[1:], '')
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)
    
    if len(args) == 0:
        usage()
        sys.exit(2)
    else:
        TestRunner = unittest.TextTestRunner
        suite = unittest.TestSuite()

        test = args[0]

        if test.startswith('test') and test.endswith('.py'):
            
            mName = test[:-3]
            m = __import__(mName)

            settings.init_logging(mName)
            
            if hasattr(m, 'test_suite'):
                print >> sys.stdout, m.__name__
                #log.info(m.__name__)
                suite.addTest(m.test_suite())
            else:
                print >> sys.stdout, m.__name__, ': no test suite found.'
                #log.info('%s: no test suite found.' % m.__name__ )
            
            
            TestRunner().run(suite)

# -- Main ---------------------------------------------------------------------
if __name__ == "__main__":
    main()
