Skip to content

Commit b84e270

Browse files
committed
Increase test coverage.
1 parent cf77534 commit b84e270

File tree

8 files changed

+103
-6
lines changed

8 files changed

+103
-6
lines changed

consolekit/_readline.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
# pragma: no cover
2-
31
# stdlib
42
import sys
53

6-
if not bool(getattr(sys, "ps1", sys.flags.interactive)):
4+
if not bool(getattr(sys, "ps1", sys.flags.interactive)): # pragma: no cover
75

86
try:
97
# stdlib

tests/test_commands.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def demo() -> None:
8080
* ~~The other~~ (deprecated)
8181
"""
8282

83+
@colour_option()
8384
@demo.command(cls=consolekit.commands.MarkdownHelpCommand)
8485
def foo() -> None:
8586
"""
@@ -207,6 +208,9 @@ def test_markdown_help_group(
207208
result = cli_runner.invoke(demo_command, args=["--help"], color=True)
208209
result.check_stdout(advanced_file_regression, extension="_command.md")
209210

211+
result = cli_runner.invoke(demo_command, args=["--no-colour", "--help"], color=True)
212+
result.check_stdout(advanced_file_regression, extension="_command_no_colour.md")
213+
210214

211215
@pytest.mark.usefixtures("force_not_pycharm")
212216
@not_windows("Windows support for bold and italics is non-existent.")

tests/test_commands_/test_markdown_help_group_command.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ Usage: foo [OPTIONS]
88
* T̶h̶e̶ ̶o̶t̶h̶e̶r̶ (deprecated)
99

1010
Options:
11-
--help Show this message and exit.
11+
--colour / --no-colour Whether to use coloured output.
12+
--help Show this message and exit.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Usage: foo [OPTIONS]
2+
3+
This is the summary line.
4+
5+
This program does the 'following':
6+
* This
7+
* That
8+
* The other (deprecated)
9+
10+
Options:
11+
--colour / --no-colour Whether to use coloured output.
12+
--help Show this message and exit.

tests/test_input.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# stdlib
2+
import sys
23
from typing import Type
34

45
# 3rd party
@@ -145,6 +146,49 @@ def fake_input(prompt: str) -> str:
145146
advanced_data_regression.check(list(StringList(capsys.readouterr().out.splitlines())))
146147

147148

149+
def test_prompt_err(
150+
capsys,
151+
monkeypatch,
152+
advanced_data_regression: AdvancedDataRegressionFixture,
153+
):
154+
155+
inputs = iter([
156+
'',
157+
'',
158+
'',
159+
'',
160+
"24",
161+
"Bond007",
162+
"badpassword",
163+
"baspassword",
164+
"badpassword",
165+
"badpassword",
166+
"badpassword",
167+
"badpassword",
168+
])
169+
170+
class FakeInput:
171+
172+
def readline(self) -> str:
173+
value = next(inputs)
174+
return value + '\n'
175+
176+
monkeypatch.setattr(sys, "stdin", FakeInput())
177+
178+
assert prompt(text="What is your age", prompt_suffix="? ", type=click.INT, err=True) == 24
179+
180+
assert prompt(text="Username", type=click.STRING, err=True) == "Bond007"
181+
assert prompt(text="Password", type=click.STRING, confirmation_prompt=True, err=True) == "badpassword"
182+
assert prompt(
183+
text="Password",
184+
type=click.STRING,
185+
confirmation_prompt="Are you sure about that? ",
186+
err=True,
187+
) == "badpassword"
188+
189+
advanced_data_regression.check(list(StringList(capsys.readouterr().err.splitlines())))
190+
191+
148192
@pytest.mark.parametrize("exception", [KeyboardInterrupt, EOFError])
149193
def test_prompt_abort(
150194
capsys,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- 'What is your age? What is your age? What is your age? What is your age? What is
2+
your age? Username: Password: Repeat for confirmation: Error: the two entered values
3+
do not match'
4+
- 'Password: Repeat for confirmation: Password: Are you sure about that?'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
- What is your age?
2+
- What is your age?
3+
- What is your age?
4+
- What is your age?
5+
- What is your age? 24
6+
- 'Username: Bond007'
7+
- 'Password: badpassword'
8+
- 'Repeat for confirmation: baspassword'
9+
- 'Error: the two entered values do not match'
10+
- 'Password: badpassword'
11+
- 'Repeat for confirmation: badpassword'
12+
- 'Password: badpassword'
13+
- Are you sure about that? badpassword

tests/test_options.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# stdlib
22
import sys
3-
from typing import Iterable
3+
from typing import Iterable, List
44

55
# 3rd party
66
import click
@@ -10,6 +10,7 @@
1010

1111
# this package
1212
from consolekit import click_command
13+
import consolekit
1314
from consolekit.options import (
1415
ChoiceOption,
1516
DescribedArgument,
@@ -353,7 +354,7 @@ def test_lazy_choice(
353354
cli_runner: CliRunner,
354355
):
355356

356-
def expensive_operation():
357+
def expensive_operation() -> List[str]:
357358
print("Performing expensive operation to get choices.")
358359
return ["Radio 1", "Radio 2", "Radio 3"]
359360

@@ -422,6 +423,26 @@ def main(station: str) -> None:
422423
assert result.exit_code == 0
423424
assert result.stdout.rstrip() == "Tuning to station: Radio 1"
424425

426+
@consolekit.option(
427+
"-s",
428+
"--station",
429+
help="The station to play.",
430+
type=click.Choice(["Radio 1", "Radio 2", "Radio 3"], case_sensitive=False),
431+
cls=PromptOption,
432+
prompt="Select a station",
433+
)
434+
@click_command()
435+
def main2(station: str) -> None:
436+
print(f"Tuning to station: {station}")
437+
438+
result = cli_runner.invoke(main2, input="Radio 4\nRadio 2\n")
439+
assert result.exit_code == 0
440+
advanced_file_regression.check(result.stdout.rstrip())
441+
442+
result = cli_runner.invoke(main2, args=["--station", "Radio 1"])
443+
assert result.exit_code == 0
444+
assert result.stdout.rstrip() == "Tuning to station: Radio 1"
445+
425446

426447
@pytest.mark.parametrize(
427448
"click_version",

0 commit comments

Comments
 (0)