Skip to content

Commit

Permalink
[watchmedo] Exclude FileOpenedEvent events from AutoRestartTrick,…
Browse files Browse the repository at this point in the history
… and `ShellCommandTrick`

In order to restore watchdog < 2.3.0 behavior.
A better solution should be found in the future.

Fixes #949.
  • Loading branch information
BoboTiG committed Feb 27, 2023
1 parent 54f2cf0 commit 25a2d1f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Changelog
2023-xx-xx • `full history <https://github.com/gorakhargosh/watchdog/compare/v2.3.0...HEAD>`__

- Run ``black`` on the entire source code
- [watchmedo] Exclude ``FileOpenedEvent`` events from ``AutoRestartTrick``, and ``ShellCommandTrick``, to restore watchdog < 2.3.0 behavior. A better solution should be found in the future. (`#949 <https://github.com/gorakhargosh/watchdog/pull/949>`__)
- Thanks to our beloved contributors: @BoboTiG

2.3.0
Expand Down
10 changes: 9 additions & 1 deletion src/watchdog/tricks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import threading
import time

from watchdog.events import PatternMatchingEventHandler
from watchdog.events import PatternMatchingEventHandler, EVENT_TYPE_OPENED
from watchdog.utils import echo
from watchdog.utils.event_debouncer import EventDebouncer
from watchdog.utils.process_watcher import ProcessWatcher
Expand Down Expand Up @@ -128,6 +128,10 @@ def __init__(
self._process_watchers = set()

def on_any_event(self, event):
if event.event_type == EVENT_TYPE_OPENED:
# FIXME: see issue #949, and find a way to better handle that scenario
return

from string import Template

if self.drop_during_process and self.is_process_running():
Expand Down Expand Up @@ -293,6 +297,10 @@ def _stop_process(self):

@echo_events
def on_any_event(self, event):
if event.event_type == EVENT_TYPE_OPENED:
# FIXME: see issue #949, and find a way to better handle that scenario
return

if self.event_debouncer is not None:
self.event_debouncer.handle_event(event)
else:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_0_watchmedo.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,36 @@ def test_shell_command_subprocess_termination_nowait(tmpdir):
assert not trick.is_process_running()


def test_shell_command_subprocess_termination_not_happening_on_file_opened_event(
tmpdir,
):
# FIXME: see issue #949, and find a way to better handle that scenario
script = make_dummy_script(tmpdir, n=1)
command = " ".join([sys.executable, script])
trick = ShellCommandTrick(command, wait_for_process=False)
assert not trick.is_process_running()
trick.on_any_event(FileOpenedEvent("foo/bar.baz"))
assert not trick.is_process_running()
time.sleep(5)
assert not trick.is_process_running()


def test_auto_restart_not_happening_on_file_opened_event(tmpdir, capfd):
# FIXME: see issue #949, and find a way to better handle that scenario
script = make_dummy_script(tmpdir, n=2)
trick = AutoRestartTrick([sys.executable, script])
trick.start()
time.sleep(1)
trick.on_any_event(FileOpenedEvent("foo/bar.baz"))
trick.on_any_event(FileOpenedEvent("foo/bar2.baz"))
trick.on_any_event(FileOpenedEvent("foo/bar3.baz"))
time.sleep(1)
trick.stop()
cap = capfd.readouterr()
assert cap.out.splitlines(keepends=False).count("+++++ 0") == 1
assert trick.restart_count == 0


def test_auto_restart_on_file_change(tmpdir, capfd):
"""Simulate changing 3 files.
Expand Down

0 comments on commit 25a2d1f

Please sign in to comment.