Repository: neovim/neovim
File: test/functional/core/server_spec.lua (Lua)

Code before:
    local current_server = clear()
    -- There should already be at least one server.
    local _n = eval('len(serverlist())')

    -- Add some servers.
    local servs = (
      is_os('win') and { [[\\.\pipe\Xtest-pipe0934]], [[\\.\pipe\Xtest-pipe4324]] }
      or { [[./Xtest-pipe0934]], [[./Xtest-pipe4324]] }
    )
    for _, s in ipairs(servs) do
      eq(s, eval("serverstart('" .. s .. "')"))
    end

    local new_servs = eval('serverlist()')

    -- Exactly #servs servers should be added.
    eq(_n + #servs, #new_servs)
    -- The new servers should be at the end of the list.
    for i = 1, #servs do
      eq(servs[i], new_servs[i + _n])
      eq(1, eval("serverstop('" .. servs[i] .. "')"))
    end
    -- After serverstop() the servers should NOT be in the list.
    eq(_n, eval('len(serverlist())'))

    -- serverlist({ peer = true }) should return servers not startet by serverstart()
    if t.is_os('win') then
      return
    end
    local client_address = n.new_pipename()
    local client = n.new_session(
      true,
      { args = { '-u', 'NONE', '--listen', client_address, '--embed' }, merge = false }
    )
    n.set_session(client)
    eq(client_address, fn.serverlist()[1])

    n.set_session(current_server)

    new_servs = fn.serverlist({ peer = true })
    eq(true, vim.list_contains(new_servs, client_address))
    client:close()
  end)
end)

describe('startup --listen', function()
  -- Tests Nvim output when failing to start, with and without "--headless".
  -- TODO(justinmk): clear() should have a way to get stdout if Nvim fails to start.
  local function _test(args, env, expected)
    local function run(cmd)
      return n.exec_lua(function(cmd_, env_)

Review comment (suggestion):
```suggestion
    -- serverlist({ peer = true }) returns servers from other Nvim sessions.
```

Code after:
    local current_server = clear()
    -- There should already be at least one server.
    local _n = eval('len(serverlist())')

    -- Add some servers.
    local servs = (
      is_os('win') and { [[\\.\pipe\Xtest-pipe0934]], [[\\.\pipe\Xtest-pipe4324]] }
      or { [[./Xtest-pipe0934]], [[./Xtest-pipe4324]] }
    )
    for _, s in ipairs(servs) do
      eq(s, eval("serverstart('" .. s .. "')"))
    end

    local new_servs = eval('serverlist()')

    -- Exactly #servs servers should be added.
    eq(_n + #servs, #new_servs)
    -- The new servers should be at the end of the list.
    for i = 1, #servs do
      eq(servs[i], new_servs[i + _n])
      eq(1, eval("serverstop('" .. servs[i] .. "')"))
    end
    -- After serverstop() the servers should NOT be in the list.
    eq(_n, eval('len(serverlist())'))

    -- serverlist({ peer = true }) returns servers from other Nvim sessions.
    if t.is_os('win') then
      return
    end
    local client_address = n.new_pipename()
    local client = n.new_session(
      true,
      { args = { '-u', 'NONE', '--listen', client_address, '--embed' }, merge = false }
    )
    n.set_session(client)
    eq(client_address, fn.serverlist()[1])

    n.set_session(current_server)

    new_servs = fn.serverlist({ peer = true })
    eq(true, vim.list_contains(new_servs, client_address))
    client:close()
  end)
end)

describe('startup --listen', function()
  -- Tests Nvim output when failing to start, with and without "--headless".
  -- TODO(justinmk): clear() should have a way to get stdout if Nvim fails to start.
  local function _test(args, env, expected)
    local function run(cmd)
      return n.exec_lua(function(cmd_, env_)