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

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

Review comment (suggestion):
there are probably better ways but in the worse case this could be
```suggestion
    local peer_addr = ('%s/../%s'):format(vim.fs.normalize(fn.stdpath('run')), peer_name)
```

format() is almost always better. `..` is clumsy.

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