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: Fix sync tests not cleaning up correctly and Linux watch behaviour #6372

Merged
merged 6 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
21 changes: 20 additions & 1 deletion samcli/lib/sync/watch_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
WatchManager for Sync Watch Logic
"""
import logging
import platform
import threading
import time
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Set

from watchdog.events import EVENT_TYPE_OPENED, FileSystemEvent
from watchdog.events import EVENT_TYPE_MODIFIED, EVENT_TYPE_OPENED, FileSystemEvent

from samcli.lib.providers.exceptions import InvalidTemplateFile, MissingCodeUri, MissingLocalDefinition
from samcli.lib.providers.provider import ResourceIdentifier, Stack, get_all_resource_ids
Expand Down Expand Up @@ -335,6 +336,24 @@ def on_code_change(event: Optional[FileSystemEvent] = None) -> None:
LOG.debug("Ignoring file system OPENED event")
return

if (
platform.system().lower() == "linux"
and event
and event.event_type == EVENT_TYPE_MODIFIED
and event.is_directory
):
# Linux machines appear to emit an additional event when
# a file gets updated; a folder modfied event
# If folder/file.txt gets updated, there will be two events:
# 1. file.txt modified event
# 2. folder modified event
# We want to ignore the second event
#
# It looks like the other way a folder modified event can happen
# is if the permissions of the folder were changed
LOG.debug(f"Ignoring file system MODIFIED event for folder {event.src_path}")
return

# sync flow factory should always exist, but guarding just incase
if not self._sync_flow_factory:
LOG.debug("Sync flow factory not defined, skipping trigger")
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/sync/test_sync_build_in_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def test_sync_builds_successfully_with_local_dependency(self):
tags="integ=true clarity=yes foo_bar=baz",
build_in_source=True,
)
sync_process_execute = run_command_with_input(sync_command_list, "y\n".encode())
sync_process_execute = run_command_with_input(sync_command_list, "y\n".encode(), cwd=self.test_data_path)
self.assertEqual(sync_process_execute.process.returncode, 0)
self.assertIn("Sync infra completed.", str(sync_process_execute.stderr))

Expand Down Expand Up @@ -313,7 +313,7 @@ def test_sync_code_builds_successfully_with_local_dependencies(self):
tags="integ=true clarity=yes foo_bar=baz",
build_in_source=True,
)
sync_process_execute = run_command_with_input(sync_command_list, "y\n".encode())
sync_process_execute = run_command_with_input(sync_command_list, "y\n".encode(), cwd=self.test_data_path)
self.assertEqual(sync_process_execute.process.returncode, 0)

# check whether dependencies were installed in the source directory
Expand All @@ -334,6 +334,7 @@ class TestSyncCode_BuildInSource_Nodejs_Without_Local_Dep(TestSyncCodeBase):

def tearDown(self):
super().tearDown()
shutil.rmtree(Path(self.test_data_path, ".aws-sam"), ignore_errors=True)

for path in self.source_dependencies_paths:
shutil.rmtree(path, ignore_errors=True)
Expand Down Expand Up @@ -429,7 +430,7 @@ def test_sync_code_builds_successfully_with_local_dependencies(self):
tags="integ=true clarity=yes foo_bar=baz",
build_in_source=True,
)
sync_process_execute = run_command_with_input(sync_command_list, "y\n".encode())
sync_process_execute = run_command_with_input(sync_command_list, "y\n".encode(), cwd=self.test_data_path)
self.assertEqual(sync_process_execute.process.returncode, 0)

# check whether dependencies were installed in the source directory
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/lib/sync/test_watch_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,22 @@ def test_on_code_change_wrapper_opened_event_not_called(self):

factory_mock.create_sync_flow.assert_not_called()

@patch("samcli.lib.sync.watch_manager.platform.system")
def test_on_code_change_wrapper_opened_event_not_called_linux_folder(self, platform_mock):
flow1 = MagicMock()
resource_id_mock = MagicMock()
factory_mock = MagicMock()
event_mock = MagicMock()
event_mock.event_type = "modified"
platform_mock.return_value = "linux"

self.watch_manager._sync_flow_factory = factory_mock
factory_mock.create_sync_flow.return_value = flow1

self.watch_manager._on_code_change_wrapper(resource_id_mock)(event_mock)

factory_mock.create_sync_flow.assert_not_called()

def test_on_code_change_wrapper_missing_factory_sync_not_called(self):
resource_id_mock = MagicMock()

Expand Down
Loading