]> jfr.im git - yt-dlp.git/blob - devscripts/run_tests.py
[ie/brightcove] Upgrade requests to HTTPS (#10202)
[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 shlex
8 import subprocess
9 import sys
10 from pathlib import Path
11
12
13 fix_test_name = functools.partial(re.compile(r'IE(_all|_\d+)?$').sub, r'\1')
14
15
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()
25
26
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))
31
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)
45
46 print(f'Running {arguments}', flush=True)
47 try:
48 return subprocess.call(arguments)
49 except FileNotFoundError:
50 pass
51
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)
63
64 print(f'Running {arguments}', flush=True)
65 return subprocess.call(arguments)
66
67
68 if __name__ == '__main__':
69 try:
70 args = parse_args()
71
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