]> jfr.im git - yt-dlp.git/blob - devscripts/run_tests.py
b0c6ee67afd91b8b3d431de56e41ceba4047c877
[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):
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 run_core:
31 arguments.extend(['-m', 'not download'])
32 elif run_download:
33 arguments.extend(['-m', 'download'])
34 elif pattern:
35 arguments.extend(['-k', pattern])
36 else:
37 arguments.extend(
38 f'test/test_download.py::TestDownload::test_{test}' for test in tests)
39
40 print(f'Running {arguments}')
41 try:
42 subprocess.run(arguments)
43 return
44 except FileNotFoundError:
45 pass
46
47 arguments = [sys.executable, '-Werror', '-m', 'unittest']
48 if run_core:
49 print('"pytest" needs to be installed to run core tests', file=sys.stderr)
50 return
51 elif run_download:
52 arguments.append('test.test_download')
53 elif pattern:
54 arguments.extend(['-k', pattern])
55 else:
56 arguments.extend(
57 f'test.test_download.TestDownload.test_{test}' for test in tests)
58
59 print(f'Running {arguments}')
60 subprocess.run(arguments)
61
62
63 if __name__ == '__main__':
64 try:
65 args = parse_args()
66
67 os.chdir(Path(__file__).parent.parent)
68 run_tests(*args.test, pattern=args.k)
69 except KeyboardInterrupt:
70 pass