mock.os.process

Submodule that mocks Run.

Attributes

P

Exceptions

MockRunError

Error raised by mock_run.

UnexpectedCommandError

Error raised when MockRun receives an unexpected command.

Classes

ArgumentChecker

Argument checker.

GlobChecker

Check an argument against a glob.

SideEffect

Function to be called when a mocked command is called.

CommandResult

Result of a command.

MockRun

Mock e3.os.process.Run.

Functions

mock_run(→ collections.abc.Iterator[MockRun])

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

Module Contents

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(config: MockRunConfig | None = None) collections.abc.Iterator[MockRun]

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

Parameters:

config – config for the mock

Returns:

new MockRun instance

class mock.os.process.ArgumentChecker

Bases: Protocol

Argument checker.

check(arg: str) bool

Check an argument.

Parameters:

arg – the argument

Returns:

if the argument is valid

__repr__() str

Return a textual representation of the expected argument.

class mock.os.process.GlobChecker(pattern: str)

Bases: ArgumentChecker

Check an argument against a glob.

pattern
check(arg: str) bool

See ArgumentChecker.

__repr__() str

See ArgumentChecker.

class mock.os.process.SideEffect

Bases: Protocol

Function to be called when a mocked command is called.

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

Run when the mocked command is called.

Parameters:
  • result – the mocked command

  • cmd – actual arguments of the command

  • args – additional arguments for Run

  • kwargs – additional keyword arguments for Run

class mock.os.process.CommandResult(cmd: list[str | ArgumentChecker], status: int | None = None, raw_out: bytes = b'', raw_err: bytes = b'', side_effect: SideEffect | 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.

cmd
status
raw_out = b''
raw_err = b''
side_effect = None
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.

config: MockRunConfig
call_count = 0
property all_called: bool

Check all expected commands have been run.

add_result(result: CommandResult | collections.abc.Iterable[CommandResult]) None

Queue one or multiple command results.

Parameters:

result – new command results

__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