Skip to content

Commit fe69c0b

Browse files
claxmancopybara-github
authored andcommitted
fix: normalize jj and hg added paths on Windows
Merge #7046 PiperOrigin-RevId: 989816214
1 parent b627a15 commit fe69c0b

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

‎scripts/check_new_py_files.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def get_vcs_added_files(root: str = '.') -> set[str] | None:
428428
p = parts[1].strip()
429429
if jj_root and not os.path.isabs(p):
430430
p = os.path.join(jj_root, p)
431-
added.add(p)
431+
added.add(p.replace(os.sep, '/'))
432432
return added
433433

434434
# 3. hg
@@ -450,9 +450,11 @@ def get_vcs_added_files(root: str = '.') -> set[str] | None:
450450
# A repository whose phases do not distinguish local work this way.
451451
_, out = _run_cmd(['hg', 'status', '--added', '--no-status'], cwd=root)
452452
return {
453-
os.path.join(hg_root, f.strip())
454-
if (hg_root and not os.path.isabs(f.strip()))
455-
else f.strip()
453+
(
454+
os.path.join(hg_root, f.strip())
455+
if (hg_root and not os.path.isabs(f.strip()))
456+
else f.strip()
457+
).replace(os.sep, '/')
456458
for f in out.splitlines()
457459
if f.strip()
458460
}

‎tests/unittests/scripts/test_check_new_py_files.py‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from __future__ import annotations
1818

19+
import ntpath
1920
import os
2021
import pathlib
2122
import shutil
@@ -804,22 +805,37 @@ def fake_run_cmd(cmd: list[str], cwd: str | None = None) -> tuple[int, str]:
804805
assert check_new_py_files.get_vcs_added_files('.') == set()
805806

806807

807-
def test_get_vcs_added_files_jj(monkeypatch: pytest.MonkeyPatch) -> None:
808+
def _patch_windows_paths(monkeypatch: pytest.MonkeyPatch) -> None:
809+
monkeypatch.setattr(check_new_py_files.os, 'path', ntpath)
810+
monkeypatch.setattr(check_new_py_files.os, 'sep', '\\')
811+
812+
813+
@pytest.mark.parametrize('windows', [False, True])
814+
def test_get_vcs_added_files_jj(
815+
monkeypatch: pytest.MonkeyPatch, windows: bool
816+
) -> None:
808817
def fake_which(cmd: str) -> str | None:
809818
return '/usr/bin/' + cmd if cmd == 'jj' else None
810819

811820
def fake_run_cmd(cmd: list[str], cwd: str | None = None) -> tuple[int, str]:
812821
if cmd == ['jj', 'root']:
813-
return 0, '/workspace'
822+
return 0, r'C:\workspace' if windows else '/workspace'
814823
if cmd == ['jj', 'diff', '--summary']:
815824
return 0, 'A src/google/adk/agents/_jj_agent.py\nM existing.py'
816825
return 1, ''
817826

818827
monkeypatch.setattr(check_new_py_files.shutil, 'which', fake_which)
819828
monkeypatch.setattr(check_new_py_files, '_run_cmd', fake_run_cmd)
829+
if windows:
830+
_patch_windows_paths(monkeypatch)
820831

832+
expected = (
833+
'C:/workspace/src/google/adk/agents/_jj_agent.py'
834+
if windows
835+
else '/workspace/src/google/adk/agents/_jj_agent.py'
836+
)
821837
added = check_new_py_files.get_vcs_added_files('.')
822-
assert added == {'/workspace/src/google/adk/agents/_jj_agent.py'}
838+
assert added == {expected}
823839

824840

825841
_HG_SYNCED_BASE_STATUS = [
@@ -833,22 +849,32 @@ def fake_run_cmd(cmd: list[str], cwd: str | None = None) -> tuple[int, str]:
833849
_HG_WORKING_DIR_STATUS = ['hg', 'status', '--added', '--no-status']
834850

835851

836-
def test_get_vcs_added_files_hg(monkeypatch: pytest.MonkeyPatch) -> None:
852+
@pytest.mark.parametrize('windows', [False, True])
853+
def test_get_vcs_added_files_hg(
854+
monkeypatch: pytest.MonkeyPatch, windows: bool
855+
) -> None:
837856
def fake_which(cmd: str) -> str | None:
838857
return '/usr/bin/' + cmd if cmd == 'hg' else None
839858

840859
def fake_run_cmd(cmd: list[str], cwd: str | None = None) -> tuple[int, str]:
841860
if cmd == ['hg', 'root']:
842-
return 0, '/workspace'
861+
return 0, r'C:\workspace' if windows else '/workspace'
843862
if cmd == _HG_SYNCED_BASE_STATUS:
844863
return 0, 'src/google/adk/agents/_hg_agent.py'
845864
return 1, ''
846865

847866
monkeypatch.setattr(check_new_py_files.shutil, 'which', fake_which)
848867
monkeypatch.setattr(check_new_py_files, '_run_cmd', fake_run_cmd)
868+
if windows:
869+
_patch_windows_paths(monkeypatch)
849870

871+
expected = (
872+
'C:/workspace/src/google/adk/agents/_hg_agent.py'
873+
if windows
874+
else '/workspace/src/google/adk/agents/_hg_agent.py'
875+
)
850876
added = check_new_py_files.get_vcs_added_files('.')
851-
assert added == {'/workspace/src/google/adk/agents/_hg_agent.py'}
877+
assert added == {expected}
852878

853879

854880
def test_get_vcs_added_files_hg_sees_an_already_committed_add(

0 commit comments

Comments
 (0)