File: run-tests

package info (click to toggle)
python-boto3 1.26.27%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,880 kB
  • sloc: python: 12,629; makefile: 128
file content (69 lines) | stat: -rwxr-xr-x 1,677 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# Don't run tests from the root repo dir.
# We want to ensure we're importing from the installed
# binary package not from the CWD.

import argparse
import os
from contextlib import contextmanager
from subprocess import check_call

_dname = os.path.dirname

REPO_ROOT = _dname(_dname(_dname(os.path.abspath(__file__))))
PACKAGE = "boto3"


@contextmanager
def cd(path):
    """Change directory while inside context manager."""
    cwd = os.getcwd()
    try:
        os.chdir(path)
        yield
    finally:
        os.chdir(cwd)


def run(command):
    return check_call(command, shell=True)


def process_args(args):
    runner = args.test_runner
    test_args = ""
    if args.with_cov:
        test_args += f"--cov={PACKAGE} --cov-report xml -v "
    dirs = " ".join(args.test_dirs)

    return runner, test_args, dirs


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "test_dirs",
        default=["unit/", "functional/"],
        nargs="*",
        help="One or more directories containing tests.",
    )
    parser.add_argument(
        "-r",
        "--test-runner",
        default="pytest",
        help="Test runner to execute tests. Defaults to pytest.",
    )
    parser.add_argument(
        "-c",
        "--with-cov",
        default=False,
        action="store_true",
        help="Run default test-runner with code coverage enabled.",
    )
    raw_args = parser.parse_args()
    test_runner, test_args, test_dirs = process_args(raw_args)

    cmd = f"{test_runner} {test_args}{test_dirs}"
    print(f"Running {cmd}...")
    with cd(os.path.join(REPO_ROOT, "tests")):
        run(cmd)