robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
5.8 KiB177 linesraw
1local t = require('test.unit.testutil')
2local describe, before_each = t.describe, t.before_each
3local itp = t.gen_itp(t.it)
4local cimported = t.cimport(
5  './src/nvim/os/shell.h',
6  './src/nvim/option_vars.h',
7  './src/nvim/main.h',
8  './src/nvim/memory.h'
9)
10local ffi, eq = t.ffi, t.eq
11local intern = t.internalize
12local to_cstr = t.to_cstr
13local NULL = ffi.cast('void *', 0)
14
15describe('shell functions', function()
16  before_each(function()
17    -- os_system() can't work when the p_sh and p_shcf variables are unset
18    cimported.p_sh = to_cstr('/bin/sh')
19    cimported.p_shcf = to_cstr('-c')
20    cimported.p_sxq = to_cstr('')
21    cimported.p_sxe = to_cstr('')
22  end)
23
24  local function shell_build_argv(cmd, extra_args)
25    local res = cimported.shell_build_argv(cmd and to_cstr(cmd), extra_args and to_cstr(extra_args))
26    -- `res` is zero-indexed (C pointer, not Lua table)!
27    local argc = 0
28    local ret = {}
29    -- Explicitly free everything, so if it is not in allocated memory it will
30    -- crash.
31    while res[argc] ~= nil do
32      ret[#ret + 1] = ffi.string(res[argc])
33      cimported.xfree(res[argc])
34      argc = argc + 1
35    end
36    cimported.xfree(res)
37    return ret
38  end
39
40  local function shell_argv_to_str(argv_table)
41    -- C string array (char **).
42    local argv = (argv_table and ffi.new('char*[?]', #argv_table + 1) or NULL)
43
44    local argc = 1
45    while argv_table ~= nil and argv_table[argc] ~= nil do
46      -- `argv` is zero-indexed (C pointer, not Lua table)!
47      argv[argc - 1] = to_cstr(argv_table[argc])
48      argc = argc + 1
49    end
50    if argv_table ~= nil then
51      argv[argc - 1] = NULL
52    end
53
54    local res = cimported.shell_argv_to_str(argv)
55    return ffi.string(res)
56  end
57
58  local function os_system(cmd, input)
59    local input_or = input and to_cstr(input) or NULL
60    local input_len = (input ~= nil) and string.len(input) or 0
61    local output = ffi.new('char *[1]')
62    local nread = ffi.new('size_t[1]')
63
64    local argv = ffi.cast('char**', cimported.shell_build_argv(to_cstr(cmd), nil))
65    local status = cimported.os_system(argv, input_or, input_len, output, nread)
66
67    return status, intern(output[0], nread[0])
68  end
69
70  describe('os_system', function()
71    itp('can echo some output (shell builtin)', function()
72      local cmd, text = 'printf "%s "', 'some text '
73      local status, output = os_system(cmd .. ' ' .. text)
74      eq(text, output)
75      eq(0, status)
76    end)
77
78    itp('can deal with empty output', function()
79      local cmd = 'printf ""'
80      local status, output = os_system(cmd)
81      eq('', output)
82      eq(0, status)
83    end)
84
85    itp('can pass input on stdin', function()
86      local cmd, input = 'cat -', 'some text\nsome other text'
87      local status, output = os_system(cmd, input)
88      eq(input, output)
89      eq(0, status)
90    end)
91
92    itp('returns non-zero exit code', function()
93      local status = os_system('exit 2')
94      eq(2, status)
95    end)
96  end)
97
98  describe('shell_build_argv', function()
99    itp('works with NULL arguments', function()
100      eq({ '/bin/sh' }, shell_build_argv(nil, nil))
101    end)
102
103    itp('works with cmd', function()
104      eq({ '/bin/sh', '-c', 'abc  def' }, shell_build_argv('abc  def', nil))
105    end)
106
107    itp('works with extra_args', function()
108      eq({ '/bin/sh', 'ghi  jkl' }, shell_build_argv(nil, 'ghi  jkl'))
109    end)
110
111    itp('works with cmd and extra_args', function()
112      eq({ '/bin/sh', 'ghi  jkl', '-c', 'abc  def' }, shell_build_argv('abc  def', 'ghi  jkl'))
113    end)
114
115    itp('splits and unquotes &shell and &shellcmdflag', function()
116      cimported.p_sh = to_cstr('/Program" "Files/zsh -f')
117      cimported.p_shcf = to_cstr('-x -o "sh word split" "-"c')
118      eq(
119        { '/Program Files/zsh', '-f', 'ghi  jkl', '-x', '-o', 'sh word split', '-c', 'abc  def' },
120        shell_build_argv('abc  def', 'ghi  jkl')
121      )
122    end)
123
124    itp('applies shellxescape (p_sxe) and shellxquote (p_sxq)', function()
125      cimported.p_sxq = to_cstr('(')
126      cimported.p_sxe = to_cstr('"&|<>()@^')
127
128      local argv = ffi.cast('char**', cimported.shell_build_argv(to_cstr('echo &|<>()@^'), nil))
129      eq('/bin/sh', ffi.string(argv[0]))
130      eq('-c', ffi.string(argv[1]))
131      eq('(echo ^&^|^<^>^(^)^@^^)', ffi.string(argv[2]))
132      eq(nil, argv[3])
133    end)
134
135    itp('applies shellxquote="(', function()
136      cimported.p_sxq = to_cstr('"(')
137      cimported.p_sxe = to_cstr('"&|<>()@^')
138
139      local argv = ffi.cast('char**', cimported.shell_build_argv(to_cstr('echo -n some text'), nil))
140      eq('/bin/sh', ffi.string(argv[0]))
141      eq('-c', ffi.string(argv[1]))
142      eq('"(echo -n some text)"', ffi.string(argv[2]))
143      eq(nil, argv[3])
144    end)
145
146    itp('applies shellxquote="', function()
147      cimported.p_sxq = to_cstr('"')
148      cimported.p_sxe = to_cstr('')
149
150      local argv = ffi.cast('char**', cimported.shell_build_argv(to_cstr('echo -n some text'), nil))
151      eq('/bin/sh', ffi.string(argv[0]))
152      eq('-c', ffi.string(argv[1]))
153      eq('"echo -n some text"', ffi.string(argv[2]))
154      eq(nil, argv[3])
155    end)
156
157    itp('with empty shellxquote/shellxescape', function()
158      local argv = ffi.cast('char**', cimported.shell_build_argv(to_cstr('echo -n some text'), nil))
159      eq('/bin/sh', ffi.string(argv[0]))
160      eq('-c', ffi.string(argv[1]))
161      eq('echo -n some text', ffi.string(argv[2]))
162      eq(nil, argv[3])
163    end)
164  end)
165
166  itp('shell_argv_to_str', function()
167    eq('', shell_argv_to_str({ nil }))
168    eq("''", shell_argv_to_str({ '' }))
169    eq("'foo' '' 'bar'", shell_argv_to_str({ 'foo', '', 'bar' }))
170    eq("'/bin/sh' '-c' 'abc  def'", shell_argv_to_str({ '/bin/sh', '-c', 'abc  def' }))
171    eq("'abc  def' 'ghi  jkl'", shell_argv_to_str({ 'abc  def', 'ghi  jkl' }))
172    eq(
173      "'/bin/sh' '-c' 'abc  def' '" .. ('x'):rep(225) .. '...',
174      shell_argv_to_str({ '/bin/sh', '-c', 'abc  def', ('x'):rep(999) })
175    )
176  end)
177end)