Exemple #1
0
    def load_module(self,name):

        module = types.new_module(name)
        exec (self.current_module_code in module.__dict__)
        sys.modules[name] = modules

        return module
Exemple #2
0
 def run(cls, pythonpath, extra_argv=None, **kwargs):
     cls._setup(pythonpath, **kwargs)
     if extra_argv:
         # Don't use subprocess, since we don't want to include the
         # current path in PYTHONPATH.
         sys.argv = extra_argv
         with open(extra_argv[0], 'r') as f:
             script = f.read()
         sys.modules['__main__'] = new_module('__main__')
         ns = dict(__name__='__main__', __file__=extra_argv[0])
         exec(script, ns)
     else:
         import code
         code.interact()
Exemple #3
0
def main(argv):
    parser = ArgumentParser(usage=__doc__.lstrip())
    parser.add_argument("--verbose",
                        "-v",
                        action="count",
                        default=1,
                        help="more verbosity")
    parser.add_argument(
        "--no-build",
        "-n",
        action="store_true",
        default=False,
        help="do not build the project (use system installed version)")
    parser.add_argument("--werror",
                        action="store_true",
                        default=False,
                        help="Treat warnings as errors")
    parser.add_argument("--build-only",
                        "-b",
                        action="store_true",
                        default=False,
                        help="just build, do not run any tests")
    parser.add_argument("--doctests",
                        action="store_true",
                        default=False,
                        help="Run doctests in module")
    parser.add_argument("--refguide-check",
                        action="store_true",
                        default=False,
                        help="Run refguide check (do not run regular tests.)")
    parser.add_argument("--coverage",
                        action="store_true",
                        default=False,
                        help=("report coverage of project code. HTML output"
                              " goes under build/coverage"))
    parser.add_argument("--gcov",
                        action="store_true",
                        default=False,
                        help=("enable C code coverage via gcov (requires GCC)."
                              " gcov output goes to build/**/*.gc*"))
    parser.add_argument("--lcov-html",
                        action="store_true",
                        default=False,
                        help=("produce HTML for C code coverage information "
                              "from a previous run with --gcov. "
                              "HTML output goes to build/lcov/"))
    parser.add_argument("--mode",
                        "-m",
                        default="fast",
                        help="'fast', 'full', or something that could be "
                        "passed to `pytest -m` as a marker expression "
                        "[default: fast]")
    parser.add_argument("--submodule",
                        "-s",
                        default=None,
                        help="Submodule whose tests to run (cluster,"
                        " constants, ...)")
    parser.add_argument("--pythonpath",
                        "-p",
                        default=None,
                        help="Paths to prepend to PYTHONPATH")
    parser.add_argument("--tests",
                        "-t",
                        action='append',
                        help="Specify tests to run")
    parser.add_argument("--python",
                        action="store_true",
                        help="Start a Python shell with PYTHONPATH set")
    parser.add_argument("--ipython",
                        "-i",
                        action="store_true",
                        help="Start IPython shell with PYTHONPATH set")
    parser.add_argument("--shell",
                        action="store_true",
                        help="Start Unix shell with PYTHONPATH set")
    parser.add_argument("--debug",
                        "-g",
                        action="store_true",
                        help="Debug build")
    parser.add_argument("--parallel",
                        "-j",
                        type=int,
                        default=1,
                        help="Number of parallel jobs for build and testing")
    parser.add_argument("--show-build-log",
                        action="store_true",
                        help="Show build output rather than using a log file")
    parser.add_argument("--bench",
                        action="store_true",
                        help="Run benchmark suite instead of test suite")
    parser.add_argument("--bench-compare",
                        action="append",
                        metavar="BEFORE",
                        help=("Compare benchmark results of current HEAD to"
                              " BEFORE. Use an additional "
                              "--bench-compare=COMMIT to override HEAD with"
                              " COMMIT. Note that you need to commit your "
                              "changes first!"))
    parser.add_argument("args",
                        metavar="ARGS",
                        default=[],
                        nargs=REMAINDER,
                        help="Arguments to pass to Nose, Python or shell")
    parser.add_argument("--pep8",
                        action="store_true",
                        default=False,
                        help="Perform pep8 check with flake8.")
    parser.add_argument("--mypy",
                        action="store_true",
                        default=False,
                        help="Run mypy on the codebase")
    parser.add_argument("--doc",
                        action="append",
                        nargs="?",
                        const="html",
                        help="Build documentation")
    parser.add_argument(
        "--win-cp-openblas",
        action="store_true",
        help="If set, and on Windows, copy OpenBLAS lib to "
        "install directory after meson install. "
        "Note: this argument may be removed in the future "
        "once a `site.cfg`-like mechanism to select BLAS/LAPACK "
        "libraries is implemented for Meson")
    parser.add_argument("--build-dir",
                        default="build",
                        help="Relative path to the build directory. "
                        "Default is 'build'")
    parser.add_argument("--install-prefix",
                        default=None,
                        help="Relative path to the install directory. "
                        "Default is <build-dir>-install.")
    args = parser.parse_args(argv)

    global PATH_INSTALLED
    build_dir = Path(args.build_dir)
    install_dir = args.install_prefix
    if not install_dir:
        install_dir = build_dir.parent / (build_dir.stem + "-install")
    PATH_INSTALLED = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                                  install_dir)

    if args.win_cp_openblas and platform.system() != 'Windows':
        raise RuntimeError('--win-cp-openblas only has effect on Windows')

    if args.pep8:
        # Lint the source using the configuration in tox.ini.
        os.system("flake8 scipy benchmarks/benchmarks")
        # Lint just the diff since branching off of main using a
        # stricter configuration.
        lint_diff = os.path.join(ROOT_DIR, 'tools', 'lint_diff.py')
        os.system(lint_diff)
        sys.exit(0)

    if args.mypy:
        sys.exit(run_mypy(args))

    if args.bench_compare:
        args.bench = True
        args.no_build = True  # ASV does the building

    if args.lcov_html:
        # generate C code coverage output
        runtests.lcov_generate()
        sys.exit(0)

    if args.pythonpath:
        for p in reversed(args.pythonpath.split(os.pathsep)):
            sys.path.insert(0, p)

    if args.gcov:
        runtests.gcov_reset_counters()

    if args.debug and args.bench:
        print("*** Benchmarks should not be run against debug version; "
              "remove -g flag ***")

    if not args.no_build:
        site_dir = build_project(args)
        sys.path.insert(0, site_dir)
        os.environ['PYTHONPATH'] = \
            os.pathsep.join((site_dir, os.environ.get('PYTHONPATH', '')))

    extra_argv = args.args[:]
    if extra_argv and extra_argv[0] == '--':
        extra_argv = extra_argv[1:]

    if args.python:
        if extra_argv:
            # Don't use subprocess, since we don't want to include the
            # current path in PYTHONPATH.
            sys.argv = extra_argv
            with open(extra_argv[0], 'r') as f:
                script = f.read()
            sys.modules['__main__'] = new_module('__main__')
            ns = dict(__name__='__main__', __file__=extra_argv[0])
            exec(script, ns)
            sys.exit(0)
        else:
            import code
            code.interact()
            sys.exit(0)

    if args.ipython:
        import IPython
        IPython.embed(user_ns={})
        sys.exit(0)

    if args.shell:
        shell = os.environ.get('SHELL', 'sh')
        print("Spawning a Unix shell...")
        os.execv(shell, [shell] + extra_argv)
        sys.exit(1)

    if args.doc:
        cmd = ["make", "-Cdoc", 'PYTHON="{}"'.format(sys.executable)]
        cmd += args.doc
        if args.parallel:
            cmd.append('SPHINXOPTS="-j{}"'.format(args.parallel))
        subprocess.run(cmd, check=True)
        sys.exit(0)

    if args.coverage:
        dst_dir = os.path.join(ROOT_DIR, args.build_dir, 'coverage')
        fn = os.path.join(dst_dir, 'coverage_html.js')
        if os.path.isdir(dst_dir) and os.path.isfile(fn):
            shutil.rmtree(dst_dir)
        extra_argv += ['--cov-report=html:' + dst_dir]

    if args.refguide_check:
        cmd = [
            os.path.join(ROOT_DIR, 'tools', 'refguide_check.py'), '--doctests'
        ]
        if args.verbose:
            cmd += ['-' + 'v' * args.verbose]
        if args.submodule:
            cmd += [args.submodule]
        os.execv(sys.executable, [sys.executable] + cmd)
        sys.exit(0)

    test, version, mod_path = get_project_info()

    if args.bench:
        # Run ASV
        items = extra_argv
        if args.tests:
            items += args.tests
        if args.submodule:
            items += [args.submodule]

        bench_args = []
        for a in items:
            bench_args.extend(['--bench', a])

        if not args.bench_compare:
            import scipy
            print("Running benchmarks for Scipy version %s at %s" %
                  (version, mod_path))
            cmd = [
                'asv', 'run', '--dry-run', '--show-stderr', '--python=same'
            ] + bench_args
            retval = runtests.run_asv(cmd)
            sys.exit(retval)
        else:
            if len(args.bench_compare) == 1:
                commit_a = args.bench_compare[0]
                commit_b = 'HEAD'
            elif len(args.bench_compare) == 2:
                commit_a, commit_b = args.bench_compare
            else:
                p.error("Too many commits to compare benchmarks for")

            # Check for uncommitted files
            if commit_b == 'HEAD':
                r1 = subprocess.call(
                    ['git', 'diff-index', '--quiet', '--cached', 'HEAD'])
                r2 = subprocess.call(['git', 'diff-files', '--quiet'])
                if r1 != 0 or r2 != 0:
                    print("*" * 80)
                    print("WARNING: you have uncommitted changes --- "
                          "these will NOT be benchmarked!")
                    print("*" * 80)

            # Fix commit ids (HEAD is local to current repo)
            p = subprocess.Popen(['git', 'rev-parse', commit_b],
                                 stdout=subprocess.PIPE)
            out, err = p.communicate()
            commit_b = out.strip()

            p = subprocess.Popen(['git', 'rev-parse', commit_a],
                                 stdout=subprocess.PIPE)
            out, err = p.communicate()
            commit_a = out.strip()

            cmd = [
                'asv', 'continuous', '--show-stderr', '--factor', '1.05',
                commit_a, commit_b
            ] + bench_args
            runtests.run_asv(cmd)
            sys.exit(1)

    if args.build_only:
        sys.exit(0)

    if args.submodule:
        tests = [PROJECT_MODULE + "." + args.submodule]
    elif args.tests:
        tests = args.tests
    else:
        tests = None

    # Run the tests

    if not args.no_build:
        test_dir = site_dir
    else:
        test_dir = os.path.join(ROOT_DIR, args.build_dir, 'test')
        if not os.path.isdir(test_dir):
            os.makedirs(test_dir)

    shutil.copyfile(os.path.join(ROOT_DIR, '.coveragerc'),
                    os.path.join(test_dir, '.coveragerc'))

    cwd = os.getcwd()
    try:
        os.chdir(test_dir)
        print("Running tests for {} version:{}, installed at:{}".format(
            PROJECT_MODULE, version, mod_path))
        result = test(args.mode,
                      verbose=args.verbose,
                      extra_argv=extra_argv,
                      doctests=args.doctests,
                      coverage=args.coverage,
                      tests=tests,
                      parallel=args.parallel)
    finally:
        os.chdir(cwd)

    if isinstance(result, bool):
        sys.exit(0 if result else 1)
    elif result.wasSuccessful():
        sys.exit(0)
    else:
        sys.exit(1)
