aboutsummaryrefslogtreecommitdiff
path: root/test/functional/api/proc_spec.lua
blob: 0fbf58a8e7414cab6790857310e57bc85f147ac7 (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
local helpers = require('test.functional.helpers')(after_each)

local clear = helpers.clear
local eq = helpers.eq
local funcs = helpers.funcs
local iswin = helpers.iswin
local neq = helpers.neq
local nvim_argv = helpers.nvim_argv
local request = helpers.request
local retry = helpers.retry
local NIL = helpers.NIL

describe('API', function()
  before_each(clear)

  describe('nvim_get_proc_children', function()
    it('returns child process ids', function()
      local this_pid = funcs.getpid()

      -- Might be non-zero already (left-over from some other test?),
      -- but this is not what is tested here.
      local initial_childs = request('nvim_get_proc_children', this_pid)

      local job1 = funcs.jobstart(nvim_argv)
      retry(nil, nil, function()
        eq(#initial_childs + 1, #request('nvim_get_proc_children', this_pid))
      end)

      local job2 = funcs.jobstart(nvim_argv)
      retry(nil, nil, function()
        eq(#initial_childs + 2, #request('nvim_get_proc_children', this_pid))
      end)

      funcs.jobstop(job1)
      retry(nil, nil, function()
        eq(#initial_childs + 1, #request('nvim_get_proc_children', this_pid))
      end)

      funcs.jobstop(job2)
      retry(nil, nil, function()
        eq(#initial_childs, #request('nvim_get_proc_children', this_pid))
      end)
    end)

    it('validates input', function()
      local status, rv = pcall(request, "nvim_get_proc_children", -1)
      eq(false, status)
      eq("Invalid pid: -1", string.match(rv, "Invalid.*"))

      status, rv = pcall(request, "nvim_get_proc_children", 0)
      eq(false, status)
      eq("Invalid pid: 0", string.match(rv, "Invalid.*"))

      -- Assume PID 99999 does not exist.
      status, rv = pcall(request, "nvim_get_proc_children", 99999)
      eq(true, status)
      eq({}, rv)
    end)
  end)

  describe('nvim_get_proc', function()
    it('returns process info', function()
      local pid = funcs.getpid()
      local pinfo = request('nvim_get_proc', pid)
      eq((iswin() and 'nvim.exe' or 'nvim'), pinfo.name)
      eq(pid, pinfo.pid)
      eq('number', type(pinfo.ppid))
      neq(pid, pinfo.ppid)
    end)

    it('validates input', function()
      local status, rv = pcall(request, "nvim_get_proc", -1)
      eq(false, status)
      eq("Invalid pid: -1", string.match(rv, "Invalid.*"))

      status, rv = pcall(request, "nvim_get_proc", 0)
      eq(false, status)
      eq("Invalid pid: 0", string.match(rv, "Invalid.*"))

      -- Assume PID 99999 does not exist.
      status, rv = pcall(request, "nvim_get_proc", 99999)
      eq(true, status)
      eq(NIL, rv)
    end)
  end)
end)