From fc2a175e5bb18757b82d9d62bc9698fb08b9c013 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Sun, 17 Dec 2023 16:09:41 -0800 Subject: [PATCH] Fix normalization of settings using a custom dest --- settngs/__init__.py | 10 +++---- tests/settngs_test.py | 61 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/settngs/__init__.py b/settngs/__init__.py index b45f70a..581284d 100644 --- a/settngs/__init__.py +++ b/settngs/__init__.py @@ -448,13 +448,13 @@ def normalize_config( value, is_default = get_option(options, setting) if not is_default or default: # User has set a custom value or has requested the default value - group_options[setting_name] = value - elif setting_name in group_options: + group_options[setting.dest] = value + elif setting.dest in group_options: # default values have been requested to be removed - del group_options[setting_name] - elif setting_name in group_options: + del group_options[setting.dest] + elif setting.dest in group_options: # Setting type (file or cmdline) has not been requested and should be removed for persistent groups - del group_options[setting_name] + del group_options[setting.dest] normalized[group_name] = group_options return Config(normalized, config.definitions) diff --git a/tests/settngs_test.py b/tests/settngs_test.py index 612bd49..a7911db 100644 --- a/tests/settngs_test.py +++ b/tests/settngs_test.py @@ -18,7 +18,18 @@ from testing.settngs import failure from testing.settngs import success -if sys.version_info < (3, 9): # pragma: no cover +if sys.version_info >= (3, 10): # pragma: no cover + List = list + help_output = '''\ +usage: __main__.py [-h] [TEST ...] + +positional arguments: + TEST + +options: + -h, --help show this help message and exit +''' +elif sys.version_info < (3, 9): # pragma: no cover from typing import List help_output = '''\ usage: __main__.py [-h] [TEST [TEST ...]] @@ -42,17 +53,6 @@ optional arguments: -h, --help show this help message and exit ''' -if sys.version_info >= (3, 10): # pragma: no cover - help_output = '''\ -usage: __main__.py [-h] [TEST ...] - -positional arguments: - TEST - -options: - -h, --help show this help message and exit -''' - @pytest.fixture def settngs_manager() -> Generator[settngs.Manager, None, None]: @@ -210,6 +210,24 @@ class TestValues: assert non_defaults_normalized.values['tst'] == {'test': 'world'} assert non_defaults_normalized.values['tst_persistent'] == {'test': 'world'} + def test_normalize_dest(self, settngs_manager): + settngs_manager.add_group('tst', lambda parser: parser.add_setting('--test', default='hello')) + settngs_manager.add_group('tst', lambda parser: parser.add_setting('--test2', dest='test', default='hello')) + settngs_manager.add_persistent_group('tst_persistent', lambda parser: parser.add_setting('--test', default='hello')) + + defaults = settngs_manager.defaults() + defaults_normalized = settngs_manager.normalize_config(defaults, file=True, default=False) + assert defaults_normalized.values['tst'] == {} + assert defaults_normalized.values['tst_persistent'] == {} + + non_defaults = settngs_manager.defaults() + non_defaults.values['tst']['test'] = 'world' + non_defaults.values['tst_persistent']['test'] = 'world' + non_defaults_normalized = settngs_manager.normalize_config(non_defaults, file=True, default=False) + + assert non_defaults_normalized.values['tst'] == {'test': 'world'} + assert non_defaults_normalized.values['tst_persistent'] == {'test': 'world'} + def test_normalize(self, settngs_manager): settngs_manager.add_group('tst', lambda parser: parser.add_setting('--test', default='hello')) settngs_manager.add_persistent_group('persistent', lambda parser: parser.add_setting('--world', default='world')) @@ -317,6 +335,23 @@ class TestNamespace: assert non_defaults_normalized.values.tst__test == 'world' assert non_defaults_normalized.values.tst_persistent__test == 'world' + def test_normalize_dest(self, settngs_manager): + settngs_manager.add_group('tst', lambda parser: parser.add_setting('--test', default='hello')) + settngs_manager.add_group('tst', lambda parser: parser.add_setting('--test2', dest='test', default='hello')) + settngs_manager.add_persistent_group('tst_persistent', lambda parser: parser.add_setting('--test', default='hello')) + + defaults = settngs_manager.defaults() + defaults_normalized = settngs_manager.get_namespace(settngs_manager.normalize_config(defaults, file=True, default=False), file=True, default=False) + assert defaults_normalized.values.__dict__ == {} + + non_defaults = settngs_manager.get_namespace(settngs_manager.defaults(), file=True, cmdline=True) + non_defaults.values.tst__test = 'world' + non_defaults.values.tst_persistent__test = 'world' + non_defaults_normalized = settngs_manager.get_namespace(settngs_manager.normalize_config(non_defaults, file=True, default=False), file=True, default=False) + + assert non_defaults_normalized.values.tst__test == 'world' + assert non_defaults_normalized.values.tst_persistent__test == 'world' + def test_normalize(self, settngs_manager): settngs_manager.add_group('tst', lambda parser: parser.add_setting('--test', default='hello')) settngs_manager.add_persistent_group('persistent', lambda parser: parser.add_setting('--world', default='world')) @@ -333,7 +368,7 @@ class TestNamespace: assert normalized.persistent__hello == 'success' assert normalized.persistent__world == 'world' - def test_normalize_unknown(self, settngs_manager): + def test_normalize_unknown_group(self, settngs_manager): manager = settngs.Manager() manager.add_group('tst', lambda parser: parser.add_setting('--test', default='hello')) manager.add_persistent_group('persistent', lambda parser: parser.add_setting('--world', default='world'))