From 74aef8972048c3288a3cbd6a8dadf17a8df3c08c Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Mon, 13 Apr 2015 23:53:16 -0400 Subject: term: use an argument vector for termopen(). Old behaviour: termopen('cmd') would run `&shell &shcf "cmd"`, which caused the functional tests to fail on some systems due to the process not "owning" the terminal. Also, it is inconsistent with jobstart(). Modify termopen() so that &shell is not invoked, but maintain the old behaviour with :terminal. Factor the common code for building the argument vector from jobstart() and modify the functional tests to call termopen() instead of :terminal (fixes #2354). Also: * Add a 'name' option for termopen() so that `:terminal {cmd}` produces a buffer named "term//{cwd}/{cmd}" and termopen() users can customize the name. * Update the documentation. * Add functional tests for `:terminal` sinse its behaviour now differs from termopen(). Add "test/functional/fixtures/shell-test.c" and move "test/functional/job/tty-test.c" there, too. Helped-by: Justin M. Keyes <@justinmk> --- test/functional/fixtures/CMakeLists.txt | 4 + test/functional/fixtures/shell-test.c | 25 +++++ test/functional/fixtures/tty-test.c | 150 ++++++++++++++++++++++++++ test/functional/job/CMakeLists.txt | 2 - test/functional/job/tty-test.c | 150 -------------------------- test/functional/terminal/cursor_spec.lua | 2 +- test/functional/terminal/ex_terminal_spec.lua | 61 +++++++++++ test/functional/terminal/helpers.lua | 2 +- test/functional/terminal/highlight_spec.lua | 4 +- test/functional/terminal/scrollback_spec.lua | 2 +- 10 files changed, 245 insertions(+), 157 deletions(-) create mode 100644 test/functional/fixtures/CMakeLists.txt create mode 100644 test/functional/fixtures/shell-test.c create mode 100644 test/functional/fixtures/tty-test.c delete mode 100644 test/functional/job/CMakeLists.txt delete mode 100644 test/functional/job/tty-test.c create mode 100644 test/functional/terminal/ex_terminal_spec.lua (limited to 'test/functional') diff --git a/test/functional/fixtures/CMakeLists.txt b/test/functional/fixtures/CMakeLists.txt new file mode 100644 index 0000000000..70aee6efa9 --- /dev/null +++ b/test/functional/fixtures/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(tty-test tty-test.c) +target_link_libraries(tty-test ${LIBUV_LIBRARIES}) + +add_executable(shell-test shell-test.c) diff --git a/test/functional/fixtures/shell-test.c b/test/functional/fixtures/shell-test.c new file mode 100644 index 0000000000..5fa8a58049 --- /dev/null +++ b/test/functional/fixtures/shell-test.c @@ -0,0 +1,25 @@ +// A simple implementation of a shell for testing +// `termopen([&sh, &shcf, '{cmd'}])` and `termopen([&sh])`. +// +// If launched with no arguments, prints "ready $ ", otherwise prints +// "ready $ {cmd}\n". + +#include +#include + +int main(int argc, char **argv) +{ + fprintf(stderr, "ready $ "); + + if (argc == 3) { + // argv should be {"terminal-test", "EXE", "prog args..."} + if (strcmp(argv[1], "EXE") != 0) { + fprintf(stderr, "first argument must be 'EXE'\n"); + return 2; + } + + fprintf(stderr, "%s\n", argv[2]); + } + + return 0; +} diff --git a/test/functional/fixtures/tty-test.c b/test/functional/fixtures/tty-test.c new file mode 100644 index 0000000000..40ba131003 --- /dev/null +++ b/test/functional/fixtures/tty-test.c @@ -0,0 +1,150 @@ +#include +#include +#include +#include + +uv_tty_t tty; + +#ifdef _WIN32 +#include +bool owns_tty(void) +{ + HWND consoleWnd = GetConsoleWindow(); + DWORD dwProcessId; + GetWindowThreadProcessId(consoleWnd, &dwProcessId); + return GetCurrentProcessId() == dwProcessId; +} +#else +#include +bool owns_tty(void) +{ + return getsid(0) == getpid(); +} +#endif + +#define is_terminal(stream) (uv_guess_handle(fileno(stream)) == UV_TTY) +#define BUF_SIZE 0xfff + +static void walk_cb(uv_handle_t *handle, void *arg) { + if (!uv_is_closing(handle)) { + uv_close(handle, NULL); + } +} + +static void sigwinch_handler(int signum) +{ + int width, height; + uv_tty_get_winsize(&tty, &width, &height); + fprintf(stderr, "rows: %d, cols: %d\n", height, width); +} + +// static void sigwinch_cb(uv_signal_t *handle, int signum) +// { +// int width, height; +// uv_tty_t *tty = handle->data; +// uv_tty_get_winsize(tty, &width, &height); +// fprintf(stderr, "rows: %d, cols: %d\n", height, width); +// } + +static void alloc_cb(uv_handle_t *handle, size_t suggested, uv_buf_t *buf) +{ + buf->len = BUF_SIZE; + buf->base = malloc(BUF_SIZE); +} + +static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf) +{ + if (cnt <= 0) { + uv_read_stop(stream); + return; + } + + int *interrupted = stream->data; + + for (int i = 0; i < cnt; i++) { + if (buf->base[i] == 3) { + (*interrupted)++; + } + } + + uv_loop_t write_loop; + uv_loop_init(&write_loop); + uv_tty_t out; + uv_tty_init(&write_loop, &out, 1, 0); + uv_write_t req; + uv_buf_t b = {.base = buf->base, .len = (size_t)cnt}; + uv_write(&req, (uv_stream_t *)&out, &b, 1, NULL); + uv_run(&write_loop, UV_RUN_DEFAULT); + uv_close((uv_handle_t *)&out, NULL); + uv_run(&write_loop, UV_RUN_DEFAULT); + if (uv_loop_close(&write_loop)) { + abort(); + } + free(buf->base); + + if (*interrupted >= 2) { + uv_walk(uv_default_loop(), walk_cb, NULL); + } else if (*interrupted == 1) { + fprintf(stderr, "interrupt received, press again to exit\n"); + } +} + +static void prepare_cb(uv_prepare_t *handle) +{ + fprintf(stderr, "tty ready\n"); + uv_prepare_stop(handle); +} + +int main(int argc, char **argv) +{ + if (!owns_tty()) { + fprintf(stderr, "process does not own the terminal\n"); + exit(2); + } + + if (!is_terminal(stdin)) { + fprintf(stderr, "stdin is not a terminal\n"); + exit(2); + } + + if (!is_terminal(stdout)) { + fprintf(stderr, "stdout is not a terminal\n"); + exit(2); + } + + if (!is_terminal(stderr)) { + fprintf(stderr, "stderr is not a terminal\n"); + exit(2); + } + + if (argc > 1) { + int count = atoi(argv[1]); + for (int i = 0; i < count; ++i) { + printf("line%d\n", i); + } + fflush(stdout); + return 0; + } + + int interrupted = 0; + uv_prepare_t prepare; + uv_prepare_init(uv_default_loop(), &prepare); + uv_prepare_start(&prepare, prepare_cb); + // uv_tty_t tty; + uv_tty_init(uv_default_loop(), &tty, fileno(stderr), 1); + uv_tty_set_mode(&tty, UV_TTY_MODE_RAW); + tty.data = &interrupted; + uv_read_start((uv_stream_t *)&tty, alloc_cb, read_cb); + struct sigaction sa; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = sigwinch_handler; + sigaction(SIGWINCH, &sa, NULL); + // uv_signal_t sigwinch_watcher; + // uv_signal_init(uv_default_loop(), &sigwinch_watcher); + // sigwinch_watcher.data = &tty; + // uv_signal_start(&sigwinch_watcher, sigwinch_cb, SIGWINCH); + uv_run(uv_default_loop(), UV_RUN_DEFAULT); + + return 0; +} diff --git a/test/functional/job/CMakeLists.txt b/test/functional/job/CMakeLists.txt deleted file mode 100644 index 14ec287816..0000000000 --- a/test/functional/job/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_executable(tty-test tty-test.c) -target_link_libraries(tty-test ${LIBUV_LIBRARIES}) diff --git a/test/functional/job/tty-test.c b/test/functional/job/tty-test.c deleted file mode 100644 index 40ba131003..0000000000 --- a/test/functional/job/tty-test.c +++ /dev/null @@ -1,150 +0,0 @@ -#include -#include -#include -#include - -uv_tty_t tty; - -#ifdef _WIN32 -#include -bool owns_tty(void) -{ - HWND consoleWnd = GetConsoleWindow(); - DWORD dwProcessId; - GetWindowThreadProcessId(consoleWnd, &dwProcessId); - return GetCurrentProcessId() == dwProcessId; -} -#else -#include -bool owns_tty(void) -{ - return getsid(0) == getpid(); -} -#endif - -#define is_terminal(stream) (uv_guess_handle(fileno(stream)) == UV_TTY) -#define BUF_SIZE 0xfff - -static void walk_cb(uv_handle_t *handle, void *arg) { - if (!uv_is_closing(handle)) { - uv_close(handle, NULL); - } -} - -static void sigwinch_handler(int signum) -{ - int width, height; - uv_tty_get_winsize(&tty, &width, &height); - fprintf(stderr, "rows: %d, cols: %d\n", height, width); -} - -// static void sigwinch_cb(uv_signal_t *handle, int signum) -// { -// int width, height; -// uv_tty_t *tty = handle->data; -// uv_tty_get_winsize(tty, &width, &height); -// fprintf(stderr, "rows: %d, cols: %d\n", height, width); -// } - -static void alloc_cb(uv_handle_t *handle, size_t suggested, uv_buf_t *buf) -{ - buf->len = BUF_SIZE; - buf->base = malloc(BUF_SIZE); -} - -static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf) -{ - if (cnt <= 0) { - uv_read_stop(stream); - return; - } - - int *interrupted = stream->data; - - for (int i = 0; i < cnt; i++) { - if (buf->base[i] == 3) { - (*interrupted)++; - } - } - - uv_loop_t write_loop; - uv_loop_init(&write_loop); - uv_tty_t out; - uv_tty_init(&write_loop, &out, 1, 0); - uv_write_t req; - uv_buf_t b = {.base = buf->base, .len = (size_t)cnt}; - uv_write(&req, (uv_stream_t *)&out, &b, 1, NULL); - uv_run(&write_loop, UV_RUN_DEFAULT); - uv_close((uv_handle_t *)&out, NULL); - uv_run(&write_loop, UV_RUN_DEFAULT); - if (uv_loop_close(&write_loop)) { - abort(); - } - free(buf->base); - - if (*interrupted >= 2) { - uv_walk(uv_default_loop(), walk_cb, NULL); - } else if (*interrupted == 1) { - fprintf(stderr, "interrupt received, press again to exit\n"); - } -} - -static void prepare_cb(uv_prepare_t *handle) -{ - fprintf(stderr, "tty ready\n"); - uv_prepare_stop(handle); -} - -int main(int argc, char **argv) -{ - if (!owns_tty()) { - fprintf(stderr, "process does not own the terminal\n"); - exit(2); - } - - if (!is_terminal(stdin)) { - fprintf(stderr, "stdin is not a terminal\n"); - exit(2); - } - - if (!is_terminal(stdout)) { - fprintf(stderr, "stdout is not a terminal\n"); - exit(2); - } - - if (!is_terminal(stderr)) { - fprintf(stderr, "stderr is not a terminal\n"); - exit(2); - } - - if (argc > 1) { - int count = atoi(argv[1]); - for (int i = 0; i < count; ++i) { - printf("line%d\n", i); - } - fflush(stdout); - return 0; - } - - int interrupted = 0; - uv_prepare_t prepare; - uv_prepare_init(uv_default_loop(), &prepare); - uv_prepare_start(&prepare, prepare_cb); - // uv_tty_t tty; - uv_tty_init(uv_default_loop(), &tty, fileno(stderr), 1); - uv_tty_set_mode(&tty, UV_TTY_MODE_RAW); - tty.data = &interrupted; - uv_read_start((uv_stream_t *)&tty, alloc_cb, read_cb); - struct sigaction sa; - sigemptyset(&sa.sa_mask); - sa.sa_flags = 0; - sa.sa_handler = sigwinch_handler; - sigaction(SIGWINCH, &sa, NULL); - // uv_signal_t sigwinch_watcher; - // uv_signal_init(uv_default_loop(), &sigwinch_watcher); - // sigwinch_watcher.data = &tty; - // uv_signal_start(&sigwinch_watcher, sigwinch_cb, SIGWINCH); - uv_run(uv_default_loop(), UV_RUN_DEFAULT); - - return 0; -} diff --git a/test/functional/terminal/cursor_spec.lua b/test/functional/terminal/cursor_spec.lua index cecb33de7c..29225f968f 100644 --- a/test/functional/terminal/cursor_spec.lua +++ b/test/functional/terminal/cursor_spec.lua @@ -144,7 +144,7 @@ describe('cursor with customized highlighting', function() [6] = {foreground = 130}, }) screen:attach(false) - execute('term "' ..nvim_dir.. '/tty-test"') + execute('call termopen(["'..nvim_dir..'/tty-test"]) | startinsert') end) it('overrides the default highlighting', function() diff --git a/test/functional/terminal/ex_terminal_spec.lua b/test/functional/terminal/ex_terminal_spec.lua new file mode 100644 index 0000000000..3855cf4b65 --- /dev/null +++ b/test/functional/terminal/ex_terminal_spec.lua @@ -0,0 +1,61 @@ +local helpers = require('test.functional.helpers') +local Screen = require('test.functional.ui.screen') +local clear, wait, nvim = helpers.clear, helpers.wait, helpers.nvim +local nvim_dir = helpers.nvim_dir +local execute, source = helpers.execute, helpers.source +local eq, neq = helpers.eq, helpers.neq + +describe(':terminal', function() + local screen + + before_each(function() + clear() + screen = Screen.new(50, 7) + screen:attach(false) + nvim('set_option', 'shell', nvim_dir..'/shell-test') + nvim('set_option', 'shellcmdflag', 'EXE') + + end) + + it('with no argument, acts like termopen()', function() + execute('terminal') + wait() + screen:expect([[ + ready $ | + [Program exited, press any key to close] | + | + | + | + | + -- TERMINAL -- | + ]]) + end) + + it('executes a given command through the shell', function() + execute('terminal echo hi') + wait() + screen:expect([[ + ready $ echo hi | + | + [Program exited, press any key to close] | + | + | + | + -- TERMINAL -- | + ]]) + end) + + it('allows quotes and slashes', function() + execute([[terminal echo 'hello' \ "world"]]) + wait() + screen:expect([[ + ready $ echo 'hello' \ "world" | + | + [Program exited, press any key to close] | + | + | + | + -- TERMINAL -- | + ]]) + end) +end) diff --git a/test/functional/terminal/helpers.lua b/test/functional/terminal/helpers.lua index 1bc6057a0b..e488495139 100644 --- a/test/functional/terminal/helpers.lua +++ b/test/functional/terminal/helpers.lua @@ -56,7 +56,7 @@ local function screen_setup(extra_height) -- tty-test puts the terminal into raw mode and echoes all input. tests are -- done by feeding it with terminfo codes to control the display and -- verifying output with screen:expect. - execute('term ' ..nvim_dir.. '/tty-test') + execute('enew | call termopen(["'..nvim_dir..'/tty-test"]) | startinsert') -- wait for "tty ready" to be printed before each test or the terminal may -- still be in canonical mode(will echo characters for example) -- diff --git a/test/functional/terminal/highlight_spec.lua b/test/functional/terminal/highlight_spec.lua index 59b0d2c19d..b72527e7b6 100644 --- a/test/functional/terminal/highlight_spec.lua +++ b/test/functional/terminal/highlight_spec.lua @@ -27,7 +27,7 @@ describe('terminal window highlighting', function() [8] = {background = 11} }) screen:attach(false) - execute('term "' ..nvim_dir.. '/tty-test"') + execute('enew | call termopen(["'..nvim_dir..'/tty-test"]) | startinsert') screen:expect([[ tty ready | | @@ -133,7 +133,7 @@ describe('terminal window highlighting with custom palette', function() }) screen:attach(true) nvim('set_var', 'terminal_color_3', '#123456') - execute('term "' ..nvim_dir.. '/tty-test"') + execute('enew | call termopen(["'..nvim_dir..'/tty-test"]) | startinsert') screen:expect([[ tty ready | | diff --git a/test/functional/terminal/scrollback_spec.lua b/test/functional/terminal/scrollback_spec.lua index d9b7534fac..f2381631a9 100644 --- a/test/functional/terminal/scrollback_spec.lua +++ b/test/functional/terminal/scrollback_spec.lua @@ -332,7 +332,7 @@ describe('terminal prints more lines than the screen height and exits', function clear() local screen = Screen.new(50, 7) screen:attach(false) - execute('term ' ..nvim_dir.. '/tty-test 10') + execute('call termopen(["'..nvim_dir..'/tty-test", "10"]) | startinsert') wait() screen:expect([[ line6 | -- cgit From 1eb33969220b267cf45adb286f0b7b6d14805eff Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Wed, 15 Apr 2015 13:05:30 -0400 Subject: unify jobstart, termopen, and system interfaces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For any of these functions, if {cmd} is a string, execute "&shell &shellcmdflag '{cmd}'", or simply {cmd} if it's a list. In termopen(), if the 'name' option is not supplied, try to guess using '{cmd}' (string) or {cmd}[0] (list). Simplify ex_terminal to use the string form of termopen(). termopen: get name from argument Convert list_to_argv to tv_to_argv. Helped-by: Björn Linse <@bfredl> Helped-by: oni-link Helped-by: Thiago de Arruda <@tarruda> --- test/functional/job/job_spec.lua | 7 +++++++ test/functional/shell/viml_system_spec.lua | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) (limited to 'test/functional') diff --git a/test/functional/job/job_spec.lua b/test/functional/job/job_spec.lua index df989b3ef9..259fa52443 100644 --- a/test/functional/job/job_spec.lua +++ b/test/functional/job/job_spec.lua @@ -29,6 +29,13 @@ describe('jobs', function() ]]) end) + it('uses &shell and &shellcmdflag if passed a string', function() + nvim('command', "let $VAR = 'abc'") + nvim('command', "let j = jobstart('echo $VAR', g:job_opts)") + eq({'notification', 'stdout', {0, {'abc', ''}}}, next_msg()) + eq({'notification', 'exit', {0, 0}}, next_msg()) + end) + it('returns 0 when it fails to start', function() local status, rv = pcall(eval, "jobstart([])") eq(false, status) diff --git a/test/functional/shell/viml_system_spec.lua b/test/functional/shell/viml_system_spec.lua index 922770ad42..bd47d31a14 100644 --- a/test/functional/shell/viml_system_spec.lua +++ b/test/functional/shell/viml_system_spec.lua @@ -44,7 +44,7 @@ describe('system()', function() eq(127, eval('v:shell_error')) end) - describe('executes shell function', function() + describe('executes shell function if passed a string', function() local screen before_each(function() @@ -192,6 +192,13 @@ describe('system()', function() end) end) end + + describe('command passed as a list', function() + it('does not execute &shell', function() + eq('* $NOTHING ~/file', + eval("system(['echo', '-n', '*', '$NOTHING', '~/file'])")) + end) + end) end) describe('systemlist()', function() -- cgit