class documentation

class CliRunner:

View In Hierarchy

The CLI runner provides functionality to invoke a Click command line script for unittesting purposes in a isolated environment. This only works in single-threaded systems without any concurrency as it changes the global interpreter state.
Parameters
charsetthe character set for the input and output data.
enva dictionary with environment variables for overriding.
echo​_stdinif this is set to True, then reading from stdin writes to stdout. This is useful for showing examples in some circumstances. Note that regular prompts will automatically echo the input.
mix​_stderrif this is set to False, then stdout and stderr are preserved as independent streams. This is useful for Unix-philosophy apps that have predictable stdout and noisy stderr, such that each may be measured independently
Method get​_default​_prog​_name Given a command object it will return the default program name for it. The default is the name attribute or "root" if not set.
Method invoke No summary
Method isolated​_filesystem No summary
Method isolation No summary
Method make​_env Returns the environment overrides for invoking a script.
Method __init__ Undocumented
Instance Variable charset Undocumented
Instance Variable echo​_stdin Undocumented
Instance Variable env Undocumented
Instance Variable mix​_stderr Undocumented
def get_default_prog_name(self, cli):
Given a command object it will return the default program name for it. The default is the name attribute or "root" if not set.
Parameters
cli:BaseCommandUndocumented
Returns
strUndocumented
def invoke(self, cli, args=None, input=None, env=None, catch_exceptions=True, color=False, **extra):

Invokes a command in an isolated environment. The arguments are forwarded directly to the command line script, the extra keyword arguments are passed to the ~clickpkg.Command.main function of the command.

This returns a Result object.

Changed in version 8.0: The result object has the return_value attribute with the value returned from the invoked command.
Changed in version 4.0: Added the color parameter.
Changed in version 3.0: Added the catch_exceptions parameter.
Changed in version 3.0: The result object has the exc_info attribute with the traceback if available.
Parameters
cli:BaseCommandthe command to invoke
args:t.Optional[t.Union[str, t.Sequence[str]]]the arguments to invoke. It may be given as an iterable or a string. When given as string it will be interpreted as a Unix shell command. More details at shlex.split.
input:t.Optional[t.Union[str, bytes, t.IO]]the input data for sys.stdin.
env:t.Optional[t.Mapping[str, t.Optional[str]]]the environment overrides.
catch​_exceptions:boolWhether to catch any other exceptions than SystemExit.
color:boolwhether the output should contain color codes. The application can still override this explicitly.
**extra:t.Anythe keyword arguments to pass to main.
Returns
ResultUndocumented
@contextlib.contextmanager
def isolated_filesystem(self, temp_dir=None):

A context manager that creates a temporary directory and changes the current working directory to it. This isolates tests that affect the contents of the CWD to prevent them from interfering with each other.

Changed in version 8.0: Added the temp_dir parameter.
Parameters
temp​_dir:t.Optional[t.Union[str, os.PathLike]]Create the temporary directory under this directory. If given, the created directory is not removed when exiting.
Returns
t.Iterator[str]Undocumented
@contextlib.contextmanager
def isolation(self, input=None, env=None, color=False):

A context manager that sets up the isolation for invoking of a command line tool. This sets up stdin with the given input data and os.environ with the overrides from the given dictionary. This also rebinds some internals in Click to be mocked (like the prompt functionality).

This is automatically done in the invoke method.

Changed in version 8.0: stderr is opened with errors="backslashreplace" instead of the default "strict".
Changed in version 4.0: Added the color parameter.
Parameters
input:t.Optional[t.Union[str, bytes, t.IO]]the input stream to put into sys.stdin.
env:t.Optional[t.Mapping[str, t.Optional[str]]]the environment overrides as dictionary.
color:boolwhether the output should contain color codes. The application can still override this explicitly.
Returns
t.Iterator[t.Tuple[io.BytesIO, t.Optional[io.BytesIO]]]Undocumented
def make_env(self, overrides=None):
Returns the environment overrides for invoking a script.
Parameters
overrides:t.Optional[t.Mapping[str, t.Optional[str]]]Undocumented
Returns
t.Mapping[str, t.Optional[str]]Undocumented
def __init__(self, charset='utf-8', env=None, echo_stdin=False, mix_stderr=True):

Undocumented

Parameters
charset:strUndocumented
env:t.Optional[t.Mapping[str, t.Optional[str]]]Undocumented
echo​_stdin:boolUndocumented
mix​_stderr:boolUndocumented
charset =

Undocumented

echo_stdin =

Undocumented

env =

Undocumented

mix_stderr =

Undocumented