예제 #1
0
def main():
    """
    Execute the nose test runner.

    Drop privileges and alter the system argument to remove the
    userid and group id arguments that are only required for the test.
    """
    if len(sys.argv) < 2:
        print (
            u'Run the test suite using drop privileges username as first '
            u'arguments. Use "-" if you do not want elevated mode.')
        sys.exit(1)

    # Delay import after coverage is started.
    from chevah.empirical.nose_memory_usage import MemoryUsage
    from chevah.empirical.nose_test_timer import TestTimer
    from chevah.empirical.nose_run_reporter import RunReporter

    from chevah.empirical import EmpiricalTestCase


    drop_user = sys.argv[1]
    EmpiricalTestCase.initialize(drop_user=drop_user)
    EmpiricalTestCase.dropPrivileges()

    new_argv = ['chevah-test-runner']
    new_argv.extend(sys.argv[2:])
    sys.argv = new_argv
    plugins = [
        TestTimer(),
        RunReporter(),
        MemoryUsage(),
        ]
    try:
        nose_main(addplugins=plugins)
    except SystemExit, error:
        if cov:
            cov.stop()
            cov.save()
        import threading
        print "Max RSS: %s" % EmpiricalTestCase.getPeakMemoryUsage()
        threads = threading.enumerate()
        if len(threads) < 2:
            # No running threads, other than main so we can exit as normal.
            sys.exit(error.code)
        else:
            print "There are still active threads: %s" % threads

            # We do a brute force exit here, since sys.exit will wait for
            # unjoined threads.
            # We have to do some manual work to compensate for skipping sys.exit()
            sys.exitfunc()
            # Don't forget to flush the toilet.
            sys.stdout.flush()
            sys.stderr.flush()
            os._exit(error.code)