Fix test fixture to not interfere with other tests

This commit is contained in:
Jared Miller 2026-02-08 13:34:46 -05:00
parent 20f33f45e1
commit e00591b6bf
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C

View file

@ -40,16 +40,18 @@ async def dummy_handler(player, args):
await player.writer.drain() await player.writer.drain()
@pytest.fixture(autouse=True) @pytest.fixture
def clean_registry(): def clean_registry():
"""Clear registry before each test.""" """Clear registry before each test."""
original_registry = commands._registry.copy()
commands._registry.clear() commands._registry.clear()
yield yield
commands._registry.clear() commands._registry.clear()
commands._registry.update(original_registry)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_exact_match_still_works(player): async def test_exact_match_still_works(player, clean_registry):
"""Test that exact matches work as before.""" """Test that exact matches work as before."""
commands.register( commands.register(
CommandDefinition(name="look", handler=dummy_handler, mode="normal") CommandDefinition(name="look", handler=dummy_handler, mode="normal")
@ -63,7 +65,7 @@ async def test_exact_match_still_works(player):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_prefix_match_resolves_unique_prefix(player): async def test_prefix_match_resolves_unique_prefix(player, clean_registry):
"""Test that a unique prefix resolves to the full command.""" """Test that a unique prefix resolves to the full command."""
commands.register( commands.register(
CommandDefinition(name="sweep", handler=dummy_handler, mode="normal") CommandDefinition(name="sweep", handler=dummy_handler, mode="normal")
@ -77,7 +79,7 @@ async def test_prefix_match_resolves_unique_prefix(player):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_ambiguous_prefix_two_matches(player): async def test_ambiguous_prefix_two_matches(player, clean_registry):
"""Test that ambiguous prefix with 2 matches shows 'A or B?' message.""" """Test that ambiguous prefix with 2 matches shows 'A or B?' message."""
commands.register( commands.register(
CommandDefinition(name="swoop", handler=dummy_handler, mode="normal") CommandDefinition(name="swoop", handler=dummy_handler, mode="normal")
@ -95,7 +97,7 @@ async def test_ambiguous_prefix_two_matches(player):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_ambiguous_prefix_three_plus_matches(player): async def test_ambiguous_prefix_three_plus_matches(player, clean_registry):
"""Test that ambiguous prefix with 3+ matches shows comma-separated.""" """Test that ambiguous prefix with 3+ matches shows comma-separated."""
commands.register( commands.register(
CommandDefinition(name="send", handler=dummy_handler, mode="normal") CommandDefinition(name="send", handler=dummy_handler, mode="normal")
@ -119,7 +121,7 @@ async def test_ambiguous_prefix_three_plus_matches(player):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_no_match_shows_unknown_command(player): async def test_no_match_shows_unknown_command(player, clean_registry):
"""Test that no match shows unknown command message.""" """Test that no match shows unknown command message."""
commands.register( commands.register(
CommandDefinition(name="look", handler=dummy_handler, mode="normal") CommandDefinition(name="look", handler=dummy_handler, mode="normal")
@ -133,7 +135,7 @@ async def test_no_match_shows_unknown_command(player):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_exact_alias_match_wins_over_prefix(player): async def test_exact_alias_match_wins_over_prefix(player, clean_registry):
"""Test that exact match on alias takes priority over prefix match.""" """Test that exact match on alias takes priority over prefix match."""
commands.register( commands.register(
CommandDefinition( CommandDefinition(
@ -156,7 +158,7 @@ async def test_exact_alias_match_wins_over_prefix(player):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_single_char_ambiguous_prefix(player): async def test_single_char_ambiguous_prefix(player, clean_registry):
"""Test that single-char ambiguous prefix shows disambiguation.""" """Test that single-char ambiguous prefix shows disambiguation."""
commands.register( commands.register(
CommandDefinition(name="quit", handler=dummy_handler, mode="normal") CommandDefinition(name="quit", handler=dummy_handler, mode="normal")
@ -173,7 +175,7 @@ async def test_single_char_ambiguous_prefix(player):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_prefix_match_deduplicates_aliases(player): async def test_prefix_match_deduplicates_aliases(player, clean_registry):
"""Test that aliases to the same command don't create multiple matches.""" """Test that aliases to the same command don't create multiple matches."""
commands.register( commands.register(
CommandDefinition( CommandDefinition(
@ -201,7 +203,7 @@ async def test_prefix_match_deduplicates_aliases(player):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_prefix_match_with_args(player): async def test_prefix_match_with_args(player, clean_registry):
"""Test that prefix matching preserves arguments.""" """Test that prefix matching preserves arguments."""
commands.register( commands.register(
CommandDefinition(name="sweep", handler=dummy_handler, mode="normal") CommandDefinition(name="sweep", handler=dummy_handler, mode="normal")