Skip to content

Commit 6f5035a

Browse files
chore: add test coverage and github actions test workflow
1 parent 85a5ed6 commit 6f5035a

File tree

6 files changed

+68
-11
lines changed

6 files changed

+68
-11
lines changed

.coveragerc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[run]
2+
branch = True
3+
include =
4+
src/**
5+
omit =
6+
__main__.py

.github/workflows/pytest.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Test Coverage
2+
on:
3+
push:
4+
pull_request:
5+
6+
jobs:
7+
test:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v3
12+
13+
- name: Setup Python
14+
uses: actions/setup-python@v4
15+
with:
16+
python-version: "3.7.13"
17+
18+
- name: Install Dependencies
19+
run: pip install '.[dev]'
20+
21+
- name: Run Tests
22+
run: pytest --cov --cov-report xml

.vscode/settings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"python.defaultInterpreterPath": ".venv/bin/python",
3-
"python.testing.pytestPath": ".venv/bin/pytest",
4-
"python.linting.flake8Path": ".venv/bin/flake8",
2+
"python.defaultInterpreterPath": "venv/bin/python",
3+
"python.testing.pytestPath": "venv/bin/pytest",
4+
"python.linting.flake8Path": "venv/bin/flake8",
55
"python.linting.flake8Enabled": true,
66
"python.testing.pytestEnabled": true
77
}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"typing_extensions",
99
],
1010
extras_require={
11-
"dev": ["pre-commit", "pytest", "flake8", "flake8-docstrings"],
11+
"dev": ["pre-commit", "pytest", "flake8", "flake8-docstrings", "pytest-cov"],
1212
},
1313
)

src/rich_click/__init__.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
__version__ = "1.6.0.dev0"
99

10-
from typing import Optional, TYPE_CHECKING
10+
from typing import Any, Callable, cast, Optional, overload, TYPE_CHECKING, Union
1111

1212
from click import * # noqa: F401, F403
1313
from click import command as click_command
@@ -19,6 +19,7 @@
1919
from . import rich_click # noqa: F401
2020

2121
from rich_click.rich_command import RichCommand
22+
from rich_click.rich_context import RichContext
2223
from rich_click.rich_group import RichGroup
2324
from rich_click.rich_help_configuration import RichHelpConfiguration
2425

@@ -36,10 +37,13 @@
3637
"version_option",
3738
"group",
3839
"command",
40+
"rich_config",
41+
"RichContext",
42+
"RichHelpConfiguration",
3943
]
4044

4145

42-
def group(*args, cls=RichGroup, **kwargs):
46+
def group(*args, cls=RichGroup, **kwargs) -> Callable[..., RichGroup]:
4347
"""
4448
Group decorator function.
4549
@@ -55,13 +59,13 @@ def wrapper(fn):
5559
context_settings.update(rich_console=console, rich_help_config=help_config)
5660
kwargs.update(context_settings=context_settings)
5761
del fn.__rich_context_settings__
58-
cmd = click_group(*args, cls=cls, **kwargs)(fn)
62+
cmd = cast(RichGroup, click_group(*args, cls=cls, **kwargs)(fn))
5963
return cmd
6064

6165
return wrapper
6266

6367

64-
def command(*args, cls=RichCommand, **kwargs):
68+
def command(*args, cls=RichCommand, **kwargs) -> Callable[..., RichCommand]:
6569
"""
6670
Command decorator function.
6771
@@ -77,7 +81,7 @@ def wrapper(fn):
7781
context_settings.update(rich_console=console, rich_help_config=help_config)
7882
kwargs.update(context_settings=context_settings)
7983
del fn.__rich_context_settings__
80-
cmd = click_command(*args, cls=cls, **kwargs)(fn)
84+
cmd = cast(RichCommand, click_command(*args, cls=cls, **kwargs)(fn))
8185
return cmd
8286

8387
return wrapper
@@ -99,6 +103,14 @@ def rich_config(console: Optional[Console] = None, help_config: Optional[RichHel
99103
Defaults to None.
100104
"""
101105

106+
@overload
107+
def decorator(obj: Union[RichCommand, RichGroup]) -> Union[RichCommand, RichGroup]:
108+
...
109+
110+
@overload
111+
def decorator(obj: Callable[..., Any]) -> Callable[..., Any]:
112+
...
113+
102114
def decorator(obj):
103115
if isinstance(obj, (RichCommand, RichGroup)):
104116
obj.context_settings.update({"rich_console": console, "rich_help_config": help_config})

tests/test_help.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from typing import Optional, Type
22

3+
import click
34
import pytest
45
from click import UsageError
56
from conftest import AssertRichFormat, AssertStr, InvokeCli
67
from rich.console import Console
78

8-
from rich_click import command, rich_config
9-
from rich_click.rich_help_configuration import RichHelpConfiguration
9+
from rich_click import command, rich_config, RichContext, RichHelpConfiguration
10+
from rich_click.rich_command import RichCommand
1011

1112

1213
@pytest.mark.parametrize(
@@ -182,6 +183,7 @@ def cli():
182183
pass
183184

184185
assert hasattr(cli, "__rich_context_settings__") is False
186+
assert cli.console is not None
185187
assert cli.__doc__ is not None
186188
assert_str(
187189
cli.__doc__,
@@ -207,3 +209,18 @@ def cli():
207209
╰──────────────────────────────────────────────────────────────────────────────╯
208210
""",
209211
)
212+
213+
214+
def test_rich_config_context_settings(invoke: InvokeCli):
215+
@click.command(
216+
cls=RichCommand, context_settings={"rich_console": Console(), "rich_help_config": RichHelpConfiguration()}
217+
)
218+
@click.pass_context
219+
def cli(ctx: RichContext):
220+
assert isinstance(ctx, RichContext)
221+
assert ctx.console is not None
222+
assert ctx.help_config is not None
223+
224+
result = invoke(cli)
225+
assert result.exit_code == 0
226+
assert result.exception is None

0 commit comments

Comments
 (0)