[mini.test] vim.fn.confirm seems to always use the default choice #2138
-
Contributing guidelines
Module(s)mini.test Question👋 I'm writing tests for a small file tree plugin, and I'm running into an issue where -- lua/confirm/init.lua
local M = {}
M.run = function()
-- roughly my use-case
local dirname = vim.fs.joinpath(vim.fs.dirname(vim.api.nvim_buf_get_name(0)), '/')
local create_path = vim.fn.input("Create a file: ", dirname)
local option = vim.fn.confirm(("Create %s?"):format(create_path), "&Yes\n&No", 2)
if option == 1 then
vim.fn.writefile({}, create_path)
return
end
vim.notify("Aborting create", vim.log.levels.INFO)
end
return M-- tests/test_run.lua
require "mini.test".setup()
local expect = MiniTest.expect
local child = MiniTest.new_child_neovim()
local T = MiniTest.new_set {
hooks = {
pre_case = function()
child.restart { "-u", "scripts/minimal_init.lua", }
child.lua [[M = require('confirm')]]
child.o.lines = 30
child.o.columns = 30
end,
post_once = child.stop,
},
}
T["confirm"] = function()
child.lua_notify [[M.run()]]
expect.reference_screenshot(child.get_screenshot())
child.type_keys("tester.txt")
expect.reference_screenshot(child.get_screenshot())
child.type_keys("<CR>")
expect.reference_screenshot(child.get_screenshot())
vim.print(child.cmd_capture("messages"))
end
return TWith this output: nvim --headless --noplugin -u ./scripts/minimal_init.lua -c "lua MiniTest.run()"
Total number of cases: 1
Total number of groups: 1
tests/test_run.lua: Aborting createo
Fails (0) and Notes (0)As you can see, I haven't been able to find anything relevant online, has anyone encountered issues with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Yes, unfortunately, The same approach is used in Neovim itself for So here it would be something like this: T["confirm"] = function()
child.lua_notify [[M.run()]]
child.type_keys("tester.txt")
mock_confirm(1)
child.type_keys("<CR>")
eq(child.cmd_capture("messages"), '')
end |
Beta Was this translation helpful? Give feedback.
Yes, unfortunately,
vim.fn.confirm()does tend to behave like this. I am not sure why, but mocking the method works pretty good here. This is what I use for 'mini.files' testing. It can then be used before invoking action that would require confirm (like this). Then validate arguments with which it was called (something like this).The same approach is used in Neovim itself for
vim.packtesting.So here it would be something like this: