Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

cli: harden config parsing and isolate CLI test logging

Sameer6305c447bf5934fe
diff --git a/semantica/cli.py b/semantica/cli.py
index fbc4ace5..176622ba 100644
--- a/semantica/cli.py
+++ b/semantica/cli.py
@@ -70,6 +70,9 @@ def _load_config_data(file_path: Path) -> Dict[str, Any]:
f"Failed to parse configuration file '{file_path}': {exc}"
) from exc
+ if config_data is None:
+ config_data = {}
+
if not isinstance(config_data, dict):
raise click.ClickException(
"Configuration file must contain a mapping/object at the root."
@@ -90,8 +93,18 @@ def _build_runtime_config(
else:
config_data = {}
+ logging_config = config_data.get("logging")
+ if logging_config is not None and not isinstance(logging_config, dict):
+ raise click.ClickException(
+ "Logging configuration section must contain a mapping/object."
+ )
+
if log_level:
- config_data.setdefault("logging", {})["level"] = log_level.upper()
+ if logging_config is None:
+ logging_config = {}
+ config_data["logging"] = logging_config
+
+ logging_config["level"] = log_level.upper()
# Keep validation disabled at CLI bootstrap to avoid blocking unrelated commands.
return config_manager.load_from_dict(config_data, validate=False)
diff --git a/tests/test_cli_foundation.py b/tests/test_cli_foundation.py
index 3401b86b..e3bd69cb 100644
--- a/tests/test_cli_foundation.py
+++ b/tests/test_cli_foundation.py
@@ -9,6 +9,11 @@ def runner():
return CliRunner()
[email protected](autouse=True)
+def disable_cli_logging(monkeypatch):
+ monkeypatch.setattr(cli_module, "setup_logging", lambda *args, **kwargs: None)
+
+
def test_root_help_shows_expected_groups(runner):
result = runner.invoke(cli_module.main, ["--help"])
@@ -156,6 +161,34 @@ def test_invalid_command_config_error_is_clean_and_click_safe(runner):
assert "Traceback" not in result.output
+def test_empty_yaml_config_is_accepted(runner):
+ with runner.isolated_filesystem():
+ with open("cfg.yml", "w", encoding="utf-8") as handle:
+ handle.write("")
+
+ result = runner.invoke(cli_module.main, ["--config", "cfg.yml", "info"])
+
+ assert result.exit_code == 0
+
+
+def test_malformed_logging_section_is_clean_and_click_safe(runner):
+ with runner.isolated_filesystem():
+ with open("cfg.yml", "w", encoding="utf-8") as handle:
+ handle.write("logging: []\n")
+
+ result = runner.invoke(
+ cli_module.main,
+ ["--config", "cfg.yml", "--log-level", "INFO", "info"],
+ )
+
+ assert result.exit_code != 0
+ assert (
+ "Logging configuration section must contain a mapping/object"
+ in result.output
+ )
+ assert "Traceback" not in result.output
+
+
@pytest.mark.parametrize(
"file_name, config_text, expected_error",
[