Skip to content

Commit

Permalink
fix: skip file opened event for template watcher (#7096)
Browse files Browse the repository at this point in the history
* chore: add debug logs for template file watcher

* update formatting

* ignore opened event

* formatting

* add unit tests

* update test name

* add missing import

* formatting
  • Loading branch information
mndeveci authored May 27, 2024
1 parent 88f1015 commit 3a337ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion samcli/lib/utils/resource_trigger.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""ResourceTrigger Classes for Creating PathHandlers According to a Resource"""

import logging
import platform
import re
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, Dict, List, Optional, cast

from typing_extensions import Protocol
from watchdog.events import FileSystemEvent, RegexMatchingEventHandler
from watchdog.events import EVENT_TYPE_OPENED, FileSystemEvent, RegexMatchingEventHandler

from samcli.lib.providers.exceptions import InvalidTemplateFile, MissingCodeUri, MissingLocalDefinition
from samcli.lib.providers.provider import Function, LayerVersion, ResourceIdentifier, Stack, get_resource_by_id
Expand All @@ -18,6 +19,9 @@
from samcli.lib.utils.resources import RESOURCES_WITH_LOCAL_PATHS
from samcli.local.lambdafn.exceptions import FunctionNotFound, ResourceNotFound

LOG = logging.getLogger(__name__)


DEFAULT_WATCH_IGNORED_RESOURCES = ["^.*\\.aws-sam.*$", "^.*node_modules.*$"]


Expand Down Expand Up @@ -134,6 +138,15 @@ def _validator_wrapper(self, event: Optional[FileSystemEvent] = None) -> None:
----------
event : Optional[FileSystemEvent], optional
"""
if event and event.event_type == EVENT_TYPE_OPENED:
# Ignore all file opened events since this event is
# added in addition to a create or modified event,
# causing an infinite loop of sync flow creations
LOG.debug("Ignoring file system OPENED event")
return
LOG.debug(
"Template watcher (%s) for stack (%s) got file event %s", self._template_file, self._stack_name, event
)
if self._validator.validate_change():
self._on_template_change(event)

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/lib/utils/test_resource_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from parameterized import parameterized
from unittest.case import TestCase
from unittest.mock import MagicMock, Mock, patch, ANY
from watchdog.events import EVENT_TYPE_OPENED
from samcli.lib.utils.resource_trigger import (
DEFAULT_WATCH_IGNORED_RESOURCES,
CodeResourceTrigger,
Expand Down Expand Up @@ -92,6 +93,18 @@ def test_validator_wrapper(self, path_mock, validator_mock):
trigger._validator_wrapper(event_mock)
on_template_change_mock.assert_called_once_with(event_mock)

@patch("samcli.lib.utils.resource_trigger.DefinitionValidator")
@patch("samcli.lib.utils.resource_trigger.Path")
def test_validator_wrapper_for_file_opened_event(self, path_mock, validator_mock):
validator_mock.return_value.raw_validate.return_value = True
on_template_change_mock = MagicMock()
event_mock = MagicMock()
event_mock.event_type = EVENT_TYPE_OPENED
validator_mock.return_value.raw_validate.return_value = True
trigger = TemplateTrigger("template.yaml", "stack", on_template_change_mock)
trigger._validator_wrapper(event_mock)
on_template_change_mock.assert_not_called()


class TestCodeResourceTrigger(TestCase):
@patch.multiple(CodeResourceTrigger, __abstractmethods__=set())
Expand Down

0 comments on commit 3a337ec

Please sign in to comment.