Skip to content

Commit

Permalink
config no longer falsely loads profiles if file doesn't exist (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
zigaLuksic authored Nov 3, 2023
1 parent 3c4501c commit b8c96e9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sentinelhub/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ def load(cls, profile: str | None = None) -> SHConfig:
profile = cls._get_profile(profile)
filename = cls.get_config_location()
if not os.path.exists(filename):
cls(use_defaults=True).save(profile) # store default configuration to standard location
cls(use_defaults=True).save() # store default configuration to standard location

with open(filename, "rb") as cfg_file:
configurations_dict = tomli.load(cfg_file)

if profile not in configurations_dict:
raise KeyError(f"Profile {profile} not found in configuration file.")
raise KeyError(f"Profile `{profile}` not found in configuration file.")

return cls(use_defaults=True, **configurations_dict[profile])

Expand Down
14 changes: 14 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ def test_profiles() -> None:
assert SHConfig(profile="beep").sh_client_id == "bap"


@pytest.mark.dependency(depends=["test_user_config_is_masked"])
@pytest.mark.usefixtures("_restore_config_file")
def test_initialize_nondefault_profile() -> None:
"""Since there is no config, loading a non-default profile should fail."""
config = SHConfig()
os.remove(config.get_config_location())

SHConfig() # works for default

os.remove(config.get_config_location())
with pytest.raises(KeyError):
SHConfig("mr_president")


@pytest.mark.dependency(depends=["test_user_config_is_masked"])
@pytest.mark.usefixtures("_restore_config_file")
def test_profiles_from_env(monkeypatch: pytest.MonkeyPatch) -> None:
Expand Down

0 comments on commit b8c96e9

Please sign in to comment.