fix(test): fix two flaky tests on Windows

- test_exec_head_tail_truncation: use temp script file instead of
  python -c to avoid cmd.exe quote-parsing issues after PR #2893
- test_grep_files_with_matches_supports_head_limit_and_offset: query
  full result set first to avoid mtime-dependent sort assumption
This commit is contained in:
chengyongru 2026-04-07 21:30:45 +08:00
parent ae27d69ecb
commit c44d4f2b2b
2 changed files with 22 additions and 12 deletions

View File

@ -172,6 +172,15 @@ async def test_grep_files_with_matches_supports_head_limit_and_offset(tmp_path:
(tmp_path / "src" / name).write_text("needle\n", encoding="utf-8")
tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
# Get the full (unpaginated) list to determine the expected ordering.
full_result = await tool.execute(
pattern="needle",
path="src",
head_limit=0,
)
all_files = full_result.splitlines()
result = await tool.execute(
pattern="needle",
path="src",
@ -179,8 +188,9 @@ async def test_grep_files_with_matches_supports_head_limit_and_offset(tmp_path:
offset=1,
)
lines = result.splitlines()
assert lines[0] == "src/b.py"
lines = [l for l in result.splitlines() if l and not l.startswith("(pagination")]
assert len(lines) == 1
assert lines[0] == all_files[1]
assert "pagination: limit=1, offset=1" in result

View File

@ -545,18 +545,18 @@ async def test_exec_always_returns_exit_code() -> None:
assert "hello" in result
async def test_exec_head_tail_truncation() -> None:
async def test_exec_head_tail_truncation(tmp_path) -> None:
"""Long output should preserve both head and tail."""
tool = ExecTool()
# Generate output that exceeds _MAX_OUTPUT (10_000 chars)
# Use current interpreter (PATH may not have `python`). ExecTool uses
# create_subprocess_shell: POSIX needs shlex.quote; Windows uses cmd.exe
# rules, so list2cmdline is appropriate there.
script = "print('A' * 6000 + '\\n' + 'B' * 6000)"
if sys.platform == "win32":
command = subprocess.list2cmdline([sys.executable, "-c", script])
else:
command = f"{shlex.quote(sys.executable)} -c {shlex.quote(script)}"
# Generate output that exceeds _MAX_OUTPUT (10_000 chars).
# Use current interpreter (PATH may not have ``python``). Write the
# script to a file to avoid shell-quoting issues on both POSIX and Windows.
script_file = tmp_path / "gen.py"
script_file.write_text(
"import sys;sys.stdout.write(chr(65)*6000);sys.stdout.write(chr(10));sys.stdout.write(chr(66)*6000)",
encoding="utf-8",
)
command = f"{sys.executable} {script_file}"
result = await tool.execute(command=command)
assert "chars truncated" in result
# Head portion should start with As