Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions aws_lambda_builders/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down
7 changes: 5 additions & 2 deletions aws_lambda_builders/workflows/python_uv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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."""
Expand Down
61 changes: 60 additions & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os
import platform
import stat
import sys
import tempfile
from pathlib import Path

from unittest import TestCase
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):
Expand Down Expand Up @@ -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)])
5 changes: 5 additions & 0 deletions tests/unit/workflows/python_uv/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading