From c8bf4bc1c929783269f79c981573a8f3ccd0354d Mon Sep 17 00:00:00 2001 From: Thomas Kosiewski Date: Mon, 23 Nov 2026 16:01:14 +0000 Subject: [PATCH 1/3] test: show that a line break in a file name runs as an Ex command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit openFile and openDiff open a file with vim.cmd("edit " .. fnameescape(path)). fnameescape() keeps a line break in the name, and vim.cmd() with a string runs each line as its own Ex command, so the text after the line break runs. The real-Neovim spec plants a file whose name holds a line break followed by a command that sets g:claudecode_injected, and opens it through openFile, diff.open_diff_blocking and the legacy diff.open_diff (as the original and as the new file the temp file is named after). It fails on Neovim 0.8.0, 0.11.6 and 0.12.3: the command runs. The unit specs expect openFile and openDiff to refuse a path that holds a line feed, a carriage return or a NUL with -32602, naming the parameter, before anything reads the path. They fail: both handlers accept such a path. _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_ --- .../integration/file_name_line_break_spec.lua | 112 ++++++++++++++++++ tests/unit/tools/open_diff_mcp_spec.lua | 33 ++++++ tests/unit/tools/open_file_spec.lua | 19 +++ 3 files changed, 164 insertions(+) create mode 100644 tests/integration/file_name_line_break_spec.lua diff --git a/tests/integration/file_name_line_break_spec.lua b/tests/integration/file_name_line_break_spec.lua new file mode 100644 index 000000000..08a8215c5 --- /dev/null +++ b/tests/integration/file_name_line_break_spec.lua @@ -0,0 +1,112 @@ +-- Real-Neovim check that openFile and the diff open a file whose name holds a line break as that one file, and run +-- nothing in the name. fnameescape() puts a backslash before a line break but keeps the line break, and vim.cmd() with a +-- string runs each of its lines as an Ex command, so `vim.cmd("edit " .. vim.fn.fnameescape(path))` ran the text after +-- the line break. Busted's mock vim runs no Ex commands, so under busted this is pending; +-- scripts/run_integration_tests_individually.sh runs it in Neovim. +if vim == nil or vim.fn.has("nvim") ~= 1 then + describe("file names with a line break", function() + pending("needs a real Neovim") + end) + return +end + +describe("file names with a line break", function() + local diff = require("claudecode.diff") + -- After the line break comes an Ex command that sets g:claudecode_injected. fnameescape() changes none of its + -- characters, and the `--` turns the `.new` that the legacy diff adds to its temp file into a Lua comment. + local name = "x\nlua(vim.g).claudecode_injected=1--" + local dir, saved_cache_home + + ---@return integer? buf The buffer named `buf_name` + local function buffer_named(buf_name) + for _, buf in ipairs(vim.api.nvim_list_bufs()) do + if vim.api.nvim_buf_get_name(buf) == buf_name then + return buf + end + end + end + + before_each(function() + vim.g.claudecode_injected = nil + -- Neovim resolves links in the folder of a buffer name (macOS: /var is /private/var), so compare real paths. + dir = vim.fn.tempname() + vim.fn.mkdir(dir, "p") + dir = (vim.uv or vim.loop).fs_realpath(dir) + -- The legacy diff writes its proposal under stdpath("cache"); keep that out of the real cache folder. + saved_cache_home = vim.env.XDG_CACHE_HOME + vim.env.XDG_CACHE_HOME = dir .. "/cache" + diff.setup({ terminal = { provider = "none" }, diff_opts = {} }) + _G.claude_deferred_responses = {} + end) + + after_each(function() + diff._cleanup_all_active_diffs("test cleanup") + vim.cmd("silent! diffoff!") + vim.cmd("silent! tabonly!") + vim.cmd("silent! only!") + vim.cmd("silent! %bwipeout!") + vim.env.XDG_CACHE_HOME = saved_cache_home + vim.fn.delete(dir, "rf") + _G.claude_deferred_responses = nil + vim.g.claudecode_injected = nil + end) + + it("openFile runs nothing in the name", function() + local path = dir .. "/" .. name + vim.fn.writefile({ "old text" }, path) + + pcall(require("claudecode.tools.open_file").handler, { filePath = path }) + + assert.is_nil(vim.g.claudecode_injected, "the text after the line break ran as an Ex command") + end) + + it("openDiff opens the original that has the name, and runs nothing in it", function() + local path = dir .. "/" .. name + vim.fn.writefile({ "old text" }, path) + + local co = coroutine.create(function() + return diff.open_diff_blocking(path, path, "new text\n", "line break name", nil) + end) + local ok, err = coroutine.resume(co) + _G.claude_deferred_responses[tostring(co)] = function() end + + assert.is_nil(vim.g.claudecode_injected, "the text after the line break ran as an Ex command") + assert.is_true(ok, vim.inspect(err)) + assert.equal("suspended", coroutine.status(co), "openDiff waits for the user") + local original = buffer_named(path) + assert.is_not_nil(original, "a buffer has the file's name") + assert.same({ "old text" }, vim.api.nvim_buf_get_lines(original, 0, -1, false)) + local win = vim.fn.win_findbuf(original)[1] + assert.is_not_nil(win, "a window shows the original") + assert.is_true(vim.wo[win].diff, "the original is in diff mode") + end) + + it("the legacy open_diff opens the original that has the name, and runs nothing in it", function() + local path = dir .. "/" .. name + vim.fn.writefile({ "old text" }, path) + + local ok, result = pcall(diff.open_diff, path, dir .. "/new.txt", "new text\n", "legacy line break old") + + assert.is_nil(vim.g.claudecode_injected, "the text after the line break ran as an Ex command") + assert.is_true(ok, vim.inspect(result)) + assert.is_true(result.success) + local original = buffer_named(path) + assert.is_not_nil(original, "a buffer has the file's name") + assert.same({ "old text" }, vim.api.nvim_buf_get_lines(original, 0, -1, false)) + end) + + it("the legacy open_diff opens its temp file named after the new file, and runs nothing in the name", function() + local old = dir .. "/old.txt" + vim.fn.writefile({ "old text" }, old) + local new_path = dir .. "/" .. name + + local ok, result = pcall(diff.open_diff, old, new_path, "new text\n", "legacy line break new") + + assert.is_nil(vim.g.claudecode_injected, "the text after the line break ran as an Ex command") + assert.is_true(ok, vim.inspect(result)) + assert.is_true(result.success) + local proposal = buffer_named(new_path .. " (New)") + assert.is_not_nil(proposal, "the proposal has the new file's name") + assert.same({ "new text" }, vim.api.nvim_buf_get_lines(proposal, 0, -1, false)) + end) +end) diff --git a/tests/unit/tools/open_diff_mcp_spec.lua b/tests/unit/tools/open_diff_mcp_spec.lua index 1f660f0d6..60ecd736d 100644 --- a/tests/unit/tools/open_diff_mcp_spec.lua +++ b/tests/unit/tools/open_diff_mcp_spec.lua @@ -138,6 +138,39 @@ describe("openDiff tool MCP compliance", function() end end) + -- A line break in a file name ended the Ex command that opened it, so the rest ran as another command. A NUL makes + -- Neovim's file functions raise (E976). + describe("paths that hold a line break or a NUL", function() + for _, param_name in ipairs({ "old_file_path", "new_file_path" }) do + for _, case in ipairs({ + { label = "a line feed", value = "/test/a.txt\nlua vim.g.ran = 1" }, + { label = "a carriage return", value = "/test/a.txt\rlua vim.g.ran = 1" }, + { label = "a NUL", value = "/test/a.txt\0b" }, + }) do + it("should refuse " .. param_name .. " with " .. case.label, function() + local params = { + old_file_path = test_old_file, + new_file_path = test_new_file, + new_file_contents = test_content_new, + tab_name = test_tab_name, + } + params[param_name] = case.value + + local co = coroutine.create(function() + open_diff_tool.handler(params) + end) + + local success, err = coroutine.resume(co) + assert.is_false(success) + assert.is_table(err) + assert.equal(-32602, err.code) + assert.equal("Invalid params", err.message) + assert_contains(err.data, param_name) + end) + end + end + end) + describe("coroutine context requirement", function() it("should error when not in coroutine context", function() local params = { diff --git a/tests/unit/tools/open_file_spec.lua b/tests/unit/tools/open_file_spec.lua index 7cc60d36d..c75547f9a 100644 --- a/tests/unit/tools/open_file_spec.lua +++ b/tests/unit/tools/open_file_spec.lua @@ -254,6 +254,25 @@ describe("Tool: open_file", function() end) end + -- A line break in a file name ended the Ex command that opened it, so the rest ran as another command. A NUL makes + -- Neovim's file functions raise (E976). + for _, case in ipairs({ + { label = "a line feed", value = "a.txt\nlua vim.g.ran = 1" }, + { label = "a carriage return", value = "a.txt\rlua vim.g.ran = 1" }, + { label = "a NUL", value = "a.txt\0b" }, + }) do + it("should refuse a filePath that holds " .. case.label .. " before it touches the path", function() + local success, err = pcall(open_file_handler, { filePath = case.value }) + expect(success).to_be_false() + expect(err).to_be_table() + expect(err.code).to_be(-32602) + assert_contains(err.message, "Invalid params") + assert_contains(err.data, "filePath") + assert.spy(_G.vim.fn.filereadable).was_not_called() + assert.are.same({}, _G.vim.cmd_history) + end) + end + it("should error if file is not readable", function() local params = { filePath = "non_readable_file.txt" } local success, err = pcall(open_file_handler, params) From 1161f60421c18418870f9b13e3235530454551fe Mon Sep 17 00:00:00 2001 From: Thomas Kosiewski Date: Mon, 23 Nov 2026 16:23:42 +0000 Subject: [PATCH 2/3] test: check only that filereadable never sees a refused filePath MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The module calls filereadable for the loaded buffers when it loads, so the spec cannot expect no call at all. It expects no call with the refused path. _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_ --- tests/unit/tools/open_file_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/tools/open_file_spec.lua b/tests/unit/tools/open_file_spec.lua index c75547f9a..e3cc315f3 100644 --- a/tests/unit/tools/open_file_spec.lua +++ b/tests/unit/tools/open_file_spec.lua @@ -268,7 +268,7 @@ describe("Tool: open_file", function() expect(err.code).to_be(-32602) assert_contains(err.message, "Invalid params") assert_contains(err.data, "filePath") - assert.spy(_G.vim.fn.filereadable).was_not_called() + assert.spy(_G.vim.fn.filereadable).was_not_called_with(case.value) assert.are.same({}, _G.vim.cmd_history) end) end From faa378a36a053cd5c3d032e9d525f0d9f9854cb2 Mon Sep 17 00:00:00 2001 From: Thomas Kosiewski Date: Mon, 23 Nov 2026 16:56:03 +0000 Subject: [PATCH 3/3] fix: open a file whose name holds a line break as that one file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit openFile and openDiff opened files with vim.cmd("edit " .. fnameescape(path)). fnameescape() puts a backslash before a line break but keeps it, and vim.cmd() with a string runs each line as its own Ex command, so a filePath, old_file_path or new_file_path with a line break ran the text after it (Lua and shell through :lua and :!). The legacy diff.open_diff names its temp file after new_file_path, so that path alone was enough there. The four sinks now use vim.cmd.edit(), which runs one command through nvim_cmd and never splits it. They keep fnameescape(): without it, nvim_cmd runs a relative name that starts with + as a +cmd. The temp file keeps :noswapfile through mods. openFile and openDiff also refuse a path that holds a line feed, a carriage return or a NUL with -32602, naming the parameter, before anything reads it. filereadable() raises E976 on a NUL. The mock vim.cmd is now callable and indexable (tests/helpers/ex_command.lua), and records vim.cmd.edit(x) as "edit x", as the string form did. The specs that replace vim.cmd and reach a changed sink use the same helper. _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_ --- .../skills/verify-diff-lifecycle/covered.lock | 8 ++--- ARCHITECTURE.md | 2 +- lua/claudecode/diff.lua | 6 ++-- lua/claudecode/tools/README.md | 3 +- lua/claudecode/tools/open_diff.lua | 11 +++++++ lua/claudecode/tools/open_file.lua | 8 ++++- tests/helpers/ex_command.lua | 32 +++++++++++++++++++ .../integration/file_name_line_break_spec.lua | 10 +++--- tests/mocks/vim.lua | 5 +-- tests/unit/diff_buffer_cleanup_spec.lua | 3 +- tests/unit/diff_spec.lua | 15 +++++---- tests/unit/tools/open_file_spec.lua | 15 +++++---- 12 files changed, 86 insertions(+), 32 deletions(-) create mode 100644 tests/helpers/ex_command.lua diff --git a/.agents/skills/verify-diff-lifecycle/covered.lock b/.agents/skills/verify-diff-lifecycle/covered.lock index 6235a74e6..7d32ae796 100644 --- a/.agents/skills/verify-diff-lifecycle/covered.lock +++ b/.agents/skills/verify-diff-lifecycle/covered.lock @@ -3,7 +3,7 @@ file lua/claudecode/config.lua 3afb1bdeff27bfb6 == lua/claudecode/cwd.lua file lua/claudecode/cwd.lua b034e82c76e8dee4 == lua/claudecode/diff.lua -file lua/claudecode/diff.lua 44c686c5bff3fc80 +file lua/claudecode/diff.lua 2b7fd97e91645f00 block resolve_saved 36fd89d5b97f0173 block resolve_rejected c7a620816abff9a1 block open_blocking f2f5ae6216c0457a @@ -211,10 +211,10 @@ file lua/claudecode/tools/get_workspace_folders.lua 80f38bdf62e55c3e file lua/claudecode/tools/init.lua a8ff3ef7cd3c9df9 block handle_invoke 5c2422dfe7e2ea93 == lua/claudecode/tools/open_diff.lua -file lua/claudecode/tools/open_diff.lua d4050352cb09dfb9 -block open_diff_tool 1a0e157e8e0a9452 +file lua/claudecode/tools/open_diff.lua c2eef8418b600af3 +block open_diff_tool fa7326d9e69eb708 == lua/claudecode/tools/open_file.lua -file lua/claudecode/tools/open_file.lua b277cbe8b86ac7c8 +file lua/claudecode/tools/open_file.lua 8ce0857bca852c81 == lua/claudecode/tools/save_document.lua file lua/claudecode/tools/save_document.lua 584b046bec6cb1f6 == lua/claudecode/types.lua diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 7250946cf..a2a3c26e4 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -73,7 +73,7 @@ M.register("openFile", { required = { "filePath" } }, function(params) -- Implementation - vim.cmd("edit " .. params.filePath) + vim.cmd.edit(vim.fn.fnameescape(params.filePath)) return { content = {{ type = "text", text = "Opened" }} } end) diff --git a/lua/claudecode/diff.lua b/lua/claudecode/diff.lua index df703c1b3..185507cbc 100644 --- a/lua/claudecode/diff.lua +++ b/lua/claudecode/diff.lua @@ -710,7 +710,7 @@ local function load_original_buffer(original_win, old_file_path, is_new_file, ex end vim.api.nvim_set_current_win(original_win) - vim.cmd("edit " .. vim.fn.fnameescape(old_file_path)) + vim.cmd.edit(vim.fn.fnameescape(old_file_path)) return vim.api.nvim_win_get_buf(original_win) end @@ -858,11 +858,11 @@ local function show_native_diff(old_file_path, new_file_path, tmp_file) end end - vim.cmd("edit " .. vim.fn.fnameescape(old_file_path)) + vim.cmd.edit(vim.fn.fnameescape(old_file_path)) local original_win = vim.api.nvim_get_current_win() create_split() -- Without a swap file, the rename below cannot meet another Neovim's swap file for " (New)" (E325). - vim.cmd("noswapfile edit " .. vim.fn.fnameescape(tmp_file)) + vim.cmd.edit({ vim.fn.fnameescape(tmp_file), mods = { noswapfile = true } }) vim.api.nvim_buf_set_name(0, new_file_path .. " (New)") -- Propagate filetype to the proposed buffer for proper syntax highlighting (#20) diff --git a/lua/claudecode/tools/README.md b/lua/claudecode/tools/README.md index 477ed35a5..a16db8402 100644 --- a/lua/claudecode/tools/README.md +++ b/lua/claudecode/tools/README.md @@ -43,10 +43,11 @@ Sources: [tools/init.lua:M.register_all], [tools/close_tab.lua:schema], [tools/o - A call without `arguments` runs the tool with `{}`. A tool with a required parameter then raises `-32602`, and `getDiagnostics` returns the diagnostics of all buffers. No handler enforces `additionalProperties = false` [tools/init.lua:M.handle_invoke] [tools/get_diagnostics.lua:handler] - Tools with `requires_coroutine` run in `coroutine.create`. If the coroutine yields, `handle_invoke` returns `{ _deferred = true, coroutine, client, params }` and sends no response yet. Only `openDiff` sets `requires_coroutine = true` [tools/init.lua:M.handle_invoke] - `handle_invoke` and the answer to a deferred `openDiff` resume the tool's coroutine through `utils.resume_tool_coroutine`. On a LuaJIT build older than 2023-09-21 (the release builds of Neovim 0.8 and 0.9 are such builds), or one whose `jit.version` the plugin cannot read, it turns the JIT off and flushes the compiled traces before it resumes the coroutine, and it gives the JIT back as it was when the resume returns. This guards against LuaJIT issue 1066, which aborts Neovim. On those builds each such resume discards the compiled traces of the whole editor, which LuaJIT compiles again when the code gets hot. Any other LuaJIT build gets a plain `coroutine.resume` [utils.lua:M.resume_tool_coroutine] [tools/init.lua:M.handle_invoke] [diff.lua:M.open_diff_blocking] -- `openDiff` raises `-32602` when a required parameter is missing or is not a string. Outside a non-main coroutine it raises `-32000` with `message = "Internal server error"` and `data = "openDiff must run in coroutine context"`. It raises `-32000` with `data = "Failed to load diff module"` when `claudecode.diff` does not load [tools/open_diff.lua:handler] +- `openDiff` raises `-32602` when a required parameter is missing or is not a string, and with `data = " must not contain a line break or a NUL"` when `old_file_path` or `new_file_path` holds a line feed, a carriage return or a NUL. Outside a non-main coroutine it raises `-32000` with `message = "Internal server error"` and `data = "openDiff must run in coroutine context"`. It raises `-32000` with `data = "Failed to load diff module"` when `claudecode.diff` does not load [tools/open_diff.lua:handler] - After a deferred `openDiff` yields, `diff.open_diff_blocking` maps the outcome with `build_response` and sends it through `_G.claude_deferred_responses[tostring(co)]`, then clears that entry [diff.lua:M.open_diff_blocking] - `openDiff` calls `diff.open_diff_blocking(old, new, contents, tab_name, client.id)`. Structured errors pass through. Other errors become `-32000` "Error opening blocking diff" [tools/open_diff.lua:handler] - `openFile`, `checkDocumentDirty`, and `saveDocument` raise `-32602` "Missing filePath parameter" when `filePath` is missing or is not a string [tools/open_file.lua:handler] +- `openFile` raises `-32602` with `data = "filePath must not contain a line break or a NUL"` when `filePath` holds a line feed, a carriage return or a NUL, before any other check reads the path: `vim.fn.filereadable` raises E976 on a NUL [tools/open_file.lua:handler] - `openFile` raises `-32602` before it opens the file when `startLine` or `endLine` is given but is not an integer (`false` included), `startLine` is below 1 or past the last line, or `endLine` is below `startLine`. The last line comes from the loaded buffer that `:edit` would reuse, which `bufexists()` and `bufadd()` find by name or by the inode Neovim cached for it, so a symlink or hard link finds it. Without one it comes from the file on disk. A buffer made before its file existed has no cached inode when another program creates the file, so a symlink to that file does not find it. For a loaded buffer without unsaved changes the file on disk counts too, because openFile loads the file again when it changed. When the buffer openFile then shows has fewer lines than `startLine`, openFile selects its last line. An `endLine` past the last line selects through the last line, and the answer names the lines it selected [tools/open_file.lua:handler] - `openFile` raises `-32000` with `message = "File operation error"` and `data = "File not found: "` when the file is not readable, and with `data = "No preview window shows the file"` when the preview window is closed or no longer has 'previewwindow' set after it gets the file, for example because an autocommand cleared it. It raises `-32000` and changes nothing while the command-line window is open, and when the user is in the middle of a command in the preview window, which openFile would reuse. Its defaults are `preview=false`, `makeFrontmost=true`, and `selectToEndOfLine=false` [tools/open_file.lua:handler] - `openFile` expands `~` only when `filePath` is `~` or starts with `~/` (`utils.expand_tilde`). When only `endLine` is given, `startLine` is 1. When only `startLine` is given, `endLine` equals it [tools/open_file.lua:handler] diff --git a/lua/claudecode/tools/open_diff.lua b/lua/claudecode/tools/open_diff.lua index 0905cf02d..4a8c536c0 100644 --- a/lua/claudecode/tools/open_diff.lua +++ b/lua/claudecode/tools/open_diff.lua @@ -46,6 +46,17 @@ local function handler(params, client) }) end end + -- An Ex command built from a path with a line break would run the text after the break, and Neovim's file functions + -- raise on a NUL (E976), so such a path stops here, before any step below reads it. tab_name is not a path. + for _, param_name in ipairs({ "old_file_path", "new_file_path" }) do + if params[param_name]:find("[%z\r\n]") then + error({ + code = -32602, + message = "Invalid params", + data = param_name .. " must not contain a line break or a NUL", + }) + end + end -- Ensure we're running in a coroutine context for blocking operation local co, is_main = coroutine.running() diff --git a/lua/claudecode/tools/open_file.lua b/lua/claudecode/tools/open_file.lua index 8086b15c6..d50d08154 100644 --- a/lua/claudecode/tools/open_file.lua +++ b/lua/claudecode/tools/open_file.lua @@ -399,7 +399,8 @@ local function open_from(win, file_path, preview, keep_mode) else show_buffer(buf, win, keep_mode, function() vim.api.nvim_win_call(win, function() - vim.cmd("edit " .. vim.fn.fnameescape(file_path)) + -- One command: vim.cmd() with a string would run the text after a line break in the name as another one. + vim.cmd.edit(vim.fn.fnameescape(file_path)) end) end) end @@ -506,6 +507,11 @@ local function handler(params) if type(params.filePath) ~= "string" then error({ code = -32602, message = "Invalid params", data = "Missing filePath parameter" }) end + -- An Ex command built from a path with a line break would run the text after the break, and Neovim's file functions + -- raise on a NUL (E976), so such a path stops here, before any step below reads it. + if params.filePath:find("[%z\r\n]") then + error({ code = -32602, message = "Invalid params", data = "filePath must not contain a line break or a NUL" }) + end if vim.fn.getcmdwintype() ~= "" then error({ code = -32000, message = "File operation error", data = "The command-line window is open" }) diff --git a/tests/helpers/ex_command.lua b/tests/helpers/ex_command.lua new file mode 100644 index 000000000..0d827e0ea --- /dev/null +++ b/tests/helpers/ex_command.lua @@ -0,0 +1,32 @@ +--- A stand-in for `vim.cmd`. Neovim's `vim.cmd` runs a string when it is called, and gives a function for each command +--- name, as in `vim.cmd.edit(path)` or `vim.cmd.edit({ path, mods = { noswapfile = true } })`. Both forms reach `run` +--- as one string, the second as " ", so a spec that records the strings sees the same string for a +--- command in either form. +---@param run fun(command: string): any +---@return table cmd +return function(run) + return setmetatable({}, { + __call = function(_, command) + return run(command) + end, + __index = function(_, name) + return function(...) + local args, parts = { ... }, {} + if #args == 1 and type(args[1]) == "table" then + args = args[1] + for mod, on in pairs(args.mods or {}) do + if on == true then + parts[#parts + 1] = mod + end + end + table.sort(parts) + end + parts[#parts + 1] = name + for _, arg in ipairs(args) do + parts[#parts + 1] = arg + end + return run(table.concat(parts, " ")) + end + end, + }) +end diff --git a/tests/integration/file_name_line_break_spec.lua b/tests/integration/file_name_line_break_spec.lua index 08a8215c5..d42156f55 100644 --- a/tests/integration/file_name_line_break_spec.lua +++ b/tests/integration/file_name_line_break_spec.lua @@ -1,8 +1,8 @@ --- Real-Neovim check that openFile and the diff open a file whose name holds a line break as that one file, and run --- nothing in the name. fnameescape() puts a backslash before a line break but keeps the line break, and vim.cmd() with a --- string runs each of its lines as an Ex command, so `vim.cmd("edit " .. vim.fn.fnameescape(path))` ran the text after --- the line break. Busted's mock vim runs no Ex commands, so under busted this is pending; --- scripts/run_integration_tests_individually.sh runs it in Neovim. +-- Real-Neovim check that a file name with a line break runs nothing: openFile refuses it, and the diff opens the file +-- that has the name. fnameescape() puts a backslash before a line break but keeps it, and vim.cmd() with a string runs +-- each of its lines as an Ex command, so `vim.cmd("edit " .. vim.fn.fnameescape(path))` ran the text after the line +-- break. The diff specs call the diff module itself, past openDiff's check of the path. Busted's mock vim runs no Ex +-- commands, so under busted this is pending; scripts/run_integration_tests_individually.sh runs it in Neovim. if vim == nil or vim.fn.has("nvim") ~= 1 then describe("file names with a line break", function() pending("needs a real Neovim") diff --git a/tests/mocks/vim.lua b/tests/mocks/vim.lua index 05704bfae..c1c3a8f31 100644 --- a/tests/mocks/vim.lua +++ b/tests/mocks/vim.lua @@ -770,7 +770,8 @@ local vim = { end, }, - cmd = function(command) + -- Callable with a command string, and indexable by command name (`vim.cmd.edit(path)`), as in Neovim. + cmd = require("tests.helpers.ex_command")(function(command) -- Store the last command for test assertions. vim._last_command = command -- Implement minimal behavior for essential commands @@ -902,7 +903,7 @@ local vim = { else -- other commands (wincmd etc.) are recorded but not simulated end - end, + end), json = { encode = function(data) diff --git a/tests/unit/diff_buffer_cleanup_spec.lua b/tests/unit/diff_buffer_cleanup_spec.lua index 5146d4f74..f9be760c5 100644 --- a/tests/unit/diff_buffer_cleanup_spec.lua +++ b/tests/unit/diff_buffer_cleanup_spec.lua @@ -1,5 +1,6 @@ -- luacheck: globals expect require("tests.busted_setup") +local ex_command = require("tests.helpers.ex_command") describe("Diff Buffer Cleanup Edge Cases", function() local diff_module @@ -125,7 +126,7 @@ describe("Diff Buffer Cleanup Edge Cases", function() end -- Mock command execution - mock_vim.cmd = function(command) end + mock_vim.cmd = ex_command(function(command) end) _G.vim = mock_vim end diff --git a/tests/unit/diff_spec.lua b/tests/unit/diff_spec.lua index f203e3650..dfd009e97 100644 --- a/tests/unit/diff_spec.lua +++ b/tests/unit/diff_spec.lua @@ -1,5 +1,6 @@ -- luacheck: globals expect require("tests.busted_setup") +local ex_command = require("tests.helpers.ex_command") local temp_dir = require("tests.helpers.temp_dir") describe("Diff Module", function() @@ -324,9 +325,9 @@ describe("Diff Module", function() if _G.vim and rawget(original_vim_functions, "cmd") == nil then original_vim_functions["cmd"] = _G.vim.cmd end - _G.vim.cmd = function(cmd) + _G.vim.cmd = ex_command(function(cmd) table.insert(commands, cmd) - end + end) local mock_file = { write = function() end, @@ -379,9 +380,9 @@ describe("Diff Module", function() if _G.vim and rawget(original_vim_functions, "cmd") == nil then original_vim_functions["cmd"] = _G.vim.cmd end - _G.vim.cmd = function(cmd) + _G.vim.cmd = ex_command(function(cmd) table.insert(commands, cmd) - end + end) local mock_file = { write = function() end, @@ -550,7 +551,7 @@ describe("Diff Module", function() _G.vim.api.nvim_buf_set_name = function() end _G.vim.api.nvim_buf_set_lines = function() end _G.vim.api.nvim_set_option_value = function() end - _G.vim.cmd = function() end + _G.vim.cmd = ex_command(function() end) local old_io_open = io.open rawset(io, "open", function() return { @@ -661,7 +662,7 @@ describe("Diff Module", function() _G.vim.api.nvim_buf_set_name = function() end _G.vim.api.nvim_buf_set_lines = function() end _G.vim.api.nvim_set_option_value = function() end - _G.vim.cmd = function() end + _G.vim.cmd = ex_command(function() end) local old_io_open = io.open rawset(io, "open", function() return { @@ -714,7 +715,7 @@ describe("Diff Module", function() _G.vim.api.nvim_buf_set_name = function() end _G.vim.api.nvim_buf_set_lines = function() end _G.vim.api.nvim_set_option_value = function() end - _G.vim.cmd = function() end + _G.vim.cmd = ex_command(function() end) local old_io_open = io.open rawset(io, "open", function() return { diff --git a/tests/unit/tools/open_file_spec.lua b/tests/unit/tools/open_file_spec.lua index e3cc315f3..f1ed45f63 100644 --- a/tests/unit/tools/open_file_spec.lua +++ b/tests/unit/tools/open_file_spec.lua @@ -1,4 +1,5 @@ require("tests.busted_setup") -- Ensure test helpers are loaded +local ex_command = require("tests.helpers.ex_command") describe("Tool: open_file", function() local open_file_handler @@ -50,7 +51,7 @@ describe("Tool: open_file", function() call_win = nil local kind_of = { v = "v", s = "v", V = "V", S = "V", ["\22"] = "\22", ["\19"] = "\22" } local select_of = { v = "s", V = "S", ["\22"] = "\19" } - _G.vim.cmd = spy.new(function(command) + _G.vim.cmd = ex_command(function(command) table.insert(_G.vim.cmd_history, command) local keys = command:match("^normal! (.*)") local in_visual = kind_of[editor_mode] ~= nil @@ -533,7 +534,7 @@ describe("Tool: open_file", function() before_each(function() local run_cmd = _G.vim.cmd - _G.vim.cmd = spy.new(function(command) + _G.vim.cmd = ex_command(function(command) local at for i, id in ipairs(order) do if id == current then @@ -910,7 +911,7 @@ describe("Tool: open_file", function() end local run_cmd = _G.vim.cmd silenced = false - _G.vim.cmd = spy.new(function(command) + _G.vim.cmd = ex_command(function(command) local recorded = command silenced = command:match("^silent! ") ~= nil command = command:gsub("^silent! ", "") @@ -1329,7 +1330,7 @@ describe("Tool: open_file", function() -- Neovim accepts a FileChangedShell autocommand for a buffer that is unloaded, so only a wiped buffer is a case. it("shows the file with no FileChangedShell autocommand when an autocommand wipes the buffer", function() local run_cmd, create = _G.vim.cmd, _G.vim.api.nvim_create_autocmd - _G.vim.cmd = function(command) + _G.vim.cmd = ex_command(function(command) if command == "edit test.txt" and not buffers[30] then buffers[30] = { lines = { "fresh" }, filetype = "lua" } -- :edit makes a new buffer for the file end @@ -1337,7 +1338,7 @@ describe("Tool: open_file", function() if command == "checktime 30" then buffers[30] = nil -- autocmd FileChangedShellPost test.txt bwipeout end - end + end) local created_for_missing = {} _G.vim.api.nvim_create_autocmd = function(event, opts) if event == "FileChangedShell" and not buffers[opts.buffer] then @@ -1496,12 +1497,12 @@ describe("Tool: open_file", function() buffers[40] = { lines = { "plugin text" }, buftype = "nofile", loaded = true, filetype = "" } -- autocmd BufWinEnter test.txt ++nested buffer 40 local run_cmd, set_buf = _G.vim.cmd, _G.vim.api.nvim_win_set_buf - _G.vim.cmd = function(command) + _G.vim.cmd = ex_command(function(command) run_cmd(command) if command == "edit test.txt" then win_buf[case.win] = 40 end - end + end) _G.vim.api.nvim_win_set_buf = function(win, buf) set_buf(win, buf) win_buf[win] = 40