diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/unit/os/shell_spec.lua | 60 | ||||
-rw-r--r-- | test/unit/strings_spec.lua | 69 |
2 files changed, 128 insertions, 1 deletions
diff --git a/test/unit/os/shell_spec.lua b/test/unit/os/shell_spec.lua index 01deefab0c..6d1a9f3589 100644 --- a/test/unit/os/shell_spec.lua +++ b/test/unit/os/shell_spec.lua @@ -15,7 +15,8 @@ local shell = helpers.cimport( './src/nvim/os/shell.h', './src/nvim/option_defs.h', './src/nvim/main.h', - './src/nvim/misc1.h' + './src/nvim/misc1.h', + './src/nvim/memory.h' ) local ffi, eq = helpers.ffi, helpers.eq local intern = helpers.internalize @@ -34,6 +35,23 @@ describe('shell functions', function() shell.event_teardown() end) + local function shell_build_argv(cmd, extra_args) + local res = shell.shell_build_argv( + cmd and to_cstr(cmd), + extra_args and to_cstr(extra_args)) + local argc = 0 + local ret = {} + -- Explicitly free everything, so if it is not in allocated memory it will + -- crash. + while res[argc] ~= nil do + ret[#ret + 1] = ffi.string(res[argc]) + shell.xfree(res[argc]) + argc = argc + 1 + end + shell.xfree(res) + return ret + end + local function os_system(cmd, input) local input_or = input and to_cstr(input) or NULL local input_len = (input ~= nil) and string.len(input) or 0 @@ -74,4 +92,44 @@ describe('shell functions', function() eq(2, status) end) end) + + describe('shell_build_argv', function() + local saved_opts = {} + + setup(function() + saved_opts.p_sh = shell.p_sh + saved_opts.p_shcf = shell.p_shcf + end) + + teardown(function() + shell.p_sh = saved_opts.p_sh + shell.p_shcf = saved_opts.p_shcf + end) + + it('works with NULL arguments', function() + eq({'/bin/bash'}, shell_build_argv(nil, nil)) + end) + + it('works with cmd', function() + eq({'/bin/bash', '-c', 'abc def'}, shell_build_argv('abc def', nil)) + end) + + it('works with extra_args', function() + eq({'/bin/bash', 'ghi jkl'}, shell_build_argv(nil, 'ghi jkl')) + end) + + it('works with cmd and extra_args', function() + eq({'/bin/bash', 'ghi jkl', '-c', 'abc def'}, shell_build_argv('abc def', 'ghi jkl')) + end) + + it('splits and unquotes &shell and &shellcmdflag', function() + shell.p_sh = to_cstr('/Program" "Files/zsh -f') + shell.p_shcf = to_cstr('-x -o "sh word split" "-"c') + eq({'/Program Files/zsh', '-f', + 'ghi jkl', + '-x', '-o', 'sh word split', + '-c', 'abc def'}, + shell_build_argv('abc def', 'ghi jkl')) + end) + end) end) diff --git a/test/unit/strings_spec.lua b/test/unit/strings_spec.lua new file mode 100644 index 0000000000..b310ccb541 --- /dev/null +++ b/test/unit/strings_spec.lua @@ -0,0 +1,69 @@ +local helpers = require("test.unit.helpers") + +local cimport = helpers.cimport +local internalize = helpers.internalize +local eq = helpers.eq +local ffi = helpers.ffi +local to_cstr = helpers.to_cstr + +local strings = cimport('stdlib.h', './src/nvim/strings.h', + './src/nvim/memory.h') + +describe('vim_strnsave_unquoted()', function() + local vim_strnsave_unquoted = function(s, len) + local res = strings.vim_strnsave_unquoted(to_cstr(s), len or #s) + local ret = ffi.string(res) + -- Explicitly free memory so we are sure it is allocated: if it was not it + -- will crash. + strings.xfree(res) + return ret + end + + it('copies unquoted strings as-is', function() + eq('-c', vim_strnsave_unquoted('-c')) + eq('', vim_strnsave_unquoted('')) + end) + + it('respects length argument', function() + eq('', vim_strnsave_unquoted('-c', 0)) + eq('-', vim_strnsave_unquoted('-c', 1)) + eq('-', vim_strnsave_unquoted('"-c', 2)) + end) + + it('unquotes fully quoted word', function() + eq('/bin/sh', vim_strnsave_unquoted('"/bin/sh"')) + end) + + it('unquotes partially quoted word', function() + eq('/Program Files/sh', vim_strnsave_unquoted('/Program" "Files/sh')) + end) + + it('removes ""', function() + eq('/Program Files/sh', vim_strnsave_unquoted('/""Program" "Files/sh')) + end) + + it('performs unescaping of "', function() + eq('/"Program Files"/sh', vim_strnsave_unquoted('/"\\""Program Files"\\""/sh')) + end) + + it('performs unescaping of \\', function() + eq('/\\Program Files\\foo/sh', vim_strnsave_unquoted('/"\\\\"Program Files"\\\\foo"/sh')) + end) + + it('strips quote when there is no pair to it', function() + eq('/Program Files/sh', vim_strnsave_unquoted('/Program" Files/sh')) + eq('', vim_strnsave_unquoted('"')) + end) + + it('allows string to end with one backslash unescaped', function() + eq('/Program Files/sh\\', vim_strnsave_unquoted('/Program" Files/sh\\')) + end) + + it('does not perform unescaping out of quotes', function() + eq('/Program\\ Files/sh\\', vim_strnsave_unquoted('/Program\\ Files/sh\\')) + end) + + it('does not unescape \\n', function() + eq('/Program\\nFiles/sh', vim_strnsave_unquoted('/Program"\\n"Files/sh')) + end) +end) |