mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-10 20:27:11 +01:00
Sixth batch of the 250-line sweep. All six gate sources named for this batch are now under cap, plus _gate_layout_probe.py, which this batch's import rewiring pulled into the changeset (over-cap count 38 -> 31). _gatelock_ui.py 477 -> 174 (palette/leaf-helper hub) _gate_layout_probe.py 346 -> 196 _gatelock_groups.py 308 -> 137 _gatelock_calendar_ui 295 -> 77 _gatelock_calendar.py 286 -> 210 _gatelock.py 263 -> 238 _gatelock_mealflow.py 259 -> 207 New siblings: _gatelock_fields (input widgets), _gatelock_layout (assembly), _gatelock_widgetgroups (per-widget mirroring), _gatelock_calendar_types (bundles/palette), _gatelock_calendar_widgets (History builders), _gatelock_dashboard and _gatelock_budgetedit (mixins), _gatelock_lockfile (the stdlib-only flock), _gate_focus_probe and _gate_measurement. The load-bearing part is the test seam, not the line counts. `_gate_fixtures._GATE_TK_MODULES` patches `tk` on a hand-listed tuple, and that list fails OPEN: a widget builder split into a new sibling that nobody adds to it builds *real* Tk widgets against fake parents. Neither existing detector sees that -- check_patch_targets.py only validates paths that still resolve, and the ~8s runtime only catches network calls. It bit twice during this split (real StringVars from make_calendar_vars returned MagicMocks instead of the asserted text), so test_gate_tk_modules.py now asserts the invariant directly: every module binding `tk` is in the patch set or named in _NO_WIDGETS with a reason. It caught a pre-existing gap too -- _gatelock_support was never in the set. Same fail-open shape drove two other choices: `escape_tab_trap` is a call-time indirection so the keyboard test's patch of `_escape_text_tab_trap` still bites, and the flock pair moved with its conftest patch target updated in the same edit (that one fails closed). build_layout is imported from _gatelock_layout directly rather than re-exported lazily, which avoids both a cycle and an import-outside-toplevel suppression. Verified: 703 Python tests / 100% branch coverage, the 5 layout-fits tests under xvfb, 555 Flutter tests (unchanged), flutter analyze clean, check_patch_targets 0, `sync` 0 (403 entries), pre-commit clean, and `gate --demo` renders both tabs with live dashboard data. ~/.local/share/diet_guard byte-identical across the batch (gate timer paused, restored after). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
120 lines
4.8 KiB
Python
120 lines
4.8 KiB
Python
"""Guard: every gate module that builds Tk widgets must be faked in tests.
|
|
|
|
``_gate_fixtures.fake_tk`` patches ``tk`` on a hand-listed tuple of modules.
|
|
That list fails **open**: split a widget builder into a new sibling, forget to
|
|
add it, and the gate tests build *real* Tk widgets parented to fake ones. The
|
|
symptom is a hang or an error far from the cause -- and on a machine with a
|
|
display, a real window during a test run.
|
|
|
|
Neither of the sweep's other detectors sees this. ``check_patch_targets.py``
|
|
only validates dotted paths that still resolve, and the suite's ~8s runtime
|
|
only catches network calls. So the invariant is asserted here directly:
|
|
*every* ``diet_guard`` module that binds the name ``tk`` is either in the
|
|
patch set or explicitly declared widget-free.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ast
|
|
import pathlib
|
|
|
|
from diet_guard.tests._gate_fixtures import _GATE_TK_MODULES
|
|
|
|
#: Modules that bind ``tk`` but build no part of the gate's widget tree, so
|
|
#: they never need the shared fake. Each is exempt for a stated reason -- this
|
|
#: set is the only sanctioned way out of the patch list.
|
|
_NO_WIDGETS = frozenset(
|
|
{
|
|
# Mirror/adapter layer over widgets built elsewhere; constructs none
|
|
# of its own, but does bind ``tk`` at runtime for its ``TclError``
|
|
# suppressions.
|
|
"diet_guard._gatelock_widgetgroups",
|
|
# Display-readiness probe. Builds a throwaway ``tk.Tk()`` to test
|
|
# whether X is reachable -- deliberately NOT part of the gate's widget
|
|
# tree, so the shared fake would defeat the very thing it probes. Its
|
|
# own tests patch ``_gatelock_support.tk`` per-test instead.
|
|
"diet_guard._gatelock_support",
|
|
},
|
|
)
|
|
|
|
_PACKAGE_DIR = pathlib.Path(__file__).resolve().parent.parent
|
|
|
|
|
|
def _is_type_checking(test: ast.expr) -> bool:
|
|
"""Whether an ``if`` test is the ``TYPE_CHECKING`` guard."""
|
|
if isinstance(test, ast.Name):
|
|
return test.id == "TYPE_CHECKING"
|
|
return isinstance(test, ast.Attribute) and test.attr == "TYPE_CHECKING"
|
|
|
|
|
|
def _binds_tk(path: pathlib.Path) -> bool:
|
|
"""Whether ``path`` binds the name ``tk`` to tkinter at module level.
|
|
|
|
That name -- not merely "imports tkinter" -- is what ``fake_tk`` patches,
|
|
so it is what the patch set has to match. A module importing only ``ttk``
|
|
(``_gatelock_calendar_ui``) has no ``tk`` to patch, and listing it would
|
|
raise "does not have the attribute 'tk'".
|
|
"""
|
|
tree = ast.parse(path.read_text(encoding="utf-8"), str(path))
|
|
# A TYPE_CHECKING-only import binds nothing at runtime, so there is no
|
|
# attribute for ``patch.object`` to replace -- and listing such a module
|
|
# would raise "does not have the attribute 'tk'". Drop those blocks
|
|
# wholesale before walking, since ``ast.walk`` would otherwise still
|
|
# descend into their bodies.
|
|
tree.body = [
|
|
node
|
|
for node in tree.body
|
|
if not (isinstance(node, ast.If) and _is_type_checking(node.test))
|
|
]
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, ast.Import):
|
|
for alias in node.names:
|
|
if alias.name == "tkinter" and alias.asname == "tk":
|
|
return True
|
|
elif (
|
|
isinstance(node, ast.ImportFrom)
|
|
and node.module
|
|
and node.module.split(".")[0] == "tkinter"
|
|
) and any(alias.asname == "tk" or alias.name == "tk" for alias in node.names):
|
|
return True
|
|
return False
|
|
|
|
|
|
def test_gate_tk_modules_complete() -> None:
|
|
"""Every ``tk``-binding diet_guard module is faked or declared widget-free.
|
|
|
|
Fails closed: a new widget-building sibling that nobody added to
|
|
``_GATE_TK_MODULES`` shows up here as a named diff, rather than as a real
|
|
Tk window opening halfway through an unrelated gate test.
|
|
"""
|
|
patched = {module.__name__ for module in _GATE_TK_MODULES}
|
|
importers = {
|
|
f"diet_guard.{path.stem}"
|
|
for path in sorted(_PACKAGE_DIR.glob("*.py"))
|
|
if _binds_tk(path)
|
|
}
|
|
unaccounted = importers - patched - _NO_WIDGETS
|
|
assert not unaccounted, (
|
|
"these diet_guard modules bind `tk` but are neither in "
|
|
"_gate_fixtures._GATE_TK_MODULES nor declared widget-free in "
|
|
f"_NO_WIDGETS: {sorted(unaccounted)}"
|
|
)
|
|
|
|
|
|
def test_no_widgets_declarations_are_live() -> None:
|
|
"""``_NO_WIDGETS`` names only modules that really do bind ``tk``.
|
|
|
|
Keeps the escape hatch honest: once a listed module stops binding ``tk``
|
|
(or is renamed away), its entry is stale and must go, rather than
|
|
lingering as a licence for some future file to skip the patch set.
|
|
"""
|
|
importers = {
|
|
f"diet_guard.{path.stem}"
|
|
for path in sorted(_PACKAGE_DIR.glob("*.py"))
|
|
if _binds_tk(path)
|
|
}
|
|
assert importers >= _NO_WIDGETS, (
|
|
"stale _NO_WIDGETS entries (no longer bind `tk`): "
|
|
f"{sorted(_NO_WIDGETS - importers)}"
|
|
)
|