Skip to content

Commit

Permalink
Invoke runner without delay in interactive mode
Browse files Browse the repository at this point in the history
Refactor Trigger logic
Invoke runner instantly in interactive mode commands
  • Loading branch information
olzhasar committed Feb 6, 2024
1 parent ac8a7f0 commit 1d47e30
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
12 changes: 6 additions & 6 deletions pytest_watcher/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class InvokeCommand(Command):
description = "Invoke test runner"

def run(self, trigger: Trigger, term: Terminal, config: Config) -> None:
trigger.emit()
trigger.emit_now()


class ResetRunnerArgsCommand(Command):
Expand All @@ -85,7 +85,7 @@ class ResetRunnerArgsCommand(Command):

def run(self, trigger: Trigger, term: Terminal, config: Config) -> None:
config.runner_args.clear()
trigger.emit()
trigger.emit_now()


class ChangeRunnerArgsCommand(Command):
Expand All @@ -99,7 +99,7 @@ def run(self, trigger: Trigger, term: Terminal, config: Config) -> None:
new_args = raw.strip().split()
config.runner_args.clear()
config.runner_args.extend(new_args)
trigger.emit()
trigger.emit_now()


class OnlyFailedCommand(Command):
Expand All @@ -110,7 +110,7 @@ class OnlyFailedCommand(Command):
def run(self, trigger: Trigger, term: Terminal, config: Config) -> None:
if "--lf" not in config.runner_args:
config.runner_args.append("--lf")
trigger.emit()
trigger.emit_now()


class PDBCommand(Command):
Expand All @@ -121,7 +121,7 @@ class PDBCommand(Command):
def run(self, trigger: Trigger, term: Terminal, config: Config) -> None:
if "--pdb" not in config.runner_args:
config.runner_args.append("--pdb")
trigger.emit()
trigger.emit_now()


class VerboseCommand(Command):
Expand All @@ -132,7 +132,7 @@ class VerboseCommand(Command):
def run(self, trigger: Trigger, term: Terminal, config: Config) -> None:
if "-v" not in config.runner_args:
config.runner_args.append("-v")
trigger.emit()
trigger.emit_now()


class EraseScreenCommand(Command):
Expand Down
30 changes: 17 additions & 13 deletions pytest_watcher/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@


class Trigger:
value: float
lock: threading.Lock
_value: float
_lock: threading.Lock

def __init__(self):
self.lock = threading.Lock()
self.value = 0
def __init__(self, delay: float = 0.0):
self._lock = threading.Lock()
self._value = 0
self._delay = delay

def emit(self):
with self.lock:
self.value = time.time()
with self._lock:
self._value = time.time() + self._delay

def emit_now(self):
with self._lock:
self._value = time.time()

def is_active(self):
return self.value != 0
return self._value != 0

def release(self):
with self.lock:
self.value = 0
with self._lock:
self._value = 0

def check(self, delay: float):
now = time.time()
return self.value > 0 and now - self.value > delay
def check(self):
return self._value > 0 and time.time() > self._value
4 changes: 2 additions & 2 deletions pytest_watcher/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
logging.basicConfig(level=logging.INFO, format="[ptw] %(message)s")


def main_loop(trigger, config: Config, term: Terminal) -> None:
if trigger.check(config.delay):
def main_loop(trigger: Trigger, config: Config, term: Terminal) -> None:
if trigger.check():
term.reset()

if config.clear:
Expand Down
8 changes: 2 additions & 6 deletions tests/test_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ def test_main_loop_does_not_invoke_runner_without_trigger(
mock_terminal: Terminal,
trigger: Trigger,
):
trigger.emit()

watcher.main_loop(trigger, config, mock_terminal)

mock_subprocess_run.assert_not_called()
mock_time_sleep.assert_called_once_with(LOOP_DELAY)

assert trigger.is_active()


@freeze_time("2020-01-01 00:00:00")
def test_main_loop_does_not_invoke_runner_before_delay(
Expand All @@ -39,7 +35,7 @@ def test_main_loop_does_not_invoke_runner_before_delay(
mock_terminal: MagicMock,
trigger: Trigger,
):
config.delay = 5
trigger = Trigger(delay=5)
trigger.emit()

with freeze_time("2020-01-01 00:00:04"):
Expand All @@ -57,8 +53,8 @@ def test_main_loop_invokes_runner_after_delay(
mock_time_sleep: MagicMock,
config: Config,
mock_terminal: MagicMock,
trigger: Trigger,
):
trigger = Trigger(delay=5)
trigger.emit()

config.runner = "custom"
Expand Down

0 comments on commit 1d47e30

Please sign in to comment.