File: run_tests.py

package info (click to toggle)
yt-dlp 2025.12.08-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 14,588 kB
  • sloc: python: 219,687; javascript: 556; makefile: 220; sh: 96
file content (97 lines) | stat: -rwxr-xr-x 2,923 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3

import argparse
import functools
import os
import re
import shlex
import subprocess
import sys
from pathlib import Path


fix_test_name = functools.partial(re.compile(r'IE(_all|_\d+)?$').sub, r'\1')


def parse_args():
    parser = argparse.ArgumentParser(description='Run selected yt-dlp tests')
    parser.add_argument(
        'test', help='an extractor test, test path, or one of "core" or "download"', nargs='*')
    parser.add_argument(
        '--flaky',
        action='store_true',
        default=None,
        help='Allow running flaky tests. (default: run, unless in CI)',
    )
    parser.add_argument(
        '--no-flaky',
        action='store_false',
        dest='flaky',
        help=argparse.SUPPRESS,
    )
    parser.add_argument(
        '-k', help='run a test matching EXPRESSION. Same as "pytest -k"', metavar='EXPRESSION')
    parser.add_argument(
        '--pytest-args', help='arguments to passthrough to pytest')
    return parser.parse_args()


def run_tests(*tests, pattern=None, ci=False, flaky: bool | None = None):
    # XXX: hatch uses `tests` if no arguments are passed
    run_core = 'core' in tests or 'tests' in tests or (not pattern and not tests)
    run_download = 'download' in tests
    run_flaky = flaky or (flaky is None and not ci)

    pytest_args = args.pytest_args or os.getenv('HATCH_TEST_ARGS', '')
    arguments = ['pytest', '-Werror', '--tb=short', *shlex.split(pytest_args)]
    if ci:
        arguments.append('--color=yes')
    if pattern:
        arguments.extend(['-k', pattern])
    if run_core:
        arguments.extend(['-m', 'not download'])
    elif run_download:
        arguments.extend(['-m', 'download'])
    else:
        arguments.extend(
            test if '/' in test
            else f'test/test_download.py::TestDownload::test_{fix_test_name(test)}'
            for test in tests)
    if not run_flaky:
        arguments.append('--disallow-flaky')

    print(f'Running {arguments}', flush=True)
    try:
        return subprocess.call(arguments)
    except FileNotFoundError:
        pass

    arguments = [sys.executable, '-Werror', '-m', 'unittest']
    if pattern:
        arguments.extend(['-k', pattern])
    if run_core:
        print('"pytest" needs to be installed to run core tests', file=sys.stderr, flush=True)
        return 1
    elif run_download:
        arguments.append('test.test_download')
    else:
        arguments.extend(
            f'test.test_download.TestDownload.test_{test}' for test in tests)

    print(f'Running {arguments}', flush=True)
    return subprocess.call(arguments)


if __name__ == '__main__':
    try:
        args = parse_args()

        os.chdir(Path(__file__).parent.parent)
        sys.exit(run_tests(
            *args.test,
            pattern=args.k,
            ci=bool(os.getenv('CI')),
            flaky=args.flaky,
        ))
    except KeyboardInterrupt:
        pass