lsp_spec.lua 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. local utils = require "lvim.utils"
  2. local helpers = require "tests.lvim.helpers"
  3. local spy = require "luassert.spy"
  4. describe("lsp workflow", function()
  5. before_each(function()
  6. vim.cmd [[
  7. let v:errmsg = ""
  8. let v:errors = []
  9. ]]
  10. end)
  11. after_each(function()
  12. local errmsg = vim.fn.eval "v:errmsg"
  13. local exception = vim.fn.eval "v:exception"
  14. local errors = vim.fn.eval "v:errors"
  15. assert.equal("", errmsg)
  16. assert.equal("", exception)
  17. assert.True(vim.tbl_isempty(errors))
  18. end)
  19. lvim.lsp.templates_dir = join_paths(get_cache_dir(), "artifacts")
  20. vim.go.loadplugins = true
  21. local plugins = require "lvim.plugins"
  22. require("lvim.plugin-loader").load { plugins, lvim.plugins }
  23. -- trigger loading event manually for mason
  24. vim.api.nvim_exec_autocmds("User", { pattern = "FileOpened" })
  25. it("should be able to delete ftplugin templates", function()
  26. if utils.is_directory(lvim.lsp.templates_dir) then
  27. vim.fn.delete(lvim.lsp.templates_dir, "rf")
  28. vim.wait(100)
  29. end
  30. assert.False(utils.is_directory(lvim.lsp.templates_dir))
  31. end)
  32. it("should be able to generate ftplugin templates", function()
  33. if utils.is_directory(lvim.lsp.templates_dir) then
  34. vim.fn.delete(lvim.lsp.templates_dir, "rf")
  35. vim.wait(100)
  36. end
  37. require("lvim.lsp").setup()
  38. vim.wait(500)
  39. local template_files = vim.fn.glob(lvim.lsp.templates_dir .. "/*.lua", 1, 1)
  40. assert.False(vim.tbl_isempty(template_files))
  41. end)
  42. it("should not include blacklisted servers in the generated templates", function()
  43. require("lvim.lsp").setup()
  44. for _, server_name in ipairs(lvim.lsp.automatic_configuration.skipped_servers) do
  45. local setup_cmd = string.format([[require("lvim.lsp.manager").setup(%q)]], server_name)
  46. local _, stdout, _ = helpers.search_file(lvim.lsp.templates_dir, setup_cmd)
  47. assert.True(vim.tbl_isempty(stdout))
  48. end
  49. end)
  50. it("should only include one server per generated template", function()
  51. require("lvim.lsp").setup()
  52. vim.wait(500)
  53. local allowed_dupes = { "tailwindcss", "ruff" }
  54. local template_files = vim.fn.glob(lvim.lsp.templates_dir .. "/*.lua", 1, 1)
  55. for _, file in ipairs(template_files) do
  56. local content = {}
  57. for entry in io.lines(file) do
  58. local server_name = entry:match [[.*setup%("(.*)"%)]]
  59. if not vim.tbl_contains(allowed_dupes, server_name) then
  60. table.insert(content, server_name)
  61. end
  62. end
  63. local err_msg = ""
  64. if #content > 1 then
  65. err_msg = string.format(
  66. "found more than one server for [%q]: \n{\n %q \n}",
  67. file:match "[^/]*.lua$",
  68. table.concat(content, ", ")
  69. )
  70. end
  71. assert.equal(err_msg, "")
  72. end
  73. end)
  74. it("should not attempt to re-generate ftplugin templates", function()
  75. local s = spy.on(require "lvim.lsp.templates", "generate_templates")
  76. require("lvim.lsp").setup()
  77. assert.spy(s):was_not_called()
  78. s:revert()
  79. end)
  80. end)