From ea893a3f4cc0e550f63f40b134d110598a7e3925 Mon Sep 17 00:00:00 2001 From: Adam Tokarski Date: Sun, 2 Oct 2022 19:00:41 +0200 Subject: [PATCH] [issue_3644] provide some tests for DeprecatedOption --- test/cli/commands_test.py | 23 +++++++++ test/cli/test_click_deprecated_option.py | 66 ++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 test/cli/test_click_deprecated_option.py diff --git a/test/cli/commands_test.py b/test/cli/commands_test.py index 02ac4105b7e..7348f7ef4ce 100644 --- a/test/cli/commands_test.py +++ b/test/cli/commands_test.py @@ -1628,6 +1628,29 @@ def test_cli_lint_disabled_progress_bar( assert "\rparsing: 0it" not in raw_output assert "\r\rlint by rules:" not in raw_output + def test_cli_lint_disabled_progress_bar_deprecated_option( + self, mock_disable_progress_bar: MagicMock + ) -> None: + """Same as above but checks additionally if deprecation warning is printed.""" + result = invoke_assert_code( + args=[ + lint, + [ + "--disable_progress_bar", + "test/fixtures/linter/passing.sql", + ], + ], + ) + raw_output = repr(result.output) + + assert "\rpath test/fixtures/linter/passing.sql:" not in raw_output + assert "\rparsing: 0it" not in raw_output + assert "\r\rlint by rules:" not in raw_output + assert ( + "DeprecationWarning: The option '--disable_progress_bar' is deprecated, " + "use '--disable-progress-bar'" + ) in raw_output + def test_cli_lint_enabled_progress_bar( self, mock_disable_progress_bar: MagicMock ) -> None: diff --git a/test/cli/test_click_deprecated_option.py b/test/cli/test_click_deprecated_option.py new file mode 100644 index 00000000000..6a6ee891c4a --- /dev/null +++ b/test/cli/test_click_deprecated_option.py @@ -0,0 +1,66 @@ +"""The Test suite for `DeprecatedOption` - extension for click options.""" +import click +import pytest + +from sqlfluff.cli.click_deprecated_option import ( + DeprecatedOption, + DeprecatedOptionsCommand, +) +from test.cli.commands_test import invoke_assert_code + + +class TestClickDeprecatedOption: + """Tests for custom click's option `DeprecatedOption`.""" + + @pytest.mark.parametrize( + "option, expected_output", + [ + ([], "{'old_option': False}\n"), + ( + ["--old_option"], + "DeprecationWarning: The option '--old_option' is deprecated, " + "use '--new_option'.\n{'old_option': True}\n", + ), + (["--new_option"], "{'old_option': True}\n"), + ], + ) + def test_cli_deprecated_option( + self, option: list[str], expected_output: str + ) -> None: + """Prepares command with option which has deprecated version and checks it.""" + + @click.command(cls=DeprecatedOptionsCommand) + @click.option( + "--old_option", + "--new_option", + is_flag=True, + cls=DeprecatedOption, + deprecated=["--old_option"], + ) + def some_command(**kwargs): + click.echo("{}".format(kwargs)) + + result = invoke_assert_code(args=[some_command, option]) + raw_output = result.output + + assert raw_output == expected_output + + def test_cli_deprecated_option_should_fail_when_missing_attr( + self, + ) -> None: + """The DeprecatedOption needs to have specified deprecated attr.""" + + @click.command(cls=DeprecatedOptionsCommand) + @click.option( + "--old_option", + "--new_option", + is_flag=True, + cls=DeprecatedOption, + ) + def some_command(**kwargs): + click.echo("{}".format(kwargs)) + + with pytest.raises(ValueError) as exc: + invoke_assert_code(args=[some_command, ["--old_option"]]) + + assert str(exc.value) == "Expected `deprecated` value for `'old_option'`"