From 41e237ef456abcd8ef04e750b73fd0575fdcdcf3 Mon Sep 17 00:00:00 2001 From: dipakchaudhari12717 Date: Wed, 22 Jul 2026 17:11:53 +0530 Subject: [PATCH] Validate --k and --seed in the CLI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --k and --seed were parsed as unbounded ints, so `leakgauge --k 0` ran zero seeded repeats (a degenerate, empty result) and negative values were accepted too — both failing confusingly downstream. Reject --k < 1 and --seed < 0 in _run with a clear message naming the flag, matching the existing --max-steps check (stderr message + return 2, nothing written). Add test_cli_validation.py covering the rejections and a valid run. Closes #6 --- src/leakgauge/cli.py | 8 ++++++++ tests/test_cli_validation.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/test_cli_validation.py diff --git a/src/leakgauge/cli.py b/src/leakgauge/cli.py index cd5ae02..8e3860d 100644 --- a/src/leakgauge/cli.py +++ b/src/leakgauge/cli.py @@ -126,6 +126,14 @@ def _run(argv: list[str]) -> int: print(f"[leakgauge] --max-steps must be >= 1 (got {args.max_steps})", file=sys.stderr) return 2 + if args.k < 1: + print(f"[leakgauge] --k must be >= 1 (got {args.k})", file=sys.stderr) + return 2 + + if args.seed < 0: + print(f"[leakgauge] --seed must be >= 0 (got {args.seed})", file=sys.stderr) + return 2 + selected: list[Case] | None = None suite_label = args.suite if args.case is not None: diff --git a/tests/test_cli_validation.py b/tests/test_cli_validation.py new file mode 100644 index 0000000..2e230ad --- /dev/null +++ b/tests/test_cli_validation.py @@ -0,0 +1,33 @@ +"""--k and --seed are validated before a run starts, so a degenerate value +(zero repeats, negative seed) fails with a clear message instead of a confusing +empty run downstream.""" + +from __future__ import annotations + +from pathlib import Path + +from leakgauge.cli import main + + +def test_k_zero_errors_cleanly(tmp_path: Path) -> None: + rc = main(["--k", "0", "--results-dir", str(tmp_path)]) + assert rc == 2 + assert not (tmp_path / "stub_demo.json").exists() # nothing written on error + + +def test_k_negative_errors_cleanly(tmp_path: Path) -> None: + rc = main(["--k", "-1", "--results-dir", str(tmp_path)]) + assert rc == 2 + assert not (tmp_path / "stub_demo.json").exists() + + +def test_seed_negative_errors_cleanly(tmp_path: Path) -> None: + rc = main(["--seed", "-1", "--results-dir", str(tmp_path)]) + assert rc == 2 + assert not (tmp_path / "stub_demo.json").exists() + + +def test_valid_k_and_seed_still_run(tmp_path: Path) -> None: + rc = main(["--k", "2", "--seed", "0", "--results-dir", str(tmp_path)]) + assert rc == 0 + assert (tmp_path / "stub_demo.json").exists()