robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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