Skip to content

Commit 8a0f784

Browse files
committedFeb 7, 2025
Remove to_posix_path function and related tests
1 parent e00f97e commit 8a0f784

File tree

3 files changed

+1
-114
lines changed

3 files changed

+1
-114
lines changed
 

‎samcli/local/docker/utils.py

-35
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
"""
44

55
import logging
6-
import os
7-
import pathlib
86
import platform
9-
import posixpath
107
import random
11-
import re
128
import socket
139

1410
import docker
@@ -20,37 +16,6 @@
2016
LOG = logging.getLogger(__name__)
2117

2218

23-
def to_posix_path(code_path):
24-
"""
25-
Change the code_path to be of unix-style if running on windows when supplied with an absolute windows path.
26-
27-
Parameters
28-
----------
29-
code_path : str
30-
Directory in the host operating system that should be mounted within the container.
31-
Returns
32-
-------
33-
str
34-
Posix equivalent of absolute windows style path.
35-
Examples
36-
--------
37-
>>> to_posix_path('/Users/UserName/sam-app')
38-
/Users/UserName/sam-app
39-
>>> to_posix_path('C:\\\\Users\\\\UserName\\\\AppData\\\\Local\\\\Temp\\\\mydir')
40-
/c/Users/UserName/AppData/Local/Temp/mydir
41-
"""
42-
43-
return (
44-
re.sub(
45-
"^([A-Za-z])+:",
46-
lambda match: posixpath.sep + match.group().replace(":", "").lower(),
47-
pathlib.PureWindowsPath(code_path).as_posix(),
48-
)
49-
if os.name == "nt"
50-
else code_path
51-
)
52-
53-
5419
def find_free_port(network_interface: str, start: int = 5000, end: int = 9000) -> int:
5520
"""
5621
Utility function which scans through a port range in a randomized manner

‎tests/unit/local/docker/test_container.py

-61
Original file line numberDiff line numberDiff line change
@@ -181,67 +181,6 @@ def test_must_create_container_including_all_optional_values(self, mock_resolve_
181181
self.mock_docker_client.networks.get.assert_not_called()
182182
mock_resolve_symlinks.assert_not_called() # When context is BUILD
183183

184-
@patch("samcli.local.docker.utils.os")
185-
@patch("samcli.local.docker.container.Container._create_mapped_symlink_files")
186-
def test_must_create_container_translate_volume_path(self, mock_resolve_symlinks, os_mock):
187-
"""
188-
Create a container with required and optional values, with windows style volume mount.
189-
:return:
190-
"""
191-
192-
os_mock.name = "nt"
193-
host_dir = "C:\\Users\\Username\\AppData\\Local\\Temp\\tmp1337"
194-
additional_volumes = {"C:\\Users\\Username\\AppData\\Local\\Temp\\tmp1338": {"blah": "blah value"}}
195-
196-
translated_volumes = {
197-
"/c/Users/Username/AppData/Local/Temp/tmp1337": {"bind": self.working_dir, "mode": "ro,delegated"}
198-
}
199-
200-
translated_additional_volumes = {"/c/Users/Username/AppData/Local/Temp/tmp1338": {"blah": "blah value"}}
201-
202-
translated_volumes.update(translated_additional_volumes)
203-
expected_memory = "{}m".format(self.memory_mb)
204-
205-
generated_id = "fooobar"
206-
self.mock_docker_client.containers.create.return_value = Mock()
207-
self.mock_docker_client.containers.create.return_value.id = generated_id
208-
209-
container = Container(
210-
self.image,
211-
self.cmd,
212-
self.working_dir,
213-
host_dir,
214-
memory_limit_mb=self.memory_mb,
215-
exposed_ports=self.exposed_ports,
216-
entrypoint=self.entrypoint,
217-
env_vars=self.env_vars,
218-
docker_client=self.mock_docker_client,
219-
container_opts=self.container_opts,
220-
additional_volumes=additional_volumes,
221-
)
222-
223-
container_id = container.create(self.container_context)
224-
self.assertEqual(container_id, generated_id)
225-
self.assertEqual(container.id, generated_id)
226-
227-
self.mock_docker_client.containers.create.assert_called_with(
228-
self.image,
229-
command=self.cmd,
230-
working_dir=self.working_dir,
231-
volumes=translated_volumes,
232-
tty=False,
233-
use_config_proxy=True,
234-
environment=self.env_vars,
235-
ports={
236-
container_port: ("127.0.0.1", host_port)
237-
for container_port, host_port in {**self.exposed_ports, **self.always_exposed_ports}.items()
238-
},
239-
entrypoint=self.entrypoint,
240-
mem_limit=expected_memory,
241-
container="opts",
242-
)
243-
self.mock_docker_client.networks.get.assert_not_called()
244-
245184
@patch("samcli.local.docker.container.Container._create_mapped_symlink_files")
246185
def test_must_connect_to_network_on_create(self, mock_resolve_symlinks):
247186
"""

‎tests/unit/local/docker/test_utils.py

+1-18
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,10 @@
88
from unittest.mock import patch, Mock
99

1010
from samcli.lib.utils.architecture import ARM64, InvalidArchitecture, X86_64
11-
from samcli.local.docker.utils import to_posix_path, find_free_port, get_rapid_name, get_docker_platform, get_image_arch
11+
from samcli.local.docker.utils import find_free_port, get_rapid_name, get_docker_platform, get_image_arch
1212
from samcli.local.docker.exceptions import NoFreePortsError
1313

1414

15-
class TestPosixPath(TestCase):
16-
def setUp(self):
17-
self.ntpath = "C:\\Users\\UserName\\AppData\\Local\\Temp\\temp1337"
18-
self.posixpath = "/c/Users/UserName/AppData/Local/Temp/temp1337"
19-
self.current_working_dir = os.getcwd()
20-
21-
@patch("samcli.local.docker.utils.os")
22-
def test_convert_posix_path_if_windows_style_path(self, mock_os):
23-
mock_os.name = "nt"
24-
self.assertEqual(self.posixpath, to_posix_path(self.ntpath))
25-
26-
@patch("samcli.local.docker.utils.os")
27-
def test_do_not_convert_posix_path(self, mock_os):
28-
mock_os.name = "posix"
29-
self.assertEqual(self.current_working_dir, to_posix_path(self.current_working_dir))
30-
31-
3215
class TestFreePorts(TestCase):
3316
@parameterized.expand([("0.0.0.0",), ("127.0.0.1",)])
3417
@patch("samcli.local.docker.utils.socket")

0 commit comments

Comments
 (0)
Failed to load comments.