diff --git a/changelog.rst b/changelog.rst index 338491424..4144f5fe4 100644 --- a/changelog.rst +++ b/changelog.rst @@ -9,6 +9,7 @@ Changelog 2023-xx-xx • `full history `__ - 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 `__) - Thanks to our beloved contributors: @BoboTiG 2.3.0 diff --git a/src/watchdog/tricks/__init__.py b/src/watchdog/tricks/__init__.py index 156ca466e..f14820b2c 100644 --- a/src/watchdog/tricks/__init__.py +++ b/src/watchdog/tricks/__init__.py @@ -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 @@ -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(): @@ -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: diff --git a/tests/test_0_watchmedo.py b/tests/test_0_watchmedo.py index 121a56406..6fe3c39e6 100644 --- a/tests/test_0_watchmedo.py +++ b/tests/test_0_watchmedo.py @@ -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.