Skip to content

Commit 46b3a93

Browse files
committed
no-mistakes(review): Replace AST/substring test-quality checks with behavioral tomllib assertions
1 parent 967d897 commit 46b3a93

1 file changed

Lines changed: 33 additions & 15 deletions

File tree

tests/test_ble_stream_glue.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77

88
from __future__ import annotations
99

10-
import ast
11-
import inspect
10+
import sys
11+
import tomllib
1212
from pathlib import Path
1313
from typing import Any, Mapping
1414
from unittest import TestCase
1515
from unittest.mock import AsyncMock, MagicMock
1616

1717
from cryptography.hazmat.primitives.asymmetric import ec
1818

19-
from tesla_fleet_api.tesla.vehicle import stream_glue
2019
from tesla_fleet_api.tesla.vehicle.bluetooth import VehicleBluetooth
2120
from tesla_fleet_api.tesla.vehicle.stream_glue import BleBroadcastStreamGlue
2221
from tesla_protocol.command.universal_message_pb2 import (
@@ -236,15 +235,14 @@ class TestDuckTypedContract(TestCase):
236235
"""Locks in the design's hard constraint: no import of teslemetry_stream."""
237236

238237
def test_module_source_never_references_teslemetry_stream(self) -> None:
239-
source = inspect.getsource(stream_glue)
240-
tree = ast.parse(source)
241-
for node in ast.walk(tree):
242-
if isinstance(node, ast.Import):
243-
for alias in node.names:
244-
self.assertNotIn("teslemetry_stream", alias.name)
245-
elif isinstance(node, ast.ImportFrom):
246-
self.assertIsNotNone(node.module)
247-
self.assertNotIn("teslemetry_stream", node.module or "")
238+
self.assertNotIn("teslemetry_stream", sys.modules)
239+
vehicle = _make_vehicle()
240+
sink = MagicMock()
241+
glue = BleBroadcastStreamGlue(vehicle, sink)
242+
vehicle._on_message(_lock(VehicleLockState_E.VEHICLELOCKSTATE_LOCKED))
243+
glue.stop()
244+
245+
self.assertNotIn("teslemetry_stream", sys.modules)
248246

249247
def test_a_plain_object_with_ingest_satisfies_the_sink(self) -> None:
250248
"""No base class, no registration - purely structural."""
@@ -269,6 +267,26 @@ def ingest(
269267

270268
def test_zero_net_new_dependency(self) -> None:
271269
pyproject = Path(__file__).resolve().parent.parent / "pyproject.toml"
272-
text = pyproject.read_text()
273-
self.assertNotIn("teslemetry-stream", text)
274-
self.assertNotIn("teslemetry_stream", text)
270+
with pyproject.open("rb") as f:
271+
data = tomllib.load(f)
272+
273+
def _names(specs: list[str]) -> set[str]:
274+
return {
275+
spec.split(";")[0].split(">=")[0].split("==")[0].strip()
276+
for spec in specs
277+
}
278+
279+
project = data["project"]
280+
dependency_names = _names(project.get("dependencies", []))
281+
for extra_deps in project.get("optional-dependencies", {}).values():
282+
dependency_names |= _names(extra_deps)
283+
for group_deps in data.get("dependency-groups", {}).values():
284+
dependency_names |= _names(
285+
dep for dep in group_deps if isinstance(dep, str)
286+
)
287+
source_names = set(data.get("tool", {}).get("uv", {}).get("sources", {}))
288+
289+
self.assertNotIn("teslemetry-stream", dependency_names)
290+
self.assertNotIn("teslemetry_stream", dependency_names)
291+
self.assertNotIn("teslemetry-stream", source_names)
292+
self.assertNotIn("teslemetry_stream", source_names)

0 commit comments

Comments
 (0)