run-tests 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. import argparse
  6. import os
  7. from contextlib import contextmanager
  8. from subprocess import check_call
  9. _dname = os.path.dirname
  10. REPO_ROOT = _dname(_dname(_dname(os.path.abspath(__file__))))
  11. PACKAGE = "s3transfer"
  12. @contextmanager
  13. def cd(path):
  14. """Change directory while inside context manager."""
  15. cwd = os.getcwd()
  16. try:
  17. os.chdir(path)
  18. yield
  19. finally:
  20. os.chdir(cwd)
  21. def run(command, env=None):
  22. return check_call(command, shell=True, env=env)
  23. def process_args(args):
  24. runner = args.test_runner
  25. test_args = ""
  26. if args.with_cov:
  27. test_args += f"--cov={PACKAGE} --cov-report xml "
  28. dirs = " ".join(args.test_dirs)
  29. return runner, test_args, dirs
  30. if __name__ == "__main__":
  31. parser = argparse.ArgumentParser()
  32. parser.add_argument(
  33. "test_dirs",
  34. default=["unit/", "functional/"],
  35. nargs="*",
  36. help="One or more directories containing tests.",
  37. )
  38. parser.add_argument(
  39. "-r",
  40. "--test-runner",
  41. default="pytest",
  42. help="Test runner to execute tests. Defaults to pytest.",
  43. )
  44. parser.add_argument(
  45. "-c",
  46. "--with-cov",
  47. default=False,
  48. action="store_true",
  49. help="Run default test-runner with code coverage enabled.",
  50. )
  51. raw_args = parser.parse_args()
  52. test_runner, test_args, test_dirs = process_args(raw_args)
  53. with cd(os.path.join(REPO_ROOT, "tests")):
  54. cmd = f"{test_runner} {test_args}{test_dirs}"
  55. print(f"Running {cmd}...")
  56. run(cmd)
  57. # Run the serial implementation of s3transfer
  58. os.environ["USE_SERIAL_EXECUTOR"] = "True"
  59. print(f"Running serial execution for {cmd}...")
  60. run(cmd, env=os.environ)