Skip to content

Commit

Permalink
Add unique label when merging lists to fix issue dynaconf#653 (dynaco…
Browse files Browse the repository at this point in the history
…nf#661)

Co-authored-by: andressa.cabistani <andressa.cabistani@thoughtworks.com>
  • Loading branch information
andressadotpy and acabistani committed Oct 4, 2021
1 parent 9c5f60b commit 912ce08
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dynaconf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,10 @@ def _merge_before_set(self, existing, value):
local_merge = (
"dynaconf_merge" in value or "dynaconf_merge_unique" in value
)
default_env = self.DEFAULT_ENV_FOR_DYNACONF == "DEFAULT"
if global_merge or local_merge:
value = list(value)
unique = False
unique = True if default_env else False
if local_merge:
try:
value.remove("dynaconf_merge")
Expand Down
33 changes: 33 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os

import pytest
import yaml

from dynaconf import Dynaconf
from dynaconf import LazySettings
from dynaconf.loaders import toml_loader
from dynaconf.strategies.filtering import PrefixFilter
from dynaconf.utils.parse_conf import true_values
from dynaconf.vendor_src.box.box_list import BoxList


def test_deleted_raise(settings):
Expand Down Expand Up @@ -1026,3 +1028,34 @@ def test_wrap_existing_settings_clone():

# assert original settings is not changes as we used a wrapped clone
assert settings.FOO == "bar"


@pytest.fixture()
def resource():
settings_test = {
"default": {
"SOME_KEY": "value",
"SOME_LIST": ["item_1", "item_2", "item_3"],
},
"other": {"SOME_KEY": "new_value", "SOME_LIST": ["item_4", "item_5"]},
}

settings_yaml = yaml.dump(settings_test)
with open("test_settings.yaml", "w") as file:
file.write(settings_yaml)


def test_list_entries_from_yaml_should_not_duplicate_when_merged(resource):
settings = Dynaconf(
settings_files="test_settings.yaml",
environments=True,
merge_enabled=True,
)

expected_default_value = BoxList(["item_1", "item_2", "item_3"])
expected_other_value = BoxList(
["item_1", "item_2", "item_3", "item_4", "item_5"]
)

assert settings.from_env("default").SOME_LIST == expected_default_value
assert settings.from_env("other").SOME_LIST == expected_other_value

0 comments on commit 912ce08

Please sign in to comment.