Skip to content

Commit fc11bf2

Browse files
committedJan 14, 2025
Remove to_posix_path function and related tests
1 parent 047edfe commit fc11bf2

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
@@ -177,67 +177,6 @@ def test_must_create_container_including_all_optional_values(self, mock_resolve_
177177
)
178178
self.mock_docker_client.networks.get.assert_not_called()
179179

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

‎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.