Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/+carriage_return.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Trigger tests in interactive mode for carriage return character
34 changes: 20 additions & 14 deletions pytest_watcher/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

import abc
import sys
from typing import Dict, Optional, Type

from .config import Config
from .terminal import Terminal
from .trigger import Trigger


class Manager:
_registry: Dict[str, Command] = {}
_registry: dict[str, Command] = {}

@classmethod
def list_commands(cls):
return cls._registry.values()

@classmethod
def get_command(cls, character: str) -> Optional[Command]:
def get_command(cls, character: str) -> Command | None:
return cls._registry.get(character)

@classmethod
def register(cls, command: Type[Command]):
if command.character in cls._registry:
raise ValueError(f"Duplicate character {repr(command.character)}")
def register(cls, command: type[Command]):
_command = command()

cls._registry[command.character] = command()
for char in command.get_characters():
if char in cls._registry:
raise ValueError(f"Duplicate character {repr(char)}")

cls._registry[char] = _command

@classmethod
def run_command(
Expand All @@ -37,7 +39,7 @@ def run_command(


class Command(abc.ABC):
character: str
character: str | tuple[str, ...]
caption: str
description: str
show_in_menu: bool = True
Expand All @@ -47,15 +49,19 @@ def __init_subclass__(cls, **kwargs) -> None:
if not hasattr(cls, field):
raise NotImplementedError(f"{cls.__name__}: {field} not specified")

assert isinstance(cls.character, (str, tuple))

super().__init_subclass__(**kwargs)
Manager.register(cls)

@abc.abstractmethod
def run(self, trigger: Trigger, term: Terminal, config: Config) -> None:
"""
Modify runner_args in-place if needed and return a bool indicating whether
tests should be triggered instantly
"""
def run(self, trigger: Trigger, term: Terminal, config: Config) -> None: ...

@classmethod
def get_characters(cls) -> tuple[str, ...]:
if isinstance(cls.character, tuple):
return cls.character
return (cls.character,)


class OpenMenuCommand(Command):
Expand All @@ -70,7 +76,7 @@ def run(self, trigger: Trigger, term: Terminal, config: Config) -> None:


class InvokeCommand(Command):
character = "\n"
character = ("\n", "\r", "\r\n")
caption = "Enter"
description = "Invoke test runner"

Expand Down
30 changes: 29 additions & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pytest_watcher.trigger import Trigger


def test_run_command(trigger: Trigger, config: Config, mock_terminal: Terminal):
def test_single_character(trigger: Trigger, config: Config, mock_terminal: Terminal):
class DummyCommand(commands.Command):
character = "0"
caption = "0"
Expand All @@ -27,3 +27,31 @@ def run(self, trigger: Trigger, term: Terminal, config: Config) -> None:
assert command.invoked is True

commands.Manager._registry.pop(DummyCommand.character)


def test_multiple_characters(trigger: Trigger, config: Config, mock_terminal: Terminal):
class DummyCommand(commands.Command):
character = ("1", "2")
caption = "1"
description = "test"
show_in_menu = False

def __init__(self):
self.invoke_count = 0

def run(self, trigger: Trigger, term: Terminal, config: Config) -> None:
self.invoke_count += 1

command = commands.Manager.get_command("1")
assert commands.Manager.get_command("2") is command

assert isinstance(command, DummyCommand)
assert command.invoke_count == 0

commands.Manager.run_command("1", trigger, mock_terminal, config)

assert command.invoke_count == 1

commands.Manager.run_command("2", trigger, mock_terminal, config)

assert command.invoke_count == 2