From 11e4ae5af7e07e2de6e7be0e1788726333e5ca88 Mon Sep 17 00:00:00 2001 From: zhang_taoming <854317252@qq.com> Date: Sun, 13 Sep 2026 15:09:19 +0800 Subject: [PATCH] fix(cli): only consume --black-box value when it is a directory --- cobol_testgen/__init__.py | 58 +++++++++++++++--------- tests/cobol_testgen/test_cli_blackbox.py | 50 ++++++++++++++++++++ 2 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 tests/cobol_testgen/test_cli_blackbox.py diff --git a/cobol_testgen/__init__.py b/cobol_testgen/__init__.py index a7383cf..9772a9a 100644 --- a/cobol_testgen/__init__.py +++ b/cobol_testgen/__init__.py @@ -1391,6 +1391,41 @@ def _inject_c01_coverage_records(records, fields, base_assignments): # ── 入口 ── +def _extract_black_box_args(args: list) -> tuple[bool, str | None]: + """就地解析 --black-box / --no-black-box,返回 (do_black_box, black_box_arg)。 + + `--black-box `:仅当后一个 token 是「已存在的目录」且非 COBOL 源码后缀时 + 才消费为目录;否则视为裸 `--black-box`(自动探测 //black_box)。 + """ + do_black_box = False + black_box_arg = None + i = 0 + while i < len(args): + a = args[i] + if a == '--no-black-box': + do_black_box = False + args.pop(i) + continue + if a == '--black-box': + do_black_box = True + if i + 1 < len(args): + nxt = args[i + 1] + if (not nxt.startswith('--') + and Path(nxt).is_dir() + and Path(nxt).suffix.upper() not in ('.CBL', '.COB', '.CPY')): + black_box_arg = nxt + args.pop(i + 1) + args.pop(i) + continue + if a.startswith('--black-box='): + do_black_box = True + black_box_arg = a.split('=', 1)[1] + args.pop(i) + continue + i += 1 + return do_black_box, black_box_arg + + def main(): if len(sys.argv) < 2: print("用法: python -m cobol_testgen [cobol文件2 ...] [输出目录]") @@ -1411,28 +1446,7 @@ def main(): logger.warning("--gcov: runner.py not found. Compile/run will be skipped. " "Use --gcov without runner only generates test data + static coverage.") - do_black_box = False - black_box_arg = None - i = 0 - while i < len(args): - a = args[i] - if a == '--no-black-box': - do_black_box = False - args.pop(i) - continue - if a == '--black-box': - do_black_box = True - if i + 1 < len(args) and not args[i + 1].startswith('--'): - black_box_arg = args[i + 1] - args.pop(i + 1) - args.pop(i) - continue - if a.startswith('--black-box='): - do_black_box = True - black_box_arg = a.split('=', 1)[1] - args.pop(i) - continue - i += 1 + do_black_box, black_box_arg = _extract_black_box_args(args) i = 0 while i < len(args): diff --git a/tests/cobol_testgen/test_cli_blackbox.py b/tests/cobol_testgen/test_cli_blackbox.py new file mode 100644 index 0000000..1ce7e80 --- /dev/null +++ b/tests/cobol_testgen/test_cli_blackbox.py @@ -0,0 +1,50 @@ +"""cobol_testgen --black-box CLI 参数解析测试""" + +import os +import sys + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))) + +from cobol_testgen import _extract_black_box_args + + +def test_black_box_dir_consumed(tmp_path): + d = tmp_path / "prog" + d.mkdir() + args = ["--black-box", str(d), "src.cbl", "out"] + do, arg = _extract_black_box_args(args) + assert do is True + assert arg == str(d) + assert args == ["src.cbl", "out"] + + +def test_black_box_bare_does_not_consume_source(): + args = ["--black-box", "src.cbl", "out"] + do, arg = _extract_black_box_args(args) + assert do is True + assert arg is None + assert args == ["src.cbl", "out"] + + +def test_black_box_equals_form(): + args = ["--black-box=bbdir", "src.cbl"] + do, arg = _extract_black_box_args(args) + assert do is True + assert arg == "bbdir" + assert args == ["src.cbl"] + + +def test_no_black_box(): + args = ["--no-black-box", "src.cbl"] + do, arg = _extract_black_box_args(args) + assert do is False + assert arg is None + assert args == ["src.cbl"] + + +def test_no_flags_leaves_args_untouched(): + args = ["src.cbl", "out"] + do, arg = _extract_black_box_args(args) + assert do is False + assert arg is None + assert args == ["src.cbl", "out"]