Skip to content

Commit

Permalink
add test for correct handling of default False args and handle --show
Browse files Browse the repository at this point in the history
  • Loading branch information
jordivandooren committed Sep 20, 2024
1 parent 4e07487 commit 59df29b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 0 additions & 1 deletion core/dbt/task/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def __init__(self, args: Flags, config: RuntimeConfig) -> None:
cli_command = CMD_DICT.get(self.previous_command_name) # type: ignore
# Remove these args when their default values are present, otherwise they'll raise an exception
args_to_remove = {
"show": lambda x: True,
"resource_types": lambda x: x == [],
"warn_error_options": lambda x: x == {"exclude": [], "include": []},
}
Expand Down
1 change: 1 addition & 0 deletions core/dbt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ def args_to_dict(args) -> dict:
"use_experimental_parser",
"static",
"empty_catalog",
"show",
)
default_empty_yaml_dict_keys = ("vars", "warn_error_options")
if key in default_false_keys and var_args[key] is False:
Expand Down
55 changes: 55 additions & 0 deletions tests/unit/test_flag_translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import click
import pytest

from dbt.cli import main as cli_main
from dbt.cli.types import Command
from dbt.task.retry import CMD_DICT
from dbt.utils import args_to_dict


def is_problematic_option(option: click.Option) -> bool:
return (
option.is_flag and not option.default and not option.secondary_opts and option.expose_value
)


def get_problemetic_options_for_command(command: click.Command) -> list[str]:
"""
Get boolean flags of a ClickCommand that are False by default, do not
have a secondary option (--no-*), and expose their value.
Why do we care? If not dealt with, these arguments are stored in run_results.json
and converted to non-existent --no-* options when running dbt retry.
"""
return [
option.name
for option in command.params
if isinstance(option, click.Option) and is_problematic_option(option)
]


def get_commands_supported_by_retry() -> list[click.Command]:
command_names = [convert_enum_to_command_function_name(value) for value in CMD_DICT.values()]
return [getattr(cli_main, name) for name in command_names]


def convert_enum_to_command_function_name(enum: Command) -> str:
return "_".join(enum.to_list()).replace("-", "_")


class FlagsDummy:
def __init__(self, args: dict[str, bool]):
self.__dict__ = args


@pytest.mark.parametrize("command", get_commands_supported_by_retry())
def test_flags_problematic_for_retry_are_dealt_with(command: click.Command):
"""
For each command supported by retry, get a list of flags that should
not be converted to --no-* when False, and assert if args_to_dict correctly
skips it.
"""
flag_names = get_problemetic_options_for_command(command)
flags = FlagsDummy({name: False for name in flag_names})
args_dict = args_to_dict(flags)
for flag_name in flag_names:
assert flag_name not in args_dict, f"add {flag_name} to default_false_keys in args_to_dict"

0 comments on commit 59df29b

Please sign in to comment.