]>
Commit | Line | Data |
---|---|---|
2d1d683a SS |
1 | #!/usr/bin/env python3 |
2 | ||
3 | import argparse | |
4 | import functools | |
5 | import os | |
6 | import re | |
e897bd82 | 7 | import shlex |
2d1d683a SS |
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') | |
e897bd82 SS |
22 | parser.add_argument( |
23 | '--pytest-args', help='arguments to passthrough to pytest') | |
2d1d683a SS |
24 | return parser.parse_args() |
25 | ||
26 | ||
225cf2b8 | 27 | def run_tests(*tests, pattern=None, ci=False): |
2d1d683a SS |
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 | ||
e897bd82 SS |
32 | pytest_args = args.pytest_args or os.getenv('HATCH_TEST_ARGS', '') |
33 | arguments = ['pytest', '-Werror', '--tb=short', *shlex.split(pytest_args)] | |
225cf2b8 SS |
34 | if ci: |
35 | arguments.append('--color=yes') | |
e897bd82 SS |
36 | if pattern: |
37 | arguments.extend(['-k', pattern]) | |
2d1d683a SS |
38 | if run_core: |
39 | arguments.extend(['-m', 'not download']) | |
40 | elif run_download: | |
41 | arguments.extend(['-m', 'download']) | |
2d1d683a SS |
42 | else: |
43 | arguments.extend( | |
44 | f'test/test_download.py::TestDownload::test_{test}' for test in tests) | |
45 | ||
225cf2b8 | 46 | print(f'Running {arguments}', flush=True) |
2d1d683a | 47 | try: |
225cf2b8 | 48 | return subprocess.call(arguments) |
2d1d683a SS |
49 | except FileNotFoundError: |
50 | pass | |
51 | ||
52 | arguments = [sys.executable, '-Werror', '-m', 'unittest'] | |
e897bd82 SS |
53 | if pattern: |
54 | arguments.extend(['-k', pattern]) | |
2d1d683a | 55 | if run_core: |
225cf2b8 SS |
56 | print('"pytest" needs to be installed to run core tests', file=sys.stderr, flush=True) |
57 | return 1 | |
2d1d683a SS |
58 | elif run_download: |
59 | arguments.append('test.test_download') | |
2d1d683a SS |
60 | else: |
61 | arguments.extend( | |
62 | f'test.test_download.TestDownload.test_{test}' for test in tests) | |
63 | ||
225cf2b8 SS |
64 | print(f'Running {arguments}', flush=True) |
65 | return subprocess.call(arguments) | |
2d1d683a SS |
66 | |
67 | ||
68 | if __name__ == '__main__': | |
69 | try: | |
70 | args = parse_args() | |
71 | ||
72 | os.chdir(Path(__file__).parent.parent) | |
225cf2b8 | 73 | sys.exit(run_tests(*args.test, pattern=args.k, ci=bool(os.getenv('CI')))) |
2d1d683a SS |
74 | except KeyboardInterrupt: |
75 | pass |