Browse Source

Adding a check for valid/invalid json config format

pull/11/head
Ryan Reed 1 year ago
parent
commit
2276126290
3 changed files with 27 additions and 1 deletions
  1. +5
    -1
      src/transpose/transpose.py
  2. +8
    -0
      tests/test_transpose.py
  3. +14
    -0
      tests/utils.py

+ 5
- 1
src/transpose/transpose.py View File

@ -85,7 +85,11 @@ class TransposeConfig:
@staticmethod @staticmethod
def load(config_path: str): # -> Self: 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() config = TransposeConfig()
try: try:
for name in in_config["entries"]: for name in in_config["entries"]:


+ 8
- 0
tests/test_transpose.py View File

@ -191,3 +191,11 @@ def test_config_load():
config.entries[ENTRY_NAME].path config.entries[ENTRY_NAME].path
== TRANSPOSE_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"))

+ 14
- 0
tests/utils.py View File

@ -79,6 +79,8 @@ def setup_store():
tests-temp/ tests-temp/
source/ source/
store/ store/
transpose-bad.json
transpose-invalid.json
transpose.json transpose.json
""" """
try: try:
@ -89,6 +91,18 @@ def setup_store():
with open(str(TRANSPOSE_CONFIG_PATH), "w") as f: with open(str(TRANSPOSE_CONFIG_PATH), "w") as f:
json.dump(TRANSPOSE_CONFIG, 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 yield
finally: finally:
# This shouldn't be necessary but is for some reason # This shouldn't be necessary but is for some reason


Loading…
Cancel
Save