LogCliRunner

class lsst.daf.butler.cli.utils.LogCliRunner(charset: str = 'utf-8', env: Optional[Mapping[str, Optional[str]]] = None, echo_stdin: bool = False, mix_stderr: bool = True)

Bases: CliRunner

A test runner to use when the logging system will be initialized by code under test, calls CliLog.resetLog(), which undoes any logging setup that was done with the CliLog interface.

lsst.log modules can not be set back to an uninitialized state (python logging modules can be set back to NOTSET), instead they are set to CliLog.defaultLsstLogLevel.

Methods Summary

get_default_prog_name(cli)

Given a command object it will return the default program name for it.

invoke(*args, **kwargs)

Invokes a command in an isolated environment.

isolated_filesystem([temp_dir])

A context manager that creates a temporary directory and changes the current working directory to it.

isolation([input, env, color])

A context manager that sets up the isolation for invoking of a command line tool.

make_env([overrides])

Returns the environment overrides for invoking a script.

Methods Documentation

get_default_prog_name(cli: BaseCommand) str

Given a command object it will return the default program name for it. The default is the name attribute or "root" if not set.

invoke(*args, **kwargs)

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 main() function of the command.

This returns a Result object.

Parameters:
  • cli – the command to invoke

  • args – 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 – the input data for sys.stdin.

  • env – the environment overrides.

  • catch_exceptions – Whether to catch any other exceptions than SystemExit.

  • extra – the keyword arguments to pass to main().

  • color – whether the output should contain color codes. The application can still override this explicitly.

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.

isolated_filesystem(temp_dir: Optional[Union[str, PathLike]] = None) Iterator[str]

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.

Parameters:

temp_dir – Create the temporary directory under this directory. If given, the created directory is not removed when exiting.

Changed in version 8.0: Added the temp_dir parameter.

isolation(input: Optional[Union[str, bytes, IO]] = None, env: Optional[Mapping[str, Optional[str]]] = None, color: bool = False) Iterator[Tuple[BytesIO, Optional[BytesIO]]]

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.

Parameters:
  • input – the input stream to put into sys.stdin.

  • env – the environment overrides as dictionary.

  • color – whether the output should contain color codes. The application can still override this explicitly.

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.

make_env(overrides: Optional[Mapping[str, Optional[str]]] = None) Mapping[str, Optional[str]]

Returns the environment overrides for invoking a script.