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