]> jfr.im git - yt-dlp.git/blame - devscripts/run_tests.py
[devscripts] `tomlparse`: Add makeshift toml parser
[yt-dlp.git] / devscripts / run_tests.py
CommitLineData
2d1d683a
SS
1#!/usr/bin/env python3
2
3import argparse
4import functools
5import os
6import re
7import subprocess
8import sys
9from pathlib import Path
10
11
12fix_test_name = functools.partial(re.compile(r'IE(_all|_\d+)?$').sub, r'\1')
13
14
15def 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
225cf2b8 24def run_tests(*tests, pattern=None, ci=False):
2d1d683a
SS
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
225cf2b8
SS
29 arguments = ['pytest', '-Werror', '--tb=short']
30 if ci:
31 arguments.append('--color=yes')
2d1d683a
SS
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
225cf2b8 42 print(f'Running {arguments}', flush=True)
2d1d683a 43 try:
225cf2b8 44 return subprocess.call(arguments)
2d1d683a
SS
45 except FileNotFoundError:
46 pass
47
48 arguments = [sys.executable, '-Werror', '-m', 'unittest']
49 if run_core:
225cf2b8
SS
50 print('"pytest" needs to be installed to run core tests', file=sys.stderr, flush=True)
51 return 1
2d1d683a
SS
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
225cf2b8
SS
60 print(f'Running {arguments}', flush=True)
61 return subprocess.call(arguments)
2d1d683a
SS
62
63
64if __name__ == '__main__':
65 try:
66 args = parse_args()
67
68 os.chdir(Path(__file__).parent.parent)
225cf2b8 69 sys.exit(run_tests(*args.test, pattern=args.k, ci=bool(os.getenv('CI'))))
2d1d683a
SS
70 except KeyboardInterrupt:
71 pass