Skip to content
Myr

test_others.py

This file

Path

tests/test_others.py

211 lines · 6.6 KiB · Python

Last changed by

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

History of this file

Take it

The walk stopped after 200 commits: lines older than 730a034 point at it.

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

lines 1–3why, one step back
1import os
2import subprocess3from pathlib import Path

:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a

line 4why, one step back
4from unittest import mock

:memo: Update release notesSebastián Ramírezbefore adoption730a034

line 5why, one step back
5

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

lines 6–7why, one step back
6import click
7import pytest

:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a

line 8why, one step back
8import shellingham

:memo: Update release notesSebastián Ramírezbefore adoption730a034

line 9why, one step back
9import typer

:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a

line 10why, one step back
10import typer.completion

:memo: Update release notesSebastián Ramírezbefore adoption730a034

lines 11–27why, one step back
11from typer.main import solve_typer_info_defaults, solve_typer_info_help
12from typer.models import TyperInfo13from typer.testing import CliRunner1415runner = CliRunner()161718def test_help_from_info():19 # Mainly for coverage/completeness20 value = solve_typer_info_help(TyperInfo())21 assert value is None222324def test_defaults_from_info():25 # Mainly for coverage/completeness26 value = solve_typer_info_defaults(TyperInfo())27 assert value

:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a

lines 28–34why, one step back
28
2930def test_install_invalid_shell():31 app = typer.Typer()3233 @app.command()34 def main():

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 35why, one step back
35 print("Hello World")

:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a

lines 36–43why, one step back
36
37 with mock.patch.object(38 shellingham, "detect_shell", return_value=("xshell", "/usr/bin/xshell")39 ):40 result = runner.invoke(app, ["--install-completion"])41 assert "Shell xshell is not supported." in result.stdout42 result = runner.invoke(app)43 assert "Hello World" in result.stdout

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

lines 44–66why, one step back
44
4546def test_callback_too_many_parameters():47 app = typer.Typer()4849 def name_callback(ctx, param, val1, val2):50 pass # pragma: nocover5152 @app.command()53 def main(name: str = typer.Option(..., callback=name_callback)):54 pass # pragma: nocover5556 with pytest.raises(click.ClickException) as exc_info:57 runner.invoke(app, ["--name", "Camila"])58 assert (59 exc_info.value.message == "Too many CLI parameter callback function parameters"60 )616263def test_callback_2_untyped_parameters():64 app = typer.Typer()6566 def name_callback(ctx, value):

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 67–68why, one step back
67 print(f"info name is: {ctx.info_name}")
68 print(f"value is: {value}")

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

lines 69–71why, one step back
69
70 @app.command()71 def main(name: str = typer.Option(..., callback=name_callback)):

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 72why, one step back
72 print("Hello World")

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

lines 73–82why, one step back
73
74 result = runner.invoke(app, ["--name", "Camila"])75 assert "info name is: main" in result.stdout76 assert "value is: Camila" in result.stdout777879def test_callback_3_untyped_parameters():80 app = typer.Typer()8182 def name_callback(ctx, param, value):

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 83–85why, one step back
83 print(f"info name is: {ctx.info_name}")
84 print(f"param name is: {param.name}")85 print(f"value is: {value}")

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

lines 86–88why, one step back
86
87 @app.command()88 def main(name: str = typer.Option(..., callback=name_callback)):

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 89why, one step back
89 print("Hello World")

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

lines 90–111why, one step back
90
91 result = runner.invoke(app, ["--name", "Camila"])92 assert "info name is: main" in result.stdout93 assert "param name is: name" in result.stdout94 assert "value is: Camila" in result.stdout959697def test_completion_untyped_parameters():98 file_path = Path(__file__).parent / "assets/completion_no_types.py"99 result = subprocess.run(100 ["coverage", "run", str(file_path)],101 stdout=subprocess.PIPE,102 stderr=subprocess.PIPE,103 encoding="utf-8",104 env={105 **os.environ,106 "_COMPLETION_NO_TYPES.PY_COMPLETE": "complete_zsh",107 "_TYPER_COMPLETE_ARGS": "completion_no_types.py --name Sebastian --name Ca",108 "_TYPER_COMPLETE_TESTING": "True",109 },110 )111 assert "info name is: completion_no_types.py" in result.stderr

