mock.os.process

Submodule that mocks Run.

Module Contents

Classes

CommandResult

Result of a command.

MockRun

Mock e3.os.process.Run.

RunPatcher

Patch e3.os.process.Run when used as a context or decorator.

Functions

mock_run(…)

Mock e3.os.process.Run as a context or decorator.

Attributes

P

mock.os.process.P
exception mock.os.process.MockRunError

Bases: Exception

Error raised by mock_run.

exception mock.os.process.UnexpectedCommandError

Bases: MockRunError

Error raised when MockRun receives an unexpected command.

mock.os.process.mock_run(func: Callable[P, T]) Callable[P, T]
mock.os.process.mock_run(func: None = None, config: MockRunConfig | None = None) RunPatcher

Mock e3.os.process.Run as a context or decorator.

Parameters:
  • func – decorated function

  • config – config for the mock

Returns:

new RunPatcher instance or decorator

class mock.os.process.CommandResult(cmd: list[str], status: int | None = None, raw_out: bytes | None = None, raw_err: bytes | None = None)

Result of a command.

When a command is supposed to be run, the actual status code and logs are taken from this object.

check(cmd: list[str]) None

Check that cmd matches the expected arguments.

Parameters:

cmd – actual command

__call__(cmd: list[str], *args: Any, **kwargs: Any) None

Allow to run code to emulate the command.

This function is called when a command is supposed to be run by Run. It takes the same arguments as Run.__init__.

Parameters:
  • cmd – actual arguments of the command

  • args – additional arguments for Run

  • kwargs – additional keyword arguments for Run

class mock.os.process.MockRun(config: MockRunConfig | None = None)

Bases: e3.os.process.Run

Mock e3.os.process.Run.

property all_called: bool

Check all expected commands have been run.

add_result(result: CommandResult) None

Queue a command result.

Parameters:

result – new command result

__call__(cmds: e3.os.process.AnyCmdLine, *args: Any, **kwargs: Any) e3.os.process.Run

Emulate how e3.os.process.Run.__init__ works.

Once e3.os.process.Run is patched, this function will be called instead.

Parameters:
  • cmds – command line

  • args – unhandled arguments

  • kwargs – unhandled keyword arguments

class mock.os.process.RunPatcher(config: MockRunConfig | None = None)

Patch e3.os.process.Run when used as a context or decorator.

_mock_run: MockRun | None

Instance of MockRun.

_nested_count = 0

How many RunPatcher are currently alive.

_mock_init_lock

Lock to avoid concurrency when patching/unpatching.

__call__(func: Callable[P, T]) Callable[P, T]

Decorate func when mock_run is used as a decorator.

Parameters:

func – decorated function

Returns:

a decorator for calling func

__enter__() MockRun

Enter the context manager.

Returns:

MockRun instance

__exit__(*args: Any) None

Exit the context manager.

start() None

Start mocking e3.os.process.Run.

stop() None

Stop mocking e3.os.process.Run.

_decorate_callable(func: Callable) Callable

Decorate the callable function.

Parameters:

func – decorated function

Returns:

a decorator for calling func