Skip to content

Commit

Permalink
Add tests and fix reading command-line settings
Browse files Browse the repository at this point in the history
To support non-string options, parse the values as YAML.
  • Loading branch information
affenull2345 committed Jan 10, 2024
1 parent 48d30bd commit 7106d40
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/cobbler_tftp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def cli(ctx):
multiple=True,
help="""Set custom settings in format:\n
<PARENT_YAML_KEY>.<CHILD_YAML_KEY>.<...>.<KEY_NAME>=<VALUE>.\n
Your settings must use single quotes. If a single quote appears within a value it must be escaped.""",
The value is parsed as YAML. Quotes around the value are recommended for strings.""",
)
def start(
daemon: Optional[bool],
Expand Down
12 changes: 7 additions & 5 deletions src/cobbler_tftp/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ def load_cli_options(
:param daemon: If the application should be run in the background as a daemon or not.
:param enable_automigration: Whether to enable the automigration or not.
:param settings: List of custom settings which can be entered manually.
Each entry has the format: ``<PARENT_YAML_KEY>.<CHILD_YAML_KEY>.<...>.<KEY_NAME>=<VALUE>``
Each entry has the format: ``<PARENT_YAML_KEY>.<CHILD_YAML_KEY>.<...>.<KEY_NAME>=<VALUE>``,
where VALUE is parsed as a YAML value.
:return _settings_dict: Settings dictionary.
"""

Expand All @@ -271,12 +272,13 @@ def load_cli_options(
if "." not in option_list[0]:
self._settings_dict.update({option_list[0]: option_list[1]})
else:
parent = option_list[0].split(".")
key_path = option_list[0].split(".")
key_path.reverse()

setting_to_update = {parent[-1]: option_list[1]}
setting_to_update = yaml.safe_load(option_list[1])

for key in range(len(parent), 0, -1):
setting_to_update = {parent[key]: setting_to_update}
for key in key_path:
setting_to_update = {key: setting_to_update}

self._settings_dict.update(setting_to_update) # type: ignore

Expand Down
18 changes: 18 additions & 0 deletions tests/unittests/application_settings/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,21 @@ def test_build_settings_with_missing_config_file(
assert captured_message.out == expected_message
assert isinstance(settings, Settings)
assert_default_settings(settings)


def test_build_settings_with_cli_args(settings_factory: SettingsFactory):
cli_settings = ["tftp.address=1.2.3.4"]

settings = settings_factory.build_settings(None, cli_arguments=cli_settings)

assert isinstance(settings, Settings)
assert settings.tftp_addr == "1.2.3.4"


def test_build_settings_with_integer_cli_args(settings_factory: SettingsFactory):
cli_settings = ["tftp.port=1969"]

settings = settings_factory.build_settings(None, cli_arguments=cli_settings)

assert isinstance(settings, Settings)
assert settings.tftp_port == 1969

0 comments on commit 7106d40

Please sign in to comment.