Skip to content

Commit 609db07

Browse files
authored
Merge pull request #513 from sunbeam-labs/512-config-modify-overwriting-too-much
Config modify doesn't overwrite more than it should
2 parents 6e2525c + 4ce06c7 commit 609db07

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

sunbeam/project/sunbeam_config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,15 @@ def modify(self, change_str: str):
9797
change_str should be a string in the format "root_key: {sub_key: value}"
9898
"""
9999
changes = yaml.safe_load(change_str)
100-
self.config.update(changes)
100+
for k, v in changes.items():
101+
if k not in self.config:
102+
self.config[k] = v
103+
else:
104+
if isinstance(v, dict):
105+
for sub_k, sub_v in v.items():
106+
self.config[k][sub_k] = sub_v
107+
else:
108+
self.config[k] = v
101109

102110
@staticmethod
103111
def get_extensions(extensions_dir: Path = None) -> dict[str, Path]:

tests/unit/test_sunbeam_config.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,37 @@ def test_fill_missing_doesnt_overwrite(test_extension):
124124
sc.fill_missing(ext_dir.parent)
125125

126126
assert sc.config["sbx_test_extension"]["bloop"] == "not_blorp"
127+
128+
129+
def test_modify(tmp_path):
130+
config = {
131+
"all": {
132+
"root": "/path/to/root",
133+
"version": __version__,
134+
},
135+
"sbx_test_extension": {
136+
"bloop": "blorp",
137+
},
138+
}
139+
sc = SunbeamConfig(config)
140+
141+
sc.modify("sbx_test_extension: {bloop: 'new_value'}")
142+
assert sc.config["sbx_test_extension"]["bloop"] == "new_value"
143+
144+
145+
def test_modify_one_of_many(tmp_path):
146+
config = {
147+
"all": {
148+
"root": "/path/to/root",
149+
"version": __version__,
150+
},
151+
"sbx_test_extension": {
152+
"bloop": "blorp",
153+
"blap": "blip",
154+
},
155+
}
156+
sc = SunbeamConfig(config)
157+
158+
sc.modify("sbx_test_extension: {bloop: 'new_value'}")
159+
assert sc.config["sbx_test_extension"]["bloop"] == "new_value"
160+
assert sc.config["sbx_test_extension"]["blap"] == "blip"

0 commit comments

Comments
 (0)