Skip to content

Commit

Permalink
Add support for file moved type events
Browse files Browse the repository at this point in the history
  • Loading branch information
olzhasar committed Oct 23, 2022
1 parent ecc7b52 commit 3097bfd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pytest_watcher/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def dispatch(self, event: events.FileSystemEvent) -> None:
def process_event(self, event: events.FileSystemEvent) -> None:
if _is_path_watched(event.src_path):
emit_trigger()
elif isinstance(event, events.FileSystemMovedEvent) and _is_path_watched(
event.dest_path
):
emit_trigger()


def _run_pytest(args) -> None:
Expand Down
35 changes: 30 additions & 5 deletions tests/test_pytest_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import pytest
from freezegun import freeze_time
from pytest_mock import MockerFixture
from pytest_watcher import __version__, watcher
from watchdog import events

from pytest_watcher import __version__, watcher


def test_version():
assert __version__ == "0.2.3"
Expand Down Expand Up @@ -50,7 +51,7 @@ def mock_emit_trigger(self, mocker: MockerFixture):
return mocker.patch("pytest_watcher.watcher.emit_trigger")

@pytest.mark.parametrize("event_type", watcher.EventHandler.EVENTS_WATCHED)
def test_ok(self, event_type, mock_emit_trigger: MagicMock):
def test_src_watched(self, event_type, mock_emit_trigger: MagicMock):
event = events.FileSystemEvent("main.py")
event.event_type = event_type

Expand All @@ -59,22 +60,46 @@ def test_ok(self, event_type, mock_emit_trigger: MagicMock):

mock_emit_trigger.assert_called_once_with()

def test_wrong_event_type(self, mock_emit_trigger: MagicMock):
event = events.FileClosedEvent("main.py")
@pytest.mark.parametrize(
"event_class", [events.FileSystemMovedEvent, events.FileMovedEvent]
)
def test_file_moved_dest_watched(self, event_class, mock_emit_trigger: MagicMock):
event = event_class("main.tmp", "main.py")

handler = watcher.EventHandler()
handler.dispatch(event)

mock_emit_trigger.assert_called_once_with()

@pytest.mark.parametrize(
"event_class", [events.FileSystemMovedEvent, events.FileMovedEvent]
)
def test_file_moved_dest_not_watched(
self, event_class, mock_emit_trigger: MagicMock
):
event = event_class("main.tmp", "main.temp")

handler = watcher.EventHandler()
handler.dispatch(event)

mock_emit_trigger.assert_not_called()

def test_file_not_watched(self, mock_emit_trigger: MagicMock):
def test_src_not_watched(self, mock_emit_trigger: MagicMock):
event = events.FileCreatedEvent("main.pyc")

handler = watcher.EventHandler()
handler.dispatch(event)

mock_emit_trigger.assert_not_called()

def test_wrong_event_type(self, mock_emit_trigger: MagicMock):
event = events.FileClosedEvent("main.py")

handler = watcher.EventHandler()
handler.dispatch(event)

mock_emit_trigger.assert_not_called()


@freeze_time("2020-01-01")
def test_emit_trigger():
Expand Down

0 comments on commit 3097bfd

Please sign in to comment.