diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/functional/eval/let_spec.lua | 39 | ||||
-rw-r--r-- | test/functional/fixtures/CMakeLists.txt | 4 | ||||
-rw-r--r-- | test/functional/fixtures/printenv-test.c | 59 | ||||
-rw-r--r-- | test/helpers.lua | 3 | ||||
-rw-r--r-- | test/unit/helpers.lua | 12 | ||||
-rw-r--r-- | test/unit/os/env_spec.lua | 79 |
6 files changed, 166 insertions, 30 deletions
diff --git a/test/functional/eval/let_spec.lua b/test/functional/eval/let_spec.lua index 1bd3405698..0cbf40137e 100644 --- a/test/functional/eval/let_spec.lua +++ b/test/functional/eval/let_spec.lua @@ -2,13 +2,16 @@ local helpers = require('test.functional.helpers')(after_each) local eq = helpers.eq local clear = helpers.clear +local command = helpers.command +local eval = helpers.eval local meths = helpers.meths local redir_exec = helpers.redir_exec local source = helpers.source +local nvim_dir = helpers.nvim_dir before_each(clear) -describe(':let command', function() +describe(':let', function() it('correctly lists variables with curly-braces', function() meths.set_var('v', {0}) eq('\nv [0]', redir_exec('let {"v"}')) @@ -42,4 +45,38 @@ describe(':let command', function() call feedkeys(":\e:echo l1 l3\n:echo 42\n:cq\n", "t") ]=]) end) + + it("multibyte env var #8398 #9267", function() + command("let $NVIM_TEST = 'AìaB'") + eq('AìaB', eval('$NVIM_TEST')) + command("let $NVIM_TEST = 'AaあB'") + eq('AaあB', eval('$NVIM_TEST')) + local mbyte = [[\p* .ม .ม .ม .ม่ .ม่ .ม่ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ ֹֻ + .ֹֻ .ֹֻ .ֹֻ ֹֻ ֹֻ ֹֻ .ֹֻ .ֹֻ .ֹֻ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ + .ֹֻ .ֹֻ .ֹֻ a a a ca ca ca à à à]] + command("let $NVIM_TEST = '"..mbyte.."'") + eq(mbyte, eval('$NVIM_TEST')) + end) + + it("multibyte env var to child process #8398 #9267", function() + if (not helpers.iswin()) and require('test.helpers').isCI() then + -- Fails on non-Windows CI. Buffering/timing issue? + pending('fails on unix CI', function() end) + end + local cmd_get_child_env = "let g:env_from_child = system(['"..nvim_dir.."/printenv-test', 'NVIM_TEST'])" + command("let $NVIM_TEST = 'AìaB'") + command(cmd_get_child_env) + eq(eval('$NVIM_TEST'), eval('g:env_from_child')) + + command("let $NVIM_TEST = 'AaあB'") + command(cmd_get_child_env) + eq(eval('$NVIM_TEST'), eval('g:env_from_child')) + + local mbyte = [[\p* .ม .ม .ม .ม่ .ม่ .ม่ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ ֹֻ + .ֹֻ .ֹֻ .ֹֻ ֹֻ ֹֻ ֹֻ .ֹֻ .ֹֻ .ֹֻ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ + .ֹֻ .ֹֻ .ֹֻ a a a ca ca ca à à à]] + command("let $NVIM_TEST = '"..mbyte.."'") + command(cmd_get_child_env) + eq(eval('$NVIM_TEST'), eval('g:env_from_child')) + end) end) diff --git a/test/functional/fixtures/CMakeLists.txt b/test/functional/fixtures/CMakeLists.txt index 8537ea390f..a7cd214b6b 100644 --- a/test/functional/fixtures/CMakeLists.txt +++ b/test/functional/fixtures/CMakeLists.txt @@ -3,3 +3,7 @@ target_link_libraries(tty-test ${LIBUV_LIBRARIES}) add_executable(shell-test shell-test.c) add_executable(printargs-test printargs-test.c) +add_executable(printenv-test printenv-test.c) +if(WIN32) + set_target_properties(printenv-test PROPERTIES LINK_FLAGS -municode) +endif() diff --git a/test/functional/fixtures/printenv-test.c b/test/functional/fixtures/printenv-test.c new file mode 100644 index 0000000000..5ac076f653 --- /dev/null +++ b/test/functional/fixtures/printenv-test.c @@ -0,0 +1,59 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check +// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +#include <stdio.h> + +#ifdef WIN32 +# include <windows.h> +#else +# include <stdlib.h> +#endif + +#ifdef WIN32 +int wmain(int argc, wchar_t **argv) +#else +int main(int argc, char **argv) +#endif +{ + if (argc != 2) { + return 1; + } + +#ifdef WIN32 + wchar_t *value = _wgetenv(argv[1]); + if (value == NULL) { + return 1; + } + int utf8_len = WideCharToMultiByte(CP_UTF8, + 0, + value, + -1, + NULL, + 0, + NULL, + NULL); + if (utf8_len == 0) { + return (int)GetLastError(); + } + char *utf8_value = (char *)calloc((size_t)utf8_len, sizeof(char)); + utf8_len = WideCharToMultiByte(CP_UTF8, + 0, + value, + -1, + utf8_value, + utf8_len, + NULL, + NULL); + fprintf(stderr, "%s", utf8_value); + free(utf8_value); +#else + char *value = getenv(argv[1]); + if (value == NULL) { + fprintf(stderr, "env var not found: %s", argv[1]); + return 1; + } + // Print to stderr to avoid buffering. + fprintf(stderr, "%s", value); +#endif + return 0; +} diff --git a/test/helpers.lua b/test/helpers.lua index 59da274e87..9f998ef919 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -708,7 +708,7 @@ end local function isCI() local is_travis = nil ~= os.getenv('TRAVIS') local is_appveyor = nil ~= os.getenv('APPVEYOR') - local is_quickbuild = nil ~= os.getenv('PR_NUMBER') + local is_quickbuild = nil ~= lfs.attributes('/usr/home/quickbuild') return is_travis or is_appveyor or is_quickbuild end @@ -751,6 +751,7 @@ local module = { hasenv = hasenv, hexdump = hexdump, intchar2lua = intchar2lua, + isCI = isCI, map = map, matches = matches, mergedicts_copy = mergedicts_copy, diff --git a/test/unit/helpers.lua b/test/unit/helpers.lua index f8143a0125..beb25f25db 100644 --- a/test/unit/helpers.lua +++ b/test/unit/helpers.lua @@ -645,16 +645,16 @@ local function itp_child(wr, func) s = s:sub(1, hook_msglen - 2) sc.write(wr, '>' .. s .. (' '):rep(hook_msglen - 2 - #s) .. '\n') end - local err, emsg = pcall(init) - if err then + local status, result = pcall(init) + if status then collectgarbage('stop') child_sethook(wr) - err, emsg = pcall(func) + status, result = pcall(func) debug.sethook() end - emsg = tostring(emsg) sc.write(wr, trace_end_msg) - if not err then + if not status then + local emsg = tostring(result) if #emsg > 99999 then emsg = emsg:sub(1, 99999) end @@ -668,7 +668,7 @@ local function itp_child(wr, func) collectgarbage() sc.write(wr, '$\n') sc.close(wr) - sc.exit(err and 0 or 1) + sc.exit(status and 0 or 1) end local function check_child_err(rd) diff --git a/test/unit/os/env_spec.lua b/test/unit/os/env_spec.lua index c54d5a9b77..c543551607 100644 --- a/test/unit/os/env_spec.lua +++ b/test/unit/os/env_spec.lua @@ -8,17 +8,22 @@ local ffi = helpers.ffi local cstr = helpers.cstr local to_cstr = helpers.to_cstr local NULL = helpers.NULL +local OK = 0 require('lfs') local cimp = cimport('./src/nvim/os/os.h') describe('env.c', function() + local function os_env_exists(name) + return cimp.os_env_exists(to_cstr(name)) + end + local function os_setenv(name, value, override) return cimp.os_setenv(to_cstr(name), to_cstr(value), override) end - local function os_unsetenv(name, _, _) + local function os_unsetenv(name) return cimp.os_unsetenv(to_cstr(name)) end @@ -31,25 +36,44 @@ describe('env.c', function() end end - describe('os_setenv', function() - local OK = 0 + itp('os_env_exists', function() + eq(false, os_env_exists('')) + eq(false, os_env_exists(' ')) + eq(false, os_env_exists('\t')) + eq(false, os_env_exists('\n')) + eq(false, os_env_exists('AaあB <= very weird name...')) - itp('sets an env variable and returns OK', function() + local varname = 'NVIM_UNIT_TEST_os_env_exists' + eq(false, os_env_exists(varname)) + eq(OK, os_setenv(varname, 'foo bar baz ...', 1)) + eq(true, os_env_exists(varname)) + end) + + describe('os_setenv', function() + itp('sets an env var and returns success', function() local name = 'NVIM_UNIT_TEST_SETENV_1N' local value = 'NVIM_UNIT_TEST_SETENV_1V' eq(nil, os.getenv(name)) - eq(OK, (os_setenv(name, value, 1))) + eq(OK, os_setenv(name, value, 1)) eq(value, os.getenv(name)) + + -- Set empty, then set non-empty, then retrieve. + eq(OK, os_setenv(name, '', 1)) + eq('', os.getenv(name)) + eq(OK, os_setenv(name, 'non-empty', 1)) + eq('non-empty', os.getenv(name)) end) - itp("dosn't overwrite an env variable if overwrite is 0", function() + itp("`overwrite` behavior", function() local name = 'NVIM_UNIT_TEST_SETENV_2N' local value = 'NVIM_UNIT_TEST_SETENV_2V' local value_updated = 'NVIM_UNIT_TEST_SETENV_2V_UPDATED' - eq(OK, (os_setenv(name, value, 0))) + eq(OK, os_setenv(name, value, 0)) eq(value, os.getenv(name)) - eq(OK, (os_setenv(name, value_updated, 0))) + eq(OK, os_setenv(name, value_updated, 0)) eq(value, os.getenv(name)) + eq(OK, os_setenv(name, value_updated, 1)) + eq(value_updated, os.getenv(name)) end) end) @@ -93,31 +117,42 @@ describe('env.c', function() end) describe('os_getenv', function() - itp('reads an env variable', function() + itp('reads an env var', function() local name = 'NVIM_UNIT_TEST_GETENV_1N' local value = 'NVIM_UNIT_TEST_GETENV_1V' eq(NULL, os_getenv(name)) -- Use os_setenv because Lua dosen't have setenv. os_setenv(name, value, 1) eq(value, os_getenv(name)) + + -- Get a big value. + local bigval = ('x'):rep(256) + eq(OK, os_setenv(name, bigval, 1)) + eq(bigval, os_getenv(name)) + + -- Set non-empty, then set empty. + eq(OK, os_setenv(name, 'non-empty', 1)) + eq('non-empty', os_getenv(name)) + eq(OK, os_setenv(name, '', 1)) + eq(NULL, os_getenv(name)) end) - itp('returns NULL if the env variable is not found', function() - local name = 'NVIM_UNIT_TEST_GETENV_NOTFOUND' - return eq(NULL, os_getenv(name)) + itp('returns NULL if the env var is not found', function() + eq(NULL, os_getenv('NVIM_UNIT_TEST_GETENV_NOTFOUND')) end) end) - describe('os_unsetenv', function() - itp('unsets environment variable', function() - local name = 'TEST_UNSETENV' - local value = 'TESTVALUE' - os_setenv(name, value, 1) - os_unsetenv(name) - neq(os_getenv(name), value) - -- Depending on the platform the var might be unset or set as '' - assert.True(os_getenv(name) == nil or os_getenv(name) == '') - end) + itp('os_unsetenv', function() + local name = 'TEST_UNSETENV' + local value = 'TESTVALUE' + os_setenv(name, value, 1) + eq(OK, os_unsetenv(name)) + neq(os_getenv(name), value) + -- Depending on the platform the var might be unset or set as '' + assert.True(os_getenv(name) == nil or os_getenv(name) == '') + if os_getenv(name) == nil then + eq(false, os_env_exists(name)) + end end) describe('os_getenvname_at_index', function() |