MCPcopy Index your code
hub / github.com/pyinvoke/invoke / MockContext

Class MockContext

invoke/context.py:414–616  ·  view source on GitHub ↗

A `.Context` whose methods' return values can be predetermined. Primarily useful for testing Invoke-using codebases. .. note:: This class wraps its ``run``, etc methods in `unittest.mock.Mock` objects. This allows you to easily assert that the methods (still re

Source from the content-addressed store, hash-verified

412
413
414class MockContext(Context):
415 """
416 A `.Context` whose methods' return values can be predetermined.
417
418 Primarily useful for testing Invoke-using codebases.
419
420 .. note::
421 This class wraps its ``run``, etc methods in `unittest.mock.Mock`
422 objects. This allows you to easily assert that the methods (still
423 returning the values you prepare them with) were actually called.
424
425 .. note::
426 Methods not given `Results <.Result>` to yield will raise
427 ``NotImplementedError`` if called (since the alternative is to call the
428 real underlying method - typically undesirable when mocking.)
429
430 .. versionadded:: 1.0
431 .. versionchanged:: 1.5
432 Added ``Mock`` wrapping of ``run`` and ``sudo``.
433 """
434
435 def __init__(self, config: Optional[Config] = None, **kwargs: Any) -> None:
436 """
437 Create a ``Context``-like object whose methods yield `.Result` objects.
438
439 :param config:
440 A Configuration object to use. Identical in behavior to `.Context`.
441
442 :param run:
443 A data structure indicating what `.Result` objects to return from
444 calls to the instantiated object&#x27;s `~.Context.run` method (instead
445 of actually executing the requested shell command).
446
447 Specifically, this kwarg accepts:
448
449 - A single `.Result` object.
450 - A boolean; if True, yields a `.Result` whose ``exited`` is ``0``,
451 and if False, ``1``.
452 - An iterable of the above values, which will be returned on each
453 subsequent call to ``.run`` (the first item on the first call,
454 the second on the second call, etc).
455 - A dict mapping command strings or compiled regexen to the above
456 values (including an iterable), allowing specific
457 call-and-response semantics instead of assuming a call order.
458
459 :param sudo:
460 Identical to ``run``, but whose values are yielded from calls to
461 `~.Context.sudo`.
462
463 :param bool repeat:
464 A flag determining whether results yielded by this class&#x27; methods
465 repeat or are consumed.
466
467 For example, when a single result is indicated, it will normally
468 only be returned once, causing ``NotImplementedError`` afterwards.
469 But when ``repeat=True`` is given, that result is returned on
470 every call, forever.
471

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…