Skip to content

Commit

Permalink
Merge from aws/aws-sam-cli/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
aws-sam-cli-bot authored Dec 6, 2023
2 parents f37cf2b + 43ead54 commit 53694c0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
19 changes: 16 additions & 3 deletions samcli/lib/utils/tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def create_tarball(
tarballfile.close()


def _validate_destinations_exists(tar_paths: List[Union[str, Path]]) -> bool:
def _validate_destinations_exists(tar_paths: Union[List[Union[str, Path]], List[Path]]) -> bool:
"""
Validates whether the destination of a symlink exists by resolving the link
and checking the resolved path.
Expand All @@ -79,9 +79,22 @@ def _validate_destinations_exists(tar_paths: List[Union[str, Path]]) -> bool:
"""
for file in tar_paths:
file_path_obj = Path(file)
resolved_path = file_path_obj.resolve()

if file_path_obj.is_symlink() and not resolved_path.exists():
try:
resolved_path = file_path_obj.resolve()
except OSError:
# this exception will occur on Windows and will return
# a WinError 123
LOG.warning(f"Failed to resolve file {file_path_obj} on the host machine")
return False

if file_path_obj.is_dir():
# recursively call this method to validate the children are not symlinks to empty locations
children = list(file_path_obj.iterdir())
if not _validate_destinations_exists(children):
# exits early
return False
elif file_path_obj.is_symlink() and not resolved_path.exists():
LOG.warning(f"Symlinked file {file_path_obj} -> {resolved_path} does not exist!")
return False

Expand Down
29 changes: 29 additions & 0 deletions tests/unit/lib/utils/test_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,38 @@ def test_validating_symlinked_tar_path(self, does_resolved_exist, path_mock):
mock_path_object.resolve.return_value = mock_resolved_object
mock_path_object.is_symlink = Mock()
mock_path_object.is_symlink.return_value = True
mock_path_object.is_dir.return_value = False

path_mock.return_value = mock_path_object

result = _validate_destinations_exists(["mock_path"])

self.assertEqual(result, does_resolved_exist)

@parameterized.expand(
[
(True,),
(False,),
]
)
@patch("samcli.lib.utils.tar.Path")
def test_validating_symlinked_tar_path_directory(self, file_exists, path_mock):
mock_child_resolve = Mock()
mock_child_resolve.exists.return_value = file_exists

mock_child = Mock()
mock_child.is_symlink.return_value = True
mock_child.is_dir.return_value = False
mock_child.resolve.return_value = mock_child_resolve

mock_dir_object = Mock()
mock_dir_object.is_symlink.return_value = False
mock_dir_object.is_dir.return_value = True
mock_dir_object.iterdir.return_value = ["mock_child"]
mock_dir_object.resolve = Mock()

path_mock.side_effect = [mock_dir_object, mock_child]

result = _validate_destinations_exists(["mock_folder"])

self.assertEqual(result, file_exists)

0 comments on commit 53694c0

Please sign in to comment.