aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/system_spec.lua
blob: 9321468f84eca9df5881cf16e360645d27a08c7d (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
local helpers = require('test.functional.helpers')(after_each)
local clear = helpers.clear
local exec_lua = helpers.exec_lua
local eq = helpers.eq

local function system_sync(cmd, opts)
  return exec_lua([[
    local obj = vim.system(...)
    local pid = obj.pid
    local res = obj:wait()

    -- Check the process is no longer running
    vim.fn.systemlist({'ps', 'p', tostring(pid)})
    assert(vim.v.shell_error == 1, 'process still exists')

    return res
  ]], cmd, opts)
end

local function system_async(cmd, opts)
  return exec_lua([[
    local cmd, opts = ...
    _G.done = false
    local obj = vim.system(cmd, opts, function(obj)
      _G.done = true
      _G.ret = obj
    end)

    local done = vim.wait(10000, function()
      return _G.done
    end)

    assert(done, 'process did not exit')

    -- Check the process is no longer running
    vim.fn.systemlist({'ps', 'p', tostring(obj.pid)})
    assert(vim.v.shell_error == 1, 'process still exists')

    return _G.ret
  ]], cmd, opts)
end

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

  for name, system in pairs{ sync = system_sync, async = system_async, } do
    describe('('..name..')', function()
      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('supports timeout', function()
        eq({
          code = 124,
          signal = 15,
          stdout = '',
          stderr = ''
        }, system({ 'sleep', '10' }, { timeout = 1 }))
      end)
    end)
  end

  it('kill processes', function()
    exec_lua([[
      local signal
      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
      vim.fn.systemlist({'ps', 'p', tostring(cmd.pid)})
      assert(vim.v.shell_error == 1, 'dwqdqd '..vim.v.shell_error)

      assert(signal == 2)
    ]])
  end)

end)