CLI testing and executionΒΆ
Todo
Write example and tutorial.
click_extra.testing
APIΒΆ
classDiagram BytesIO <|-- BytesIOCopy CliRunner <|-- ExtraCliRunner Result <|-- ExtraResult
CLI testing and simulation of their execution.
- click_extra.testing.INDENT = ' 'ΒΆ
Constants for rendering of CLI execution.
- click_extra.testing.EnvVarsΒΆ
Type for
dict
-like environment variables.
- click_extra.testing.NestedArgsΒΆ
Types for arbitrary nested CLI arguments.
Arguments can be
str
,pathlib.Path
objects orNone
values.
- click_extra.testing.args_cleanup(*args)[source]ΒΆ
Flatten recursive iterables, remove all
None
, and cast each element to strings.Helps serialize
pathlib.Path
and other objects.It also allows for nested iterables and
None
values as CLI arguments for convenience. We just need to flatten and filters them out.
- click_extra.testing.format_cli_prompt(cmd_args, extra_env=None)[source]ΒΆ
Simulate the console prompt used to invoke the CLI.
- Return type:
- click_extra.testing.print_cli_run(args, result, env=None)[source]ΒΆ
Prints the full simulation of CLI execution, including output.
Mostly used to print debug traces to user or in test results.
- Return type:
- click_extra.testing.env_copy(extend=None)[source]ΒΆ
Returns a copy of the current environment variables and eventually
extend
it.Mimics Pythonβs original implementation by returning
None
if noextend
content are provided.Environment variables are expected to be a
dict
ofstr:str
.
- click_extra.testing.run_cmd(*args, extra_env=None, print_output=True)[source]ΒΆ
Run a system command, print output and return results.
- click_extra.testing.INVOKE_ARGS = {'args', 'catch_exceptions', 'cli', 'color', 'env', 'input', 'self'}ΒΆ
Parameter IDs of
click.testing.CliRunner.invoke()
.We need to collect them to help us identify which extra parameters passed to
invoke()
collides with its original signature.Warning
This has been reported upstream to Click project but has been rejected and not considered an issue worth fixing.
- class click_extra.testing.BytesIOCopy(copy_to)[source]ΒΆ
Bases:
BytesIO
Patch
io.BytesIO
to let the written stream be copied to another.Caution
This has been proposed upstream to Click project but has not been merged yet.
- class click_extra.testing.StreamMixer(mix_stderr)[source]ΒΆ
Bases:
object
Mixes
<stdout>
and<stderr>
streams ifmix_stderr=True
.The result is available in the
output
attribute.If
mix_stderr=False
, the<stdout>
and<stderr>
streams are kept independent and theoutput
is the same as the<stdout>
stream.Caution
This has been proposed upstream to Click project but has not been merged yet.
- class click_extra.testing.ExtraResult(runner, stdout_bytes, stderr_bytes, output_bytes, return_value, exit_code, exception, exc_info=None)[source]ΒΆ
Bases:
Result
Like
click.testing.Result
, with finer<stdout>
and<stderr>
streams.Caution
This has been proposed upstream to Click project but has not been merged yet.
Same as original but adds
output_bytes
parameter.Also makes
stderr_bytes
mandatory.
- class click_extra.testing.ExtraCliRunner(charset='utf-8', env=None, echo_stdin=False, mix_stderr=True)[source]ΒΆ
Bases:
CliRunner
Augment
click.testing.CliRunner
with extra features and bug fixes.- env: t.Mapping[str, t.Optional[str]]ΒΆ
- force_color: bool = FalseΒΆ
Global class attribute to override the
color
parameter ininvoke
.Note
This was initially developed to force the initialization of the runner during the setup of Sphinx new directives. This was the only way we found, as to patch some code we had to operate at the class level.
- isolation(input=None, env=None, color=False)[source]ΒΆ
Copy of
click.testing.CliRunner.isolation()
with extra features. :rtype:Iterator
[tuple
[BytesIO
,BytesIO
,BytesIO
]]An additional output stream is returned, which is a mix of
<stdout>
and<stderr>
streams ifmix_stderr=True
.Always returns the
<stderr>
stream.
Caution
This is a hard-copy of the modified
isolation()
method from click#2523 PR which has not been merged upstream yet.Todo
Reduce the code duplication here by using clever monkeypatching?
- invoke2(cli, args=None, input=None, env=None, catch_exceptions=True, color=False, **extra)[source]ΒΆ
Copy of
click.testing.CliRunner.invoke()
with extra<output>
stream. :rtype:ExtraResult
Caution
This is a hard-copy of the modified
invoke()
method from click#2523 PR which has not been merged upstream yet.Todo
Reduce the code duplication here by using clever monkeypatching?
- invoke(cli, *args, input=None, env=None, catch_exceptions=True, color=None, **extra)[source]ΒΆ
Same as
click.testing.CliRunner.invoke()
with extra features.The first positional parameter is the CLI to invoke. The remaining positional parameters of the function are the CLI arguments. All other parameters are required to be named.
The CLI arguments can be nested iterables of arbitrary depth. This is useful for argument composition of test cases with @pytest.mark.parametrize.
Allow forcing of the
color
property at the class-level viaforce_color
attribute.Adds a special case in the form of
color="forced"
parameter, which allows colored output to be kept, while forcing the initialization ofContext.color = True
. This is not allowed in current implementation ofclick.testing.CliRunner.invoke()
because of colliding parameters.Strips all ANSI codes from results if
color
was explicirely set toFalse
.Always prints a simulation of the CLI execution as the user would see it in its terminal. Including colors.
Pretty-prints a formatted exception traceback if the command fails.
- Parameters:
cli (
BaseCommand
) β CLI to invoke.*args β
can be nested iterables composed of
str
,pathlib.Path
objects andNone
values. The nested structure will be flattened andNone
values will be filtered out. Then all elements will be casted tostr
. Seeargs_cleanup()
for details.input (
str
|bytes
|IO
|None
) β same asclick.testing.CliRunner.invoke()
.env (
Optional
[Mapping
[str
,Optional
[str
]]]) β same asclick.testing.CliRunner.invoke()
.catch_exceptions (
bool
) β same asclick.testing.CliRunner.invoke()
.color (
Union
[bool
,Literal
['forced'
],None
]) β If a boolean, the parameter will be passed as-is toclick.testing.CliRunner.isolation()
. If"forced"
, the parameter will be passed asTrue
toclick.testing.CliRunner.isolation()
and an extracolor=True
parameter will be passed to the invoked CLI.**extra β
same as
click.testing.CliRunner.invoke()
, but colliding parameters are allowed and properly passed on to the invoked CLI.
- Return type:
Result