aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/system_spec.lua
blob: 03e354c1fe5b8e48ccf0d20d3c79bcfc7cdb3833 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local describe, it, before_each = t.describe, t.it, t.before_each
local clear = n.clear
local exec_lua = n.exec_lua
local eq = t.eq

describe('vim.system', function()
  before_each(function()
    clear()
  end)

  for name, system in pairs { sync = n.system_sync, async = n.system_async } do
    describe('(' .. name .. ')', function()
      it('failure modes', function()
        t.matches('cmd: expected non%-empty list of strings', t.pcall_err(system, {}))
        t.matches('cmd: expected non%-empty list of strings', t.pcall_err(system, { 42 }))

        t.matches(
          "ENOENT%: no such file .* %(cmd%): 'non%-existent%-cmd'",
          t.pcall_err(system, { 'non-existent-cmd', 'arg1', 'arg2' }, { text = true })
        )

        t.matches(
          "ENOENT%: no such file .* %(cwd%): 'non%-existent%-cwd'",
          t.pcall_err(system, { 'echo', 'hello' }, { cwd = 'non-existent-cwd', text = true })
        )
      end)

      it('can run simple commands', function()
        eq('hello\n', system({ 'echo', 'hello' }, { text = true }).stdout)
      end)

      it('handle input', function()
        eq('hellocat', system({ 'cat' }, { stdin = 'hellocat', text = true }).stdout)
      end)

      it('uses real pipes for stdin/stdout #35984', function()
        if t.is_os('win') then
          return -- Not applicable for Windows.
        end
        local res = system(
          { 'bash', '-c', 'wc /dev/stdin > /dev/stdout' },
          { stdin = 'pipe\npipy text\n' }
        )
        eq({ '2', '3', '15', '/dev/stdin' }, vim.split(res.stdout, '%s+', { trimempty = true }))
      end)

      it('can set environment', function()
        local function test_env(opt)
          eq('TESTVAL', system({ n.testprg('printenv-test'), 'TEST' }, opt).stdout)
        end

        test_env({ env = { TEST = 'TESTVAL' }, text = true })
        test_env({ clear_env = true, env = { TEST = 'TESTVAL' }, text = true })
      end)

      it('can set environment with clear_env = true and env = nil', function()
        exec_lua(function()
          vim.env.TEST = 'TESTVAL'
        end)

        -- Not passing env with clear_env should not clear the environment
        eq(
          'TESTVAL',
          system({ n.testprg('printenv-test'), 'TEST' }, { clear_env = true, text = true }).stdout
        )

        -- Passing env = {} with clear_env should clear the environment
        eq(
          '',
          system(
            { n.testprg('printenv-test'), 'TEST' },
            { env = {}, clear_env = true, text = true }
          ).stdout
        )
      end)

      it('supports timeout', function()
        eq({
          code = 124,
          signal = 15,
          stdout = '',
          stderr = '',
        }, system({ 'sleep', '10' }, { timeout = 1000 }))
      end)
    end)
  end

  it('kill processes', function()
    exec_lua(function()
      local signal --- @type integer?
      local cmd = vim.system({ 'cat', '-' }, { stdin = true }, function(r)
        signal = r.signal
      end) -- run forever

      cmd:kill('sigint')

      -- wait for the process not to exist
      local done = vim.wait(2000, function()
        return signal ~= nil
      end)

      assert(done, 'process did not exit')

      -- Check the process is no longer running
      local proc = vim.api.nvim_get_proc(cmd.pid)
      assert(not proc, 'process still exists')

      assert(signal == 2)
    end)
  end)

  it('SystemObj:wait() does not process non-fast events #27292', function()
    eq(
      false,
      exec_lua(function()
        _G.processed = false
        local cmd = vim.system({ 'sleep', '1' })
        vim.schedule(function()
          _G.processed = true
        end)
        cmd:wait()
        return _G.processed
      end)
    )
    eq(true, exec_lua([[return _G.processed]]))
  end)

  if t.is_os('win') then
    it('can resolve windows command extensions', function()
      t.write_file('test.bat', 'echo hello world')
      n.system_sync({ 'chmod', '+x', 'test.bat' })
      n.system_sync({ './test' })
    end)

    it('launches cmd.exe when shellslash is set', function()
      n.command('set shellslash')
      local path = exec_lua([[return vim.fn.exepath('cmd.exe')]])
      t.neq(nil, path:find('/', 1, true))

      local result = n.system_sync({ 'cmd.exe', '/c', 'echo OK' })
      eq(0, result.code)
      eq('OK\r\n', result.stdout)
      eq('', result.stderr)
    end)
  end

  it('always captures all content of stdout/stderr #30846', function()
    t.skip(n.fn.executable('git') == 0, 'N/A: missing "git" command')
    t.skip(n.fn.isdirectory('.git') == 0, 'N/A: missing ".git" directory')
    eq(
      0,
      exec_lua(function()
        local done = 0
        local fail = 0
        for _ = 1, 200 do
          vim.system(
            { 'git', 'show', ':0:test/functional/plugin/lsp_spec.lua' },
            { text = true },
            function(o)
              if o.code ~= 0 or #o.stdout == 0 then
                fail = fail + 1
              end
              done = done + 1
            end
          )
        end

        local ok = vim.wait(10000, function()
          return done == 200
        end, 200)
        return fail + (ok and 0 or 1)
      end)
    )
  end)
end)