Skip to content

Commit de1ae05

Browse files
committed
fix(cli): exit on --version and --help before side effects
1 parent 20e845c commit de1ae05

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎CodeEntropy/cli.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
correlation entropy workflow.
55
66
The entry point is intentionally small and only responsible for:
7-
1) Creating a job folder.
8-
2) Constructing a CodeEntropyRunner.
9-
3) Executing the entropy workflow.
10-
4) Handling fatal errors with a non-zero exit code.
7+
1) Handling informational flags such as ``--version``.
8+
2) Creating a job folder.
9+
3) Constructing a CodeEntropyRunner.
10+
4) Executing the entropy workflow.
11+
5) Handling fatal errors with a non-zero exit code.
1112
"""
1213

1314
from __future__ import annotations
1415

1516
import logging
1617

18+
from CodeEntropy.config.argparse import ConfigResolver
1719
from CodeEntropy.config.runtime import CodeEntropyRunner
1820

1921
logger = logging.getLogger(__name__)
@@ -22,9 +24,15 @@
2224
def main() -> None:
2325
"""Run the entropy workflow.
2426
27+
Arguments are parsed before any side effects so that informational flags such
28+
as ``--version`` and ``--help`` exit without creating a job folder or
29+
rendering the splash screen.
30+
2531
Raises:
2632
SystemExit: Exits with status code 1 on any unhandled exception.
2733
"""
34+
ConfigResolver().build_parser().parse_known_args()
35+
2836
folder = CodeEntropyRunner.create_job_folder()
2937

3038
try:

‎tests/unit/CodeEntropy/cli/test_cli.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,17 @@ def test_main_logs_and_exits_nonzero_on_exception(monkeypatch):
3636

3737
assert exc.value.code == 1
3838
fake_runner.run_entropy_workflow.assert_called_once_with()
39+
40+
41+
def test_main_version_flag_exits_before_creating_job_folder(monkeypatch, capsys):
42+
fake_runner_cls = MagicMock()
43+
monkeypatch.setattr(entry, "CodeEntropyRunner", fake_runner_cls)
44+
monkeypatch.setattr("sys.argv", ["CodeEntropy", "--version"])
45+
46+
with pytest.raises(SystemExit) as exc:
47+
entry.main()
48+
49+
assert exc.value.code == 0
50+
assert "CodeEntropy" in capsys.readouterr().out
51+
fake_runner_cls.create_job_folder.assert_not_called()
52+
fake_runner_cls.assert_not_called()

0 commit comments

Comments
 (0)