Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip file opened event for template watcher #7096

Merged
merged 9 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading