Skip to content

Commit 0dbc48f

Browse files
cliffhallclaude
andcommitted
test: make the filesystem and git suites pass on Windows (#1149)
The first Windows run of the new test-windows legs found no product bugs; every failure was a Linux assumption in a test. Each assertion now holds on Windows, and the Linux and macOS checks are as strict as before. filesystem: - POSIX mode bits: Windows reports 666, so assert that there and 640/600 elsewhere - 8.3 temp paths: build expectations with realpathSync.native - / resolves to the current drive: pick a different drive in the cross-drive check - ENOTDIR on Windows comes from scandir, not realpath - /etc/hostname resolves to <drive>:\etc\hostname git: - set core.autocrlf=false in test repos so diffs carry LF on every OS - rmtree clears git's read-only object files before deleting - the traversal error echoes str(Path(repo_path)) Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent aa67a7a commit 0dbc48f

6 files changed

Lines changed: 73 additions & 12 deletions

File tree

‎src/filesystem/__tests__/path-validation.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,11 @@ describe("Path Validation", () => {
369369

370370
// But only on the same filesystem root
371371
if (path.sep === "\\") {
372-
expect(isPathWithinAllowedDirectories("D:\\other", ["/"])).toBe(false);
372+
// "/" resolves to the current drive's root, so use another drive.
373+
const otherDrive = /^D:/i.test(path.resolve("/")) ? "E:" : "D:";
374+
expect(
375+
isPathWithinAllowedDirectories(`${otherDrive}\\other`, ["/"]),
376+
).toBe(false);
373377
}
374378
});
375379
});

