10 from pathlib
import Path
13 fix_test_name
= functools
.partial(re
.compile(r
'IE(_all|_\d+)?$').sub
, r
'\1')
17 parser
= argparse
.ArgumentParser(description
='Run selected yt-dlp tests')
19 'test', help='a extractor tests, or one of "core" or "download"', nargs
='*')
21 '-k', help='run a test matching EXPRESSION. Same as "pytest -k"', metavar
='EXPRESSION')
23 '--pytest-args', help='arguments to passthrough to pytest')
24 return parser
.parse_args()
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
))
32 pytest_args
= args
.pytest_args
or os
.getenv('HATCH_TEST_ARGS', '')
33 arguments
= ['pytest', '-Werror', '--tb=short', *shlex
.split(pytest_args
)]
35 arguments
.append('--color=yes')
37 arguments
.extend(['-k', pattern
])
39 arguments
.extend(['-m', 'not download'])
41 arguments
.extend(['-m', 'download'])
44 f
'test/test_download.py::TestDownload::test_{test}' for test
in tests
)
46 print(f
'Running {arguments}', flush
=True)
48 return subprocess
.call(arguments
)
49 except FileNotFoundError
:
52 arguments
= [sys
.executable
, '-Werror', '-m', 'unittest']
54 arguments
.extend(['-k', pattern
])
56 print('"pytest" needs to be installed to run core tests', file=sys
.stderr
, flush
=True)
59 arguments
.append('test.test_download')
62 f
'test.test_download.TestDownload.test_{test}' for test
in tests
)
64 print(f
'Running {arguments}', flush
=True)
65 return subprocess
.call(arguments
)
68 if __name__
== '__main__':
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: