From fc11bf2a02c900fa366e54ab602a7ba3aa7b7348 Mon Sep 17 00:00:00 2001 From: y-stm Date: Thu, 9 Jan 2025 20:03:06 +0900 Subject: [PATCH] Remove to_posix_path function and related tests --- samcli/local/docker/utils.py | 35 ------------- tests/unit/local/docker/test_container.py | 61 ----------------------- tests/unit/local/docker/test_utils.py | 19 +------ 3 files changed, 1 insertion(+), 114 deletions(-) diff --git a/samcli/local/docker/utils.py b/samcli/local/docker/utils.py index d4c4ab5ba7..348616f945 100644 --- a/samcli/local/docker/utils.py +++ b/samcli/local/docker/utils.py @@ -3,12 +3,8 @@ """ import logging -import os -import pathlib import platform -import posixpath import random -import re import socket import docker @@ -20,37 +16,6 @@ LOG = logging.getLogger(__name__) -def to_posix_path(code_path): - """ - Change the code_path to be of unix-style if running on windows when supplied with an absolute windows path. - - Parameters - ---------- - code_path : str - Directory in the host operating system that should be mounted within the container. - Returns - ------- - str - Posix equivalent of absolute windows style path. - Examples - -------- - >>> to_posix_path('/Users/UserName/sam-app') - /Users/UserName/sam-app - >>> to_posix_path('C:\\\\Users\\\\UserName\\\\AppData\\\\Local\\\\Temp\\\\mydir') - /c/Users/UserName/AppData/Local/Temp/mydir - """ - - return ( - re.sub( - "^([A-Za-z])+:", - lambda match: posixpath.sep + match.group().replace(":", "").lower(), - pathlib.PureWindowsPath(code_path).as_posix(), - ) - if os.name == "nt" - else code_path - ) - - def find_free_port(network_interface: str, start: int = 5000, end: int = 9000) -> int: """ Utility function which scans through a port range in a randomized manner diff --git a/tests/unit/local/docker/test_container.py b/tests/unit/local/docker/test_container.py index b181ce33fa..0c28023001 100644 --- a/tests/unit/local/docker/test_container.py +++ b/tests/unit/local/docker/test_container.py @@ -177,67 +177,6 @@ def test_must_create_container_including_all_optional_values(self, mock_resolve_ ) self.mock_docker_client.networks.get.assert_not_called() - @patch("samcli.local.docker.utils.os") - @patch("samcli.local.docker.container.Container._create_mapped_symlink_files") - def test_must_create_container_translate_volume_path(self, mock_resolve_symlinks, os_mock): - """ - Create a container with required and optional values, with windows style volume mount. - :return: - """ - - os_mock.name = "nt" - host_dir = "C:\\Users\\Username\\AppData\\Local\\Temp\\tmp1337" - additional_volumes = {"C:\\Users\\Username\\AppData\\Local\\Temp\\tmp1338": {"blah": "blah value"}} - - translated_volumes = { - "/c/Users/Username/AppData/Local/Temp/tmp1337": {"bind": self.working_dir, "mode": "ro,delegated"} - } - - translated_additional_volumes = {"/c/Users/Username/AppData/Local/Temp/tmp1338": {"blah": "blah value"}} - - translated_volumes.update(translated_additional_volumes) - expected_memory = "{}m".format(self.memory_mb) - - generated_id = "fooobar" - self.mock_docker_client.containers.create.return_value = Mock() - self.mock_docker_client.containers.create.return_value.id = generated_id - - container = Container( - self.image, - self.cmd, - self.working_dir, - host_dir, - memory_limit_mb=self.memory_mb, - exposed_ports=self.exposed_ports, - entrypoint=self.entrypoint, - env_vars=self.env_vars, - docker_client=self.mock_docker_client, - container_opts=self.container_opts, - additional_volumes=additional_volumes, - ) - - container_id = container.create() - self.assertEqual(container_id, generated_id) - self.assertEqual(container.id, generated_id) - - self.mock_docker_client.containers.create.assert_called_with( - self.image, - command=self.cmd, - working_dir=self.working_dir, - volumes=translated_volumes, - tty=False, - use_config_proxy=True, - environment=self.env_vars, - ports={ - container_port: ("127.0.0.1", host_port) - for container_port, host_port in {**self.exposed_ports, **self.always_exposed_ports}.items() - }, - entrypoint=self.entrypoint, - mem_limit=expected_memory, - container="opts", - ) - self.mock_docker_client.networks.get.assert_not_called() - @patch("samcli.local.docker.container.Container._create_mapped_symlink_files") def test_must_connect_to_network_on_create(self, mock_resolve_symlinks): """ diff --git a/tests/unit/local/docker/test_utils.py b/tests/unit/local/docker/test_utils.py index 0f0ef57bbd..0886bcb602 100644 --- a/tests/unit/local/docker/test_utils.py +++ b/tests/unit/local/docker/test_utils.py @@ -8,27 +8,10 @@ from unittest.mock import patch, Mock from samcli.lib.utils.architecture import ARM64, InvalidArchitecture, X86_64 -from samcli.local.docker.utils import to_posix_path, find_free_port, get_rapid_name, get_docker_platform, get_image_arch +from samcli.local.docker.utils import find_free_port, get_rapid_name, get_docker_platform, get_image_arch from samcli.local.docker.exceptions import NoFreePortsError -class TestPosixPath(TestCase): - def setUp(self): - self.ntpath = "C:\\Users\\UserName\\AppData\\Local\\Temp\\temp1337" - self.posixpath = "/c/Users/UserName/AppData/Local/Temp/temp1337" - self.current_working_dir = os.getcwd() - - @patch("samcli.local.docker.utils.os") - def test_convert_posix_path_if_windows_style_path(self, mock_os): - mock_os.name = "nt" - self.assertEqual(self.posixpath, to_posix_path(self.ntpath)) - - @patch("samcli.local.docker.utils.os") - def test_do_not_convert_posix_path(self, mock_os): - mock_os.name = "posix" - self.assertEqual(self.current_working_dir, to_posix_path(self.current_working_dir)) - - class TestFreePorts(TestCase): @parameterized.expand([("0.0.0.0",), ("127.0.0.1",)]) @patch("samcli.local.docker.utils.socket")