Skip to content

Commit e608383

Browse files
authored
fix: Fix copying symlink files for Windows (#7351)
* fix: Fix copying symlink files for Windows * make black happy
1 parent 7b5cf94 commit e608383

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

samcli/lib/utils/osutils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Common OS utilities
33
"""
44

5+
import errno
56
import io
67
import logging
78
import os
@@ -178,7 +179,15 @@ def copytree(source, destination, ignore=None):
178179
if os.path.isdir(new_source):
179180
copytree(new_source, new_destination, ignore=ignore)
180181
else:
181-
shutil.copy2(new_source, new_destination)
182+
try:
183+
shutil.copy2(new_source, new_destination)
184+
except OSError as e:
185+
if e.errno != errno.EINVAL:
186+
raise e
187+
188+
# Symlinks do not get copied for Windows using shutil.copy2, which is why
189+
# they are handled separately here.
190+
create_symlink_or_copy(new_source, new_destination)
182191

183192

184193
def convert_files_to_unix_line_endings(path: str, target_files: Optional[List[str]] = None) -> None:

tests/unit/lib/utils/test_osutils.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,69 @@ def test_must_delete_if_path_exist(self, patched_rmtree, patched_path):
9090
patched_rmtree.assert_called_with(mock_path_obj)
9191

9292

93+
class Test_copytree(TestCase):
94+
@patch("samcli.lib.utils.osutils.Path")
95+
@patch("samcli.lib.utils.osutils.os")
96+
@patch("samcli.lib.utils.osutils.shutil.copy2")
97+
def test_must_copytree(self, patched_copy2, patched_os, patched_path):
98+
source_path = "mock-source/path"
99+
destination_path = "mock-destination/path"
100+
mock_path_obj = Mock()
101+
patched_path.exists.return_value = True
102+
patched_os.path.return_value = mock_path_obj
103+
104+
patched_os.path.join.side_effect = [source_path, destination_path]
105+
patched_os.path.isdir.return_value = False
106+
patched_os.listdir.return_value = ["mock-source-file1"]
107+
osutils.copytree(source_path, destination_path)
108+
109+
patched_os.path.join.assert_called()
110+
patched_copy2.assert_called_with(source_path, destination_path)
111+
112+
@patch("samcli.lib.utils.osutils.Path")
113+
@patch("samcli.lib.utils.osutils.os")
114+
@patch("samcli.lib.utils.osutils.shutil.copy2")
115+
def test_copytree_throws_oserror_path_exists(self, patched_copy2, patched_os, patched_path):
116+
source_path = "mock-source/path"
117+
destination_path = "mock-destination/path"
118+
mock_path_obj = Mock()
119+
patched_path.exists.return_value = True
120+
patched_os.path.return_value = mock_path_obj
121+
patched_copy2.side_effect = OSError("mock-os-error")
122+
123+
patched_os.path.join.side_effect = [source_path, destination_path]
124+
patched_os.path.isdir.return_value = False
125+
patched_os.listdir.return_value = ["mock-source-file1"]
126+
with self.assertRaises(OSError):
127+
osutils.copytree(source_path, destination_path)
128+
129+
patched_os.path.join.assert_called()
130+
patched_copy2.assert_called_with(source_path, destination_path)
131+
132+
@patch("samcli.lib.utils.osutils.create_symlink_or_copy")
133+
@patch("samcli.lib.utils.osutils.Path")
134+
@patch("samcli.lib.utils.osutils.os")
135+
@patch("samcli.lib.utils.osutils.shutil.copy2")
136+
def test_copytree_symlink_copy_error_handling(
137+
self, patched_copy2, patched_os, patched_path, patched_create_symlink_or_copy
138+
):
139+
source_path = "mock-source/path"
140+
destination_path = "mock-destination/path"
141+
mock_path_obj = Mock()
142+
patched_path.exists.return_value = True
143+
patched_os.path.return_value = mock_path_obj
144+
patched_copy2.side_effect = OSError(22, "mock-os-error")
145+
146+
patched_os.path.join.side_effect = [source_path, destination_path]
147+
patched_os.path.isdir.return_value = False
148+
patched_os.listdir.return_value = ["mock-source-file1"]
149+
osutils.copytree(source_path, destination_path)
150+
151+
patched_os.path.join.assert_called()
152+
patched_copy2.assert_called_with(source_path, destination_path)
153+
patched_create_symlink_or_copy.assert_called_with(source_path, destination_path)
154+
155+
93156
class Test_create_symlink_or_copy(TestCase):
94157
@patch("samcli.lib.utils.osutils.Path")
95158
@patch("samcli.lib.utils.osutils.os")

0 commit comments

Comments
 (0)