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