Skip to content

Commit

Permalink
fix: Fix copying symlink files for Windows (#7351)
Browse files Browse the repository at this point in the history
* fix: Fix copying symlink files for Windows

* make black happy
  • Loading branch information
hnnasit authored Aug 14, 2024
1 parent 7b5cf94 commit e608383
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
11 changes: 10 additions & 1 deletion samcli/lib/utils/osutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Common OS utilities
"""

import errno
import io
import logging
import os
Expand Down Expand Up @@ -178,7 +179,15 @@ def copytree(source, destination, ignore=None):
if os.path.isdir(new_source):
copytree(new_source, new_destination, ignore=ignore)
else:
shutil.copy2(new_source, new_destination)
try:
shutil.copy2(new_source, new_destination)
except OSError as e:
if e.errno != errno.EINVAL:
raise e

# Symlinks do not get copied for Windows using shutil.copy2, which is why
# they are handled separately here.
create_symlink_or_copy(new_source, new_destination)


def convert_files_to_unix_line_endings(path: str, target_files: Optional[List[str]] = None) -> None:
Expand Down
63 changes: 63 additions & 0 deletions tests/unit/lib/utils/test_osutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,69 @@ def test_must_delete_if_path_exist(self, patched_rmtree, patched_path):
patched_rmtree.assert_called_with(mock_path_obj)


class Test_copytree(TestCase):
@patch("samcli.lib.utils.osutils.Path")
@patch("samcli.lib.utils.osutils.os")
@patch("samcli.lib.utils.osutils.shutil.copy2")
def test_must_copytree(self, patched_copy2, patched_os, patched_path):
source_path = "mock-source/path"
destination_path = "mock-destination/path"
mock_path_obj = Mock()
patched_path.exists.return_value = True
patched_os.path.return_value = mock_path_obj

patched_os.path.join.side_effect = [source_path, destination_path]
patched_os.path.isdir.return_value = False
patched_os.listdir.return_value = ["mock-source-file1"]
osutils.copytree(source_path, destination_path)

patched_os.path.join.assert_called()
patched_copy2.assert_called_with(source_path, destination_path)

@patch("samcli.lib.utils.osutils.Path")
@patch("samcli.lib.utils.osutils.os")
@patch("samcli.lib.utils.osutils.shutil.copy2")
def test_copytree_throws_oserror_path_exists(self, patched_copy2, patched_os, patched_path):
source_path = "mock-source/path"
destination_path = "mock-destination/path"
mock_path_obj = Mock()
patched_path.exists.return_value = True
patched_os.path.return_value = mock_path_obj
patched_copy2.side_effect = OSError("mock-os-error")

patched_os.path.join.side_effect = [source_path, destination_path]
patched_os.path.isdir.return_value = False
patched_os.listdir.return_value = ["mock-source-file1"]
with self.assertRaises(OSError):
osutils.copytree(source_path, destination_path)

patched_os.path.join.assert_called()
patched_copy2.assert_called_with(source_path, destination_path)

@patch("samcli.lib.utils.osutils.create_symlink_or_copy")
@patch("samcli.lib.utils.osutils.Path")
@patch("samcli.lib.utils.osutils.os")
@patch("samcli.lib.utils.osutils.shutil.copy2")
def test_copytree_symlink_copy_error_handling(
self, patched_copy2, patched_os, patched_path, patched_create_symlink_or_copy
):
source_path = "mock-source/path"
destination_path = "mock-destination/path"
mock_path_obj = Mock()
patched_path.exists.return_value = True
patched_os.path.return_value = mock_path_obj
patched_copy2.side_effect = OSError(22, "mock-os-error")

patched_os.path.join.side_effect = [source_path, destination_path]
patched_os.path.isdir.return_value = False
patched_os.listdir.return_value = ["mock-source-file1"]
osutils.copytree(source_path, destination_path)

patched_os.path.join.assert_called()
patched_copy2.assert_called_with(source_path, destination_path)
patched_create_symlink_or_copy.assert_called_with(source_path, destination_path)


class Test_create_symlink_or_copy(TestCase):
@patch("samcli.lib.utils.osutils.Path")
@patch("samcli.lib.utils.osutils.os")
Expand Down

0 comments on commit e608383

Please sign in to comment.