diff --git a/tests/test_config.py b/tests/test_config.py index 9aa5518..c181f7d 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -34,6 +34,7 @@ known_cli_path, load_config, ) +from th_cli.exceptions import ConfigurationError # --------------------------------------------------------------------------- @@ -231,19 +232,19 @@ def test_falls_back_to_defaults_when_no_files(self, tmp_path): assert isinstance(result, Config) assert result.hostname == "localhost" - def test_skips_malformed_config_json(self, tmp_path): + def test_raises_on_malformed_config_json(self, tmp_path): config_file = tmp_path / "config.json" config_file.write_text("{this is: not valid json}") good_dir = tmp_path / "good" good_dir.mkdir() - good_config = good_dir / "config.json" - good_config.write_text(json.dumps({"hostname": "goodserver"})) + (good_dir / "config.json").write_text(json.dumps({"hostname": "goodserver"})) with patch("th_cli.config.get_config_search_paths", return_value=[tmp_path, good_dir]): - result = load_config() + with pytest.raises(ConfigurationError) as exc_info: + load_config() - assert isinstance(result, Config) + assert str(config_file) in exc_info.value.message def test_handles_config_json_with_comments_in_example(self, tmp_path): """config.json.example may have comment lines (# ...).""" diff --git a/th_cli/config.py b/th_cli/config.py index ff5ade6..f9bda62 100644 --- a/th_cli/config.py +++ b/th_cli/config.py @@ -14,12 +14,13 @@ # limitations under the License. # import json +import sys from enum import Enum from pathlib import Path from pydantic import BaseModel -from th_cli.exceptions import CLIError +from th_cli.exceptions import ConfigurationError def known_cli_path() -> Path: @@ -120,8 +121,7 @@ def load_config(): try: return Config.model_validate_json(config_path.read_text(encoding="utf-8")) except Exception as e: - CLIError(f"Could not load config from {config_path}: {e}") - continue + raise ConfigurationError(f"Could not load config from {config_path}: {e}") from e # Try to create config from example file example_locations = [] @@ -147,7 +147,12 @@ def load_config(): # Load the configuration -config = load_config() +try: + config = load_config() +except ConfigurationError as e: + # Raised at import time, before click is running, so report it like click would + e.show() + sys.exit(e.exit_code) class PairingMode(str, Enum):