Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config no longer falsely loads profiles if file doesn't exist #486

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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