From 20cad3b7031cbef5dd4ccc91d0eaf6987a4f6682 Mon Sep 17 00:00:00 2001 From: PerchunPak Date: Sun, 17 Jul 2022 21:58:55 +0200 Subject: [PATCH] Increase coverage in `tests/` folder Actually it will not add new tests, just fix some dead ones and add `no cover` comments where it's needs. --- mcstatus/tests/protocol/test_connection.py | 14 +++++++------- mcstatus/tests/test_deprecated_decorator.py | 5 +++-- mcstatus/tests/test_retry_decorator.py | 20 ++++++++++---------- mcstatus/tests/test_timeout.py | 4 ++-- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/mcstatus/tests/protocol/test_connection.py b/mcstatus/tests/protocol/test_connection.py index 63fc0b57..e3365b1b 100644 --- a/mcstatus/tests/protocol/test_connection.py +++ b/mcstatus/tests/protocol/test_connection.py @@ -205,7 +205,7 @@ def test_write_buffer(self): assert self.connection.flush() == bytearray.fromhex("027FAA") -class TCPSocketConnectionTest: +class TestTCPSocketConnection: def setup_method(self): self.test_addr = Address("localhost", 1234) @@ -217,15 +217,15 @@ def setup_method(self): self.connection = TCPSocketConnection(self.test_addr) def test_flush(self): - with pytest.raises(TypeError): + with pytest.raises(NotImplementedError): self.connection.flush() def test_receive(self): - with pytest.raises(TypeError): + with pytest.raises(NotImplementedError): self.connection.receive("") # type: ignore # This is desired to produce TypeError def test_remaining(self): - with pytest.raises(TypeError): + with pytest.raises(NotImplementedError): self.connection.remaining() def test_read(self): @@ -245,7 +245,7 @@ def test_write(self): self.connection.socket.send.assert_called_once_with(bytearray.fromhex("7FAA")) # type: ignore[attr-defined] -class UDPSocketConnectionTest: +class TestUDPSocketConnection: def setup_method(self): self.test_addr = Address("localhost", 1234) @@ -257,11 +257,11 @@ def setup_method(self): self.connection = UDPSocketConnection(self.test_addr) def test_flush(self): - with pytest.raises(TypeError): + with pytest.raises(NotImplementedError): self.connection.flush() def test_receive(self): - with pytest.raises(TypeError): + with pytest.raises(NotImplementedError): self.connection.receive("") # type: ignore # This is desired to produce TypeError def test_remaining(self): diff --git a/mcstatus/tests/test_deprecated_decorator.py b/mcstatus/tests/test_deprecated_decorator.py index c0ef8699..5b18904b 100644 --- a/mcstatus/tests/test_deprecated_decorator.py +++ b/mcstatus/tests/test_deprecated_decorator.py @@ -46,11 +46,12 @@ def test_deprecated_class_return_result(): def test_deprecated_function_with_methods(): with pytest.raises(ValueError): - deprecated(methods=["__str__"])(lambda: None) + deprecated(methods=["__str__"])(lambda: None) # pragma: no branch def test_deprecated_class_without_methods(): - test_cls: Any = type("TestCls", (), {"foo": lambda x: x}) # https://github.com/microsoft/pyright/discussions/3438 + # https://github.com/microsoft/pyright/discussions/3438 + test_cls: Any = type("TestCls", (), {"foo": lambda x: x}) # pragma: no branch with pytest.raises(ValueError): deprecated()(test_cls) diff --git a/mcstatus/tests/test_retry_decorator.py b/mcstatus/tests/test_retry_decorator.py index 200b9113..37417a1b 100644 --- a/mcstatus/tests/test_retry_decorator.py +++ b/mcstatus/tests/test_retry_decorator.py @@ -19,15 +19,15 @@ def func(): def test_sync_fail(): - x = -1 + already_ran = False @retry(tries=2) def func(): - nonlocal x - x += 1 - if x == 0: + nonlocal already_ran + if already_ran: + already_ran = True raise OSError("First error") - elif x == 1: + else: raise RuntimeError("Second error") # We should get the last exception on failure (not OSError) @@ -50,15 +50,15 @@ async def func(): def test_async_fail(): - x = -1 + already_ran = False @retry(tries=2) async def func(): - nonlocal x - x += 1 - if x == 0: + nonlocal already_ran + if already_ran: + already_ran = True raise OSError("First error") - elif x == 1: + else: raise RuntimeError("Second error") # We should get the last exception on failure (not OSError) diff --git a/mcstatus/tests/test_timeout.py b/mcstatus/tests/test_timeout.py index 92c189d9..83d9c6bb 100644 --- a/mcstatus/tests/test_timeout.py +++ b/mcstatus/tests/test_timeout.py @@ -10,7 +10,7 @@ class FakeAsyncStream(asyncio.StreamReader): async def read(self, n: int) -> bytes: await asyncio.sleep(delay=2) - return bytes([0] * n) + raise NotImplementedError("This line should not be reached") # pragma: no cover async def fake_asyncio_asyncio_open_connection(hostname: str, port: int): @@ -25,7 +25,7 @@ def setup_method(self): def test_tcp_socket_read(self): try: from asyncio.exceptions import TimeoutError # type: ignore # (Import for older versions) - except ImportError: + except ImportError: # pragma: no cover from asyncio import TimeoutError with patch("asyncio.open_connection", fake_asyncio_asyncio_open_connection):