From 869e98e7980692d3cb4ee7154918c361dec9f264 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 31 Mar 2021 16:13:21 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Stuff=20is=20actually=20working?= =?UTF-8?q?=20now=3F!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- poetry.lock | 4 +-- pyproject.toml | 5 ++++ royalnet_console/bullets.py | 20 +++++++++------ royalnet_console/tests/test_pda.py | 40 ++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/poetry.lock b/poetry.lock index 4920fd78..e779eedc 100644 --- a/poetry.lock +++ b/poetry.lock @@ -284,7 +284,7 @@ description = "A multipurpose bot framework" category = "main" optional = false python-versions = "^3.8" -develop = true +develop = false [package.dependencies] async-property = "^0.2.1" @@ -483,7 +483,7 @@ brotli = ["brotlipy (>=0.6.0)"] [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "43ee92bfde25cfaf2a425e3369da281a5cc1288dd5b4ccdac2fa0b1d230cf2e7" +content-hash = "899653d0350658ba17c1f8bb15f6ef73874325558ba67a2e191590869a24b4db" [metadata.files] alabaster = [ diff --git a/pyproject.toml b/pyproject.toml index 5aaece44..4da959c2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,11 @@ pytest-asyncio = "^0.14.0" sphinx = "^3.3.1" sphinx_rtd_theme = "^0.5.0" +[tool.pytest.ini_options] +minversion = "6.0" +log_cli = true +log_cli_level = "DEBUG" + [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api" diff --git a/royalnet_console/bullets.py b/royalnet_console/bullets.py index 4a207394..a037b027 100644 --- a/royalnet_console/bullets.py +++ b/royalnet_console/bullets.py @@ -15,6 +15,7 @@ import getpass import psutil import royalnet.engineer as engi import click +import async_property as ap # Special global objects log = logging.getLogger(__name__) @@ -48,22 +49,23 @@ class ConsoleUser(engi.User): def __hash__(self) -> int: return os.getuid() + @ap.async_property async def name(self) -> str: return getpass.getuser() - async def send_message(self, *, - text: str = None, - files: t.List[t.BinaryIO] = None) -> engi.Message: - return await console_message(text=text, files=files) + async def slide(self) -> "engi.Channel": + return ConsoleChannel() class ConsoleChannel(engi.Channel): def __hash__(self) -> int: return os.getpid() + @ap.async_property async def name(self) -> str: return psutil.Process(os.getpid()).name() + @ap.async_property async def users(self) -> t.List[engi.User]: return [ConsoleUser()] @@ -86,18 +88,21 @@ class ConsoleMessage(engi.Message): def __hash__(self) -> int: return self._instance_number + @ap.async_property async def text(self) -> str: return self._text + @ap.async_property async def timestamp(self) -> datetime.datetime: return self._timestamp + @ap.async_property async def channel(self) -> engi.Channel: return ConsoleChannel() - async def send_reply(self, *, - text: str = None, - files: t.List[t.BinaryIO] = None) -> engi.Message: + async def reply(self, *, + text: str = None, + files: t.List[t.BinaryIO] = None) -> engi.Message: return await console_message(text=text, files=files) @@ -113,6 +118,7 @@ class ConsoleMessageReceived(engi.MessageReceived): def __hash__(self): return + @ap.async_property async def message(self) -> ConsoleMessage: return self._msg diff --git a/royalnet_console/tests/test_pda.py b/royalnet_console/tests/test_pda.py index 271ecd7c..0541e033 100644 --- a/royalnet_console/tests/test_pda.py +++ b/royalnet_console/tests/test_pda.py @@ -1,5 +1,6 @@ import pytest from royalnet_console.pda import ConsolePDA +from royalnet.engineer import PartialCommand, Sentry, Message def test_construction(): @@ -10,3 +11,42 @@ def test_construction(): @pytest.fixture def pda(): return ConsolePDA() + + +@pytest.fixture +def command(): + @PartialCommand.new(syntax="") + async def test(*, _sentry: Sentry, _msg: Message, **__): + """ + Ah, non lo so io! + """ + await _msg.reply(text=r"test") + + return test + + +def test_registration(pda: ConsolePDA, command: PartialCommand): + pda.register_partial(command, ["test"]) + + +@pytest.fixture +def pda_with_command(pda: ConsolePDA, command: PartialCommand): + pda.register_partial(command, ["test"]) + return pda + + +@pytest.mark.asyncio +async def test_run(pda_with_command: ConsolePDA, monkeypatch): + check = False + + def trigger(text): + assert text == "test" + nonlocal check + check = True + + monkeypatch.setattr("click.prompt", lambda *_, **__: "test") + monkeypatch.setattr("click.echo", trigger) + + await pda_with_command.run(cycles=1) + + assert check is True