#!/usr/bin/env python

import os
import sys
import re
import unittest
from optparse import OptionParser

os.environ['DJANGO_SETTINGS_MODULE'] = 'desire.django.settings.main'

if __name__ == "__main__":
    usage  = 'usage: %prog [options] [module_name]'
    usage += '\n\tmodule_name specifies the module to test. '
    usage += 'If none supplied then run all tests.'
    parser = OptionParser(usage)
    parser.add_option('-v', '--verbose',
        action='store_true', dest='verbose', default=False,
        help='Be verbose in printing status messages')
    parser.add_option('-l', '--level',
        action='store', type='int', dest='level', default=1,
        help='Verbosity level of test runner')
    
    (options, args) = parser.parse_args()
    # by default always 1 argument (name of file itself)
    suiteToRun = None
    testModuleName = 'desire.test'
    if len(args) == 1:
        testModuleName = args[0]
    elif len(args) >= 1:
        parser.print_help()
        sys.exit(1)

    import desire.soleInstance

    testRunner = unittest.TextTestRunner(verbosity=options.level)
    testModule = __import__(testModuleName,'','','*')
    testSuite = testModule.suite()
    result = testRunner.run(testSuite)
    if not result.wasSuccessful():
        sys.exit(1)

