Merge branch 'maintenance' of github.com:OctoPrint/OctoPrint into maintenance
This commit is contained in:
commit
e16d9fbeae
5 files changed
+171
-171
No files matched your search
@@ -618,7 +618,9 @@ class HierarchicalChainMap:
|
||||
else:
|
||||
current = self._chainmap
|
||||
|
||||
return self._path_to_key(path) in current
|
||||
key = self._path_to_key(path)
|
||||
prefix = key + _CHAINMAP_SEP
|
||||
return key in current or any(map(lambda x: x.startswith(prefix), current.keys()))
|
||||
|
||||
def get_by_path(self, path, only_local=False, only_defaults=False, merged=False):
|
||||
if only_defaults:
|
||||
@@ -656,14 +658,17 @@ class HierarchicalChainMap:
|
||||
return result
|
||||
|
||||
def set_by_path(self, path, value):
|
||||
current = self._chainmap
|
||||
current = self._chainmap.maps[0]
|
||||
key = self._path_to_key(path)
|
||||
|
||||
# delete any subkeys
|
||||
subkeys_start = key + _CHAINMAP_SEP
|
||||
to_delete = []
|
||||
for k in current.keys():
|
||||
if k.startswith(subkeys_start):
|
||||
del current[key]
|
||||
to_delete.append(k)
|
||||
for k in to_delete:
|
||||
del current[k]
|
||||
|
||||
if isinstance(value, dict):
|
||||
current.update(self._flatten(value, key))
|
||||
@@ -702,7 +707,8 @@ class HierarchicalChainMap:
|
||||
# a/b = {}
|
||||
# a/b/c = "a" # deleted
|
||||
up_one_path = self._path_to_key(path[:-1])
|
||||
if sum(1 for k in current.keys() if k.startswith(up_one_path)) == 0:
|
||||
up_one_path_prefix = up_one_path + _CHAINMAP_SEP
|
||||
if not any(map(lambda k: k.startswith(up_one_path_prefix), current.keys())):
|
||||
current[up_one_path] = {}
|
||||
|
||||
def with_config_defaults(self, config=None, defaults=None):
|
||||
@@ -720,7 +726,7 @@ class HierarchicalChainMap:
|
||||
if config is not None:
|
||||
config = self._flatten(config)
|
||||
else:
|
||||
config = self.top_map
|
||||
config = self._chainmap.maps[0]
|
||||
|
||||
if defaults is not None:
|
||||
defaults = [self._flatten(defaults)]
|
||||
|
||||
@@ -12,3 +12,5 @@ server:
|
||||
devel:
|
||||
virtualPrinter:
|
||||
enabled: true
|
||||
capabilities:
|
||||
autoreport_temp: false
|
||||
@@ -13,9 +13,9 @@ serial:
|
||||
server:
|
||||
host: 0.0.0.0
|
||||
port: 5000
|
||||
devel:
|
||||
virtualPrinter:
|
||||
enabled: false
|
||||
sendWait: true
|
||||
waitInterval: 1.0
|
||||
repetierStyleResends: false
|
||||
devel: {}
|
||||
test:
|
||||
a:
|
||||
a1: 1
|
||||
a2: 2
|
||||
a3: 3
|
||||
@@ -0,0 +1,10 @@
|
||||
devel:
|
||||
virtualPrinter:
|
||||
enabled: false
|
||||
sendWait: true
|
||||
waitInterval: 1.0
|
||||
repetierStyleResends: false
|
||||
test:
|
||||
b:
|
||||
b1: 1
|
||||
b2: 2
|
||||
+142
-160
@@ -18,34 +18,40 @@ import time
|
||||
import unittest
|
||||
|
||||
import ddt
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
import octoprint.settings
|
||||
from octoprint.util import dict_merge
|
||||
|
||||
base_path = os.path.join(os.path.dirname(__file__), "_files")
|
||||
|
||||
|
||||
def _load_yaml(fname):
|
||||
with open(fname, encoding="utf-8") as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
|
||||
def _dump_yaml(fname, config):
|
||||
with open(fname, "wt", encoding="utf-8") as f:
|
||||
yaml.safe_dump(config, f)
|
||||
|
||||
|
||||
@ddt.ddt
|
||||
class SettingsTest(unittest.TestCase):
|
||||
def _load_yaml(self, fname):
|
||||
with open(fname, encoding="utf-8") as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
def _dump_yaml(self, fname, config):
|
||||
with open(fname, "wt", encoding="utf-8") as f:
|
||||
yaml.safe_dump(config, f)
|
||||
|
||||
def setUp(self):
|
||||
self.base_path = os.path.join(os.path.dirname(__file__), "_files")
|
||||
self.config_path = os.path.realpath(os.path.join(self.base_path, "config.yaml"))
|
||||
self.defaults_path = os.path.realpath(
|
||||
os.path.join(self.base_path, "defaults.yaml")
|
||||
self.config_path = os.path.realpath(os.path.join(base_path, "config.yaml"))
|
||||
self.overlay_path = os.path.realpath(os.path.join(base_path, "overlay.yaml"))
|
||||
self.defaults_path = os.path.realpath(os.path.join(base_path, "defaults.yaml"))
|
||||
|
||||
self.config = _load_yaml(self.config_path)
|
||||
self.overlay = _load_yaml(self.overlay_path)
|
||||
self.defaults = _load_yaml(self.defaults_path)
|
||||
|
||||
self.expected_effective = dict_merge(
|
||||
dict_merge(self.defaults, self.overlay), self.config
|
||||
)
|
||||
|
||||
self.config = self._load_yaml(self.config_path)
|
||||
self.defaults = self._load_yaml(self.defaults_path)
|
||||
|
||||
from octoprint.util import dict_merge
|
||||
|
||||
self.expected_effective = dict_merge(self.defaults, self.config)
|
||||
self.expected_effective[octoprint.settings.Settings.OVERLAY_KEY] = "overlay"
|
||||
|
||||
def test_basedir_initialization(self):
|
||||
with self.mocked_basedir() as basedir:
|
||||
@@ -209,111 +215,79 @@ class SettingsTest(unittest.TestCase):
|
||||
##~~ test getters
|
||||
|
||||
def test_get(self):
|
||||
with self.mocked_config():
|
||||
with self.settings() as settings:
|
||||
expected_api_key = "test"
|
||||
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
api_key = settings.get(["api", "key"])
|
||||
|
||||
self.assertIsNotNone(api_key)
|
||||
self.assertEqual(api_key, expected_api_key)
|
||||
|
||||
def test_get_int(self):
|
||||
with self.mocked_config():
|
||||
with self.settings() as settings:
|
||||
expected_server_port = 8080
|
||||
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
server_port = settings.get(["server", "port"])
|
||||
|
||||
self.assertIsNotNone(server_port)
|
||||
self.assertEqual(server_port, expected_server_port)
|
||||
|
||||
def test_get_int_converted(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
value = settings.getInt(["serial", "timeout", "connection"])
|
||||
|
||||
self.assertEqual(5, value)
|
||||
|
||||
def test_get_int_invalid(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
value = settings.getInt(["server", "host"])
|
||||
|
||||
self.assertIsNone(value)
|
||||
|
||||
def test_get_float(self):
|
||||
with self.mocked_config():
|
||||
with self.settings() as settings:
|
||||
expected_serial_timeout = 1.0
|
||||
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
serial_timeout = settings.get(["serial", "timeout", "detection"])
|
||||
|
||||
self.assertIsNotNone(serial_timeout)
|
||||
self.assertEqual(serial_timeout, expected_serial_timeout)
|
||||
|
||||
def test_get_float_converted(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
value = settings.getFloat(["serial", "timeout", "connection"])
|
||||
|
||||
self.assertEqual(5.0, value)
|
||||
|
||||
def test_get_float_invalid(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
value = settings.getFloat(["server", "host"])
|
||||
|
||||
self.assertIsNone(value)
|
||||
|
||||
def test_get_boolean(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
value = settings.get(["devel", "virtualPrinter", "enabled"])
|
||||
|
||||
self.assertTrue(value)
|
||||
|
||||
def test_get_list(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
data = settings.get(["serial", "additionalPorts"])
|
||||
|
||||
self.assertEqual(len(data), 2)
|
||||
self.assertListEqual(["/dev/portA", "/dev/portB"], data)
|
||||
|
||||
def test_get_map(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
data = settings.get(["devel", "virtualPrinter"])
|
||||
|
||||
self.assertDictEqual({"enabled": True}, data)
|
||||
self.assertEqual(len(data), 1)
|
||||
self.assertDictEqual(self.config["devel"]["virtualPrinter"], data)
|
||||
|
||||
def test_get_map_merged(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
data = settings.get(["devel", "virtualPrinter"], merged=True)
|
||||
|
||||
self.assertGreater(len(data), 1)
|
||||
test_dict = {"enabled": True, "sendWait": True, "waitInterval": 1.0}
|
||||
test_data = {k: v for k, v in data.items() if k in test_dict}
|
||||
self.assertEqual(test_dict, test_data)
|
||||
expected = dict_merge(
|
||||
self.overlay["devel"]["virtualPrinter"],
|
||||
self.config["devel"]["virtualPrinter"],
|
||||
)
|
||||
self.assertEqual(expected, data)
|
||||
|
||||
def test_get_multiple(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
data = settings.get(["serial", ["timeout", "additionalPorts"]])
|
||||
|
||||
self.assertIsInstance(data, list)
|
||||
@@ -323,9 +297,7 @@ class SettingsTest(unittest.TestCase):
|
||||
self.assertIsInstance(data[1], list)
|
||||
|
||||
def test_get_multiple_asdict(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
data = settings.get(["serial", ["timeout", "additionalPorts"]], asdict=True)
|
||||
|
||||
self.assertIsInstance(data, dict)
|
||||
@@ -335,17 +307,13 @@ class SettingsTest(unittest.TestCase):
|
||||
self.assertTrue("additionalPorts" in data)
|
||||
|
||||
def test_get_invalid(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
value = settings.get(["i", "do", "not", "exist"])
|
||||
|
||||
self.assertIsNone(value)
|
||||
|
||||
def test_get_invalid_error(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
try:
|
||||
settings.get(["i", "do", "not", "exist"], error_on_path=True)
|
||||
self.fail("Expected NoSuchSettingsPath")
|
||||
@@ -353,9 +321,7 @@ class SettingsTest(unittest.TestCase):
|
||||
pass
|
||||
|
||||
def test_get_custom_config(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
server_port = settings.getInt(
|
||||
["server", "port"], config={"server": {"port": 9090}}
|
||||
)
|
||||
@@ -363,9 +329,7 @@ class SettingsTest(unittest.TestCase):
|
||||
self.assertEqual(9090, server_port)
|
||||
|
||||
def test_get_custom_defaults(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
api_enabled = settings.getBoolean(
|
||||
["api", "enabled"], defaults={"api": {"enabled": False}}
|
||||
)
|
||||
@@ -373,8 +337,7 @@ class SettingsTest(unittest.TestCase):
|
||||
self.assertFalse(api_enabled)
|
||||
|
||||
def test_get_empty_path(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
with self.settings() as settings:
|
||||
self.assertIsNone(settings.get([]))
|
||||
|
||||
try:
|
||||
@@ -386,60 +349,40 @@ class SettingsTest(unittest.TestCase):
|
||||
##~~ test setters
|
||||
|
||||
def test_set(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
settings.set(["server", "host"], "127.0.0.1")
|
||||
|
||||
self.assertEqual("127.0.0.1", settings._config["server"]["host"])
|
||||
|
||||
def test_set_int(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
settings.setInt(["server", "port"], 8181)
|
||||
|
||||
self.assertEqual(8181, settings._config["server"]["port"])
|
||||
|
||||
def test_set_int_convert(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
settings.setInt(["server", "port"], "8181")
|
||||
|
||||
self.assertEqual(8181, settings._config["server"]["port"])
|
||||
|
||||
def test_set_float(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
settings.setFloat(["serial", "timeout", "detection"], 1.2)
|
||||
|
||||
self.assertEqual(1.2, settings._config["serial"]["timeout"]["detection"])
|
||||
|
||||
def test_set_float_convert(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
settings.setFloat(["serial", "timeout", "detection"], "1.2")
|
||||
|
||||
self.assertEqual(1.2, settings._config["serial"]["timeout"]["detection"])
|
||||
|
||||
def test_set_boolean(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
settings.setBoolean(["devel", "virtualPrinter", "sendWait"], False)
|
||||
|
||||
self.assertEqual(
|
||||
False, settings._config["devel"]["virtualPrinter"]["sendWait"]
|
||||
)
|
||||
|
||||
@ddt.data("1", "yes", "true", "TrUe", "y", "Y", "YES")
|
||||
def test_set_boolean_convert_string_true(self, value):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
settings.setBoolean(
|
||||
["devel", "virtualPrinter", "repetierStyleResends"], value
|
||||
)
|
||||
@@ -450,17 +393,13 @@ class SettingsTest(unittest.TestCase):
|
||||
|
||||
@ddt.data("0", "no", "false", ["some", "list"], {"a": "dictionary"}, lambda: None)
|
||||
def test_set_boolean_convert_any_false(self, value):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
settings.setBoolean(["api", "enabled"], value)
|
||||
|
||||
self.assertEqual(False, settings._config["api"]["enabled"])
|
||||
|
||||
def test_set_default(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
self.assertEqual(8080, settings._config["server"]["port"])
|
||||
|
||||
settings.set(["server", "port"], 5000)
|
||||
@@ -469,9 +408,7 @@ class SettingsTest(unittest.TestCase):
|
||||
self.assertEqual(5000, settings.get(["server", "port"]))
|
||||
|
||||
def test_set_none(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
self.assertTrue("port" in settings._config["server"])
|
||||
|
||||
settings.set(["server", "port"], None)
|
||||
@@ -482,9 +419,7 @@ class SettingsTest(unittest.TestCase):
|
||||
[], ["api", "lock"], ["api", "lock", "door"], ["serial", "additionalPorts", "key"]
|
||||
)
|
||||
def test_set_invalid(self, path):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
try:
|
||||
settings.set(path, "value", error_on_path=True)
|
||||
self.fail("Expected NoSuchSettingsPath")
|
||||
@@ -494,9 +429,7 @@ class SettingsTest(unittest.TestCase):
|
||||
##~~ test remove
|
||||
|
||||
def test_remove(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
self.assertTrue("port" in settings._config["server"])
|
||||
|
||||
settings.remove(["server", "port"])
|
||||
@@ -508,9 +441,7 @@ class SettingsTest(unittest.TestCase):
|
||||
|
||||
@ddt.data([], ["server", "lock"], ["serial", "additionalPorts", "key"])
|
||||
def test_remove_invalid(self, path):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
try:
|
||||
settings.remove(path, error_on_path=True)
|
||||
self.fail("Expected NoSuchSettingsPath")
|
||||
@@ -520,62 +451,53 @@ class SettingsTest(unittest.TestCase):
|
||||
##~~ test has
|
||||
|
||||
def test_has(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
self.assertTrue(settings.has(["api", "key"]))
|
||||
self.assertFalse(settings.has(["api", "lock"]))
|
||||
|
||||
##~~ test properties
|
||||
|
||||
def test_effective(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
with self.settings() as settings:
|
||||
effective = settings.effective
|
||||
|
||||
self.assertDictEqual(self.expected_effective, effective)
|
||||
|
||||
def test_effective_hash(self):
|
||||
with self.mocked_config():
|
||||
with self.settings() as settings:
|
||||
hash = hashlib.md5()
|
||||
hash.update(yaml.safe_dump(self.expected_effective).encode("utf-8"))
|
||||
expected_effective_hash = hash.hexdigest()
|
||||
print(yaml.safe_dump(self.expected_effective))
|
||||
|
||||
settings = octoprint.settings.Settings()
|
||||
effective_hash = settings.effective_hash
|
||||
print(yaml.safe_dump(settings.effective))
|
||||
|
||||
self.assertEqual(expected_effective_hash, effective_hash)
|
||||
|
||||
def test_config_hash(self):
|
||||
with self.mocked_config():
|
||||
with self.settings() as settings:
|
||||
hash = hashlib.md5()
|
||||
hash.update(yaml.safe_dump(self.config).encode("utf-8"))
|
||||
expected_config_hash = hash.hexdigest()
|
||||
|
||||
settings = octoprint.settings.Settings()
|
||||
config_hash = settings.config_hash
|
||||
|
||||
self.assertEqual(expected_config_hash, config_hash)
|
||||
|
||||
def test_last_modified(self):
|
||||
with self.mocked_config() as paths:
|
||||
basedir, configfile = paths
|
||||
settings = octoprint.settings.Settings()
|
||||
|
||||
with self.settings() as settings:
|
||||
configfile = settings._configfile
|
||||
last_modified = os.stat(configfile).st_mtime
|
||||
self.assertEqual(settings.last_modified, last_modified)
|
||||
|
||||
##~~ test preprocessors
|
||||
|
||||
def test_get_preprocessor(self):
|
||||
with self.mocked_config():
|
||||
with self.settings() as settings:
|
||||
config = {}
|
||||
defaults = {"test": "some string"}
|
||||
preprocessors = {"test": lambda x: x.upper()}
|
||||
|
||||
settings = octoprint.settings.Settings()
|
||||
value = settings.get(
|
||||
["test"], config=config, defaults=defaults, preprocessors=preprocessors
|
||||
)
|
||||
@@ -583,12 +505,11 @@ class SettingsTest(unittest.TestCase):
|
||||
self.assertEqual("SOME STRING", value)
|
||||
|
||||
def test_set_preprocessor(self):
|
||||
with self.mocked_config():
|
||||
with self.settings() as settings:
|
||||
config = {}
|
||||
defaults = {"foo": {"bar": "fnord"}}
|
||||
preprocessors = {"foo": {"bar": lambda x: x.upper()}}
|
||||
|
||||
settings = octoprint.settings.Settings()
|
||||
settings.set(
|
||||
["foo", "bar"],
|
||||
"value",
|
||||
@@ -600,9 +521,8 @@ class SettingsTest(unittest.TestCase):
|
||||
self.assertEqual("VALUE", config["foo"]["bar"])
|
||||
|
||||
def test_set_external_modification(self):
|
||||
with self.mocked_config() as paths:
|
||||
basedir, configfile = paths
|
||||
settings = octoprint.settings.Settings()
|
||||
with self.settings() as settings:
|
||||
configfile = settings._configfile
|
||||
|
||||
# Make sure the config files last modified time changes
|
||||
time.sleep(1.0)
|
||||
@@ -610,9 +530,9 @@ class SettingsTest(unittest.TestCase):
|
||||
self.assertEqual("0.0.0.0", settings.get(["server", "host"]))
|
||||
|
||||
# modify yaml file externally
|
||||
config = self._load_yaml(configfile)
|
||||
config = _load_yaml(configfile)
|
||||
config["server"]["host"] = "127.0.0.1"
|
||||
self._dump_yaml(configfile, config)
|
||||
_dump_yaml(configfile, config)
|
||||
|
||||
# set some value, should also reload file before setting new api key
|
||||
settings.set(["api", "key"], "key")
|
||||
@@ -624,9 +544,8 @@ class SettingsTest(unittest.TestCase):
|
||||
##~~ test save
|
||||
|
||||
def test_save(self):
|
||||
with self.mocked_config() as paths:
|
||||
basedir, config_path = paths
|
||||
settings = octoprint.settings.Settings()
|
||||
with self.settings() as settings:
|
||||
config_path = settings._configfile
|
||||
|
||||
# current modification date of config.yaml
|
||||
current_modified = os.stat(config_path).st_mtime
|
||||
@@ -645,8 +564,7 @@ class SettingsTest(unittest.TestCase):
|
||||
self.assertNotEqual(current_modified, os.stat(config_path).st_mtime)
|
||||
|
||||
def test_save_unmodified(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
with self.settings() as settings:
|
||||
last_modified = settings.last_modified
|
||||
|
||||
# sleep a bit to make sure we do have a change in the timestamp
|
||||
@@ -686,10 +604,17 @@ class SettingsTest(unittest.TestCase):
|
||||
shutil.copy(self.config_path, fresh_config_path)
|
||||
try:
|
||||
octoprint.settings.default_settings = self.defaults
|
||||
yield basedir, fresh_config_path
|
||||
yield
|
||||
finally:
|
||||
octoprint.settings.default_settings = orig_defaults
|
||||
|
||||
@contextlib.contextmanager
|
||||
def settings(self):
|
||||
with self.mocked_config():
|
||||
settings = octoprint.settings.Settings()
|
||||
settings.add_overlay(self.overlay, key="overlay")
|
||||
yield settings
|
||||
|
||||
|
||||
@ddt.ddt
|
||||
class HelpersTest(unittest.TestCase):
|
||||
@@ -720,6 +645,63 @@ def _key(*path):
|
||||
|
||||
@ddt.ddt
|
||||
class ChainmapTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.config_path = os.path.realpath(os.path.join(base_path, "config.yaml"))
|
||||
self.overlay_path = os.path.realpath(os.path.join(base_path, "overlay.yaml"))
|
||||
self.defaults_path = os.path.realpath(os.path.join(base_path, "defaults.yaml"))
|
||||
|
||||
self.config = _load_yaml(self.config_path)
|
||||
self.overlay = _load_yaml(self.overlay_path)
|
||||
self.defaults = _load_yaml(self.defaults_path)
|
||||
|
||||
self.chainmap = octoprint.settings.HierarchicalChainMap(
|
||||
self.config, self.overlay, self.defaults
|
||||
)
|
||||
|
||||
def test_has_path(self):
|
||||
self.assertTrue(self.chainmap.has_path(["api", "key"]))
|
||||
self.assertTrue(self.chainmap.has_path(["devel"]))
|
||||
self.assertTrue(self.chainmap.has_path(["devel", "virtualPrinter"]))
|
||||
self.assertTrue(self.chainmap.has_path(["devel", "virtualPrinter", "enabled"]))
|
||||
|
||||
self.assertFalse(self.chainmap.has_path(["api", "lock"]))
|
||||
|
||||
def test_get_by_path(self):
|
||||
self.assertEqual(
|
||||
True, self.chainmap.get_by_path(["devel", "virtualPrinter", "enabled"])
|
||||
)
|
||||
self.assertEqual(
|
||||
False,
|
||||
self.chainmap.get_by_path(
|
||||
["devel", "virtualPrinter", "enabled"], only_defaults=True
|
||||
),
|
||||
)
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
self.assertEqual(None, self.chainmap.get_by_path(["test"], only_local=True))
|
||||
self.assertEqual(self.overlay["test"], self.chainmap.get_by_path(["test"]))
|
||||
self.assertEqual(
|
||||
dict_merge(self.defaults["test"], self.overlay["test"]),
|
||||
self.chainmap.get_by_path(["test"], merged=True),
|
||||
)
|
||||
|
||||
def test_set_by_path(self):
|
||||
self.chainmap.set_by_path(["devel", "virtualPrinter", "sendWait"], False)
|
||||
|
||||
updated = dict(self.config)
|
||||
updated["devel"]["virtualPrinter"]["sendWait"] = False
|
||||
flattened = octoprint.settings.HierarchicalChainMap._flatten(updated)
|
||||
|
||||
self.assertEqual(flattened, self.chainmap._chainmap.maps[0])
|
||||
|
||||
def test_del_by_path(self):
|
||||
self.chainmap.del_by_path(
|
||||
["devel", "virtualPrinter", "capabilities", "autoreport_temp"]
|
||||
)
|
||||
self.assertEqual(
|
||||
{}, self.chainmap.get_by_path(["devel", "virtualPrinter", "capabilities"])
|
||||
)
|
||||
|
||||
@ddt.data(
|
||||
(
|
||||
{"a": 1},
|
||||
|
||||
Reference in new issue
Block a user