Skip to content

Commit

Permalink
31 get 100 % test coverage (#38)
Browse files Browse the repository at this point in the history
* Removed mypy matrix. Simplified tests, got 100 % coverage

* Extended test scripts to get 100 % coverage

* Make CI fail if coverage is less than 100
  • Loading branch information
rhjdjong authored Mar 15, 2024
1 parent 7ce7c60 commit 227ef21
Show file tree
Hide file tree
Showing 27 changed files with 772 additions and 469 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
run: hatch run style:check

- name: Check types
run: hatch run types.py${{ matrix.python-version }}:check
run: hatch run types:check

- name: Run static analysis
run: hatch fmt --check
Expand Down
2 changes: 1 addition & 1 deletion examples/echoserver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
.. code:: bash
$ python server_ipv6.py
$ python server.py
Slip server listening on localhost, port 59454
Incoming connection from ('127.0.0.1', 59458)
Raw data received: b'\\xc0hallo\\xc0'
Expand Down
7 changes: 6 additions & 1 deletion examples/echoserver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

import sliplib

if __name__ == "__main__":

def main() -> None:
if len(sys.argv) != 2: # noqa: PLR2004
print("Usage: python client.py <port>")
sys.exit(1)
Expand All @@ -37,3 +38,7 @@
sock.send_msg(b_message)
b_reply = sock.recv_msg()
print("Response:", b_reply)


if __name__ == "__main__":
main()
48 changes: 21 additions & 27 deletions examples/echoserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
the raw data that is received and sent.
The request handler prints the decoded message,
and then reverses the order of the bytes in the encoded message
(so ``abc`` becomes ``cab``),
(so ``abc`` becomes ``cba``),
and sends it back to the client.
"""

Expand All @@ -24,44 +24,34 @@
import socket
import sys
from socketserver import TCPServer
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
if sys.version_info >= (3, 12): # noqa: UP036
from collections.abc import Buffer
else:
from typing_extensions import Buffer

from _socket import dup

from sliplib import SlipRequestHandler
from typing import Any

from sliplib import SlipRequestHandler, SlipSocket

class _ChattySocket(socket.socket):
"""A socket subclass that prints the raw data that is received and sent."""

def __init__(self, sock: socket.socket) -> None:
fd = dup(sock.fileno())
super().__init__(sock.family, sock.type, sock.proto, fileno=fd)
super().settimeout(sock.gettimeout())
class _ChattySocket(SlipSocket):
"""A SlipSocket subclass that prints the raw data that is received and sent."""

def recv(self, chunksize: int, *args: Any) -> bytes:
data = super().recv(chunksize, *args)
def recv_bytes(self) -> bytes:
data = super().recv_bytes()
print("Raw data received:", data)
return data

def sendall(self, data: Buffer, *args: Any) -> None:
def send_bytes(self, data: bytes) -> None:
print("Sending raw data:", data)
super().sendall(data, *args)
super().send_bytes(data)


class SlipHandler(SlipRequestHandler):
"""A SlipRequestHandler that echoes the received message with the bytes in reversed order."""

def setup(self) -> None:
self.request = _ChattySocket(self.request)
print(f"Incoming connection from {self.request.getpeername()}")
super().setup()
def __init__(self, request: socket.socket | SlipSocket, *args: Any) -> None:
if isinstance(request, SlipSocket):
request.__class__ = _ChattySocket
else:
request = _ChattySocket(request)
print(f"Incoming connection from {request.getpeername()}")
super().__init__(request, *args)

# Dedicated handler to show the encoded bytes.
def handle(self) -> None:
Expand All @@ -81,10 +71,14 @@ class TCPServerIPv6(TCPServer):
address_family = socket.AF_INET6


if __name__ == "__main__":
def main() -> None:
if len(sys.argv) > 1 and sys.argv[1].lower() == "ipv6":
server = TCPServerIPv6(("localhost", 0), SlipHandler) # type: TCPServer
else:
server = TCPServer(("localhost", 0), SlipHandler)
print("Slip server listening on localhost, port", server.server_address[1])
server.handle_request()


if __name__ == "__main__":
main()
20 changes: 12 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ python = "3.12"
dependencies = [
"pytest",
"pytest-mock",
"semantic-version-check",
]

[[tool.hatch.envs.test.matrix]]
Expand All @@ -61,13 +62,11 @@ cov = [
"cov-report",
]

[[tool.hatch.envs.types.matrix]]
python = ["3.8", "3.9", "3.10", "3.11", "3.12"]

[tool.hatch.envs.types]
python = "3.12"
extra-dependencies = [
"mypy>=1.0.0",
"typing_extensions"
"typing_extensions",
]

[tool.hatch.envs.types.scripts]
Expand Down Expand Up @@ -97,13 +96,17 @@ fix = [
source_pkgs = ["sliplib", "tests"]
branch = true
parallel = true
concurrency = [
"multiprocessing",
"thread",
]
omit = [
"src/sliplib/__about__.py",
]

[tool.coverage.paths]
sliplib = ["src/sliplib", "*/SlipLib/src/sliplib"]
tests = ["tests", "*/SlipLib/tests"]
sliplib = ["src/sliplib"]
tests = ["tests"]

[tool.coverage.report]
show_missing = true
Expand All @@ -112,6 +115,7 @@ exclude_lines = [
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]
fail_under = 100

[tool.hatch.build.targets.sdist]
include = [
Expand All @@ -136,5 +140,5 @@ line-length = 120
profile = "black"
line_length = 120

[tool.ruff]
target-version = "py312"
[tool.mypy]
python_version = "3.8"
5 changes: 3 additions & 2 deletions src/sliplib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
.. automodule:: sliplib.slipwrapper
.. automodule:: sliplib.slipstream
.. automodule:: sliplib.slipsocket
.. automodule:: sliplib.sliprequesthandler
.. automodule:: sliplib.slipserver
Exceptions
----------
Expand All @@ -77,7 +77,7 @@
"""

from sliplib.slip import END, ESC, ESC_END, ESC_ESC, Driver, ProtocolError, decode, encode, is_valid
from sliplib.sliprequesthandler import SlipRequestHandler
from sliplib.slipserver import SlipRequestHandler, SlipServer
from sliplib.slipsocket import SlipSocket
from sliplib.slipstream import SlipStream
from sliplib.slipwrapper import SlipWrapper
Expand All @@ -90,6 +90,7 @@
"SlipWrapper",
"SlipSocket",
"SlipRequestHandler",
"SlipServer",
"SlipStream",
"ProtocolError",
"END",
Expand Down
2 changes: 1 addition & 1 deletion src/sliplib/slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def flush(self) -> list[bytes]:
try:
msg = decode(packet)
except ProtocolError:
# Add any already decoded messages to the exception
# Add any already decoded messages to the internal message buffer
self._messages = messages
raise
messages.append(msg)
Expand Down
76 changes: 0 additions & 76 deletions src/sliplib/sliprequesthandler.py

This file was deleted.

Loading

0 comments on commit 227ef21

Please sign in to comment.