view tests/test.py @ 68:e62d2fddb275

use the actual paths i mean
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 02 Mar 2012 09:36:58 -0800
parents 83327bc715be
children
line wrap: on
line source

#!/usr/bin/env python

"""
doctest runner for simpypi tests
"""

# XXX could use http://blog.ianbicking.org/2010/04/02/webtest-http-testing/
# vs paste.fixture.TestApp:
# http://pythonpaste.org/testing-applications.html

import doctest
import multipart
import os
import shutil
import sys
import tempfile
import testserver
import virtualenv
from optparse import OptionParser

def create_virtualenv(path):
    """create a virtualenv and return the path to the python interpreter therein"""

    virtualenv.create_environment(path)

    for scripts, ext in (('bin', ''), ('Scripts', '.exe')):
        scripts = os.path.join(path, scripts)
        if os.path.exists(scripts):
            break
    else:
        raise Exception("bin/Scripts not found in %s" % path)
    binaries = ['python', 'easy_install', 'pip']
    binaries = dict([(i, os.path.join(scripts, i + ext)) for i in binaries])
    return type('virtualenv', (), binaries)

def run_tests(raise_on_error=False, report_first=False):

    # add results here
    results = {}

    # doctest arguments
    directory = os.path.dirname(os.path.abspath(__file__))
    extraglobs = {'here': directory,
                  'create_virtualenv': create_virtualenv,
                  'testserver': testserver.TestWSGIServer,
                  'MultiPartForm': multipart.MultiPartForm
                  }
    doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
    if report_first:
        doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE

    # gather tests
    tests =  [test for test in os.listdir(directory)
              if test.endswith('.txt')]

    # run the tests
    for test in tests:

        # make a temporary directory
        tmpdir = tempfile.mkdtemp()
        doctest_args['extraglobs']['directory'] = tmpdir

        try:
            results[test] = doctest.testfile(test, **doctest_args)
        except doctest.DocTestFailure, failure:
            raise
        except doctest.UnexpectedException, failure:
            raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2]
        finally:
            if os.path.exists(tmpdir):
                shutil.rmtree(tmpdir)

    return results

def main(args=sys.argv[1:]):

    # parse command line args
    parser = OptionParser(description=__doc__)
    parser.add_option('--raise', dest='raise_on_error',
                      default=False, action='store_true',
                      help="raise on first error")
    parser.add_option('--report-first', dest='report_first',
                      default=False, action='store_true',
                      help="report the first error only (all tests will still run)")
    options, args = parser.parse_args(args)

    # run the tests
    results = run_tests(**options.__dict__)
    if sum([i.failed for i in results.values()]):
        sys.exit(1) # error

if __name__ == '__main__':
    main()