晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
| DIR:/opt/cloudlinux/venv/lib64/python3.11/site-packages/pytest_subprocess/ |
| Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/pytest_subprocess/fake_process.py |
"""FakeProcess class declaration"""
from collections import defaultdict
from collections import deque
from typing import Any as AnyType
from typing import Callable
from typing import ClassVar
from typing import DefaultDict
from typing import Deque
from typing import Dict
from typing import List
from typing import Optional
from typing import Type
from typing import Union
from . import exceptions
from .process_dispatcher import ProcessDispatcher
from .process_recorder import ProcessRecorder
from .types import COMMAND
from .types import OPTIONAL_TEXT_OR_ITERABLE
from .utils import Any
from .utils import Command
from .utils import Program
class FakeProcess:
"""Main class responsible for process operations"""
any: ClassVar[Type[Any]] = Any
program: ClassVar[Type[Program]] = Program
def __init__(self) -> None:
self.definitions: DefaultDict[Command, Deque[Union[Dict, bool]]] = defaultdict(
deque
)
self.calls: Deque[COMMAND] = deque()
self._allow_unregistered: bool = False
self._keep_last_process: bool = False
self.exceptions = exceptions
def register(
self,
command: COMMAND,
stdout: OPTIONAL_TEXT_OR_ITERABLE = None,
stderr: OPTIONAL_TEXT_OR_ITERABLE = None,
returncode: int = 0,
wait: Optional[float] = None,
callback: Optional[Callable] = None,
callback_kwargs: Optional[Dict[str, AnyType]] = None,
signal_callback: Optional[Callable] = None,
occurrences: int = 1,
stdin_callable: Optional[Callable] = None,
) -> ProcessRecorder:
"""
Main method for registering the subprocess instances.
Args:
command: register the command that will be faked
stdout: value of the standard output
stderr: value of the error output
returncode: return code of the faked process
wait: artificially wait for the process to finish
callback: function that will be executed instead of the process
callback_kwargs: keyword arguments that will be passed into callback
occurrences: allow multiple usages of the same command
stdin_callable: function that will interact with stdin
"""
if wait is not None and callback is not None:
raise exceptions.IncorrectProcessDefinition(
"The 'callback' and 'wait' arguments cannot be used "
"together. Add sleep() to your callback instead."
)
if not isinstance(command, Command):
command = Command(command)
recorder = ProcessRecorder()
self.definitions[command].extend(
[
{
"command": command,
"stdout": stdout,
"stderr": stderr,
"returncode": returncode,
"wait": wait,
"callback": callback,
"callback_kwargs": callback_kwargs,
"signal_callback": signal_callback,
"stdin_callable": stdin_callable,
"recorder": recorder,
}
]
* occurrences
)
return recorder
register_subprocess = register
def pass_command(
self,
command: COMMAND,
occurrences: int = 1,
) -> None:
"""
Allow to use a real subprocess together with faked ones.
Args:
command: allow to execute the supplied command
occurrences: allow multiple usages of the same command
"""
if not isinstance(command, Command):
command = Command(command)
self.definitions[command].extend([True] * occurrences)
def __enter__(self) -> "FakeProcess":
ProcessDispatcher.register(self)
return self
def __exit__(self, *args: List, **kwargs: Dict) -> None:
ProcessDispatcher.deregister(self)
def allow_unregistered(self, allow: bool) -> None:
"""
Allow / block unregistered processes execution. When allowed, the real
subprocesses will be called. Blocking will raise the exception.
Args:
allow: decide whether the unregistered process shall be allowed
"""
self._allow_unregistered = allow
def call_count(self, command: COMMAND) -> int:
"""
Count how many times a certain command was called. Can be used
together with `fake_process.any()`.
Args:
command: lookup command
Returns:
number of times a command was called
"""
if not isinstance(command, Command):
command_instance = Command(command)
return len(tuple(filter(lambda elem: elem == command_instance, self.calls)))
def keep_last_process(self, keep: bool) -> None:
"""
Keep last process definition from being removed. That can allow / block
multiple execution of the same command.
Args:
keep: decide whether last process shall be kept
"""
self._keep_last_process = keep
@classmethod
def context(cls) -> "FakeProcess":
"""Return a new FakeProcess instance to use it as a context manager."""
return cls()
|