Skip to content

fix(ext): cross-platform IS_UV_VENV check without grep subprocess - #8221

Open
Vimal Sahani (VimalN2005) wants to merge 1 commit into
microsoft:mainfrom
VimalN2005:fix/windows-test-grep-check
Open

fix(ext): cross-platform IS_UV_VENV check without grep subprocess#8221
Vimal Sahani (VimalN2005) wants to merge 1 commit into
microsoft:mainfrom
VimalN2005:fix/windows-test-grep-check

Conversation

@VimalN2005

Copy link
Copy Markdown

Summary

Fixes pytest test collection failure on Windows by replacing the Unix grep subprocess call in IS_UV_VENV with cross-platform Python file inspection.

Problem

In packages/autogen-ext/tests/code_executors/test_commandline_code_executor.py, IS_UV_VENV was defined as:
python subprocess.run( ["grep", "-q", "^uv = ", os.path.join(venv_path, "pyvenv.cfg")], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ).returncode == 0

On Windows environments where grep is not natively installed in PATH, running pytest fails immediately during collection at import time:
ext packages\autogen-ext\tests\code_executors\test_commandline_code_executor.py:38: in <lambda> subprocess.run( ... FileNotFoundError: [WinError 2] The system cannot find the file specified

Solution

Replaced the external grep invocation with native Python file reading via Path.read_text():
`python
def _check_is_uv_venv() -> bool:
venv_path = os.environ.get("VIRTUAL_ENV")
if not venv_path:
return False
cfg_path = Path(venv_path) / "pyvenv.cfg"
if not cfg_path.is_file():
return False
try:
content = cfg_path.read_text(encoding="utf-8", errors="ignore")
return any(line.startswith("uv = ") for line in content.splitlines())
except OSError:
return False

IS_UV_VENV: bool = _check_is_uv_venv()
`

Benefits

  1. Cross-Platform: Operates identically across Windows, macOS, and Linux without depending on shell utilities.
  2. Performance: Avoids spawning an external process during pytest test collection.
  3. Robustness: Gracefully handles missing pyvenv.cfg or OS permissions.

Testing

  • Verified with uv run pytest packages/autogen-ext/tests/code_executors/test_commandline_code_executor.py on Windows (Python 3.12). Test collection succeeds and tests execute cleanly.
  • Formatted and linted with uv run ruff check and uv run ruff format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant