diff --git a/src/transpose/transpose.py b/src/transpose/transpose.py index eec06ce..f6d424c 100644 --- a/src/transpose/transpose.py +++ b/src/transpose/transpose.py @@ -85,7 +85,11 @@ class TransposeConfig: @staticmethod def load(config_path: str): # -> Self: - in_config = json.load(open(config_path, "r")) + try: + in_config = json.load(open(config_path, "r")) + except json.decoder.JSONDecodeError as e: + raise TransposeError(f"Invalid JSON format for '{config_path}': {e}") + config = TransposeConfig() try: for name in in_config["entries"]: diff --git a/tests/test_transpose.py b/tests/test_transpose.py index f4f97eb..0ad3784 100644 --- a/tests/test_transpose.py +++ b/tests/test_transpose.py @@ -191,3 +191,11 @@ def test_config_load(): config.entries[ENTRY_NAME].path == TRANSPOSE_CONFIG["entries"][ENTRY_NAME]["path"] ) + + with pytest.raises( + TransposeError, match="Unrecognized Transpose config file format" + ): + config = TransposeConfig.load(STORE_PATH.joinpath("transpose-bad.json")) + + with pytest.raises(TransposeError, match="Invalid JSON format"): + config = TransposeConfig.load(STORE_PATH.joinpath("transpose-invalid.json")) diff --git a/tests/utils.py b/tests/utils.py index 2f44f61..0481918 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -79,6 +79,8 @@ def setup_store(): tests-temp/ ├── source/ └── store/ + │ ├── transpose-bad.json + │ ├── transpose-invalid.json └── transpose.json """ try: @@ -89,6 +91,18 @@ def setup_store(): with open(str(TRANSPOSE_CONFIG_PATH), "w") as f: json.dump(TRANSPOSE_CONFIG, f) + bad_config_path = STORE_PATH.joinpath("transpose-bad.json") + bad_config = '{"version": "1.0.0"}' # Missing entries + with open(str(bad_config_path), "w") as f: + f.write(bad_config) + assert bad_config_path.exists() + + invalid_config_path = STORE_PATH.joinpath("transpose-invalid.json") + invalid_config = "[{'invalidJSONFormat'}]" + with open(str(invalid_config_path), "w") as f: + f.write(invalid_config) + assert invalid_config_path.exists() + yield finally: # This shouldn't be necessary but is for some reason