From 271b7e7844ea9e4f2884395d3c9f056eaffd37a1 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 2 Oct 2025 21:29:45 +0200 Subject: [PATCH] Allow selection of which tests to run on command line invocation. --- src/run.py | 49 ++++++++++++++++----- src/tests/{pingpong.test => _pingpong.test} | 0 2 files changed, 38 insertions(+), 11 deletions(-) rename src/tests/{pingpong.test => _pingpong.test} (100%) diff --git a/src/run.py b/src/run.py index 3d0d4fe..2b17f64 100755 --- a/src/run.py +++ b/src/run.py @@ -2,6 +2,7 @@ 'Attempt at an IRC client.' # standard libs +from pathlib import Path from queue import SimpleQueue from sys import argv, exit as sys_exit # non-standard libs @@ -42,15 +43,41 @@ def main_loop(cls_term: type[TerminalInterface], cls_tui: type[BaseTui] if __name__ == '__main__': - if len(argv) == 1: - main_loop(Terminal, ClientTui) - elif len(argv) == 2 and argv[1] == 'test': - for path in PATH_TESTS.iterdir(): - if path.parts[-1].endswith('.toml'): - continue - print(f'running test: {path}') - main_loop(TestTerminal, TestingClientTui.on_file(path)) - print('(success!)') - else: - print(f'unrecognized argument(s): {argv[1:]}') + class _HandledException(BaseException): + 'To fail with only passed message rather than stack listing' + + try: + _ARG_TEST = 'test' + _ARG_TEST_SEP = ':' + if len(argv) == 1: + main_loop(Terminal, ClientTui) + elif len(argv) == 2\ + and (argv[1] == _ARG_TEST + or argv[1].startswith(_ARG_TEST + _ARG_TEST_SEP)): + _EXT_TEST = '.test' + collected_paths: list[Path] = [] + candidates = [path for path in PATH_TESTS.iterdir() + if path.parts[-1].endswith(_EXT_TEST)] + _ARG_TEST_ALL = '*' + selector = (_ARG_TEST_ALL if argv[1] == _ARG_TEST + else argv[1].split(_ARG_TEST_SEP, maxsplit=1)[1]) + if selector == '': + collected_paths = [path for path in candidates + if not path.parts[-1].startswith('_')] + elif selector == _ARG_TEST_ALL: + collected_paths = candidates[:] + else: + collected_paths = [path for path in candidates + if path.parts[-1].startswith(selector)] + if not collected_paths: + raise _HandledException( + f'no test files in {PATH_TESTS} matching [{selector}]') + for path in collected_paths: + print(f'running test: {path}') + main_loop(TestTerminal, TestingClientTui.on_file(path)) + print('(success!)') + else: + raise _HandledException(f'unrecognized argument(s): {argv[1:]}') + except _HandledException as e: + print(e) sys_exit(1) diff --git a/src/tests/pingpong.test b/src/tests/_pingpong.test similarity index 100% rename from src/tests/pingpong.test rename to src/tests/_pingpong.test -- 2.30.2