Exemple #4
0
def main(argv):
    parser = ArgumentParser(usage=__doc__.lstrip())
    parser.add_argument("--verbose", "-v", action="count", default=1,
                        help="more verbosity")
    parser.add_argument("--no-build", "-n", action="store_true", default=False,
                        help="do not build the project (use system installed version)")
    parser.add_argument("--build-only", "-b", action="store_true", default=False,
                        help="just build, do not run any tests")
    parser.add_argument("--doctests", action="store_true", default=False,
                        help="Run doctests in module")
    parser.add_argument("--refguide-check", action="store_true", default=False,
                        help="Run refguide check (do not run regular tests.)")
    parser.add_argument("--coverage", action="store_true", default=False,
                        help=("report coverage of project code. HTML output"
                              " goes under build/coverage"))
    parser.add_argument("--gcov", action="store_true", default=False,
                        help=("enable C code coverage via gcov (requires GCC)."
                              " gcov output goes to build/**/*.gc*"))
    parser.add_argument("--lcov-html", action="store_true", default=False,
                        help=("produce HTML for C code coverage information "
                              "from a previous run with --gcov. "
                              "HTML output goes to build/lcov/"))
    parser.add_argument("--mode", "-m", default="fast",
                        help="'fast', 'full', or something that could be "
                             "passed to nosetests -A [default: fast]")
    parser.add_argument("--submodule", "-s", default=None,
                        help="Submodule whose tests to run (cluster,"
                             " constants, ...)")
    parser.add_argument("--pythonpath", "-p", default=None,
                        help="Paths to prepend to PYTHONPATH")
    parser.add_argument("--tests", "-t", action='append',
                        help="Specify tests to run")
    parser.add_argument("--python", action="store_true",
                        help="Start a Python shell with PYTHONPATH set")
    parser.add_argument("--ipython", "-i", action="store_true",
                        help="Start IPython shell with PYTHONPATH set")
    parser.add_argument("--shell", action="store_true",
                        help="Start Unix shell with PYTHONPATH set")
    parser.add_argument("--debug", "-g", action="store_true",
                        help="Debug build")
    parser.add_argument("--parallel", "-j", type=int, default=1,
                        help="Number of parallel jobs during build (requires "
                             "NumPy 1.10 or greater).")
    parser.add_argument("--show-build-log", action="store_true",
                        help="Show build output rather than using a log file")
    parser.add_argument("--bench", action="store_true",
                        help="Run benchmark suite instead of test suite")
    parser.add_argument("--bench-compare", action="append", metavar="BEFORE",
                        help=("Compare benchmark results of current HEAD to"
                              " BEFORE. Use an additional "
                              "--bench-compare=COMMIT to override HEAD with"
                              " COMMIT. Note that you need to commit your "
                              "changes first!"
                              ))
    parser.add_argument("args", metavar="ARGS", default=[], nargs=REMAINDER,
                        help="Arguments to pass to Nose, Python or shell")
    args = parser.parse_args(argv)

    if args.bench_compare:
        args.bench = True
        args.no_build = True  # ASV does the building

    if args.lcov_html:
        # generate C code coverage output
        lcov_generate()
        sys.exit(0)

    if args.pythonpath:
        for p in reversed(args.pythonpath.split(os.pathsep)):
            sys.path.insert(0, p)

    if args.gcov:
        gcov_reset_counters()

    if args.debug and args.bench:
        print("*** Benchmarks should not be run against debug version; "
              "remove -g flag ***")

    if not args.no_build:
        site_dir = build_project(args)
        sys.path.insert(0, site_dir)
        os.environ['PYTHONPATH'] = site_dir

    extra_argv = args.args[:]
    if extra_argv and extra_argv[0] == '--':
        extra_argv = extra_argv[1:]

    if args.python:
        if extra_argv:
            # Don't use subprocess, since we don't want to include the
            # current path in PYTHONPATH.
            sys.argv = extra_argv
            with open(extra_argv[0], 'r') as f:
                script = f.read()
            sys.modules['__main__'] = new_module('__main__')
            ns = dict(__name__='__main__',
                      __file__=extra_argv[0])
            exec_(script, ns)
            sys.exit(0)
        else:
            import code
            code.interact()
            sys.exit(0)

    if args.ipython:
        import IPython
        IPython.embed(user_ns={})
        sys.exit(0)

    if args.shell:
        shell = os.environ.get('SHELL', 'sh')
        print("Spawning a Unix shell...")
        os.execv(shell, [shell] + extra_argv)
        sys.exit(1)

    if args.coverage:
        dst_dir = os.path.join(ROOT_DIR, 'build', 'coverage')
        fn = os.path.join(dst_dir, 'coverage_html.js')
        if os.path.isdir(dst_dir) and os.path.isfile(fn):
            shutil.rmtree(dst_dir)
        extra_argv += ['--cov-report=html:' + dst_dir]

    if args.refguide_check:
        cmd = [os.path.join(ROOT_DIR, 'tools', 'refguide_check.py'),
               '--doctests']
        if args.submodule:
            cmd += [args.submodule]
        os.execv(sys.executable, [sys.executable] + cmd)
        sys.exit(0)

    if args.bench:
        # Run ASV
        items = extra_argv
        if args.tests:
            items += args.tests
        if args.submodule:
            items += [args.submodule]

        bench_args = []
        for a in items:
            bench_args.extend(['--bench', a])

        if not args.bench_compare:
            cmd = [os.path.join(ROOT_DIR, 'benchmarks', 'run.py'),
                   'run', '-n', '-e', '--python=same'] + bench_args
            os.execv(sys.executable, [sys.executable] + cmd)
            sys.exit(1)
        else:
            if len(args.bench_compare) == 1:
                commit_a = args.bench_compare[0]
                commit_b = 'HEAD'
            elif len(args.bench_compare) == 2:
                commit_a, commit_b = args.bench_compare
            else:
                p.error("Too many commits to compare benchmarks for")

            # Check for uncommitted files
            if commit_b == 'HEAD':
                r1 = subprocess.call(['git', 'diff-index', '--quiet',
                                      '--cached', 'HEAD'])
                r2 = subprocess.call(['git', 'diff-files', '--quiet'])
                if r1 != 0 or r2 != 0:
                    print("*"*80)
                    print("WARNING: you have uncommitted changes --- "
                          "these will NOT be benchmarked!")
                    print("*"*80)

            # Fix commit ids (HEAD is local to current repo)
            p = subprocess.Popen(['git', 'rev-parse', commit_b],
                                 stdout=subprocess.PIPE)
            out, err = p.communicate()
            commit_b = out.strip()

            p = subprocess.Popen(['git', 'rev-parse', commit_a],
                                 stdout=subprocess.PIPE)
            out, err = p.communicate()
            commit_a = out.strip()

            cmd = [os.path.join(ROOT_DIR, 'benchmarks', 'run.py'),
                   'continuous', '-e', '-f', '1.05',
                   commit_a, commit_b] + bench_args
            os.execv(sys.executable, [sys.executable] + cmd)
            sys.exit(1)

    if args.build_only:
        sys.exit(0)
    else:
        __import__(PROJECT_MODULE)
        test = sys.modules[PROJECT_MODULE].test

    if args.submodule:
        tests = [PROJECT_MODULE + "." + args.submodule]
    elif args.tests:
        tests = args.tests
    else:
        tests = None

    # Run the tests

    if not args.no_build:
        test_dir = site_dir
    else:
        test_dir = os.path.join(ROOT_DIR, 'build', 'test')
        if not os.path.isdir(test_dir):
            os.makedirs(test_dir)

    shutil.copyfile(os.path.join(ROOT_DIR, '.coveragerc'),
                    os.path.join(test_dir, '.coveragerc'))

    cwd = os.getcwd()
    try:
        os.chdir(test_dir)
        result = test(args.mode,
                      verbose=args.verbose,
                      extra_argv=extra_argv,
                      doctests=args.doctests,
                      coverage=args.coverage,
                      tests=tests)
    finally:
        os.chdir(cwd)

    if isinstance(result, bool):
        sys.exit(0 if result else 1)
    elif result.wasSuccessful():
        sys.exit(0)
    else:
        sys.exit(1)
