🥅 Protect against None set subtrees

Remove one in our own defaults.

Also add tests for flatten and unflatten.
This commit is contained in:
Gina Häußge committed 2022-02-16 12:03:15 +01:00
1 parent 3cce49fe79
commit 698dcc2aa1
2 files changed
+54 -2

No files matched your search

+2 -2
View File
@@ -416,7 +416,7 @@ default_settings = {
"remoteUserHeader": "REMOTE_USER",
"addRemoteUsers": False,
},
"slicing": {"enabled": True, "defaultSlicer": None, "defaultProfiles": None},
"slicing": {"enabled": True, "defaultSlicer": None, "defaultProfiles": {}},
"events": {"enabled": True, "subscriptions": []},
"api": {"key": None, "allowCrossOrigin": False, "apps": {}},
"terminalFilters": [
@@ -566,7 +566,7 @@ class HierarchicalChainMap:
subkeys = key[len(prefix) :].split(_CHAINMAP_SEP)
current = result
for subkey in subkeys[:-1]:
if subkey not in current:
if subkey not in current or current[subkey] is None:
current[subkey] = {}
current = current[subkey]
current[subkeys[-1]] = value
+52
View File
@@ -29,3 +29,55 @@ class TestHelpers(unittest.TestCase):
@ddt.unpack
def test_valid_boolean_trues(self, value, expected):
self.assertEqual(expected, value in octoprint.settings.valid_boolean_trues)
def _key(*path):
return octoprint.settings._CHAINMAP_SEP.join(path)
@ddt.ddt
class ChainmapTest(unittest.TestCase):
@ddt.data(
(
{"a": 1},
{_key("a"): 1},
),
({"a": {"b": "b"}}, {_key("a", "b"): "b"}),
(
{"a": {"b": "b", "c": "c", "d": {"e": "e"}}},
{_key("a", "b"): "b", _key("a", "c"): "c", _key("a", "d", "e"): "e"},
),
)
@ddt.unpack
def test_flatten(self, value, expected):
self.assertEqual(
expected, octoprint.settings.HierarchicalChainMap._flatten(value)
)
@ddt.data(
(
[
(_key("a"), 1),
],
{"a": 1},
),
(
[
(_key("a", "b"), "b"),
],
{"a": {"b": "b"}},
),
(
[(_key("a", "b"), "b"), (_key("a", "c"), "c"), (_key("a", "d", "e"), "e")],
{"a": {"b": "b", "c": "c", "d": {"e": "e"}}},
),
(
[(_key("a"), None), (_key("a", "b"), "b")],
{"a": {"b": "b"}},
),
)
@ddt.unpack
def test_unflatten(self, value, expected):
self.assertEqual(
expected, octoprint.settings.HierarchicalChainMap._unflatten(value)
)