editorconfig_spec.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local command = n.command
  5. local eq = t.eq
  6. local pathsep = n.get_pathsep()
  7. local fn = n.fn
  8. local api = n.api
  9. local testdir = 'Xtest-editorconfig'
  10. --- @param name string
  11. --- @param expected table<string,any>
  12. local function test_case(name, expected)
  13. local filename = testdir .. pathsep .. name
  14. command('edit ' .. filename)
  15. for opt, val in pairs(expected) do
  16. local opt_info = api.nvim_get_option_info2(opt, {})
  17. if opt_info.scope == 'win' then
  18. eq(val, api.nvim_get_option_value(opt, { win = 0 }), name)
  19. elseif opt_info.scope == 'buf' then
  20. eq(val, api.nvim_get_option_value(opt, { buf = 0 }), name)
  21. else
  22. eq(val, api.nvim_get_option_value(opt, {}), name)
  23. end
  24. end
  25. end
  26. setup(function()
  27. n.mkdir_p(testdir)
  28. t.write_file(
  29. testdir .. pathsep .. '.editorconfig',
  30. [[
  31. root = true
  32. [3_space.txt]
  33. indent_style = space
  34. indent_size = 3
  35. tab_width = 3
  36. [4_space.py]
  37. indent_style = space
  38. indent_size = 4
  39. tab_width = 8
  40. [space.txt]
  41. indent_style = space
  42. indent_size = tab
  43. [tab.txt]
  44. indent_style = tab
  45. [4_tab.txt]
  46. indent_style = tab
  47. indent_size = 4
  48. tab_width = 4
  49. [4_tab_width_of_8.txt]
  50. indent_style = tab
  51. indent_size = 4
  52. tab_width = 8
  53. [lf.txt]
  54. end_of_line = lf
  55. [crlf.txt]
  56. end_of_line = crlf
  57. [cr.txt]
  58. end_of_line = cr
  59. [utf-8.txt]
  60. charset = utf-8
  61. [utf-8-bom.txt]
  62. charset = utf-8-bom
  63. [utf-16be.txt]
  64. charset = utf-16be
  65. [utf-16le.txt]
  66. charset = utf-16le
  67. [latin1.txt]
  68. charset = latin1
  69. [with_newline.txt]
  70. insert_final_newline = true
  71. [without_newline.txt]
  72. insert_final_newline = false
  73. [trim.txt]
  74. trim_trailing_whitespace = true
  75. [no_trim.txt]
  76. trim_trailing_whitespace = false
  77. [max_line_length.txt]
  78. max_line_length = 42
  79. [short_spelling_language.txt]
  80. spelling_language = de
  81. [long_spelling_language.txt]
  82. spelling_language = en-NZ
  83. ]]
  84. )
  85. end)
  86. teardown(function()
  87. n.rmdir(testdir)
  88. end)
  89. describe('editorconfig', function()
  90. before_each(function()
  91. -- Remove -u NONE so that plugins (i.e. editorconfig.lua) are loaded
  92. clear({ args_rm = { '-u' } })
  93. end)
  94. it('sets indent options', function()
  95. test_case('3_space.txt', {
  96. expandtab = true,
  97. shiftwidth = 3,
  98. softtabstop = -1,
  99. tabstop = 3,
  100. })
  101. test_case('4_space.py', {
  102. expandtab = true,
  103. shiftwidth = 4,
  104. softtabstop = -1,
  105. tabstop = 8,
  106. })
  107. test_case('space.txt', {
  108. expandtab = true,
  109. shiftwidth = 0,
  110. softtabstop = 0,
  111. })
  112. test_case('tab.txt', {
  113. expandtab = false,
  114. shiftwidth = 0,
  115. softtabstop = 0,
  116. })
  117. test_case('4_tab.txt', {
  118. expandtab = false,
  119. shiftwidth = 4,
  120. softtabstop = -1,
  121. tabstop = 4,
  122. })
  123. test_case('4_tab_width_of_8.txt', {
  124. expandtab = false,
  125. shiftwidth = 4,
  126. softtabstop = -1,
  127. tabstop = 8,
  128. })
  129. end)
  130. it('sets end-of-line options', function()
  131. test_case('lf.txt', { fileformat = 'unix' })
  132. test_case('crlf.txt', { fileformat = 'dos' })
  133. test_case('cr.txt', { fileformat = 'mac' })
  134. end)
  135. it('sets encoding options', function()
  136. test_case('utf-8.txt', { fileencoding = 'utf-8', bomb = false })
  137. test_case('utf-8-bom.txt', { fileencoding = 'utf-8', bomb = true })
  138. test_case('utf-16be.txt', { fileencoding = 'utf-16', bomb = false })
  139. test_case('utf-16le.txt', { fileencoding = 'utf-16le', bomb = false })
  140. test_case('latin1.txt', { fileencoding = 'latin1', bomb = false })
  141. end)
  142. it('sets newline options', function()
  143. test_case('with_newline.txt', { fixendofline = true })
  144. test_case('without_newline.txt', { fixendofline = false })
  145. end)
  146. it('respects trim_trailing_whitespace', function()
  147. local filename = testdir .. pathsep .. 'trim.txt'
  148. -- luacheck: push ignore 613
  149. local untrimmed = [[
  150. This line ends in whitespace
  151. So does this one
  152. And this one
  153. But not this one
  154. ]]
  155. -- luacheck: pop
  156. local trimmed = untrimmed:gsub('%s+\n', '\n')
  157. t.write_file(filename, untrimmed)
  158. command('edit ' .. filename)
  159. command('write')
  160. command('bdelete')
  161. eq(trimmed, t.read_file(filename))
  162. filename = testdir .. pathsep .. 'no_trim.txt'
  163. t.write_file(filename, untrimmed)
  164. command('edit ' .. filename)
  165. command('write')
  166. command('bdelete')
  167. eq(untrimmed, t.read_file(filename))
  168. end)
  169. it('sets textwidth', function()
  170. test_case('max_line_length.txt', { textwidth = 42 })
  171. end)
  172. it('can be disabled globally', function()
  173. api.nvim_set_var('editorconfig', false)
  174. api.nvim_set_option_value('shiftwidth', 42, {})
  175. test_case('3_space.txt', { shiftwidth = 42 })
  176. end)
  177. it('can be disabled per-buffer', function()
  178. api.nvim_set_option_value('shiftwidth', 42, {})
  179. local bufnr = fn.bufadd(testdir .. pathsep .. '3_space.txt')
  180. api.nvim_buf_set_var(bufnr, 'editorconfig', false)
  181. test_case('3_space.txt', { shiftwidth = 42 })
  182. test_case('4_space.py', { shiftwidth = 4 })
  183. end)
  184. it('does not operate on invalid buffers', function()
  185. local ok, err = unpack(n.exec_lua(function()
  186. vim.cmd.edit('test.txt')
  187. local bufnr = vim.api.nvim_get_current_buf()
  188. vim.cmd.bwipeout(bufnr)
  189. return { pcall(require('editorconfig').config, bufnr) }
  190. end))
  191. eq(true, ok, err)
  192. end)
  193. it('sets spelllang', function()
  194. test_case('short_spelling_language.txt', { spelllang = 'de' })
  195. test_case('long_spelling_language.txt', { spelllang = 'en_nz' })
  196. end)
  197. end)