Skip to content

Commit fa7cdbd

Browse files
committed
Add tests for MultiValueOption, and make its default value ()
1 parent 570abef commit fa7cdbd

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

consolekit/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def __init__(
326326
hidden: bool = False,
327327
type: Optional[_ConvertibleType] = None, # noqa: A002
328328
required: bool = False,
329-
default: Optional[Any] = None,
329+
default: Optional[Any] = (),
330330
callback: Optional[Callback] = None,
331331
metavar: Optional[str] = None,
332332
expose_value: bool = True,

tests/test_options.py

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# stdlib
2-
from typing import Optional
2+
import sys
3+
from typing import Iterable
34

45
# 3rd party
56
import click
@@ -10,25 +11,32 @@
1011
# this package
1112
from consolekit import click_command
1213
from consolekit.options import (
14+
MultiValueOption,
1315
auto_default_option,
1416
colour_option,
1517
flag_option,
1618
force_option,
1719
no_pager_option,
18-
verbose_option
20+
verbose_option,
21+
version_option
1922
)
23+
from consolekit.terminal_colours import ColourTrilean
2024

2125

22-
def test_auto_default_option():
26+
def test_auto_default_option(file_regression: FileRegressionFixture):
2327

24-
@auto_default_option("--width", type=click.INT)
28+
@auto_default_option("--width", type=click.INT, help="The max width to display.", show_default=True)
2529
@click_command()
2630
def main(width: int = 80):
2731
print(width)
2832

2933
runner = CliRunner()
3034

31-
result: Result = runner.invoke(main, catch_exceptions=False)
35+
result: Result = runner.invoke(main, catch_exceptions=False, args="--help")
36+
check_file_regression(result.stdout.rstrip(), file_regression, extension=".md")
37+
assert result.exit_code == 0
38+
39+
result = runner.invoke(main, catch_exceptions=False)
3240
assert result.stdout.rstrip() == "80"
3341
assert result.exit_code == 0
3442

@@ -147,7 +155,7 @@ def test_colour_option(file_regression: FileRegressionFixture):
147155

148156
@colour_option()
149157
@click_command()
150-
def main(colour: Optional[bool]):
158+
def main(colour: ColourTrilean):
151159
print(colour)
152160

153161
runner = CliRunner()
@@ -167,3 +175,66 @@ def main(colour: Optional[bool]):
167175
result = runner.invoke(main, catch_exceptions=False, args="-h")
168176
check_file_regression(result.stdout.rstrip(), file_regression)
169177
assert result.exit_code == 0
178+
179+
180+
def test_version_option(file_regression: FileRegressionFixture):
181+
182+
def version_callback(ctx: click.Context, param: click.Option, value: int):
183+
if not value or ctx.resilient_parsing:
184+
return
185+
186+
if value > 1:
187+
click.echo("consolekit version 1.2.3, Python 3.8.5")
188+
else:
189+
click.echo("consolekit version 1.2.3")
190+
191+
ctx.exit()
192+
193+
@version_option(version_callback)
194+
@click_command()
195+
def main():
196+
sys.exit(1)
197+
198+
runner = CliRunner()
199+
200+
result = runner.invoke(main, catch_exceptions=False, args="--version")
201+
assert result.stdout.rstrip() == "consolekit version 1.2.3"
202+
assert result.exit_code == 0
203+
204+
result = runner.invoke(main, catch_exceptions=False, args=["--version", "--version"])
205+
assert result.stdout.rstrip() == "consolekit version 1.2.3, Python 3.8.5"
206+
assert result.exit_code == 0
207+
208+
209+
def test_multi_value_option(file_regression: FileRegressionFixture):
210+
211+
@click.option(
212+
"--select",
213+
type=click.STRING,
214+
help="The checks to enable",
215+
cls=MultiValueOption,
216+
)
217+
@colour_option()
218+
@click_command()
219+
def main(select: Iterable[str], colour: bool):
220+
select = list(select)
221+
print(*select)
222+
print(", ".join(select))
223+
224+
runner = CliRunner()
225+
226+
result: Result = runner.invoke(main, catch_exceptions=False)
227+
assert result.stdout.rstrip() == ''
228+
assert result.exit_code == 0
229+
230+
result = runner.invoke(main, catch_exceptions=False, args=["--select", "E102", "F223"])
231+
assert result.stdout.rstrip() == "E102 F223\nE102, F223"
232+
assert result.exit_code == 0
233+
234+
result = runner.invoke(main, catch_exceptions=False, args=["--select", "E102", "F223", "--colour"])
235+
assert result.stdout.rstrip() == "E102 F223\nE102, F223"
236+
assert result.exit_code == 0
237+
238+
result = runner.invoke(main, catch_exceptions=False, args=["--select", "E102"])
239+
assert result.stdout.rstrip() == "E102\nE102"
240+
assert result.exit_code == 0

0 commit comments

Comments
 (0)