api: Make sure dict_set_var doesn’t edit read-only values

Fixes #6147
This commit is contained in:
ZyX 2017-02-23 01:34:25 +03:00
parent 8faa4af396
commit 858ac9d8e5
7 changed files with 61 additions and 8 deletions

View File

@ -131,6 +131,19 @@ Object dict_set_var(dict_T *dict, String key, Object value, bool del,
dictitem_T *di = dict_find(dict, (char_u *)key.data, (int)key.size);
if (di != NULL) {
if (di->di_flags & DI_FLAGS_RO) {
api_set_error(err, Exception, _("Key is read-only: %s"), key.data);
return rv;
} else if (di->di_flags & DI_FLAGS_FIX) {
api_set_error(err, Exception, _("Key is fixed: %s"), key.data);
return rv;
} else if (di->di_flags & DI_FLAGS_LOCK) {
api_set_error(err, Exception, _("Key is locked: %s"), key.data);
return rv;
}
}
if (del) {
// Delete the key
if (di == NULL) {

View File

@ -6,6 +6,8 @@ local funcs = helpers.funcs
local request = helpers.request
local neq = helpers.neq
local NIL = helpers.NIL
local meth_pcall = helpers.meth_pcall
local command = helpers.command
describe('api/buf', function()
before_each(clear)
@ -251,6 +253,15 @@ describe('api/buf', function()
eq(1, funcs.exists('b:lua'))
curbufmeths.del_var('lua')
eq(0, funcs.exists('b:lua'))
eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(curbufmeths.del_var, 'lua'))
curbufmeths.set_var('lua', 1)
command('lockvar b:lua')
eq({false, 'Key is locked: lua'}, meth_pcall(curbufmeths.del_var, 'lua'))
eq({false, 'Key is locked: lua'}, meth_pcall(curbufmeths.set_var, 'lua', 1))
eq({false, 'Key is read-only: changedtick'},
meth_pcall(curbufmeths.del_var, 'changedtick'))
eq({false, 'Key is read-only: changedtick'},
meth_pcall(curbufmeths.set_var, 'changedtick', 1))
end)
end)

View File

@ -6,6 +6,8 @@ local curtabmeths = helpers.curtabmeths
local funcs = helpers.funcs
local request = helpers.request
local NIL = helpers.NIL
local meth_pcall = helpers.meth_pcall
local command = helpers.command
describe('api/tabpage', function()
before_each(clear)
@ -32,6 +34,11 @@ describe('api/tabpage', function()
eq(1, funcs.exists('t:lua'))
curtabmeths.del_var('lua')
eq(0, funcs.exists('t:lua'))
eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(curtabmeths.del_var, 'lua'))
curtabmeths.set_var('lua', 1)
command('lockvar t:lua')
eq({false, 'Key is locked: lua'}, meth_pcall(curtabmeths.del_var, 'lua'))
eq({false, 'Key is locked: lua'}, meth_pcall(curtabmeths.set_var, 'lua', 1))
end)
it('tabpage_set_var returns the old value', function()

View File

@ -7,6 +7,8 @@ local os_name = helpers.os_name
local meths = helpers.meths
local funcs = helpers.funcs
local request = helpers.request
local meth_pcall = helpers.meth_pcall
local command = helpers.command
describe('api', function()
before_each(clear)
@ -117,6 +119,11 @@ describe('api', function()
eq(1, funcs.exists('g:lua'))
meths.del_var('lua')
eq(0, funcs.exists('g:lua'))
eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(meths.del_var, 'lua'))
meths.set_var('lua', 1)
command('lockvar lua')
eq({false, 'Key is locked: lua'}, meth_pcall(meths.del_var, 'lua'))
eq({false, 'Key is locked: lua'}, meth_pcall(meths.set_var, 'lua', 1))
end)
it('vim_set_var returns the old value', function()

View File

@ -8,6 +8,8 @@ local curwinmeths = helpers.curwinmeths
local funcs = helpers.funcs
local request = helpers.request
local NIL = helpers.NIL
local meth_pcall = helpers.meth_pcall
local command = helpers.command
-- check if str is visible at the beginning of some line
local function is_visible(str)
@ -137,6 +139,11 @@ describe('api/win', function()
eq(1, funcs.exists('w:lua'))
curwinmeths.del_var('lua')
eq(0, funcs.exists('w:lua'))
eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(curwinmeths.del_var, 'lua'))
curwinmeths.set_var('lua', 1)
command('lockvar w:lua')
eq({false, 'Key is locked: lua'}, meth_pcall(curwinmeths.del_var, 'lua'))
eq({false, 'Key is locked: lua'}, meth_pcall(curwinmeths.set_var, 'lua', 1))
end)
it('window_set_var returns the old value', function()

View File

@ -1,16 +1,17 @@
local helpers = require('test.functional.helpers')(after_each)
local curbufmeths = helpers.curbufmeths
local clear = helpers.clear
local eq = helpers.eq
local neq = helpers.neq
local eval = helpers.eval
local feed = helpers.feed
local clear = helpers.clear
local funcs = helpers.funcs
local meths = helpers.meths
local command = helpers.command
local exc_exec = helpers.exc_exec
local redir_exec = helpers.redir_exec
local meth_pcall = helpers.meth_pcall
local curbufmeths = helpers.curbufmeths
before_each(clear)
@ -66,9 +67,8 @@ describe('b:changedtick', function()
redir_exec('let b:.changedtick = ' .. ctn))
eq('\nE46: Cannot change read-only variable "d.changedtick"',
redir_exec('let d.changedtick = ' .. ctn))
-- FIXME
-- eq({fales, ''},
-- {pcall(curbufmeths.set_var, 'changedtick', ctn)})
eq({false, 'Key is read-only: changedtick'},
meth_pcall(curbufmeths.set_var, 'changedtick', ctn))
eq('\nE795: Cannot delete variable b:changedtick',
redir_exec('unlet b:changedtick'))
@ -78,9 +78,8 @@ describe('b:changedtick', function()
redir_exec('unlet b:["changedtick"]'))
eq('\nE46: Cannot change read-only variable "d.changedtick"',
redir_exec('unlet d.changedtick'))
-- FIXME
-- eq({},
-- {pcall(curbufmeths.del_var, 'changedtick')})
eq({false, 'Key is read-only: changedtick'},
meth_pcall(curbufmeths.del_var, 'changedtick'))
eq(ct, changedtick())
eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',

View File

@ -534,6 +534,14 @@ local function skip_fragile(pending_fn, cond)
return false
end
local function meth_pcall(...)
local ret = {pcall(...)}
if type(ret[2]) == 'string' then
ret[2] = ret[2]:gsub('^[^:]+:%d+: ', '')
end
return ret
end
local funcs = create_callindex(nvim_call)
local meths = create_callindex(nvim)
local uimeths = create_callindex(ui)
@ -604,6 +612,7 @@ local M = {
skip_fragile = skip_fragile,
set_shell_powershell = set_shell_powershell,
tmpname = tmpname,
meth_pcall = meth_pcall,
NIL = mpack.NIL,
}