‎src/filesystem/__tests__/roots-utils.test.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,18 @@ describe("getValidRootDirectories", () => {
2727

2828
beforeEach(() => {
2929
vi.spyOn(console, "error").mockImplementation(() => {});
30-
testDir1 = realpathSync(mkdtempSync(join(tmpdir(), "mcp-roots-test1-")));
31-
testDir2 = realpathSync(mkdtempSync(join(tmpdir(), "mcp-roots-test2-")));
32-
testDir3 = realpathSync(mkdtempSync(join(tmpdir(), "mcp-roots-test3-")));
30+
// realpathSync.native, like the fs.promises.realpath the code uses,
31+
// expands a Windows 8.3 short name (RUNNER~1) in os.tmpdir(); the JS
32+
// realpathSync does not.
33+
testDir1 = realpathSync.native(
34+
mkdtempSync(join(tmpdir(), "mcp-roots-test1-")),
35+
);
36+
testDir2 = realpathSync.native(
37+
mkdtempSync(join(tmpdir(), "mcp-roots-test2-")),
38+
);
39+
testDir3 = realpathSync.native(
40+
mkdtempSync(join(tmpdir(), "mcp-roots-test3-")),
41+
);
3342
testFile = join(testDir1, "test-file.txt");
3443
writeFileSync(testFile, "test content");
3544
});
@@ -61,7 +70,7 @@ describe("getValidRootDirectories", () => {
6170
});
6271

6372
it("expands ~ and ~/ in a plain path to the home directory", async () => {
64-
const home = realpathSync(homedir());
73+
const home = realpathSync.native(homedir());
6574
expect(await getValidRootDirectories([{ uri: "~" }])).toEqual([home]);
6675
expect(await getValidRootDirectories([{ uri: "~/" }])).toEqual([home]);
6776
});

‎src/filesystem/__tests__/server-paths.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,12 @@ describe("symlinks", () => {
213213
it("reports a realpath failure other than ENOENT as is", async () => {
214214
await fs.writeFile(path.join(dir, "file.txt"), "");
215215
const result = await read(path.join(dir, "file.txt", "child"));
216-
expect(textOf(result)).toMatch(/^ENOTDIR: not a directory, realpath/);
216+
// Windows reports the failing syscall as scandir rather than realpath.
217+
expect(textOf(result)).toMatch(
218+
process.platform === "win32"
219+
? /^ENOTDIR: not a directory/
220+
: /^ENOTDIR: not a directory, realpath/,
221+
);
217222
});
218223
});
219224

‎src/filesystem/__tests__/server-tools.test.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ describe("read_multiple_files", () => {
484484
);
485485
expect(parts[2]).toBe(`${b}:\nbeta\n`);
486486
expect(parts[3]).toBe(
487-
`/etc/hostname: Error - Access denied - path outside allowed directories: /etc/hostname not in ${dir}`,
487+
`/etc/hostname: Error - Access denied - path outside allowed directories: ${path.resolve("/etc/hostname")} not in ${dir}`,
488488
);
489489
});
490490

@@ -541,7 +541,10 @@ describe("write_file", () => {
541541
expect(await fs.readFile(file, "utf-8")).toBe("new");
542542
expect(after.ino).toBe(before.ino);
543543
expect(after.birthtimeMs).toBe(before.birthtimeMs);
544-
expect(after.mode & 0o777).toBe(0o640);
544+
// Windows has no POSIX mode bits: a writable file always reports 666.
545+
expect(after.mode & 0o777).toBe(
546+
process.platform === "win32" ? 0o666 : 0o640,
547+
);
545548
});
546549

547550
it("keeps a hard link on overwrite (#4512)", async () => {
@@ -912,7 +915,10 @@ describe("edit_file", () => {
912915
expect(await fs.readFile(file, "utf-8")).toContain("return 2;");
913916
expect(after.ino).toBe(before.ino);
914917
expect(after.birthtimeMs).toBe(before.birthtimeMs);
915-
expect(after.mode & 0o777).toBe(0o600);
918+
// Windows has no POSIX mode bits: a writable file always reports 666.
919+
expect(after.mode & 0o777).toBe(
920+
process.platform === "win32" ? 0o666 : 0o600,
921+
);
916922
});
917923

918924
it("keeps a hard link on edit (#4512)", async () => {
@@ -1298,7 +1304,10 @@ describe("get_file_info", () => {
12981304
expect(lines[0]).toBe("size: 5");
12991305
expect(lines[4]).toBe("isDirectory: false");
13001306
expect(lines[5]).toBe("isFile: true");
1301-
expect(lines[6]).toBe("permissions: 640");
1307+
// Windows has no POSIX mode bits: a writable file always reports 666.
1308+
expect(lines[6]).toBe(
1309+
process.platform === "win32" ? "permissions: 666" : "permissions: 640",
1310+
);
13021311
// Dates are rendered with Date#toString, in the server's local time zone.
13031312
expect(lines[2]).toMatch(/^modified: \w{3} \w{3} \d{2} \d{4} /);
13041313
});

‎src/git/tests/conftest.py‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
assertion is then made on what a client receives over the wire.
77
"""
88

9+
import os
910
import shutil
11+
import stat
1012
from collections.abc import AsyncIterator, Iterator
1113
from contextlib import asynccontextmanager
1214
from pathlib import Path
@@ -28,6 +30,7 @@
2830
def test_repository(tmp_path: Path) -> Iterator[git.Repo]:
2931
repo_path = tmp_path / "temp_test_repo"
3032
test_repo = git.Repo.init(repo_path)
33+
disable_autocrlf(test_repo)
3134

3235
Path(repo_path / "test.txt").write_text("test")
3336
test_repo.index.add(["test.txt"])
@@ -39,12 +42,41 @@ def test_repository(tmp_path: Path) -> Iterator[git.Repo]:
3942
# their cwd is inside the repository, so rmtree fails with WinError 32
4043
# (#4855, unblocks #1149).
4144
test_repo.close()
42-
shutil.rmtree(repo_path)
45+
rmtree(repo_path)
46+
47+
48+
def disable_autocrlf(repo: git.Repo) -> None:
49+
"""Commit and check out bytes unchanged, whatever the host's git config.
50+
51+
Git for Windows defaults to `core.autocrlf=true`, which rewrites the
52+
fixtures' LF line endings to CRLF in the working tree, so diffs and `show`
53+
output carry CRLF there and nowhere else. Set it repo-locally, before
54+
anything is committed, so every OS sees the same bytes.
55+
"""
56+
with repo.config_writer() as config:
57+
config.set_value("core", "autocrlf", "false")
58+
59+
60+
def _clear_readonly_and_retry(func: Any, path: str, _exc_info: Any) -> None:
61+
# Git writes object files read-only; on Windows that makes them
62+
# undeletable (WinError 5) until the read-only bit is cleared.
63+
os.chmod(path, stat.S_IWRITE)
64+
func(path)
65+
66+
67+
def rmtree(path: Path) -> None:
68+
"""`shutil.rmtree` that also removes git's read-only files on Windows.
69+
70+
Uses `onerror`, not `onexc`: the server pins Python 3.10, and `onexc` is
71+
3.12+.
72+
"""
73+
shutil.rmtree(path, onerror=_clear_readonly_and_retry)
4374

4475

4576
def make_repo(path: Path) -> git.Repo:
4677
"""A repository on branch `main` with one commit of `test.txt`."""
4778
repo = git.Repo.init(path, initial_branch="main")
79+
disable_autocrlf(repo)
4880
with repo.config_writer() as config:
4981
config.set_value("user", "name", "Test User")
5082
config.set_value("user", "email", "[email protected]")

‎src/git/tests/test_protocol.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,10 @@ async def test_restricted_server_rejects_dotdot_traversal(
985985
other = make_repo(tmp_path / "other")
986986
try:
987987
sneaky = f"{root}/../other"
988+
# The server echoes the path as `Path(repo_path)` renders it: unchanged
989+
# on POSIX, with backslash separators on Windows.
988990
assert await call(root, "git_status", {"repo_path": sneaky}) == outside_error(
989-
sneaky, root
991+
Path(sneaky), root
990992
)
991993
finally:
992994
other.close()

0 commit comments

Comments
 (0)