|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pathlib |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from finecode.wm_server.runner import finecode_cmd |
| 8 | + |
| 9 | +# These tests exercise the win32 branch of `_recorded_venv_path` / `get_python_cmd` |
| 10 | +# regardless of the host OS the suite runs on, by monkeypatching `sys.platform` as |
| 11 | +# seen by `finecode_cmd` and building a Windows-style `Scripts/` venv layout. |
| 12 | + |
| 13 | + |
| 14 | +def _make_windows_venv( |
| 15 | + venv_dir_path: pathlib.Path, *, activate_bat_content: str |
| 16 | +) -> None: |
| 17 | + scripts_dir = venv_dir_path / "Scripts" |
| 18 | + scripts_dir.mkdir(parents=True) |
| 19 | + (scripts_dir / "python.exe").write_text("") |
| 20 | + (scripts_dir / "activate.bat").write_text(activate_bat_content) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(autouse=True) |
| 24 | +def _win32(monkeypatch: pytest.MonkeyPatch) -> None: |
| 25 | + monkeypatch.setattr(finecode_cmd.sys, "platform", "win32") |
| 26 | + |
| 27 | + |
| 28 | +def test_plain_set_format_is_not_mistaken_for_relocation( |
| 29 | + tmp_path: pathlib.Path, |
| 30 | +) -> None: |
| 31 | + """Regression test: a plain `set "VIRTUAL_ENV=<path>"` line quotes the whole |
| 32 | + assignment, not just the path. The old regex captured everything inside the |
| 33 | + quotes (including the literal `VIRTUAL_ENV=` prefix), so the recorded path |
| 34 | + could never equal the real venv path and every such venv looked "relocated" |
| 35 | + even when it was not. |
| 36 | + """ |
| 37 | + project_path = tmp_path / "myproject" |
| 38 | + venv_dir_path = finecode_cmd.get_venv_dir_path(project_path, "dev_workspace") |
| 39 | + _make_windows_venv( |
| 40 | + venv_dir_path, |
| 41 | + activate_bat_content=f'set "VIRTUAL_ENV={venv_dir_path}"\n', |
| 42 | + ) |
| 43 | + |
| 44 | + result = finecode_cmd.get_python_cmd(project_path, "dev_workspace") |
| 45 | + |
| 46 | + assert result == (venv_dir_path / "Scripts" / "python.exe").as_posix() |
| 47 | + |
| 48 | + |
| 49 | +def test_plain_set_format_raises_when_path_actually_differs( |
| 50 | + tmp_path: pathlib.Path, |
| 51 | +) -> None: |
| 52 | + project_path = tmp_path / "new_location" / "myproject" |
| 53 | + venv_dir_path = finecode_cmd.get_venv_dir_path(project_path, "dev_workspace") |
| 54 | + old_venv_dir_path = ( |
| 55 | + tmp_path / "old_location" / "myproject" / ".venvs" / "dev_workspace" |
| 56 | + ) |
| 57 | + _make_windows_venv( |
| 58 | + venv_dir_path, |
| 59 | + activate_bat_content=f'set "VIRTUAL_ENV={old_venv_dir_path}"\n', |
| 60 | + ) |
| 61 | + |
| 62 | + with pytest.raises(finecode_cmd.VenvRelocatedError): |
| 63 | + finecode_cmd.get_python_cmd(project_path, "dev_workspace") |
| 64 | + |
| 65 | + |
| 66 | +def test_for_loop_indirection_format_is_not_mistaken_for_relocation( |
| 67 | + tmp_path: pathlib.Path, |
| 68 | +) -> None: |
| 69 | + """uv-generated activate.bat files route VIRTUAL_ENV through a |
| 70 | + `for %%i in ("...") do @set "VIRTUAL_ENV=%%~fi"` indirection instead of a |
| 71 | + plain `set`.""" |
| 72 | + project_path = tmp_path / "myproject" |
| 73 | + venv_dir_path = finecode_cmd.get_venv_dir_path(project_path, "dev_workspace") |
| 74 | + _make_windows_venv( |
| 75 | + venv_dir_path, |
| 76 | + activate_bat_content=( |
| 77 | + f'for %%i in ("{venv_dir_path}") do @set "VIRTUAL_ENV=%%~fi"\n' |
| 78 | + ), |
| 79 | + ) |
| 80 | + |
| 81 | + result = finecode_cmd.get_python_cmd(project_path, "dev_workspace") |
| 82 | + |
| 83 | + assert result == (venv_dir_path / "Scripts" / "python.exe").as_posix() |
| 84 | + |
| 85 | + |
| 86 | +def test_for_loop_indirection_format_raises_when_path_actually_differs( |
| 87 | + tmp_path: pathlib.Path, |
| 88 | +) -> None: |
| 89 | + project_path = tmp_path / "new_location" / "myproject" |
| 90 | + venv_dir_path = finecode_cmd.get_venv_dir_path(project_path, "dev_workspace") |
| 91 | + old_venv_dir_path = ( |
| 92 | + tmp_path / "old_location" / "myproject" / ".venvs" / "dev_workspace" |
| 93 | + ) |
| 94 | + _make_windows_venv( |
| 95 | + venv_dir_path, |
| 96 | + activate_bat_content=( |
| 97 | + f'for %%i in ("{old_venv_dir_path}") do @set "VIRTUAL_ENV=%%~fi"\n' |
| 98 | + ), |
| 99 | + ) |
| 100 | + |
| 101 | + with pytest.raises(finecode_cmd.VenvRelocatedError): |
| 102 | + finecode_cmd.get_python_cmd(project_path, "dev_workspace") |
0 commit comments