aboutsummaryrefslogtreecommitdiff
path: root/test/unit/os/shell_spec.lua
blob: 37274502de84afd98c2599fd387d76f81df95f3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
local helpers = require('test.unit.helpers')(after_each)
local itp = helpers.gen_itp(it)
local cimported = helpers.cimport(
  './src/nvim/os/shell.h',
  './src/nvim/option_defs.h',
  './src/nvim/main.h',
  './src/nvim/misc1.h',
  './src/nvim/memory.h'
)
local ffi, eq = helpers.ffi, helpers.eq
local intern = helpers.internalize
local to_cstr = helpers.to_cstr
local NULL = ffi.cast('void *', 0)

describe('shell functions', function()
  before_each(function()
    -- os_system() can't work when the p_sh and p_shcf variables are unset
    cimported.p_sh = to_cstr('/bin/sh')
    cimported.p_shcf = to_cstr('-c')
    cimported.p_sxq = to_cstr('')
    cimported.p_sxe = to_cstr('')
  end)

  local function shell_build_argv(cmd, extra_args)
    local res = cimported.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])
      cimported.xfree(res[argc])
      argc = argc + 1
    end
    cimported.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
    local output = ffi.new('char *[1]')
    local nread = ffi.new('size_t[1]')

    local argv = ffi.cast('char**',
                          cimported.shell_build_argv(to_cstr(cmd), nil))
    local status = cimported.os_system(argv, input_or, input_len, output, nread)

    return status, intern(output[0], nread[0])
  end

  describe('os_system', function()
    itp('can echo some output (shell builtin)', function()
      local cmd, text = 'printf "%s "', 'some text '
      local status, output = os_system(cmd .. ' ' .. text)
      eq(text, output)
      eq(0, status)
    end)

    itp('can deal with empty output', function()
      local cmd = 'printf ""'
      local status, output = os_system(cmd)
      eq('', output)
      eq(0, status)
    end)

    itp('can pass input on stdin', function()
      local cmd, input = 'cat -', 'some text\nsome other text'
      local status, output = os_system(cmd, input)
      eq(input, output)
      eq(0, status)
    end)

    itp('returns non-zero exit code', function()
      local status = os_system('exit 2')
      eq(2, status)
    end)
  end)

  describe('shell_build_argv', function()
    itp('works with NULL arguments', function()
      eq({'/bin/sh'}, shell_build_argv(nil, nil))
    end)

    itp('works with cmd', function()
      eq({'/bin/sh', '-c', 'abc  def'}, shell_build_argv('abc  def', nil))
    end)

    itp('works with extra_args', function()
      eq({'/bin/sh', 'ghi  jkl'}, shell_build_argv(nil, 'ghi  jkl'))
    end)

    itp('works with cmd and extra_args', function()
      eq({'/bin/sh', 'ghi  jkl', '-c', 'abc  def'}, shell_build_argv('abc  def', 'ghi  jkl'))
    end)

    itp('splits and unquotes &shell and &shellcmdflag', function()
      cimported.p_sh = to_cstr('/Program" "Files/zsh -f')
      cimported.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)

    itp('applies shellxescape (p_sxe) and shellxquote (p_sxq)', function()
      cimported.p_sxq = to_cstr('(')
      cimported.p_sxe = to_cstr('"&|<>()@^')

      local argv = ffi.cast('char**',
                        cimported.shell_build_argv(to_cstr('echo &|<>()@^'), nil))
      eq(ffi.string(argv[0]), '/bin/sh')
      eq(ffi.string(argv[1]), '-c')
      eq(ffi.string(argv[2]), '(echo ^&^|^<^>^(^)^@^^)')
      eq(nil, argv[3])
    end)

    itp('applies shellxquote="(', function()
      cimported.p_sxq = to_cstr('"(')
      cimported.p_sxe = to_cstr('"&|<>()@^')

      local argv = ffi.cast('char**', cimported.shell_build_argv(
                                          to_cstr('echo -n some text'), nil))
      eq(ffi.string(argv[0]), '/bin/sh')
      eq(ffi.string(argv[1]), '-c')
      eq(ffi.string(argv[2]), '"(echo -n some text)"')
      eq(nil, argv[3])
    end)

    itp('applies shellxquote="', function()
      cimported.p_sxq = to_cstr('"')
      cimported.p_sxe = to_cstr('')

      local argv = ffi.cast('char**', cimported.shell_build_argv(
                                          to_cstr('echo -n some text'), nil))
      eq(ffi.string(argv[0]), '/bin/sh')
      eq(ffi.string(argv[1]), '-c')
      eq(ffi.string(argv[2]), '"echo -n some text"')
      eq(nil, argv[3])
    end)

    itp('with empty shellxquote/shellxescape', function()
      local argv = ffi.cast('char**', cimported.shell_build_argv(
                                          to_cstr('echo -n some text'), nil))
      eq(ffi.string(argv[0]), '/bin/sh')
      eq(ffi.string(argv[1]), '-c')
      eq(ffi.string(argv[2]), 'echo -n some text')
      eq(nil, argv[3])
    end)
  end)
end)