Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset input buffer #25

Merged
merged 5 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13-dev']
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']

steps:
- uses: actions/checkout@v4
Expand Down
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Changelog

## [0.4.3] - Development
## [0.4.4] - Development

## [0.4.3] - 2024-12-22

### Added

- Add support for Python 3.13 ([`c28f520`](https://github.com/bessman/pytest-reserial/commit/c28f5201222528867eea0d9e7daae8abbbb06cc6))

### Removed

- Remove support for EOL Python 3.8 ([`c28f520`](https://github.com/bessman/pytest-reserial/commit/c28f5201222528867eea0d9e7daae8abbbb06cc6))

### Fixed

- Patch `Serial.reset_input_buffer` during replay ([`2115eff`](https://github.com/bessman/pytest-reserial/commit/2115eff037b5003174b1705123b89c1594176713))

## [0.4.2] - 2024-07-23

Expand Down
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ name = "pytest-reserial"
authors = [{name = "Alexander Bessman", email = "[email protected]"}]
dynamic = ["version", "description"]
readme = "README.md"
requires-python = ">=3.8"
requires-python = ">=3.9"
license = {file = "LICENSE"}
dependencies = [
"pytest",
"pyserial",
]
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Development Status :: 4 - Beta",
"License :: OSI Approved :: MIT License",
]
Expand Down Expand Up @@ -66,8 +66,6 @@ ignore = [
"G004", # logging-f-string
"N818", # error-suffix-on-exception-name
"S101", # assert
"ANN101", # missing-type-self
"PT004", # pytest-missing-fixture-name-underscore
]

[tool.ruff.lint.pydocstyle]
Expand Down
29 changes: 22 additions & 7 deletions src/pytest_reserial/reserial.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@
import json
from enum import IntEnum
from pathlib import Path
from typing import Callable, Dict, Iterator, Literal, Tuple
from typing import TYPE_CHECKING, Callable, Literal

import pytest
from serial import PortNotOpenError, Serial # type: ignore[import-untyped]

TrafficLog = Dict[Literal["rx", "tx"], bytes]
PatchMethods = Tuple[
if TYPE_CHECKING:
from collections.abc import Iterator

TrafficLog = dict[Literal["rx", "tx"], bytes]
PatchMethods = tuple[
Callable[[Serial, int], bytes], # read
Callable[[Serial, bytes], int], # write
Callable[[Serial], None], # open
Callable[[Serial], None], # close
Callable[[Serial, bool], None], # _reconfigure_port
Callable[[Serial], int], # in_waiting
Callable[[Serial], None], # reset_input_buffer
]


Expand Down Expand Up @@ -47,7 +51,7 @@ class Mode(IntEnum):
INVALID = 3


@pytest.fixture()
@pytest.fixture
def reserial(
monkeypatch: pytest.MonkeyPatch,
request: pytest.FixtureRequest,
Expand All @@ -74,13 +78,15 @@ def reserial(
close_patch,
reconfigure_port_patch,
in_waiting_patch,
reset_input_buffer,
) = get_patched_methods(mode, log)
monkeypatch.setattr(Serial, "read", read_patch)
monkeypatch.setattr(Serial, "write", write_patch)
monkeypatch.setattr(Serial, "open", open_patch)
monkeypatch.setattr(Serial, "close", close_patch)
monkeypatch.setattr(Serial, "_reconfigure_port", reconfigure_port_patch)
monkeypatch.setattr(Serial, "in_waiting", in_waiting_patch)
monkeypatch.setattr(Serial, "reset_input_buffer", reset_input_buffer)

yield

Expand Down Expand Up @@ -169,6 +175,8 @@ def get_patched_methods(mode: Mode, log: TrafficLog) -> PatchMethods:
Monkeypatch this over `Serial._reconfigure_port`.
in_waiting_patch: Callable[[Serial], int]
Monkeypatch this over `Serial.in_waiting`.
reset_input_buffer_patch: Callable[[Serial], None]
Monkeypatch this over `Serial.reset_input_buffer`.
"""
if mode == Mode.REPLAY:
return get_replay_methods(log)
Expand All @@ -181,6 +189,7 @@ def get_patched_methods(mode: Mode, log: TrafficLog) -> PatchMethods:
Serial.close,
Serial._reconfigure_port, # noqa: SLF001
Serial.in_waiting,
Serial.reset_input_buffer,
)


Expand Down Expand Up @@ -267,10 +276,11 @@ def replay_in_waiting(
replay_close,
replay_reconfigure_port,
replay_in_waiting,
replay_reset_input_buffer,
)


# The open/close method patches don't need access to logs, so they can stay down here.
# These patches don't need access to logs, so they can stay down here.
def replay_open(self: Serial) -> None:
"""Pretend that port was opened."""
self.is_open = True
Expand All @@ -283,8 +293,8 @@ def replay_close(self: Serial) -> None:


def replay_reconfigure_port(
self: Serial, # noqa: ARG001
force_update: bool = False, # noqa: ARG001, FBT001, FBT002
self: Serial,
force_update: bool = False, # noqa: FBT001, FBT002
) -> None:
"""Don't try to set parameters on the mocked port.

Expand All @@ -294,6 +304,10 @@ def replay_reconfigure_port(
"""


def replay_reset_input_buffer(self: Serial) -> None:
"""Pretend to flush input buffer."""


def get_record_methods(log: TrafficLog) -> PatchMethods:
"""Return patched read, write, open, etc methods for recording traffic.

Expand Down Expand Up @@ -349,6 +363,7 @@ def record_read(self: Serial, size: int = 1) -> bytes:
Serial.close,
Serial._reconfigure_port, # noqa: SLF001
Serial.in_waiting,
Serial.reset_input_buffer,
)


Expand Down
5 changes: 2 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ commands =

[gh-actions]
python =
3.8: test
3.9: test
3.10: test
3.11: test
3.12: test, lint
3.13-dev: test, lint
3.12: test
3.13: test, lint
Loading