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

Code before:

    -- 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 peer_temp = n.new_pipename()

    -- Find the last instance of '/' to extract the servername
    local slash_ind = peer_temp:match(".*".."/".."()")
    if slash_ind == nil then
      slash_ind = 0
    end
    local peer_name = peer_temp:sub(slash_ind)
    local peer_addr = vim.fs.normalize(fn.stdpath('run') .. '/..') .. '/' .. peer_name

    local client =
      n.new_session(true, { args = { '--clean', '--listen', peer_addr, '--embed' }, merge = false })
    n.set_session(client)
    eq(peer_addr, fn.serverlist()[1])

    n.set_session(current_server)

    new_servs = fn.serverlist({ peer = true })
    eq(true, vim.list_contains(new_servs, peer_addr))
    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_)
        return vim
          .system(cmd_, {
            text = true,
            env = vim.tbl_extend(
              'force',
              -- Avoid noise in the logs; we expect failures for these tests.
              { NVIM_LOG_FILE = testlog },

Review comment (security):
This feels a bit hacky but it does work. However if `XDG_RUNTIME_DIR` is set (to something like `/run/user/1000`, I think ubuntu sets this by default), the server might not launch because of permissions. I'm unsure how exactly to tackle this.

Code after:

    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 old_servs_num = #fn.serverlist({ peer = true })
    local peer_temp = n.new_pipename()
    local peer_name = peer_temp:match('[^/]*$')

    local tmp_dir2 = assert(vim.uv.fs_mkdtemp(vim.fs.dirname(t.tmpname(false)) .. '/XXXXXX'))
    local peer_addr = ('%s/%s'):format(tmp_dir2, peer_name)
    -- Set XDG_RUNTIME_DIR to a temp dir in this session to properly test serverlist({peer = true}). See #35492
    local client = n.new_session(true, {
      args = { '--clean', '--listen', peer_addr, '--embed' },
      env = { XDG_RUNTIME_DIR = tmp_dir2 },
      merge = false,
    })
    n.set_session(client)
    eq(peer_addr, fn.serverlist()[1])

    n.set_session(current_server)

    new_servs = fn.serverlist({ peer = true })
    local servers_without_peer = fn.serverlist()
    eq(true, vim.list_contains(new_servs, peer_addr))
    eq(true, #servers_without_peer < #new_servs)
    eq(true, old_servs_num < #new_servs)
    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_)
        return vim