From cec2aea9285aea394d3b9adb3ef262ea8541b24b Mon Sep 17 00:00:00 2001 From: mhchia Date: Thu, 29 Aug 2019 22:38:08 +0800 Subject: [PATCH] Move shared fixtures and constants to files --- tests/interop/conftest.py | 24 ++++++++++++++++++++++++ tests/interop/constants.py | 1 + tests/interop/test_echo.py | 29 ++++------------------------- 3 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 tests/interop/conftest.py create mode 100644 tests/interop/constants.py diff --git a/tests/interop/conftest.py b/tests/interop/conftest.py new file mode 100644 index 0000000..e85f2f6 --- /dev/null +++ b/tests/interop/conftest.py @@ -0,0 +1,24 @@ +import sys + +import pexpect +import pytest + + +@pytest.fixture +def proc_factory(): + procs = [] + + def call_proc(cmd, args, logfile=None, encoding=None): + if logfile is None: + logfile = sys.stdout + if encoding is None: + encoding = "utf-8" + proc = pexpect.spawn(cmd, args, logfile=logfile, encoding=encoding) + procs.append(proc) + return proc + + try: + yield call_proc + finally: + for proc in procs: + proc.close() diff --git a/tests/interop/constants.py b/tests/interop/constants.py new file mode 100644 index 0000000..dbef043 --- /dev/null +++ b/tests/interop/constants.py @@ -0,0 +1 @@ +PEXPECT_NEW_LINE = "\r\n" diff --git a/tests/interop/test_echo.py b/tests/interop/test_echo.py index f805e23..9b170db 100644 --- a/tests/interop/test_echo.py +++ b/tests/interop/test_echo.py @@ -1,39 +1,18 @@ import asyncio import os import pathlib -import sys from multiaddr import Multiaddr -import pexpect import pytest from libp2p.peer.peerinfo import info_from_p2p_addr from libp2p.typing import TProtocol +from .constants import PEXPECT_NEW_LINE + GOPATH = pathlib.Path(os.environ["GOPATH"]) ECHO_PATH = GOPATH / "bin" / "echo" ECHO_PROTOCOL_ID = TProtocol("/echo/1.0.0") -NEW_LINE = "\r\n" - - -@pytest.fixture -def proc_factory(): - procs = [] - - def call_proc(cmd, args, logfile=None, encoding=None): - if logfile is None: - logfile = sys.stdout - if encoding is None: - encoding = "utf-8" - proc = pexpect.spawn(cmd, args, logfile=logfile, encoding=encoding) - procs.append(proc) - return proc - - try: - yield call_proc - finally: - for proc in procs: - proc.close() async def make_echo_proc( @@ -44,8 +23,8 @@ async def make_echo_proc( args.append("-insecure") if destination is not None: args.append(f"-d={str(destination)}") - echo_proc = proc_factory(str(ECHO_PATH), args, logfile=sys.stdout, encoding="utf-8") - await echo_proc.expect(r"I am ([\w\./]+)" + NEW_LINE, async_=True) + echo_proc = proc_factory(str(ECHO_PATH), args) + await echo_proc.expect(r"I am ([\w\./]+)" + PEXPECT_NEW_LINE, async_=True) maddr_str_ipfs = echo_proc.match.group(1) maddr_str = maddr_str_ipfs.replace("ipfs", "p2p") maddr = Multiaddr(maddr_str)