Skip to content

Commit

Permalink
activate multiple profiles (#45)
Browse files Browse the repository at this point in the history
* add activation of multiple profiles
  • Loading branch information
livioribeiro authored Apr 16, 2024
1 parent 065a240 commit 36ef2d6
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 14 deletions.
5 changes: 5 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ As an example, if we define `SELVA_PROFILE=dev`, the file `settings_dev.yaml` wi
be loaded. If instead we define `SELVA_PROFILE=prod`, then the file `settings_prod.yaml`
will be loaded.

Multiple profiles can be activated by setting `SELVA_PROFILE` with a comma separated
list of profiles, for example `SELVA_PROFILE=dev,prod`. The framework will iterate
over the list and merge the settings found on each one. The precedence is from last
to first, so settings from one profile overwrite settings from the previous ones.

## Environment variables

Settings can also be defined with environment variables whose names start with `SELVA__`,
Expand Down
7 changes: 4 additions & 3 deletions src/selva/configuration/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ def _get_settings_nocache() -> Settings:
# merge with main settings file (settings.yaml)
merge_recursive(settings, get_settings_for_profile())

# merge with environment settings file (settings_$SELVA_PROFILE.yaml)
if active_profile := os.getenv(SELVA_PROFILE):
merge_recursive(settings, get_settings_for_profile(active_profile))
# merge with profile settings files (settings_$SELVA_PROFILE.yaml)
if active_profile_list := os.getenv(SELVA_PROFILE):
for active_profile in (p.strip() for p in active_profile_list.split(",")):
merge_recursive(settings, get_settings_for_profile(active_profile))

# merge with environment variables (SELVA_*)
from_env_vars = parse_settings_from_env(os.environ)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
environment: prd
profile: prd
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
profile_dev: true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
profile_prd: true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
profile_stg: true
Original file line number Diff line number Diff line change
@@ -1 +1 @@
environment: dev
profile: dev
Original file line number Diff line number Diff line change
@@ -1 +1 @@
environment: prd
profile: prd
Original file line number Diff line number Diff line change
@@ -1 +1 @@
environment: stg
profile: stg
29 changes: 22 additions & 7 deletions tests/configuration/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,32 @@ def test_get_settings_with_profile(monkeypatch, profile):
result = _get_settings_nocache()
assert result == default_settings | {
"name": "application",
"environment": profile,
"profile": profile,
}


@pytest.mark.parametrize(
["profile_a", "profile_b"],
[["dev", "stg"], ["stg", "prd"], ["prd", "dev"]],
)
def test_get_settings_with_multiple_profiles(monkeypatch, profile_a, profile_b):
monkeypatch.chdir(Path(__file__).parent / "multiple_profiles")
monkeypatch.setenv("SELVA_PROFILE", f"{profile_a}, {profile_b}")

result = _get_settings_nocache()
assert result == default_settings | {
f"profile_{profile_a}": True,
f"profile_{profile_b}": True,
}


@pytest.mark.parametrize(
"profile,expected",
[
(None, {"name": "application"}),
("dev", {"environment": "dev"}),
("stg", {"environment": "stg"}),
("prd", {"environment": "prd"}),
("dev", {"profile": "dev"}),
("stg", {"profile": "stg"}),
("prd", {"profile": "prd"}),
],
ids=["None", "dev", "stg", "prd"],
)
Expand Down Expand Up @@ -145,7 +160,7 @@ def test_configure_settings_file_with_profile(monkeypatch):

result = _get_settings_nocache()
assert result == default_settings | {
"environment": "prd",
"profile": "prd",
"prop": "value",
"list": ["1", "2", "3"],
"dict": {
Expand All @@ -170,7 +185,7 @@ def test_configure_env_setttings(monkeypatch, env):
result = _get_settings_nocache()
assert result == default_settings | {
"name": "application",
"environment": env,
"profile": env,
}


Expand Down Expand Up @@ -213,7 +228,7 @@ def test_setttings_class_env(monkeypatch, env):

settings = _get_settings_nocache()
assert settings["name"] == "application"
assert settings["environment"] == env
assert settings["profile"] == env


def test_no_profile_settings_file_should_log_warning(monkeypatch, caplog):
Expand Down

0 comments on commit 36ef2d6

Please sign in to comment.