fix(cli): only consume --black-box value when it is a directory
This commit is contained in:
+36
-22
@@ -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 <dir>`:仅当后一个 token 是「已存在的目录」且非 COBOL 源码后缀时
|
||||||
|
才消费为目录;否则视为裸 `--black-box`(自动探测 <outdir>/<PGM>/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():
|
def main():
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
print("用法: python -m cobol_testgen <cobol文件1> [cobol文件2 ...] [输出目录]")
|
print("用法: python -m cobol_testgen <cobol文件1> [cobol文件2 ...] [输出目录]")
|
||||||
@@ -1411,28 +1446,7 @@ def main():
|
|||||||
logger.warning("--gcov: runner.py not found. Compile/run will be skipped. "
|
logger.warning("--gcov: runner.py not found. Compile/run will be skipped. "
|
||||||
"Use --gcov without runner only generates test data + static coverage.")
|
"Use --gcov without runner only generates test data + static coverage.")
|
||||||
|
|
||||||
do_black_box = False
|
do_black_box, black_box_arg = _extract_black_box_args(args)
|
||||||
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
|
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(args):
|
while i < len(args):
|
||||||
|
|||||||
@@ -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"]
|
||||||
Reference in New Issue
Block a user