From 02814833d5a84d6cd241918ab9fb6b564e43c3e1 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 13 Sep 2021 00:50:36 +0200 Subject: [PATCH] Misc fixes, try to fix tests --- locales/en.json | 1 - src/yunohost/tests/test_app_config.py | 10 +++++---- src/yunohost/utils/config.py | 29 ++++++++++++++++----------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/locales/en.json b/locales/en.json index fe340fff2..f3b1fc906 100644 --- a/locales/en.json +++ b/locales/en.json @@ -143,7 +143,6 @@ "config_apply_failed": "Applying the new configuration failed: {error}", "config_cant_set_value_on_section": "You can't set a single value on an entire config section.", "config_forbidden_keyword": "The keyword '{keyword}' is reserved, you can't create or use a config panel with a question with this id.", - "config_missing_init_value": "Config panel question '{question}' should be initialize with a value during install or upgrade.", "config_no_panel": "No config panel found.", "config_unknown_filter_key": "The filter key '{filter_key}' is incorrect.", "config_validate_color": "Should be a valid RGB hexadecimal color", diff --git a/src/yunohost/tests/test_app_config.py b/src/yunohost/tests/test_app_config.py index 4ace0aaf9..52f458b55 100644 --- a/src/yunohost/tests/test_app_config.py +++ b/src/yunohost/tests/test_app_config.py @@ -105,9 +105,7 @@ def test_app_config_get(config_app): assert isinstance(app_config_get(config_app, export=True), dict) assert isinstance(app_config_get(config_app, "main"), dict) assert isinstance(app_config_get(config_app, "main.components"), dict) - # Is it expected that we return None if no value defined yet ? - # c.f. the whole discussion about "should we have defaults" etc. - assert app_config_get(config_app, "main.components.boolean") is None + assert app_config_get(config_app, "main.components.boolean") == "0" def test_app_config_nopanel(legacy_app): @@ -130,10 +128,14 @@ def test_app_config_get_nonexistentstuff(config_app): with pytest.raises(YunohostValidationError): app_config_get(config_app, "main.components.nonexistent") + app_setting(config_app, "boolean", delete=True) + with pytest.raises(YunohostValidationError): + app_config_get(config_app, "main.components.boolean") + def test_app_config_regular_setting(config_app): - assert app_config_get(config_app, "main.components.boolean") is None + assert app_config_get(config_app, "main.components.boolean") == "0" app_config_set(config_app, "main.components.boolean", "no") diff --git a/src/yunohost/utils/config.py b/src/yunohost/utils/config.py index 6cc693740..2ee01f97f 100644 --- a/src/yunohost/utils/config.py +++ b/src/yunohost/utils/config.py @@ -209,6 +209,7 @@ class ConfigPanel: "default": {} } } + def convert(toml_node, node_type): """Convert TOML in internal format ('full' mode used by webadmin) Here are some properties of 1.0 config panel in toml: @@ -284,7 +285,7 @@ class ConfigPanel: if option["type"] in allowed_empty_type or option["bind"] == "null": continue else: - raise YunohostError("config_missing_init_value", question=option["id"]) + raise YunohostError(f"Config panel question '{option['id']}' should be initialized with a value during install or upgrade.") value = self.values[option["name"]] # In general, the value is just a simple value. # Sometimes it could be a dict used to overwrite the option itself @@ -538,22 +539,25 @@ class StringQuestion(Question): argument_type = "string" default_value = "" + class EmailQuestion(StringQuestion): pattern = { - "regexp": "^.+@.+", - "error": "config_validate_email" + "regexp": r"^.+@.+", + "error": "config_validate_email" # i18n: config_validate_email } + class URLQuestion(StringQuestion): pattern = { - "regexp": "^https?://.*$", - "error": "config_validate_url" + "regexp": r"^https?://.*$", + "error": "config_validate_url" # i18n: config_validate_url } + class DateQuestion(StringQuestion): pattern = { - "regexp": "^\d{4}-\d\d-\d\d$", - "error": "config_validate_date" + "regexp": r"^\d{4}-\d\d-\d\d$", + "error": "config_validate_date" # i18n: config_validate_date } def _prevalidate(self): @@ -566,16 +570,18 @@ class DateQuestion(StringQuestion): except ValueError: raise YunohostValidationError("config_validate_date") + class TimeQuestion(StringQuestion): pattern = { - "regexp": "^(1[12]|0?\d):[0-5]\d$", - "error": "config_validate_time" + "regexp": r"^(1[12]|0?\d):[0-5]\d$", + "error": "config_validate_time" # i18n: config_validate_time } + class ColorQuestion(StringQuestion): pattern = { - "regexp": "^#[ABCDEFabcdef\d]{3,6}$", - "error": "config_validate_color" + "regexp": r"^#[ABCDEFabcdef\d]{3,6}$", + "error": "config_validate_color" # i18n: config_validate_color } @@ -799,7 +805,6 @@ class NumberQuestion(Question): ) - class DisplayTextQuestion(Question): argument_type = "display_text" readonly = True