From 5e7ffd1b15e33f67a06ecefa522dd277d390b4a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 17 Feb 2022 15:09:09 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20a=20settings=20behaviour?= =?UTF-8?q?=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/settings.py | 10 +++++ tests/settings/test_settings.py | 73 +++++++++++++++++++++++++++++- tests/test_settings.py | 79 --------------------------------- 3 files changed, 82 insertions(+), 80 deletions(-) delete mode 100644 tests/test_settings.py diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 0410712e9..d441fa67a 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -633,11 +633,21 @@ class HierarchicalChainMap: # we do something a bit odd here: if merged is not true, we don't include the # full contents of the key. Instead, we only include the contents of the key on # the first level where we find the value. + # + # TODO 2.0.0 remove this weird & historic behaviour and make 'merged' the default if not merged and not only_local: + # first check if we can find the exact key & if so use that layer for layer in current.maps: if key in layer: current = layer break + else: + # next, check if we can find the key as a prefix for a subtree & if so + # use that layer + for layer in current.maps: + if any(k.startswith(key) for k in layer): + current = layer + break if key not in current: # we might be trying to grab a dict, look for children diff --git a/tests/settings/test_settings.py b/tests/settings/test_settings.py index f0d6a89ed..a9e45ec55 100644 --- a/tests/settings/test_settings.py +++ b/tests/settings/test_settings.py @@ -24,7 +24,7 @@ import octoprint.settings @ddt.ddt -class TestSettings(unittest.TestCase): +class SettingsTest(unittest.TestCase): def _load_yaml(self, fname): with open(fname, encoding="utf-8") as f: return yaml.safe_load(f) @@ -689,3 +689,74 @@ class TestSettings(unittest.TestCase): yield basedir, fresh_config_path finally: octoprint.settings.default_settings = orig_defaults + + +@ddt.ddt +class HelpersTest(unittest.TestCase): + @ddt.data( + (True, True), + ("true", True), + ("True", True), + ("tRuE", True), + ("yes", True), + ("YES", True), + ("y", True), + ("Y", True), + ("1", True), + (1, True), + (False, False), + ("Truuuuuuuuue", False), + ("Nope", False), + (None, False), + ) + @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) + ) diff --git a/tests/test_settings.py b/tests/test_settings.py deleted file mode 100644 index 706a6e916..000000000 --- a/tests/test_settings.py +++ /dev/null @@ -1,79 +0,0 @@ -__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" -__copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms of the AGPLv3 License" - -import unittest - -import ddt - -import octoprint.settings - - -@ddt.ddt -class TestHelpers(unittest.TestCase): - @ddt.data( - (True, True), - ("true", True), - ("True", True), - ("tRuE", True), - ("yes", True), - ("YES", True), - ("y", True), - ("Y", True), - ("1", True), - (1, True), - (False, False), - ("Truuuuuuuuue", False), - ("Nope", False), - (None, False), - ) - @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) - )