-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathnoxfile.py
More file actions
152 lines (127 loc) · 5.44 KB
/
noxfile.py
File metadata and controls
152 lines (127 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from __future__ import annotations
import shutil
from pathlib import Path
import nox
nox.options.sessions = ["test", "mypy", "lint", "format"]
PYTHONS = ["3.11", "3.12", "3.13"]
@nox.session(python=False, default=False)
def clean(session):
pats = ["dist", "build", "html*", ".coverage*", ".*cache", "src/*.egg-info", "*.log", "*.tmp", ".nox"]
for w in pats:
for f in Path.cwd().glob(w):
session.log(f"Removing: {f}")
if f.is_dir():
shutil.rmtree(f, ignore_errors=True)
else:
f.unlink(missing_ok=True)
for f in Path.cwd().rglob("__pycache__"):
session.log(f"Removing: {f}")
shutil.rmtree(f, ignore_errors=True)
@nox.session(python=PYTHONS)
def test(session: nox.Session) -> None:
session.install("-e", ".[udp,pythoncan]", "pytest", "pytest-asyncio", "pytest-timeout", "coverage")
session.run("coverage", "run", "-m", "pytest", "--timeout=60", "tests/", *session.posargs)
session.run("coverage", "report")
session.run("coverage", "html")
@nox.session(python=PYTHONS[0])
def mypy(session: nox.Session) -> None:
session.install(".[udp,pythoncan]", "mypy", "pytest", "pytest-asyncio")
session.run("mypy", "src/pycyphal2", "tests")
@nox.session(python=PYTHONS[0])
def lint(session: nox.Session) -> None:
session.install("ruff")
session.run("ruff", "check", "src", "tests", "examples")
@nox.session(python=PYTHONS[0])
def format(session: nox.Session) -> None:
session.install("black")
session.run("black", "--check", "--diff", "src", "tests", "examples")
@nox.session(python=PYTHONS[0], reuse_venv=True)
def docs(session: nox.Session) -> None:
session.install("-e", ".[udp,pythoncan]", "pdoc")
session.run("python", "docs/build.py")
session.log("Docs written to html_docs/")
@nox.session(python=PYTHONS[0], default=False)
def examples(session: nox.Session) -> None:
import json as _json
import subprocess
import sys
import time
if sys.platform == "darwin":
session.skip("Examples smoke is skipped on macOS")
session.install(".[udp]")
topic = "demo/time"
python = shutil.which("python", path=session.bin)
assert python is not None
def terminate_process(proc: subprocess.Popen[str] | None) -> None:
if proc is None or proc.poll() is not None:
return
proc.terminate()
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait(timeout=5)
def run_case(label: str, extra_args: list[str]) -> None:
session.log(f"--- examples smoke: {label} ---")
sub_proc = subprocess.Popen(
[python, "examples/subscribe_demo.py", topic, "--timeout", "10", *extra_args],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
time.sleep(1) # let the subscriber set up
session.run(python, "examples/publish_time.py", topic, "--count", "3", *extra_args, external=True)
time.sleep(1) # let the last message propagate
sub_proc.terminate()
stdout, _ = sub_proc.communicate(timeout=5)
lines = [ln for ln in stdout.decode().splitlines() if ln.strip()]
session.log(f"Subscriber captured {len(lines)} line(s)")
assert len(lines) >= 1, f"Expected at least 1 JSONL line, got {len(lines)}"
for ln in lines:
obj = _json.loads(ln)
assert "ts" in obj
assert "remote_id" in obj
assert "topic" in obj
assert "message_b64" in obj
def run_streaming_case() -> None:
session.log("--- examples smoke: streaming ---")
server_proc = None
try:
server_proc = subprocess.Popen(
[python, "examples/streaming_server.py"],
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
text=True,
)
time.sleep(1)
client_proc = subprocess.Popen(
[python, "examples/streaming_client.py", "--count=3", "--period=0.2"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
client_stdout, client_stderr = client_proc.communicate(timeout=20)
assert client_proc.returncode == 0, f"Streaming client failed: {client_stderr}"
time.sleep(1)
assert server_proc.poll() is None, "Streaming server exited unexpectedly"
finally:
terminate_process(server_proc)
_, server_stderr = server_proc.communicate(timeout=5)
lines = [ln for ln in client_stdout.splitlines() if ln.strip()]
session.log(f"Streaming client captured {len(lines)} line(s)")
assert len(lines) == 2, f"Expected 2 JSONL responses, got {len(lines)}"
objs = [_json.loads(ln) for ln in lines]
assert [obj["seqno"] for obj in objs] == [0, 1]
assert len({obj["remote_id"] for obj in objs}) == 1
for obj in objs:
assert "ts" in obj
assert "stream_id" in obj
assert "requested_count" in obj
assert "period" in obj
assert "remaining" in obj
assert "sent_at" in obj
run_case("udp", [])
if sys.platform == "linux" and Path("/sys/class/net/vcan0").exists():
run_case("socketcan:vcan0", ["--transport", "socketcan:vcan0"])
else:
session.log("Skipping socketcan:vcan0 case (vcan0 not available)")
run_streaming_case()