#!/usr/bin/env python2.5

"""Main test file for refactor (2to3 and back again).

Running "python test.py" will run all tests in tests/test_*.py.
"""
# Original Author: Collin Winter

import unittest
from optparse import OptionParser, OptionGroup
from sys import exit

# Note more imports below, based on optparse output.

usage = "usage: %prog [options] arg"
usage += "\n\narg can be:\n"
usage += "test suite: run tests in refactor/tests/<test suite>\n"
usage += "test class: run tests in <test suite>.<test class>\n"
usage += "(default: run all tests in refactor/tests/test_*.py)"

parser = OptionParser(usage=usage)
parser.add_option("--source",
                  dest="source",
                  help="source version of Python to refactor")
parser.add_option("--target",
                  dest="target",
                  help="target version of Python")
parser.add_option("--base",
                  dest="base", default="refactor",
                  help="base package, e.g. lib2to3 or refactor")

(options, args) = parser.parse_args()

# It's too late at night to figure out why __import__ is failing.
exec "from %s import tests" % options.base
exec "from %s.tests import support" % options.base
exec "from %s.tests.test_fixers import FixerTestCase as Fixer" % options.base

old_version = support.parse_version(options.source)
new_version = support.parse_version(options.target)

if old_version:
    Fixer.old_version = old_version
if new_version:
    Fixer.new_version = new_version

if len(args) > 0:
    arg = args[0]
    mod = tests
    for m in arg.split("."):
        mod = getattr(mod, m, None)
        if not mod:
            print "Error importing %s" %(m)
            exit(1)

    if arg.find(".") == -1:
        # Just the module was specified, load all the tests
        suite = unittest.TestLoader().loadTestsFromModule(mod)
    else:
        # A class was specified, load that
        suite = unittest.makeSuite(mod)
else:
    suite = tests.all_tests

try:
    tests.support.run_all_tests(tests=suite)
except KeyboardInterrupt:
    pass
