|
| 1 | +// The device-name and trailing dot/space refusals (#5444) on the real host, |
| 2 | +// with no path simulation. On Windows (the CI Windows leg) the tools refuse |
| 3 | +// a device name and a trailing-dot name and write nothing. On POSIX those are |
| 4 | +// ordinary, distinct file names, so they must keep working: the refusal is |
| 5 | +// Windows-only. |
| 6 | + |
| 7 | +import fs from "fs/promises"; |
| 8 | +import path from "path"; |
| 9 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 10 | +import { |
| 11 | + call, |
| 12 | + connect, |
| 13 | + makeTempDir, |
| 14 | + quietStderr, |
| 15 | + textOf, |
| 16 | + type Connected, |
| 17 | +} from "./helpers.js"; |
| 18 | + |
| 19 | +const DEVICE = /Windows reserved device names .* are not supported/; |
| 20 | +const TRAILING = /ending in a dot or a space are not supported/; |
| 21 | + |
| 22 | +let dir: string; |
| 23 | +let conn: Connected; |
| 24 | + |
| 25 | +beforeEach(async () => { |
| 26 | + quietStderr(); |
| 27 | + dir = await makeTempDir("mcp-fs-devnames-"); |
| 28 | + conn = await connect([dir]); |
| 29 | +}); |
| 30 | + |
| 31 | +afterEach(async () => { |
| 32 | + vi.restoreAllMocks(); |
| 33 | + await conn.close(); |
| 34 | + await fs.rm(dir, { recursive: true, force: true }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe.runIf(process.platform === "win32")("on Windows", () => { |
| 38 | + it("refuses a device name and a trailing dot or space", async () => { |
| 39 | + const write = await call(conn.client, "write_file", { |
| 40 | + path: path.join(dir, "NUL.txt"), |
| 41 | + content: "payload", |
| 42 | + }); |
| 43 | + expect(write.isError).toBe(true); |
| 44 | + expect(textOf(write)).toMatch(DEVICE); |
| 45 | + |
| 46 | + await fs.writeFile(path.join(dir, "notes.txt"), "visible"); |
| 47 | + const dotted = await call(conn.client, "write_file", { |
| 48 | + path: `${path.join(dir, "notes.txt")}.`, |
| 49 | + content: "overwritten", |
| 50 | + }); |
| 51 | + expect(dotted.isError).toBe(true); |
| 52 | + expect(textOf(dotted)).toMatch(TRAILING); |
| 53 | + expect(await fs.readFile(path.join(dir, "notes.txt"), "utf8")).toBe( |
| 54 | + "visible", |
| 55 | + ); |
| 56 | + |
| 57 | + const spaced = await call(conn.client, "read_text_file", { |
| 58 | + path: `${path.join(dir, "notes.txt")} `, |
| 59 | + }); |
| 60 | + expect(spaced.isError).toBe(true); |
| 61 | + expect(textOf(spaced)).toMatch(TRAILING); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe.skipIf(process.platform === "win32")("on POSIX", () => { |
| 66 | + it.each(["NUL", "con.txt", "COM1", "trailing.", "trailing "])( |
| 67 | + "still writes and reads a file named %j", |
| 68 | + async (name) => { |
| 69 | + const file = path.join(dir, name); |
| 70 | + const write = await call(conn.client, "write_file", { |
| 71 | + path: file, |
| 72 | + content: `plain ${name}`, |
| 73 | + }); |
| 74 | + expect(write.isError).toBeFalsy(); |
| 75 | + expect(await fs.readFile(file, "utf8")).toBe(`plain ${name}`); |
| 76 | + |
| 77 | + const read = await call(conn.client, "read_text_file", { path: file }); |
| 78 | + expect(read.isError).toBeFalsy(); |
| 79 | + expect(textOf(read)).toBe(`plain ${name}`); |
| 80 | + }, |
| 81 | + ); |
| 82 | +}); |
0 commit comments