refactor: rework parameter validation in vim.secure.trust() (#21223)

This commit is contained in:
Gregory Anders 2022-11-28 15:40:50 -07:00 committed by GitHub
parent f004812b33
commit 80b6edabe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 6 deletions

View File

@ -148,19 +148,23 @@ function M.trust(opts)
local bufnr = opts.bufnr local bufnr = opts.bufnr
local action = opts.action local action = opts.action
if path and bufnr then assert(not path or not bufnr, '"path" and "bufnr" are mutually exclusive')
error('path and bufnr are mutually exclusive', 2)
if action == 'allow' then
assert(not path, '"path" is not valid when action is "allow"')
end end
local fullpath local fullpath
if path then if path then
fullpath = vim.loop.fs_realpath(vim.fs.normalize(path)) fullpath = vim.loop.fs_realpath(vim.fs.normalize(path))
else elseif bufnr then
local bufname = vim.api.nvim_buf_get_name(bufnr) local bufname = vim.api.nvim_buf_get_name(bufnr)
if bufname == '' then if bufname == '' then
return false, 'buffer is not associated with a file' return false, 'buffer is not associated with a file'
end end
fullpath = vim.loop.fs_realpath(vim.fs.normalize(bufname)) fullpath = vim.loop.fs_realpath(vim.fs.normalize(bufname))
else
error('one of "path" or "bufnr" is required')
end end
if not fullpath then if not fullpath then
@ -170,8 +174,6 @@ function M.trust(opts)
local trust = read_trust() local trust = read_trust()
if action == 'allow' then if action == 'allow' then
assert(bufnr, 'bufnr is required when action is "allow"')
local newline = vim.bo[bufnr].fileformat == 'unix' and '\n' or '\r\n' local newline = vim.bo[bufnr].fileformat == 'unix' and '\n' or '\r\n'
local contents = table.concat(vim.api.nvim_buf_get_lines(bufnr, 0, -1, false), newline) local contents = table.concat(vim.api.nvim_buf_get_lines(bufnr, 0, -1, false), newline)
if vim.bo[bufnr].endofline then if vim.bo[bufnr].endofline then

View File

@ -12,6 +12,7 @@ local feed_command = helpers.feed_command
local feed = helpers.feed local feed = helpers.feed
local funcs = helpers.funcs local funcs = helpers.funcs
local pcall_err = helpers.pcall_err local pcall_err = helpers.pcall_err
local matches = helpers.matches
describe('vim.secure', function() describe('vim.secure', function()
describe('read()', function() describe('read()', function()
@ -189,10 +190,15 @@ describe('vim.secure', function()
end) end)
it('returns error when passing both path and bufnr', function() it('returns error when passing both path and bufnr', function()
eq('path and bufnr are mutually exclusive', matches('"path" and "bufnr" are mutually exclusive',
pcall_err(exec_lua, [[vim.secure.trust({action='deny', bufnr=0, path='test_file'})]])) pcall_err(exec_lua, [[vim.secure.trust({action='deny', bufnr=0, path='test_file'})]]))
end) end)
it('returns error when passing neither path or bufnr', function()
matches('one of "path" or "bufnr" is required',
pcall_err(exec_lua, [[vim.secure.trust({action='deny'})]]))
end)
it('trust then deny then remove a file using bufnr', function() it('trust then deny then remove a file using bufnr', function()
local cwd = funcs.getcwd() local cwd = funcs.getcwd()
local hash = funcs.sha256(helpers.read_file('test_file')) local hash = funcs.sha256(helpers.read_file('test_file'))