diagnostics_spec.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. local mock = require("luassert.mock")
  2. local stub = require("luassert.stub")
  3. local diagnostics = require("cspell.diagnostics")
  4. local code_actions = require("cspell.code_actions")
  5. local helpers = require("cspell.helpers")
  6. local uv = vim.loop
  7. local CSPELL_CONFIG_PATH = uv.fs_realpath("./cspell.json")
  8. mock(require("null-ls.logger"), true)
  9. describe("diagnostics", function()
  10. local content = [[local misspelled_variabl = 'hi']]
  11. describe("parser", function()
  12. local parser = diagnostics._opts.on_output
  13. it("should create a diagnostic", function()
  14. local output = "/some/path/file.lua:1:18 - Unknown word (variabl)"
  15. local diagnostic = parser(output, { content = content })
  16. assert.same({
  17. col = "18",
  18. message = "Unknown word (variabl)",
  19. row = "1",
  20. }, diagnostic)
  21. end)
  22. it("includes suggestions", function()
  23. local output =
  24. "/some/path/file.lua:1:18 - Unknown word (variabl) Suggestions: [variable, variably, variables, variant, variate]"
  25. local diagnostic = parser(output, { content = content })
  26. assert.same({
  27. col = "18",
  28. message = "Unknown word (variabl)",
  29. row = "1",
  30. user_data = {
  31. misspelled = "variabl",
  32. suggestions = { "variable", "variably", "variables", "variant", "variate" },
  33. },
  34. }, diagnostic)
  35. end)
  36. end)
  37. describe("args", function()
  38. local args
  39. local get_source
  40. local async_get_config_info
  41. local args_fn = diagnostics._opts.args
  42. describe("without code actions", function()
  43. before_each(function()
  44. helpers.clear_cache()
  45. async_get_config_info = stub(helpers, "async_get_config_info")
  46. get_source = stub(require("null-ls.sources"), "get")
  47. get_source.returns({})
  48. args = args_fn({
  49. ft = "lua",
  50. bufname = "file.txt",
  51. get_config = function()
  52. return {}
  53. end,
  54. })
  55. end)
  56. after_each(function()
  57. get_source:revert()
  58. async_get_config_info:revert()
  59. end)
  60. it("won't try to load the cspell config file", function()
  61. assert.stub(async_get_config_info).was_not_called()
  62. end)
  63. it("does not include a suggestions param", function()
  64. assert.same({
  65. "-c",
  66. CSPELL_CONFIG_PATH,
  67. "lint",
  68. "--language-id",
  69. "lua",
  70. "stdin://file.txt",
  71. }, args)
  72. end)
  73. end)
  74. describe("with code actions", function()
  75. before_each(function()
  76. helpers.clear_cache()
  77. async_get_config_info = stub(helpers, "async_get_config_info")
  78. get_source = stub(require("null-ls.sources"), "get")
  79. get_source.returns({ code_actions })
  80. args = args_fn({
  81. ft = "lua",
  82. bufname = "file.txt",
  83. get_config = function()
  84. return {}
  85. end,
  86. })
  87. end)
  88. after_each(function()
  89. get_source:revert()
  90. async_get_config_info:revert()
  91. end)
  92. it("warms up the config cache", function()
  93. assert.stub(async_get_config_info).was_called()
  94. end)
  95. it("includes a suggestions param", function()
  96. assert.same({
  97. "--show-suggestions",
  98. "-c",
  99. CSPELL_CONFIG_PATH,
  100. "lint",
  101. "--language-id",
  102. "lua",
  103. "stdin://file.txt",
  104. }, args)
  105. end)
  106. end)
  107. describe("with custom json config file", function()
  108. before_each(function()
  109. helpers.clear_cache()
  110. async_get_config_info = stub(helpers, "async_get_config_info")
  111. get_source = stub(require("null-ls.sources"), "get")
  112. get_source.returns({})
  113. end)
  114. after_each(function()
  115. get_source:revert()
  116. async_get_config_info:revert()
  117. end)
  118. it("includes a suggestions param", function()
  119. args = args_fn({
  120. ft = "lua",
  121. bufname = "file.txt",
  122. get_config = function()
  123. return {
  124. find_json = function()
  125. return "some/custom/path/cspell.json"
  126. end,
  127. }
  128. end,
  129. })
  130. assert.same({
  131. "-c",
  132. "some/custom/path/cspell.json",
  133. "lint",
  134. "--language-id",
  135. "lua",
  136. "stdin://file.txt",
  137. }, args)
  138. end)
  139. end)
  140. end)
  141. end)