Modify the stored mock results for given ``attname`` (e.g. ``run``). This is similar to how one instantiates `MockContext` with a ``run`` or ``sudo`` dict kwarg. For example, this:: mc = MockContext(run={'mycommand': Result("mystdout")}) assert mc.r
(
self, attname: str, command: str, result: Result
)
| 577 | return self._yield_result("__sudo", command) |
| 578 | |
| 579 | def set_result_for( |
| 580 | self, attname: str, command: str, result: Result |
| 581 | ) -> None: |
| 582 | """ |
| 583 | Modify the stored mock results for given ``attname`` (e.g. ``run``). |
| 584 | |
| 585 | This is similar to how one instantiates `MockContext` with a ``run`` or |
| 586 | ``sudo`` dict kwarg. For example, this:: |
| 587 | |
| 588 | mc = MockContext(run={'mycommand': Result("mystdout")}) |
| 589 | assert mc.run('mycommand').stdout == "mystdout" |
| 590 | |
| 591 | is functionally equivalent to this:: |
| 592 | |
| 593 | mc = MockContext() |
| 594 | mc.set_result_for('run', 'mycommand', Result("mystdout")) |
| 595 | assert mc.run('mycommand').stdout == "mystdout" |
| 596 | |
| 597 | `set_result_for` is mostly useful for modifying an already-instantiated |
| 598 | `MockContext`, such as one created by test setup or helper methods. |
| 599 | |
| 600 | .. versionadded:: 1.0 |
| 601 | """ |
| 602 | attname = "__{}".format(attname) |
| 603 | heck = TypeError( |
| 604 | "Can't update results for non-dict or nonexistent mock results!" |
| 605 | ) |
| 606 | # Get value & complain if it's not a dict. |
| 607 | # TODO: should we allow this to set non-dict values too? Seems vaguely |
| 608 | # pointless, at that point, just make a new MockContext eh? |
| 609 | try: |
| 610 | value = getattr(self, attname) |
| 611 | except AttributeError: |
| 612 | raise heck |
| 613 | if not isinstance(value, dict): |
| 614 | raise heck |
| 615 | # OK, we're good to modify, so do so. |
| 616 | value[command] = self._normalize(result) |