2023-12-26 17:30:04 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import functools
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
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='a extractor tests, or one of "core" or "download"', nargs='*')
|
|
|
|
parser.add_argument(
|
|
|
|
'-k', help='run a test matching EXPRESSION. Same as "pytest -k"', metavar='EXPRESSION')
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2023-12-26 18:55:30 +00:00
|
|
|
def run_tests(*tests, pattern=None, ci=False):
|
2023-12-26 17:30:04 +00:00
|
|
|
run_core = 'core' in tests or (not pattern and not tests)
|
|
|
|
run_download = 'download' in tests
|
|
|
|
tests = list(map(fix_test_name, tests))
|
|
|
|
|
2023-12-26 18:55:30 +00:00
|
|
|
arguments = ['pytest', '-Werror', '--tb=short']
|
|
|
|
if ci:
|
|
|
|
arguments.append('--color=yes')
|
2023-12-26 17:30:04 +00:00
|
|
|
if run_core:
|
|
|
|
arguments.extend(['-m', 'not download'])
|
|
|
|
elif run_download:
|
|
|
|
arguments.extend(['-m', 'download'])
|
|
|
|
elif pattern:
|
|
|
|
arguments.extend(['-k', pattern])
|
|
|
|
else:
|
|
|
|
arguments.extend(
|
|
|
|
f'test/test_download.py::TestDownload::test_{test}' for test in tests)
|
|
|
|
|
2023-12-26 18:55:30 +00:00
|
|
|
print(f'Running {arguments}', flush=True)
|
2023-12-26 17:30:04 +00:00
|
|
|
try:
|
2023-12-26 18:55:30 +00:00
|
|
|
return subprocess.call(arguments)
|
2023-12-26 17:30:04 +00:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
arguments = [sys.executable, '-Werror', '-m', 'unittest']
|
|
|
|
if run_core:
|
2023-12-26 18:55:30 +00:00
|
|
|
print('"pytest" needs to be installed to run core tests', file=sys.stderr, flush=True)
|
|
|
|
return 1
|
2023-12-26 17:30:04 +00:00
|
|
|
elif run_download:
|
|
|
|
arguments.append('test.test_download')
|
|
|
|
elif pattern:
|
|
|
|
arguments.extend(['-k', pattern])
|
|
|
|
else:
|
|
|
|
arguments.extend(
|
|
|
|
f'test.test_download.TestDownload.test_{test}' for test in tests)
|
|
|
|
|
2023-12-26 18:55:30 +00:00
|
|
|
print(f'Running {arguments}', flush=True)
|
|
|
|
return subprocess.call(arguments)
|
2023-12-26 17:30:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
args = parse_args()
|
|
|
|
|
|
|
|
os.chdir(Path(__file__).parent.parent)
|
2023-12-26 18:55:30 +00:00
|
|
|
sys.exit(run_tests(*args.test, pattern=args.k, ci=bool(os.getenv('CI'))))
|
2023-12-26 17:30:04 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|