✨ Add support for Click 8 while keeping compatibility with Click 7 (#317)Sebastián Ramírezbefore adoptionb972981

lines 112–116why, one step back
112 # TODO: when deprecating Click 7, remove second option
113 assert (114 "args is: []" in result.stderr115 or "args is: ['--name', 'Sebastian', '--name']" in result.stderr116 )

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

lines 117–144why, one step back
117 assert "incomplete is: Ca" in result.stderr
118 assert '"Camila":"The reader of books."' in result.stdout119 assert '"Carlos":"The writer of scripts."' in result.stdout120121 result = subprocess.run(122 ["coverage", "run", str(file_path)],123 stdout=subprocess.PIPE,124 stderr=subprocess.PIPE,125 encoding="utf-8",126 )127 assert "Hello World" in result.stdout128129130def test_completion_untyped_parameters_different_order_correct_names():131 file_path = Path(__file__).parent / "assets/completion_no_types_order.py"132 result = subprocess.run(133 ["coverage", "run", str(file_path)],134 stdout=subprocess.PIPE,135 stderr=subprocess.PIPE,136 encoding="utf-8",137 env={138 **os.environ,139 "_COMPLETION_NO_TYPES_ORDER.PY_COMPLETE": "complete_zsh",140 "_TYPER_COMPLETE_ARGS": "completion_no_types_order.py --name Sebastian --name Ca",141 "_TYPER_COMPLETE_TESTING": "True",142 },143 )144 assert "info name is: completion_no_types_order.py" in result.stderr

✨ Add support for Click 8 while keeping compatibility with Click 7 (#317)Sebastián Ramírezbefore adoptionb972981

lines 145–149why, one step back
145 # TODO: when deprecating Click 7, remove second option
146 assert (147 "args is: []" in result.stderr148 or "args is: ['--name', 'Sebastian', '--name']" in result.stderr149 )

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

lines 150–175why, one step back
150 assert "incomplete is: Ca" in result.stderr
151 assert '"Camila":"The reader of books."' in result.stdout152 assert '"Carlos":"The writer of scripts."' in result.stdout153154 result = subprocess.run(155 ["coverage", "run", str(file_path)],156 stdout=subprocess.PIPE,157 stderr=subprocess.PIPE,158 encoding="utf-8",159 )160 assert "Hello World" in result.stdout161162163def test_autocompletion_too_many_parameters():164 app = typer.Typer()165166 def name_callback(ctx, args, incomplete, val2):167 pass # pragma: nocover168169 @app.command()170 def main(name: str = typer.Option(..., autocompletion=name_callback)):171 pass # pragma: nocover172173 with pytest.raises(click.ClickException) as exc_info:174 runner.invoke(app, ["--name", "Camila"])175 assert exc_info.value.message == "Invalid autocompletion callback parameters: val2"

:sparkles: Add support for forward references (#93)Sebastián Ramírezbefore adoptiona93e6b2

lines 176–182why, one step back
176
177178def test_forward_references():179 app = typer.Typer()180181 @app.command()182 def main(arg1, arg2: int, arg3: "int", arg4: bool = False, arg5: "bool" = False):

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 183–187why, one step back
183 print(f"arg1: {type(arg1)} {arg1}")
184 print(f"arg2: {type(arg2)} {arg2}")185 print(f"arg3: {type(arg3)} {arg3}")186 print(f"arg4: {type(arg4)} {arg4}")187 print(f"arg5: {type(arg5)} {arg5}")

:sparkles: Add support for forward references (#93)Sebastián Ramírezbefore adoptiona93e6b2

lines 188–189why, one step back
188
189 result = runner.invoke(app, ["Hello", "2", "invalid"])

✨ Add support for Click 8 while keeping compatibility with Click 7 (#317)Sebastián Ramírezbefore adoptionb972981

lines 190–191why, one step back
190 # TODO: when deprecating Click 7, remove second option
191

:sparkles: Add support for forward references (#93)Sebastián Ramírezbefore adoptiona93e6b2

line 192why, one step back
192 assert (

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 193–194why, one step back
193 "Invalid value for 'ARG3': 'invalid' is not a valid integer" in result.stdout
194 or "Invalid value for 'ARG3': invalid is not a valid integer" in result.stdout

:sparkles: Add support for forward references (#93)Sebastián Ramírezbefore adoptiona93e6b2

lines 195–200why, one step back
195 )
196 result = runner.invoke(app, ["Hello", "2", "3", "--arg4", "--arg5"])197 assert (198 "arg1: <class 'str'> Hello\narg2: <class 'int'> 2\narg3: <class 'int'> 3\narg4: <class 'bool'> True\narg5: <class 'bool'> True\n"199 in result.stdout200 )

🐛 Fix `context_settings` for a Typer app with a single command (#210)Leron Graybefore adoption778d5bd

lines 201–211why, one step back
201
202203def test_context_settings_inheritance_single_command():204 app = typer.Typer(context_settings=dict(help_option_names=["-h", "--help"]))205206 @app.command()207 def main(name: str):208 pass # pragma: nocover209210 result = runner.invoke(app, ["main", "-h"])211 assert "Show this message and exit." in result.stdout