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

Code before:
  it('prefers XDG_CONFIG_DIRS over VIM', function()
    clear {
      args = { '--cmd', 'set nomore undodir=. directory=. belloff=' },
      args_rm = { '-u', '--cmd' },
      env = { HOME = xhome, XDG_CONFIG_DIRS = xdgdir, VIM = vimdir },
    }
    eq(
      'loaded 1 xdg 1 vim 0',
      eval('printf("loaded %d xdg %d vim %d", g:loaded, get(g:, "xdg", 0), get(g:, "vim", 0))')
    )
  end)

  it('uses VIM if XDG_CONFIG_DIRS unset', function()
    clear {
      args = { '--cmd', 'set nomore undodir=. directory=. belloff=' },
      args_rm = { '-u', '--cmd' },
      env = { HOME = xhome, XDG_CONFIG_DIRS = '', VIM = vimdir },
    }
    eq(
      'loaded 1 xdg 0 vim 1',
      eval('printf("loaded %d xdg %d vim %d", g:loaded, get(g:, "xdg", 0), get(g:, "vim", 0))')
    )
  end)
end)

describe('sysinit with NVIM_APPNAME', function()
  local xdgdir = 'Xxdg'
  local xhome = 'Xhome'
  local pathsep = n.get_pathsep()
  local appname = 'mysysinitapp'

  before_each(function()
    rmdir(xdgdir)
    rmdir(xhome)

    mkdir(xdgdir)
    mkdir(xdgdir .. pathsep .. appname)
    write_file(
      table.concat({ xdgdir, appname, 'sysinit.vim' }, pathsep),
      [[
      let g:loaded = get(g:, "loaded", 0) + 1
      let g:xdg = 1
    ]]
    )

    mkdir(xhome)
  end)

  after_each(function()
    rmdir(xdgdir)
    rmdir(xhome)

Review comment (suggestion):
related tests live here: https://github.com/neovim/neovim/blob/f1dcde6cba3b2d69787f852a379ae2f7bc91c53d/test/functional/core/startup_spec.lua#L1134

can we use that block instead of duplicating the before_each/after_each things here.

the new test code is the main cost of this PR, if we can minimize it, and especially avoid duplication, then we are close to the finish line.

Code after:
  it('prefers XDG_CONFIG_DIRS over VIM', function()
    clear {
      args = { '--cmd', 'set nomore undodir=. directory=. belloff=' },
      args_rm = { '-u', '--cmd' },
      env = { HOME = xhome, XDG_CONFIG_DIRS = xdgdir, VIM = vimdir },
    }
    eq(
      'loaded 1 xdg 1 vim 0',
      eval('printf("loaded %d xdg %d vim %d", g:loaded, get(g:, "xdg", 0), get(g:, "vim", 0))')
    )
  end)

  it('uses VIM if XDG_CONFIG_DIRS unset', function()
    clear {
      args = { '--cmd', 'set nomore undodir=. directory=. belloff=' },
      args_rm = { '-u', '--cmd' },
      env = { HOME = xhome, XDG_CONFIG_DIRS = '', VIM = vimdir },
    }
    eq(
      'loaded 1 xdg 0 vim 1',
      eval('printf("loaded %d xdg %d vim %d", g:loaded, get(g:, "xdg", 0), get(g:, "vim", 0))')
    )
  end)

  it('respects NVIM_APPNAME in XDG_CONFIG_DIRS', function()
    local appname = 'mysysinitapp'
    mkdir(xdgdir .. pathsep .. appname)
    write_file(
      table.concat({ xdgdir, appname, 'sysinit.vim' }, pathsep),
      [[let g:appname_sysinit = 1]]
    )
    clear {
      args_rm = { '-u' },
      env = { HOME = xhome, XDG_CONFIG_DIRS = xdgdir, NVIM_APPNAME = appname },
    }
    eq(1, eval('g:appname_sysinit'))
    -- Should not load from nvim/ subdir (which has the default sysinit.vim from before_each)
    eq(0, eval('get(g:, "xdg", 0)'))
  end)
end)

describe('user config init', function()
  local xhome = 'Xhome'
  local pathsep = n.get_pathsep()
  local xconfig = xhome .. pathsep .. 'Xconfig'
  local xdata = xhome .. pathsep .. 'Xdata'
  local init_lua_path = table.concat({ xconfig, 'nvim', 'init.lua' }, pathsep)
  local xenv = { XDG_CONFIG_HOME = xconfig, XDG_DATA_HOME = xdata }

  before_each(function()
    rmdir(xhome)