| 1 |
#!/usr/bin/env python |
| 2 |
# Don't run tests from the root repo dir. |
| 3 |
# We want to ensure we're importing from the installed |
| 4 |
# binary package not from the CWD. |
| 5 |
|
| 6 |
import argparse |
| 7 |
import os |
| 8 |
from contextlib import contextmanager |
| 9 |
from subprocess import check_call |
| 10 |
|
| 11 |
_dname = os.path.dirname |
| 12 |
|
| 13 |
REPO_ROOT = _dname(_dname(_dname(os.path.abspath(__file__)))) |
| 14 |
PACKAGE = "s3transfer" |
| 15 |
|
| 16 |
|
| 17 |
@contextmanager |
| 18 |
def cd(path): |
| 19 |
"""Change directory while inside context manager.""" |
| 20 |
cwd = os.getcwd() |
| 21 |
try: |
| 22 |
os.chdir(path) |
| 23 |
yield |
| 24 |
finally: |
| 25 |
os.chdir(cwd) |
| 26 |
|
| 27 |
|
| 28 |
def run(command, env=None): |
| 29 |
return check_call(command, shell=True, env=env) |
| 30 |
|
| 31 |
|
| 32 |
def process_args(args): |
| 33 |
runner = args.test_runner |
| 34 |
test_args = "" |
| 35 |
if args.with_cov: |
| 36 |
test_args += f"--cov={PACKAGE} --cov-report xml " |
| 37 |
dirs = " ".join(args.test_dirs) |
| 38 |
|
| 39 |
return runner, test_args, dirs |
| 40 |
|
| 41 |
|
| 42 |
if __name__ == "__main__": |
| 43 |
parser = argparse.ArgumentParser() |
| 44 |
parser.add_argument( |
| 45 |
"test_dirs", |
| 46 |
default=["unit/", "functional/"], |
| 47 |
nargs="*", |
| 48 |
help="One or more directories containing tests.", |
| 49 |
) |
| 50 |
parser.add_argument( |
| 51 |
"-r", |
| 52 |
"--test-runner", |
| 53 |
default="pytest", |
| 54 |
help="Test runner to execute tests. Defaults to pytest.", |
| 55 |
) |
| 56 |
parser.add_argument( |
| 57 |
"-c", |
| 58 |
"--with-cov", |
| 59 |
default=False, |
| 60 |
action="store_true", |
| 61 |
help="Run default test-runner with code coverage enabled.", |
| 62 |
) |
| 63 |
raw_args = parser.parse_args() |
| 64 |
test_runner, test_args, test_dirs = process_args(raw_args) |
| 65 |
|
| 66 |
with cd(os.path.join(REPO_ROOT, "tests")): |
| 67 |
cmd = f"{test_runner} {test_args}{test_dirs}" |
| 68 |
print(f"Running {cmd}...") |
| 69 |
run(cmd) |
| 70 |
|
| 71 |
# Run the serial implementation of s3transfer |
| 72 |
os.environ["USE_SERIAL_EXECUTOR"] = "True" |
| 73 |
print(f"Running serial execution for {cmd}...") |
| 74 |
run(cmd, env=os.environ) |