diff --git a/aws_lambda_builders/utils.py b/aws_lambda_builders/utils.py index dc3dac294..a0cb43bb5 100644 --- a/aws_lambda_builders/utils.py +++ b/aws_lambda_builders/utils.py @@ -175,10 +175,6 @@ def _access_check(fn, mode): path = executable_search_paths + path if sys.platform == "win32": - # The current directory takes precedence on Windows. - if os.curdir not in path: - path.insert(0, os.curdir) - # PATHEXT is necessary to check on Windows. pathext = os.environ.get("PATHEXT", "").split(os.pathsep) # See if the given file matches any of the expected path @@ -198,13 +194,16 @@ def _access_check(fn, mode): paths = [] for dir in path: + if not dir: + # Skip empty entries (e.g. from a trailing path separator), which would resolve to the working directory + continue normdir = os.path.normcase(dir) if normdir not in seen: seen.add(normdir) for thefile in files: name = os.path.join(dir, thefile) if _access_check(name, mode): - paths.append(name) + paths.append(os.path.abspath(name)) return paths diff --git a/aws_lambda_builders/workflows/python_uv/utils.py b/aws_lambda_builders/workflows/python_uv/utils.py index 39bba89a0..4f7b3a39d 100644 --- a/aws_lambda_builders/workflows/python_uv/utils.py +++ b/aws_lambda_builders/workflows/python_uv/utils.py @@ -5,10 +5,10 @@ import logging import os import re -import shutil import subprocess from typing import List, Optional +from aws_lambda_builders.utils import which from aws_lambda_builders.workflows.python_pip.utils import OSUtils as BaseOSUtils LOG = logging.getLogger(__name__) @@ -26,7 +26,10 @@ class OSUtils(BaseOSUtils): def which(self, executable): """Find executable in PATH.""" - return shutil.which(executable) + paths = which(executable) + if isinstance(paths, str): + return paths + return paths[0] if paths else None def run_subprocess(self, cmd, cwd=None, env=None): """Run subprocess and return result.""" diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index d7c801f63..328141294 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,5 +1,7 @@ import os import platform +import stat +import sys import tempfile from pathlib import Path @@ -7,7 +9,7 @@ from unittest.mock import patch, Mock, MagicMock from aws_lambda_builders import utils -from aws_lambda_builders.utils import decode +from aws_lambda_builders.utils import decode, which class Test_create_symlink_or_copy(TestCase): @@ -268,3 +270,60 @@ def test_escaping_with_parent_reference_is_not_within(self): @patch("aws_lambda_builders.utils.os.path.commonpath", side_effect=ValueError("different drives")) def test_paths_on_different_drives_are_not_within(self, _): self.assertFalse(utils._is_within_directory("C:\\my\\path", "D:\\file")) + + +class TestWhich(TestCase): + def setUp(self): + self.temp_dir = tempfile.TemporaryDirectory() + root = Path(self.temp_dir.name) + self.working_dir = root / "working" + self.path_dir = root / "bin" + self.working_dir.mkdir() + self.path_dir.mkdir() + self.original_cwd = os.getcwd() + os.chdir(self.working_dir) + + def tearDown(self): + os.chdir(self.original_cwd) + self.temp_dir.cleanup() + + @staticmethod + def _create_executable(path): + path.write_text("") + path.chmod(path.stat().st_mode | stat.S_IXUSR) + + @staticmethod + def _tool_file_and_env(): + """ + Return the file name that which("tool") looks for on this platform, and the environment it needs. + On Windows, which() only tries the name with each PATHEXT extension. + """ + if sys.platform == "win32": + return "tool.CMD", {"PATHEXT": os.pathsep.join([".EXE", ".CMD"])} + return "tool", {} + + @patch("aws_lambda_builders.utils.sys") + def test_windows_lookup_uses_only_search_paths(self, mock_sys): + mock_sys.platform = "win32" + self._create_executable(self.working_dir / "tool.CMD") + self._create_executable(self.path_dir / "tool.CMD") + env = {"PATH": str(self.path_dir), "PATHEXT": os.pathsep.join([".EXE", ".CMD"])} + + with patch.dict(os.environ, env): + self.assertEqual(which("tool"), [os.path.abspath(self.path_dir / "tool.CMD")]) + + def test_ignores_empty_path_entries(self): + tool_file, env = self._tool_file_and_env() + self._create_executable(self.working_dir / tool_file) + env["PATH"] = str(self.path_dir) + os.pathsep + + with patch.dict(os.environ, env): + self.assertEqual(which("tool"), []) + + def test_returns_absolute_paths(self): + tool_file, env = self._tool_file_and_env() + self._create_executable(self.working_dir / tool_file) + env["PATH"] = str(self.path_dir) + + with patch.dict(os.environ, env): + self.assertEqual(which("tool", executable_search_paths=["."]), [os.path.join(os.getcwd(), tool_file)]) diff --git a/tests/unit/workflows/python_uv/test_utils.py b/tests/unit/workflows/python_uv/test_utils.py index e1966a7a4..f4e65102c 100644 --- a/tests/unit/workflows/python_uv/test_utils.py +++ b/tests/unit/workflows/python_uv/test_utils.py @@ -28,6 +28,11 @@ def test_which_nonexistent_command(self): result = self.osutils.which("nonexistent_command_12345") self.assertIsNone(result) + @patch("aws_lambda_builders.workflows.python_uv.utils.which") + def test_which_returns_first_match(self, mock_which): + mock_which.return_value = ["/first/uv", "/second/uv"] + self.assertEqual(self.osutils.which("uv"), "/first/uv") + def test_run_subprocess_success(self): rc, stdout, stderr = self.osutils.run_subprocess(["echo", "hello"]) self.assertEqual(rc, 0)