Release 2024.12.13
[yt-dlp.git] / devscripts / run_tests.py
blobeb614fe591abbe62a06e1f45c8c0c443c4b6c1a5
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='an extractor test, test path, 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
31 pytest_args = args.pytest_args or os.getenv('HATCH_TEST_ARGS', '')
32 arguments = ['pytest', '-Werror', '--tb=short', *shlex.split(pytest_args)]
33 if ci:
34 arguments.append('--color=yes')
35 if pattern:
36 arguments.extend(['-k', pattern])
37 if run_core:
38 arguments.extend(['-m', 'not download'])
39 elif run_download:
40 arguments.extend(['-m', 'download'])
41 else:
42 arguments.extend(
43 test if '/' in test
44 else f'test/test_download.py::TestDownload::test_{fix_test_name(test)}'
45 for test in tests)
47 print(f'Running {arguments}', flush=True)
48 try:
49 return subprocess.call(arguments)
50 except FileNotFoundError:
51 pass
53 arguments = [sys.executable, '-Werror', '-m', 'unittest']
54 if pattern:
55 arguments.extend(['-k', pattern])
56 if run_core:
57 print('"pytest" needs to be installed to run core tests', file=sys.stderr, flush=True)
58 return 1
59 elif run_download:
60 arguments.append('test.test_download')
61 else:
62 arguments.extend(
63 f'test.test_download.TestDownload.test_{test}' for test in tests)
65 print(f'Running {arguments}', flush=True)
66 return subprocess.call(arguments)
69 if __name__ == '__main__':
70 try:
71 args = parse_args()
73 os.chdir(Path(__file__).parent.parent)
74 sys.exit(run_tests(*args.test, pattern=args.k, ci=bool(os.getenv('CI'))))
75 except KeyboardInterrupt:
76 pass