diff options
-rw-r--r-- | src/nvim/api/vim.c | 13 | ||||
-rw-r--r-- | src/nvim/eval.c | 47 | ||||
-rw-r--r-- | src/nvim/eval.h | 1 | ||||
-rw-r--r-- | src/nvim/ex_cmds.c | 16 | ||||
-rw-r--r-- | src/nvim/msgpack_rpc/channel.c | 4 | ||||
-rw-r--r-- | src/nvim/os/input.c | 16 | ||||
-rw-r--r-- | src/nvim/os/job.c | 3 | ||||
-rw-r--r-- | src/nvim/os/rstream.c | 1 | ||||
-rw-r--r-- | test/functional/ex_cmds/sign_spec.lua | 25 | ||||
-rw-r--r-- | test/functional/helpers.lua | 108 | ||||
-rw-r--r-- | test/functional/job/job_spec.lua | 59 | ||||
-rw-r--r-- | test/functional/shell/viml_system_spec.lua | 4 |
12 files changed, 208 insertions, 89 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index c90e7039ce..9afefd6fa3 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -96,6 +96,19 @@ String vim_replace_termcodes(String str, Boolean from_part, Boolean do_lt, return cstr_as_string(ptr); } +String vim_command_output(String str, Error *err) +{ + do_cmdline_cmd((char_u *)"redir => v:command_output"); + vim_command(str, err); + do_cmdline_cmd((char_u *)"redir END"); + + if (err->set) { + return (String) STRING_INIT; + } + + return cstr_to_string((char *)get_vim_var_str(VV_COMMAND_OUTPUT)); +} + /// Evaluates the expression str using the vim internal expression /// evaluator (see |expression|). /// Dictionaries and lists are recursively expanded. diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 3fc4104258..59fb82134d 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -424,7 +424,8 @@ static struct vimvar { {VV_NAME("oldfiles", VAR_LIST), 0}, {VV_NAME("windowid", VAR_NUMBER), VV_RO}, {VV_NAME("progpath", VAR_STRING), VV_RO}, - {VV_NAME("job_data", VAR_LIST), 0} + {VV_NAME("job_data", VAR_LIST), 0}, + {VV_NAME("command_output", VAR_STRING), 0} }; /* shorthand */ @@ -448,9 +449,8 @@ static dictitem_T vimvars_var; /* variable used for v: */ // Memory pool for reusing JobEvent structures typedef struct { - Job *job; - RStream *rstream; - char *type; + int id; + char *name, *type, *received; } JobEvent; #define JobEventFreer(x) KMEMPOOL_INIT(JobEventPool, JobEvent, JobEventFreer) @@ -19527,8 +19527,15 @@ char_u *do_string_sub(char_u *str, char_u *pat, char_u *sub, char_u *flags) #define push_job_event(j, r, t) \ do { \ JobEvent *event_data = kmp_alloc(JobEventPool, job_event_pool); \ - event_data->job = j; \ - event_data->rstream = r; \ + event_data->received = NULL; \ + if (r) { \ + size_t read_count = rstream_pending(r); \ + event_data->received = xmalloc(read_count + 1); \ + rstream_read(r, event_data->received, read_count); \ + event_data->received[read_count] = NUL; \ + } \ + event_data->id = job_id(j); \ + event_data->name = job_data(j); \ event_data->type = t; \ event_push((Event) { \ .handler = on_job_event, \ @@ -19552,39 +19559,28 @@ static void on_job_stderr(RStream *rstream, void *data, bool eof) static void on_job_exit(Job *job, void *data) { - push_job_event(data, NULL, "exit"); + push_job_event(job, NULL, "exit"); } static void on_job_event(Event event) { JobEvent *data = event.data; - Job *job = data->job; - char *str = NULL; - - if (data->rstream) { - // Read event - size_t read_count = rstream_pending(data->rstream); - str = xmalloc(read_count + 1); - - rstream_read(data->rstream, str, read_count); - str[read_count] = NUL; - } - apply_job_autocmds(job, job_data(job), data->type, str); + apply_job_autocmds(data->id, data->name, data->type, data->received); kmp_free(JobEventPool, job_event_pool, data); } -static void apply_job_autocmds(Job *job, char *name, char *type, char *str) +static void apply_job_autocmds(int id, char *name, char *type, char *received) { // Create the list which will be set to v:job_data list_T *list = list_alloc(); - list_append_number(list, job_id(job)); + list_append_number(list, id); list_append_string(list, (uint8_t *)type, -1); - if (str) { + if (received) { listitem_T *str_slot = listitem_alloc(); str_slot->li_tv.v_type = VAR_STRING; str_slot->li_tv.v_lock = 0; - str_slot->li_tv.vval.v_string = (uint8_t *)str; + str_slot->li_tv.vval.v_string = (uint8_t *)received; list_append(list, str_slot); } @@ -19592,6 +19588,11 @@ static void apply_job_autocmds(Job *job, char *name, char *type, char *str) set_vim_var_list(VV_JOB_DATA, list); // Call JobActivity autocommands apply_autocmds(EVENT_JOBACTIVITY, (uint8_t *)name, NULL, TRUE, NULL); + + if (!received) { + // This must be the exit event. Free the name. + free(name); + } } static void script_host_eval(char *method, typval_T *argvars, typval_T *rettv) diff --git a/src/nvim/eval.h b/src/nvim/eval.h index 2f36a46f70..e96106dfb3 100644 --- a/src/nvim/eval.h +++ b/src/nvim/eval.h @@ -64,6 +64,7 @@ enum { VV_WINDOWID, VV_PROGPATH, VV_JOB_DATA, + VV_COMMAND_OUTPUT, VV_LEN, /* number of v: vars */ }; diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 5db950f120..f5fa16a139 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -5908,12 +5908,13 @@ void ex_sign(exarg_T *eap) arg = skipwhite(arg); if (idx == SIGNCMD_UNPLACE && *arg == NUL) { - /* ":sign unplace {id}": remove placed sign by number */ - FOR_ALL_BUFFERS(buf) { - if ((lnum = buf_delsign(buf, id)) != 0) - update_debug_sign(buf, lnum); - return; - } + // ":sign unplace {id}": remove placed sign by number + FOR_ALL_BUFFERS(buf) { + if ((lnum = buf_delsign(buf, id)) != 0) { + update_debug_sign(buf, lnum); + } + } + return; } } } @@ -5923,7 +5924,7 @@ void ex_sign(exarg_T *eap) * Leave "arg" pointing to {fname}. */ - buf_T *buf = NULL; + buf_T *buf = NULL; for (;;) { if (STRNCMP(arg, "line=", 5) == 0) @@ -6343,3 +6344,4 @@ void set_context_in_sign_cmd(expand_T *xp, char_u *arg) } } +// vim: tabstop=8 diff --git a/src/nvim/msgpack_rpc/channel.c b/src/nvim/msgpack_rpc/channel.c index 3325b294dd..43bed54b2c 100644 --- a/src/nvim/msgpack_rpc/channel.c +++ b/src/nvim/msgpack_rpc/channel.c @@ -338,6 +338,8 @@ static void parse_msgpack(RStream *rstream, void *data, bool eof) if (eof) { close_channel(channel); + call_set_error(channel, "Channel was closed by the client"); + return; } size_t count = rstream_pending(rstream); @@ -730,7 +732,7 @@ static WBuffer *serialize_response(uint64_t channel_id, } #if MIN_LOG_LEVEL <= DEBUG_LOG_LEVEL -#define REQ "[response] " +#define REQ "[request] " #define RES "[response] " #define NOT "[notification] " diff --git a/src/nvim/os/input.c b/src/nvim/os/input.c index 2c8026d099..e4501aeb82 100644 --- a/src/nvim/os/input.c +++ b/src/nvim/os/input.c @@ -1,3 +1,4 @@ +#include <assert.h> #include <string.h> #include <stdint.h> #include <stdbool.h> @@ -237,18 +238,23 @@ static void convert_input(void) if (convert) { // Perform input conversion according to `input_conv` - size_t unconverted_length; + size_t unconverted_length = 0; data = (char *)string_convert_ext(&input_conv, (uint8_t *)data, (int *)&converted_length, (int *)&unconverted_length); - data_length = rbuffer_pending(read_buffer) - unconverted_length; + data_length -= unconverted_length; } - // Write processed data to input buffer - size_t consumed = rbuffer_write(input_buffer, data, data_length); + // The conversion code will be gone eventually, for now assume `input_buffer` + // always has space for the converted data(it's many times the size of + // `read_buffer`, so it's hard to imagine a scenario where the converted data + // doesn't fit) + assert(converted_length <= rbuffer_available(input_buffer)); + // Write processed data to input buffer. + (void)rbuffer_write(input_buffer, data, converted_length); // Adjust raw buffer pointers - rbuffer_consumed(read_buffer, consumed); + rbuffer_consumed(read_buffer, data_length); if (convert) { // data points to memory allocated by `string_convert_ext`, free it. diff --git a/src/nvim/os/job.c b/src/nvim/os/job.c index caada5616b..9a11ecd1fd 100644 --- a/src/nvim/os/job.c +++ b/src/nvim/os/job.c @@ -216,9 +216,6 @@ Job *job_start(char **argv, // Spawn the job if (uv_spawn(uv_default_loop(), &job->proc, &job->proc_opts) != 0) { - close_job_in(job); - close_job_out(job); - close_job_err(job); *status = -1; return NULL; } diff --git a/src/nvim/os/rstream.c b/src/nvim/os/rstream.c index d96b3d931c..beff404fd0 100644 --- a/src/nvim/os/rstream.c +++ b/src/nvim/os/rstream.c @@ -396,6 +396,7 @@ static void close_cb(uv_handle_t *handle) static void rbuffer_relocate(RBuffer *rbuffer) { + assert(rbuffer->rpos <= rbuffer->wpos); // Move data ... memmove( rbuffer->data, // ...to the beginning of the buffer(rpos 0) diff --git a/test/functional/ex_cmds/sign_spec.lua b/test/functional/ex_cmds/sign_spec.lua new file mode 100644 index 0000000000..74e1aa4702 --- /dev/null +++ b/test/functional/ex_cmds/sign_spec.lua @@ -0,0 +1,25 @@ +local helpers = require('test.functional.helpers') +local clear, nvim, buffer, curbuf, curwin, eq, ok = + helpers.clear, helpers.nvim, helpers.buffer, helpers.curbuf, helpers.curwin, + helpers.eq, helpers.ok + +describe('sign', function() + describe('unplace {id}', function() + describe('without specifying buffer', function() + it('deletes the sign from all buffers', function() + -- place a sign with id 34 to first buffer + nvim('command', 'sign define Foo text=+ texthl=Delimiter linehl=Comment') + local buf1 = nvim('eval', 'bufnr("%")') + nvim('command', 'sign place 34 line=3 name=Foo buffer='..buf1) + -- create a second buffer and place the sign on it as well + nvim('command', 'new') + local buf2 = nvim('eval', 'bufnr("%")') + nvim('command', 'sign place 34 line=3 name=Foo buffer='..buf2) + -- now unplace without specifying a buffer + nvim('command', 'sign unplace 34') + eq("\n--- Signs ---\n", nvim('command_output', 'sign place buffer='..buf1)) + eq("\n--- Signs ---\n", nvim('command_output', 'sign place buffer='..buf2)) + end) + end) + end) +end) diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index 324af6a232..d9107543ea 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -25,19 +25,63 @@ end local session +local rawfeed local function restart() local loop = Loop.new() local msgpack_stream = MsgpackStream.new(loop) local async_session = AsyncSession.new(msgpack_stream) session = Session.new(async_session) loop:spawn(nvim_argv) + rawfeed([[:function BeforeEachTest() + set all& + redir => groups + silent augroup + redir END + for group in split(groups) + exe 'augroup '.group + autocmd! + augroup END + endfor + autocmd! + tabnew + let curbufnum = eval(bufnr('%')) + redir => buflist + silent ls! + redir END + let bufnums = [] + for buf in split(buflist, '\n') + let bufnum = eval(split(buf, '[ u]')[0]) + if bufnum != curbufnum + call add(bufnums, bufnum) + endif + endfor + if len(bufnums) > 0 + exe 'silent bwipeout! '.join(bufnums, ' ') + endif + silent tabonly + for k in keys(g:) + exe 'unlet g:'.k + endfor + filetype plugin indent off + mapclear + mapclear! + abclear + comclear + endfunction + ]]) end -restart() + +local loop_running, last_error local function request(method, ...) local status, rv = session:request(method, ...) if not status then - error(rv[2]) + if loop_running then + last_error = rv[2] + session:stop() + else + error(rv[2]) + end end return rv end @@ -47,7 +91,14 @@ local function next_message() end local function run(request_cb, notification_cb, setup_cb) + loop_running = true session:run(request_cb, notification_cb, setup_cb) + loop_running = false + if last_error then + local err = last_error + last_error = nil + error(err) + end end local function stop() @@ -115,7 +166,7 @@ local function feed(...) end end -local function rawfeed(...) +function rawfeed(...) for _, v in ipairs({...}) do nvim_feed(dedent(v), 'nt') end @@ -138,14 +189,6 @@ local function execute(...) end end -local function eval(expr) - local status, result = pcall(function() return nvim_eval(expr) end) - if not status then - error('Failed to evaluate expression "' .. expr .. '"') - end - return result -end - local function eq(expected, actual) return assert.are.same(expected, actual) end @@ -158,44 +201,6 @@ local function expect(contents, first, last, buffer_index) return eq(dedent(contents), buffer_slice(first, last, buffer_index)) end -rawfeed([[:function BeforeEachTest() - set all& - redir => groups - silent augroup - redir END - for group in split(groups) - exe 'augroup '.group - autocmd! - augroup END - endfor - autocmd! - tabnew - let curbufnum = eval(bufnr('%')) - redir => buflist - silent ls! - redir END - let bufnums = [] - for buf in split(buflist, '\n') - let bufnum = eval(split(buf, '[ u]')[0]) - if bufnum != curbufnum - call add(bufnums, bufnum) - endif - endfor - if len(bufnums) > 0 - exe 'silent bwipeout! '.join(bufnums, ' ') - endif - silent tabonly - for k in keys(g:) - exe 'unlet g:'.k - endfor - filetype plugin indent off - mapclear - mapclear! - abclear - comclear -endfunction -]]) - local function ok(expr) assert.is_true(expr) @@ -245,6 +250,8 @@ local function curtab(method, ...) return tabpage(method, tab, ...) end +restart() + return { clear = clear, restart = restart, @@ -252,7 +259,8 @@ return { insert = insert, feed = feed, execute = execute, - eval = eval, + eval = nvim_eval, + command = nvim_command, request = request, next_message = next_message, run = run, diff --git a/test/functional/job/job_spec.lua b/test/functional/job/job_spec.lua new file mode 100644 index 0000000000..b2a65f8269 --- /dev/null +++ b/test/functional/job/job_spec.lua @@ -0,0 +1,59 @@ + +local helpers = require('test.functional.helpers') +local clear, nvim, eq, neq, ok, expect, eval, next_message, run, stop, session + = helpers.clear, helpers.nvim, helpers.eq, helpers.neq, helpers.ok, + helpers.expect, helpers.eval, helpers.next_message, helpers.run, + helpers.stop, helpers.session + +local channel = nvim('get_api_info')[1] + +describe('jobs', function() + before_each(clear) + + -- Creates the string to make an autocmd to notify us. + local notify_str = function(expr) + return "au! JobActivity xxx call rpcnotify("..channel..", "..expr..")" + end + + it('returns 0 when it fails to start', function() + local status, rv = pcall(eval, "jobstart('', '')") + eq(false, status) + ok(rv ~= nil) + end) + + it('calls JobActivity when the job writes and exits', function() + nvim('command', notify_str('v:job_data[1]')) + nvim('command', "call jobstart('xxx', 'echo')") + eq({'notification', 'stdout', {}}, next_message()) + eq({'notification', 'exit', {}}, next_message()) + end) + + it('allows interactive commands', function() + nvim('command', notify_str('v:job_data[2]')) + nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") + neq(0, eval('j')) + nvim('command', "call jobsend(j, 'abc')") + eq({'notification', 'abc', {}}, next_message()) + nvim('command', "call jobsend(j, '123')") + eq({'notification', '123', {}}, next_message()) + nvim('command', notify_str('v:job_data[1])')) + nvim('command', "call jobstop(j)") + eq({'notification', 'exit', {}}, next_message()) + end) + + it('will not allow jobsend/stop on a non-existent job', function() + eq(false, pcall(eval, "jobsend(-1, 'lol')")) + eq(false, pcall(eval, "jobstop(-1, 'lol')")) + end) + + it('will not allow jobstop twice on the same job', function() + nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") + neq(0, eval('j')) + eq(true, pcall(eval, "jobstop(j)")) + eq(false, pcall(eval, "jobstop(j)")) + end) + + it('will not cause a memory leak if we leave a job running', function() + nvim('command', "call jobstart('xxx', 'cat', ['-'])") + end) +end) diff --git a/test/functional/shell/viml_system_spec.lua b/test/functional/shell/viml_system_spec.lua index 9ed1016f73..a8bab8e26e 100644 --- a/test/functional/shell/viml_system_spec.lua +++ b/test/functional/shell/viml_system_spec.lua @@ -39,6 +39,8 @@ describe('system()', function() eq(1, eval('v:shell_error')) eval([[system("sh -c 'exit 5'")]]) eq(5, eval('v:shell_error')) + eval([[system('this-should-not-exist')]]) + eq(127, eval('v:shell_error')) end) describe('passing no input', function() @@ -117,6 +119,8 @@ describe('systemlist()', function() eq(1, eval('v:shell_error')) eval([[systemlist("sh -c 'exit 5'")]]) eq(5, eval('v:shell_error')) + eval([[systemlist('this-should-not-exist')]]) + eq(127, eval('v:shell_error')) end) describe('passing string with linefeed characters as input', function() |