Skip to content

Commit

Permalink
[issue_3644] provide some tests for DeprecatedOption
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-tokarski committed Oct 2, 2022
1 parent a38512f commit ea893a3
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
23 changes: 23 additions & 0 deletions test/cli/commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
66 changes: 66 additions & 0 deletions test/cli/test_click_deprecated_option.py
Original file line number Diff line number Diff line change
@@ -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'`"

0 comments on commit ea893a3

Please sign in to comment.