from __future__ import absolute_import
import click
import unittest
import sys
import os
import sputr
from .config import version

@click.command(
    help='Simple Python Unit Test Runner',
    epilog="""\b\bExamples:\n
sputr                      -run all tests in the current directory\n
sputr dirname              -run all tests in the specified directory\n
sputr filename.py          -run all tests in files with this name\n
sputr dirname/filename.py  -run all tests in a specific file\n
sputr test_name            -run all tests that match a pattern\n
sputr test_name_*          -run all tests that match a pattern
    """,
    context_settings={
        'help_option_names': ['-h','--help'],
    }
)
@click.argument('pattern', default='')
@click.version_option(version=version)
@click.option('--start_dir', '-s', default='.', show_default=True, help='Directory to start discovery')
@click.option('--verbose', '-v', is_flag=True, help='More output')
@click.option('--quiet', '-q', is_flag=True, help='Less output')
@click.option('--failfast', '-f', is_flag=True, help='Stop execution on test failure or error')
@click.option('--buffer', '-b', is_flag=True, help='Buffer stdout and stderr during tests')
@click.option('--catch', '-c', is_flag=True, help='Display test results after keyboard interrupt (control-c)')
@click.option('--top_level_dir', '-t', help='Top level directory of the project')
def cli(pattern, start_dir, verbose, quiet, failfast, buffer, catch, top_level_dir):

    if sys.path[0] != os.getcwd():
        sys.path.insert(0, os.getcwd())

    suite = sputr.discover(
        start_dir=start_dir, 
        pattern=pattern, 
        top_level_dir=top_level_dir
    )

    if verbose:
        verbosity = 2
    elif quiet:
        verbosity = 0
    else:
        verbosity = 1

    if catch:
        unittest.installHandler()

    result = unittest.TextTestRunner(
        verbosity=verbosity,
        failfast=failfast,
        buffer=buffer
    ).run(suite)

    if catch:
        unittest.removeHandler()

    sys.exit(0 if result.wasSuccessful() else 1)

if __name__ == '__main__':
    cli()


@@ -82,16 +82,34 @@
 port os%0A
+import importlib%0A%0A
 import s
@@ -1411,16 +1411,169 @@
 oject')%0A
+@click.option(%0A    '--runner', '-r', %0A    default='unittest.TextTestRunner', %0A    show_default=True, %0A    help='Test runner to use when running tests'%0A)%0A
 def cli(
@@ -1646,16 +1646,24 @@
 evel_dir
+, runner
 ):%0A%0A    
@@ -1742,325 +1742,283 @@
     
-suite = sputr.discover(%0A        start_dir=start_dir, %0A        pattern=pattern, %0A        top_level_dir=top_level_dir%0A    )%0A%0A    if verbose:%0A        verbosity = 2%0A    elif quiet:%0A        verbosity = 0%0A    else:%0A        verbosity = 1%0A%0A    if catch:%0A        unittest.installHandler()%0A%0A    result = unittest.TextTestR
+if verbose:%0A        verbosity = 2%0A    elif quiet:%0A        verbosity = 0%0A    else:%0A        verbosity = 1%0A%0A    last = runner.rfind('.')%0A    package = runner%5B:last%5D%0A    runner = runner%5Blast + 1:%5D%0A    module = importlib.import_module(package)%0A    runner = getattr(module, r
 unner
+)
 (%0A  
@@ -2098,16 +2098,212 @@
 er%0A    )
+%0A%0A    suite = sputr.discover(%0A        start_dir=start_dir, %0A        pattern=pattern, %0A        top_level_dir=top_level_dir%0A    )%0A%0A    if catch:%0A        unittest.installHandler()%0A    result = runner
 .run(sui
@@ -2306,17 +2306,16 @@
 (suite)%0A
-%0A
     if c

support alternate test runners