Skip to content

Commit 08d04a7

Browse files
committed
Refactor CLI unit tests + add deprecation tests
- Move CLI unit tests to `tests/unit/cli/` - Add initial test cases for the deprecation callback helper
1 parent b016212 commit 08d04a7

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

tests/unit/cli/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pytest
2+
from click.testing import CliRunner
3+
4+
5+
@pytest.fixture
6+
def runner() -> CliRunner:
7+
return CliRunner(mix_stderr=False)

tests/unit/cli/test_callbacks.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import click
2+
import pytest
3+
4+
from check_jsonschema.cli.warnings import deprecation_warning_callback
5+
6+
7+
@click.command("foo")
8+
@click.option(
9+
"--bar",
10+
is_flag=True,
11+
callback=deprecation_warning_callback("--bar", is_flag=True),
12+
)
13+
@click.option(
14+
"--baz",
15+
callback=deprecation_warning_callback(
16+
"--baz", append_message="Use --frob instead!"
17+
),
18+
)
19+
def mycli(bar, baz):
20+
print(bar)
21+
if baz:
22+
print(baz)
23+
24+
25+
def test_deprecation_warning_callback_on_missing_opts(runner):
26+
result = runner.invoke(mycli, [])
27+
assert result.exit_code == 0
28+
assert result.stdout == "False\n"
29+
30+
31+
def test_deprecation_warning_callback_on_flag(runner):
32+
with pytest.warns(
33+
UserWarning,
34+
match="'--bar' is deprecated and will be removed in a future release",
35+
):
36+
result = runner.invoke(mycli, ["--bar"], catch_exceptions=False)
37+
assert result.exit_code == 0, result.stdout
38+
assert result.stdout == "True\n"
39+
40+
41+
def test_deprecation_warning_callback_added_message(runner):
42+
with pytest.warns(
43+
UserWarning,
44+
match=(
45+
"'--baz' is deprecated and will be removed in a future release. "
46+
"Use --frob instead!"
47+
),
48+
):
49+
result = runner.invoke(mycli, ["--baz", "ok"], catch_exceptions=False)
50+
assert result.exit_code == 0, result.stdout
51+
assert result.stdout == "False\nok\n"

tests/unit/test_cli_parse.py renamed to tests/unit/cli/test_parse.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import click
66
import pytest
7-
from click.testing import CliRunner
87

98
from check_jsonschema import main as cli_main
109
from check_jsonschema.cli.parse_result import ParseResult, SchemaLoadingMode
@@ -43,11 +42,6 @@ def get_ctx(*args):
4342
yield m
4443

4544

46-
@pytest.fixture
47-
def runner() -> CliRunner:
48-
return CliRunner(mix_stderr=False)
49-
50-
5145
@pytest.mark.parametrize(
5246
"schemafile,builtin_schema,check_metaschema,expect_mode",
5347
[

0 commit comments

Comments
 (0)