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

Code before:
      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

    -- Unset XDG_RUNTIME_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 = nil },
      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 (bug):
why didn't the asserts change? we should assert that "greater than x" servers were found. one reason why the bug went unnoticed was because we weren't doing that. we should assert both case. the `peer=true` case should find more servers than the `peer=false` case.

and this should not require a lot of code.

Code after:
    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
          .system(cmd_, {
            text = true,
            env = vim.tbl_extend(
              'force',
              -- Avoid noise in the logs; we expect failures for these tests.
              { NVIM_LOG_FILE = testlog },