run_tests.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. import argparse
  3. import functools
  4. import os
  5. import re
  6. import subprocess
  7. import sys
  8. from pathlib import Path
  9. fix_test_name = functools.partial(re.compile(r'IE(_all|_\d+)?$').sub, r'\1')
  10. def parse_args():
  11. parser = argparse.ArgumentParser(description='Run selected yt-dlp tests')
  12. parser.add_argument(
  13. 'test', help='a extractor tests, or one of "core" or "download"', nargs='*')
  14. parser.add_argument(
  15. '-k', help='run a test matching EXPRESSION. Same as "pytest -k"', metavar='EXPRESSION')
  16. return parser.parse_args()
  17. def run_tests(*tests, pattern=None, ci=False):
  18. run_core = 'core' in tests or (not pattern and not tests)
  19. run_download = 'download' in tests
  20. tests = list(map(fix_test_name, tests))
  21. arguments = ['pytest', '-Werror', '--tb=short']
  22. if ci:
  23. arguments.append('--color=yes')
  24. if run_core:
  25. arguments.extend(['-m', 'not download'])
  26. elif run_download:
  27. arguments.extend(['-m', 'download'])
  28. elif pattern:
  29. arguments.extend(['-k', pattern])
  30. else:
  31. arguments.extend(
  32. f'test/test_download.py::TestDownload::test_{test}' for test in tests)
  33. print(f'Running {arguments}', flush=True)
  34. try:
  35. return subprocess.call(arguments)
  36. except FileNotFoundError:
  37. pass
  38. arguments = [sys.executable, '-Werror', '-m', 'unittest']
  39. if run_core:
  40. print('"pytest" needs to be installed to run core tests', file=sys.stderr, flush=True)
  41. return 1
  42. elif run_download:
  43. arguments.append('test.test_download')
  44. elif pattern:
  45. arguments.extend(['-k', pattern])
  46. else:
  47. arguments.extend(
  48. f'test.test_download.TestDownload.test_{test}' for test in tests)
  49. print(f'Running {arguments}', flush=True)
  50. return subprocess.call(arguments)
  51. if __name__ == '__main__':
  52. try:
  53. args = parse_args()
  54. os.chdir(Path(__file__).parent.parent)
  55. sys.exit(run_tests(*args.test, pattern=args.k, ci=bool(os.getenv('CI'))))
  56. except KeyboardInterrupt:
  57. pass