diff --git a/tests/support/keymap_discovery.py b/tests/support/keymap_discovery.py new file mode 100644 index 000000000..0bf98ba2a --- /dev/null +++ b/tests/support/keymap_discovery.py @@ -0,0 +1,19 @@ +"""Run as its own interpreter: keymap discovery imports a screen nothing in it has opened. + +tests/textual/test_keymap_discovery.py starts it with `python -m`, because the app test +template imports every toad module before it forks a test. +""" + +import sys + +from toad.key_modes import KeyMode, Standard +from toad.keymap import KeymapCatalog, KeymapRow + +assert "toad.screens.welcome" not in sys.modules +catalog = KeymapCatalog.capture(tuple(KeyMode.members_with(KeyMode))) +assert "toad.screens.welcome" in sys.modules +from toad.screens.welcome import NewSessionAction # noqa: E402 - discovery imported it + +row = KeymapRow.of(NewSessionAction(), Standard) +assert catalog.require(row.declaration).key_title() == NewSessionAction().key_title() +assert catalog.defaults(row) == NewSessionAction.keys[Standard.binding_mode()] diff --git a/tests/textual/test_keymap_discovery.py b/tests/textual/test_keymap_discovery.py index 654bc7bab..61694b636 100644 --- a/tests/textual/test_keymap_discovery.py +++ b/tests/textual/test_keymap_discovery.py @@ -1,18 +1,15 @@ """Discovery includes screens that the current application has not opened.""" +from pathlib import Path +import subprocess import sys -from toad.key_modes import KeyMode, Standard -from toad.keymap import KeymapCatalog, KeymapRow +TESTS = Path(__file__).resolve().parents[1] def test_unopened_screen_invocations_are_discovered_in_a_fresh_process(workspace): - # `workspace` gives this case its own interpreter, independent of other imports. - assert "toad.screens.welcome" not in sys.modules - catalog = KeymapCatalog.capture(tuple(KeyMode.members_with(KeyMode))) - assert "toad.screens.welcome" in sys.modules - from toad.screens.welcome import NewSessionAction - - row = KeymapRow.of(NewSessionAction(), Standard) - assert catalog.require(row.declaration).key_title() == NewSessionAction().key_title() - assert catalog.defaults(row) == NewSessionAction.keys[Standard.binding_mode()] + # The app test template imports every toad module before it forks, so only a new + # interpreter starts without the welcome screen; `workspace` gives it this case's roots. + run = subprocess.run([sys.executable, "-m", "support.keymap_discovery"], + cwd=TESTS, capture_output=True, text=True, timeout=60) + assert run.returncode == 0, run.stderr