def main(argv):
    parser = ArgumentParser(usage=__doc__.lstrip())
    parser.add_argument("--verbose",
                        "-v",
                        action="count",
                        default=1,
                        help="more verbosity")
    parser.add_argument(
        "--no-build",
        "-n",
        action="store_true",
        default=False,
        help="do not build the project (use system installed version)")
    parser.add_argument("--build-only",
                        "-b",
                        action="store_true",
                        default=False,
                        help="just build, do not run any tests")
    parser.add_argument("--doctests",
                        action="store_true",
                        default=False,
                        help="Run doctests in module")
    parser.add_argument(
        "--coverage",
        action="store_true",
        default=False,
        help=("report coverage of project code. HTML output goes "
              "under build/coverage"))
    parser.add_argument(
        "--gcov",
        action="store_true",
        default=False,
        help=("enable C code coverage via gcov (requires GCC). "
              "gcov output goes to build/**/*.gc*"))
    parser.add_argument("--lcov-html",
                        action="store_true",
                        default=False,
                        help=("produce HTML for C code coverage information "
                              "from a previous run with --gcov. "
                              "HTML output goes to build/lcov/"))
    parser.add_argument("--mode",
                        "-m",
                        default="fast",
                        help="'fast', 'full', or something that could be "
                        "passed to nosetests -A [default: fast]")
    parser.add_argument(
        "--submodule",
        "-s",
        default=None,
        help="Submodule whose tests to run (cluster, constants, ...)")
    parser.add_argument("--pythonpath",
                        "-p",
                        default=None,
                        help="Paths to prepend to PYTHONPATH")
    parser.add_argument("--tests",
                        "-t",
                        action='append',
                        help="Specify tests to run")
    parser.add_argument("--python",
                        action="store_true",
                        help="Start a Python shell with PYTHONPATH set")
    parser.add_argument("--ipython",
                        "-i",
                        action="store_true",
                        help="Start IPython shell with PYTHONPATH set")
    parser.add_argument("--shell",
                        action="store_true",
                        help="Start Unix shell with PYTHONPATH set")
    parser.add_argument("--debug",
                        "-g",
                        action="store_true",
                        help="Debug build")
    parser.add_argument("--show-build-log",
                        action="store_true",
                        help="Show build output rather than using a log file")
    parser.add_argument("--bench",
                        action="store_true",
                        help="Run benchmark suite instead of test suite")
    parser.add_argument("args",
                        metavar="ARGS",
                        default=[],
                        nargs=REMAINDER,
                        help="Arguments to pass to Nose, Python or shell")
    args = parser.parse_args(argv)

    if args.lcov_html:
        # generate C code coverage output
        lcov_generate()
        sys.exit(0)

    if args.pythonpath:
        for p in reversed(args.pythonpath.split(os.pathsep)):
            sys.path.insert(0, p)

    if args.gcov:
        gcov_reset_counters()

    if not args.no_build:
        site_dir = build_project(args)
        sys.path.insert(0, site_dir)
        os.environ['PYTHONPATH'] = site_dir

    extra_argv = args.args[:]
    if extra_argv and extra_argv[0] == '--':
        extra_argv = extra_argv[1:]

    if args.python:
        if extra_argv:
            # Don't use subprocess, since we don't want to include the
            # current path in PYTHONPATH.
            sys.argv = extra_argv
            with open(extra_argv[0], 'r') as f:
                script = f.read()
            sys.modules['__main__'] = new_module('__main__')
            ns = dict(__name__='__main__', __file__=extra_argv[0])
            exec_(script, ns)
            sys.exit(0)
        else:
            import code
            code.interact()
            sys.exit(0)

    if args.ipython:
        import IPython
        IPython.embed(user_ns={})
        sys.exit(0)

    if args.shell:
        shell = os.environ.get('SHELL', 'sh')
        print("Spawning a Unix shell...")
        os.execv(shell, [shell] + extra_argv)
        sys.exit(1)

    if args.coverage:
        dst_dir = os.path.join(ROOT_DIR, 'build', 'coverage')
        fn = os.path.join(dst_dir, 'coverage_html.js')
        if os.path.isdir(dst_dir) and os.path.isfile(fn):
            shutil.rmtree(dst_dir)
        extra_argv += ['--cover-html', '--cover-html-dir=' + dst_dir]

    test_dir = os.path.join(ROOT_DIR, 'build', 'test')

    if args.build_only:
        sys.exit(0)
    elif args.submodule:
        modname = PROJECT_MODULE + '.' + args.submodule
        try:
            __import__(modname)
            if args.bench:
                test = sys.modules[modname].bench
            else:
                test = sys.modules[modname].test
        except (ImportError, KeyError, AttributeError) as e:
            print("Cannot run tests for %s (%s)" % (modname, e))
            sys.exit(2)
    elif args.tests:

        def fix_test_path(x):
            # fix up test path
            p = x.split(':')
            p[0] = os.path.relpath(os.path.abspath(p[0]), test_dir)
            return ':'.join(p)

        tests = [fix_test_path(x) for x in args.tests]

        def test(*a, **kw):
            extra_argv = kw.pop('extra_argv', ())
            extra_argv = extra_argv + tests[1:]
            kw['extra_argv'] = extra_argv
            from numpy.testing import Tester
            if args.bench:
                return Tester(tests[0]).bench(*a, **kw)
            else:
                return Tester(tests[0]).test(*a, **kw)
    else:
        __import__(PROJECT_MODULE)
        if args.bench:
            test = sys.modules[PROJECT_MODULE].bench
        else:
            test = sys.modules[PROJECT_MODULE].test

    # Run the tests under build/test
    try:
        shutil.rmtree(test_dir)
    except OSError:
        pass
    try:
        os.makedirs(test_dir)
    except OSError:
        pass

    shutil.copyfile(os.path.join(ROOT_DIR, '.coveragerc'),
                    os.path.join(test_dir, '.coveragerc'))

    cwd = os.getcwd()
    try:
        os.chdir(test_dir)
        if args.bench:
            result = test(args.mode,
                          verbose=args.verbose,
                          extra_argv=extra_argv)
        else:
            result = test(args.mode,
                          verbose=args.verbose,
                          extra_argv=extra_argv,
                          doctests=args.doctests,
                          coverage=args.coverage)
    finally:
        os.chdir(cwd)

    if isinstance(result, bool):
        sys.exit(0 if result else 1)
    elif result.wasSuccessful():
        sys.exit(0)
    else:
        sys.exit(1)