From 1d47e3059b3c574eee5edc3559efc4ec5fdafdbc Mon Sep 17 00:00:00 2001 From: olzhasar Date: Wed, 7 Feb 2024 03:09:51 +0800 Subject: [PATCH] Invoke runner without delay in interactive mode Refactor Trigger logic Invoke runner instantly in interactive mode commands --- pytest_watcher/commands.py | 12 ++++++------ pytest_watcher/trigger.py | 30 +++++++++++++++++------------- pytest_watcher/watcher.py | 4 ++-- tests/test_watcher.py | 8 ++------ 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/pytest_watcher/commands.py b/pytest_watcher/commands.py index d03a2a4..354070a 100644 --- a/pytest_watcher/commands.py +++ b/pytest_watcher/commands.py @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): diff --git a/pytest_watcher/trigger.py b/pytest_watcher/trigger.py index a84a2df..89f8dc4 100644 --- a/pytest_watcher/trigger.py +++ b/pytest_watcher/trigger.py @@ -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 diff --git a/pytest_watcher/watcher.py b/pytest_watcher/watcher.py index 7620606..e1aa7e9 100644 --- a/pytest_watcher/watcher.py +++ b/pytest_watcher/watcher.py @@ -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: diff --git a/tests/test_watcher.py b/tests/test_watcher.py index 7a9f5ea..14c3ddf 100644 --- a/tests/test_watcher.py +++ b/tests/test_watcher.py @@ -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( @@ -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"): @@ -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"