From ab4b571654531ef0ff1afc2da24b94fc3cbf8194 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Tue, 30 May 2023 10:42:37 -0400 Subject: [PATCH 1/2] fix error message for empty/None: --warn-error-options handling --- .changes/unreleased/Fixes-20230530-104228.yaml | 6 ++++++ core/dbt/cli/option_types.py | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 .changes/unreleased/Fixes-20230530-104228.yaml diff --git a/.changes/unreleased/Fixes-20230530-104228.yaml b/.changes/unreleased/Fixes-20230530-104228.yaml new file mode 100644 index 00000000000..fac86ed1dc5 --- /dev/null +++ b/.changes/unreleased/Fixes-20230530-104228.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Fix empty --warn-error-options error message +time: 2023-05-30T10:42:28.382804-04:00 +custom: + Author: michelleark + Issue: "7730" diff --git a/core/dbt/cli/option_types.py b/core/dbt/cli/option_types.py index b5eea995624..0a3bf1c1c88 100644 --- a/core/dbt/cli/option_types.py +++ b/core/dbt/cli/option_types.py @@ -1,6 +1,6 @@ from click import ParamType, Choice -from dbt.config.utils import parse_cli_vars +from dbt.config.utils import parse_cli_yaml_string from dbt.exceptions import ValidationError from dbt.helper_types import WarnErrorOptions @@ -16,7 +16,8 @@ def convert(self, value, param, ctx): if not isinstance(value, str): self.fail(f"Cannot load YAML from type {type(value)}", param, ctx) try: - return parse_cli_vars(value) + param_option_name = param.opts[0] if param.opts else param.name + return parse_cli_yaml_string(value, param_option_name.strip("-")) except ValidationError: self.fail(f"String '{value}' is not valid YAML", param, ctx) From 5ddd328a3e8dd0a69b4968de4971a6b7248cc61c Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Tue, 30 May 2023 12:30:37 -0400 Subject: [PATCH 2/2] test + less verbose error dump --- core/dbt/cli/option_types.py | 4 ++-- core/dbt/config/utils.py | 2 +- tests/unit/test_option_types.py | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 tests/unit/test_option_types.py diff --git a/core/dbt/cli/option_types.py b/core/dbt/cli/option_types.py index 0a3bf1c1c88..34d7314e867 100644 --- a/core/dbt/cli/option_types.py +++ b/core/dbt/cli/option_types.py @@ -1,7 +1,7 @@ from click import ParamType, Choice from dbt.config.utils import parse_cli_yaml_string -from dbt.exceptions import ValidationError +from dbt.exceptions import ValidationError, DbtValidationError, OptionNotYamlDictError from dbt.helper_types import WarnErrorOptions @@ -18,7 +18,7 @@ def convert(self, value, param, ctx): try: param_option_name = param.opts[0] if param.opts else param.name return parse_cli_yaml_string(value, param_option_name.strip("-")) - except ValidationError: + except (ValidationError, DbtValidationError, OptionNotYamlDictError): self.fail(f"String '{value}' is not valid YAML", param, ctx) diff --git a/core/dbt/config/utils.py b/core/dbt/config/utils.py index 8367d0716ba..18951665c53 100644 --- a/core/dbt/config/utils.py +++ b/core/dbt/config/utils.py @@ -19,6 +19,6 @@ def parse_cli_yaml_string(var_string: str, cli_option_name: str) -> Dict[str, An return cli_vars else: raise OptionNotYamlDictError(var_type, cli_option_name) - except DbtValidationError: + except (DbtValidationError, OptionNotYamlDictError): fire_event(InvalidOptionYAML(option_name=cli_option_name)) raise diff --git a/tests/unit/test_option_types.py b/tests/unit/test_option_types.py new file mode 100644 index 00000000000..67d3c5e941f --- /dev/null +++ b/tests/unit/test_option_types.py @@ -0,0 +1,26 @@ +from click import Option, BadParameter +import pytest + +from dbt.cli.option_types import YAML + + +class TestYAML: + @pytest.mark.parametrize( + "raw_value,expected_converted_value", + [ + ("{}", {}), + ("{'test_var_key': 'test_var_value'}", {"test_var_key": "test_var_value"}), + ], + ) + def test_yaml_init(self, raw_value, expected_converted_value): + converted_value = YAML().convert(raw_value, Option(["--vars"]), None) + assert converted_value == expected_converted_value + + @pytest.mark.parametrize( + "invalid_yaml_str", + ["{", ""], + ) + def test_yaml_init_invalid_yaml_str(self, invalid_yaml_str): + with pytest.raises(BadParameter) as e: + YAML().convert(invalid_yaml_str, Option(["--vars"]), None) + assert "--vars" in e.value.format_message()