neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

ftplugin_spec.lua (3592B)


      1 -- Tests for filetype-plugin behavior (files in runtime/ftplugin/*).
      2 
      3 local t = require('test.testutil')
      4 local n = require('test.functional.testnvim')()
      5 
      6 local exec_lua = n.exec_lua
      7 local command = n.command
      8 local eq = t.eq
      9 
     10 ---@param type string
     11 ---@return string
     12 local function stdpath(type)
     13  return exec_lua([[return vim.fs.abspath(vim.fn.stdpath(...))]], type)
     14 end
     15 
     16 ---@return string
     17 local function vimruntime()
     18  return exec_lua [[ return vim.fs.abspath(vim.env.VIMRUNTIME) ]]
     19 end
     20 
     21 ---@param module string
     22 ---@return string
     23 local function lua_includeexpr(module)
     24  return exec_lua([[return vim.fs.abspath(require 'vim._ftplugin.lua'.includeexpr(...))]], module)
     25 end
     26 
     27 describe("ftplugin: Lua 'includeexpr'", function()
     28  local repo_root = vim.fs.normalize(t.paths.test_source_path)
     29  local temp_dir = ''
     30 
     31  setup(function()
     32    temp_dir = t.tmpname(false)
     33    n.clear()
     34    n.api.nvim_set_current_dir(repo_root)
     35  end)
     36 
     37  teardown(function()
     38    n.expect_exit(n.command, 'qall!')
     39    n.rmdir(repo_root .. '/runtime/lua/foo/')
     40  end)
     41 
     42  before_each(function()
     43    command(([[
     44      edit `=stdpath('config') .. '/lua/user-foo/init.lua'`
     45      write ++p
     46      edit `=stdpath('config') .. '/lua/user-foo/bar.lua'`
     47      write ++p
     48      edit `=stdpath('data') .. '/site/pack/packer/start/plugin-foo/lua/plugin-foo/init.lua'`
     49      write ++p
     50      edit `=stdpath('data') .. '/site/pack/packer/start/plugin-foo/lua/plugin-foo/bar.lua'`
     51      write ++p
     52 
     53      edit runtime/lua/foo/init.lua
     54      write ++p
     55      edit runtime/lua/foo/bar/init.lua
     56      write ++p
     57 
     58      edit %s/lua/runtime-foo/init.lua
     59      write ++p
     60      edit %s/lua/runtime-foo/bar.lua
     61      write ++p
     62 
     63      edit %s/general-foo/bar/init.lua
     64      write ++p
     65      edit %s/general-foo/bar/baz.lua
     66      write ++p
     67    ]]):format(temp_dir, temp_dir, temp_dir, temp_dir))
     68  end)
     69 
     70  it('finds module in current repo', function()
     71    command [[ edit runtime/lua/vim/_ftplugin/lua.lua ]]
     72    eq(repo_root .. '/runtime/lua/vim/_ftplugin/lua.lua', lua_includeexpr('vim._ftplugin.lua'))
     73    eq(repo_root .. '/runtime/lua/editorconfig.lua', lua_includeexpr('editorconfig'))
     74    eq(repo_root .. '/runtime/lua/foo/init.lua', lua_includeexpr('foo'))
     75    eq(repo_root .. '/runtime/lua/foo/bar/init.lua', lua_includeexpr('foo.bar'))
     76  end)
     77 
     78  it('finds module in packpath/start', function()
     79    eq(
     80      stdpath('data') .. '/site/pack/packer/start/plugin-foo/lua/plugin-foo/init.lua',
     81      lua_includeexpr('plugin-foo')
     82    )
     83    eq(
     84      stdpath('data') .. '/site/pack/packer/start/plugin-foo/lua/plugin-foo/bar.lua',
     85      lua_includeexpr('plugin-foo.bar')
     86    )
     87  end)
     88 
     89  it('finds module in $VIMRUNTIME', function()
     90    command('edit ' .. repo_root)
     91    eq(vimruntime() .. '/lua/vim/_ftplugin/lua.lua', lua_includeexpr('vim._ftplugin.lua'))
     92    eq(vimruntime() .. '/lua/editorconfig.lua', lua_includeexpr('editorconfig'))
     93  end)
     94 
     95  it('finds module in runtimepath', function()
     96    eq(stdpath('config') .. '/lua/user-foo/init.lua', lua_includeexpr('user-foo'))
     97    eq(stdpath('config') .. '/lua/user-foo/bar.lua', lua_includeexpr('user-foo.bar'))
     98    command('set rtp+=' .. temp_dir)
     99    eq(temp_dir .. '/lua/runtime-foo/init.lua', lua_includeexpr('runtime-foo'))
    100    eq(temp_dir .. '/lua/runtime-foo/bar.lua', lua_includeexpr('runtime-foo.bar'))
    101  end)
    102 
    103  it('non-Nvim-style Lua modules', function()
    104    command('cd ' .. temp_dir)
    105    eq(temp_dir .. '/general-foo/bar/init.lua', lua_includeexpr('general-foo.bar'))
    106    eq(temp_dir .. '/general-foo/bar/baz.lua', lua_includeexpr('general-foo.bar.baz'))
    107    command('cd -')
    108  end)
    109 end)