robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
8.0 KiB316 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, setup, teardown =
5  t.describe, t.it, t.before_each, t.setup, t.teardown
6local clear = n.clear
7local command = n.command
8local eq = t.eq
9local pathsep = n.get_pathsep()
10local fn = n.fn
11local api = n.api
12local feed = n.feed
13
14local testdir = 'Xtest-editorconfig'
15
16--- @param name string
17--- @param expected table<string,any>
18local function test_case(name, expected)
19  local filename = testdir .. pathsep .. name
20  command('edit ' .. filename)
21
22  for opt, val in pairs(expected) do
23    local opt_info = api.nvim_get_option_info2(opt, {})
24    if opt_info.scope == 'win' then
25      eq(val, api.nvim_get_option_value(opt, { win = 0 }), name)
26    elseif opt_info.scope == 'buf' then
27      eq(val, api.nvim_get_option_value(opt, { buf = 0 }), name)
28    else
29      eq(val, api.nvim_get_option_value(opt, {}), name)
30    end
31  end
32end
33
34setup(function()
35  n.mkdir_p(testdir)
36  t.write_file(
37    testdir .. pathsep .. '.editorconfig',
38    [[
39    root = true
40
41    [3_space.txt]
42    indent_style = space
43    indent_size = 3
44    tab_width = 3
45
46    [4_space.py]
47    indent_style = space
48    indent_size = 4
49    tab_width = 8
50
51    [space.txt]
52    indent_style = space
53    indent_size = tab
54
55    [tab.txt]
56    indent_style = tab
57
58    [4_tab.txt]
59    indent_style = tab
60    indent_size = 4
61    tab_width = 4
62
63    [4_tab_width_of_8.txt]
64    indent_style = tab
65    indent_size = 4
66    tab_width = 8
67
68    [lf.txt]
69    end_of_line = lf
70
71    [crlf.txt]
72    end_of_line = crlf
73
74    [cr.txt]
75    end_of_line = cr
76
77    [utf-8.txt]
78    charset = utf-8
79
80    [utf-8-bom.txt]
81    charset = utf-8-bom
82
83    [utf-16be.txt]
84    charset = utf-16be
85
86    [utf-16le.txt]
87    charset = utf-16le
88
89    [latin1.txt]
90    charset = latin1
91
92    [with_newline.txt]
93    insert_final_newline = true
94
95    [without_newline.txt]
96    insert_final_newline = false
97
98    [trim.txt]
99    trim_trailing_whitespace = true
100
101    [no_trim.txt]
102    trim_trailing_whitespace = false
103
104    [skip_trim_when_insert.txt]
105    trim_trailing_whitespace = true
106
107    [skip_trim_when_replace.txt]
108    trim_trailing_whitespace = true
109
110    [skip_trim_when_insert_normal.txt]
111    trim_trailing_whitespace = true
112
113    [max_line_length.txt]
114    max_line_length = 42
115
116    [short_spelling_language.txt]
117    spelling_language = de
118
119    [long_spelling_language.txt]
120    spelling_language = en-NZ
121
122    [custom_properties.txt]
123    property1 = something1
124    property2 = x[something2]x
125    ]]
126  )
127end)
128
129teardown(function()
130  n.rmdir(testdir)
131end)
132
133describe('editorconfig', function()
134  before_each(function()
135    -- Use --clean so that plugins (i.e. editorconfig.lua) are loaded
136    clear({ args = { '--clean' } })
137  end)
138
139  it('stops at root even when a child sets root=false', function()
140    local root = testdir .. pathsep .. 'root'
141    local child = root .. pathsep .. 'child'
142    n.mkdir_p(child)
143    t.write_file(root .. pathsep .. '.editorconfig', 'root = true\n[*]\nindent_style = space\n')
144    t.write_file(child .. pathsep .. '.editorconfig', 'root = false\n[*]\ntab_width = 5\n')
145
146    command('set shiftwidth=4')
147    -- The outer .editorconfig sets indent_size=3 for this filename.
148    test_case('root' .. pathsep .. 'child' .. pathsep .. '3_space.txt', {
149      expandtab = true,
150      shiftwidth = 4,
151      tabstop = 5,
152    })
153    eq({ indent_style = 'space', tab_width = '5' }, fn.luaeval('vim.b.editorconfig'))
154  end)
155
156  it('sets indent options', function()
157    test_case('3_space.txt', {
158      expandtab = true,
159      shiftwidth = 3,
160      softtabstop = -1,
161      tabstop = 3,
162    })
163
164    test_case('4_space.py', {
165      expandtab = true,
166      shiftwidth = 4,
167      softtabstop = -1,
168      tabstop = 8,
169    })
170
171    test_case('space.txt', {
172      expandtab = true,
173      shiftwidth = 0,
174      softtabstop = 0,
175    })
176
177    test_case('tab.txt', {
178      expandtab = false,
179      shiftwidth = 0,
180      softtabstop = 0,
181    })
182
183    test_case('4_tab.txt', {
184      expandtab = false,
185      shiftwidth = 4,
186      softtabstop = -1,
187      tabstop = 4,
188    })
189
190    test_case('4_tab_width_of_8.txt', {
191      expandtab = false,
192      shiftwidth = 4,
193      softtabstop = -1,
194      tabstop = 8,
195    })
196  end)
197
198  it('sets end-of-line options', function()
199    test_case('lf.txt', { fileformat = 'unix' })
200    test_case('crlf.txt', { fileformat = 'dos' })
201    test_case('cr.txt', { fileformat = 'mac' })
202  end)
203
204  it('sets encoding options', function()
205    test_case('utf-8.txt', { fileencoding = 'utf-8', bomb = false })
206    test_case('utf-8-bom.txt', { fileencoding = 'utf-8', bomb = true })
207    test_case('utf-16be.txt', { fileencoding = 'utf-16', bomb = false })
208    test_case('utf-16le.txt', { fileencoding = 'utf-16le', bomb = false })
209    test_case('latin1.txt', { fileencoding = 'latin1', bomb = false })
210  end)
211
212  it('sets newline options', function()
213    test_case('with_newline.txt', { fixendofline = true })
214    test_case('without_newline.txt', { fixendofline = false })
215  end)
216
217  it('respects trim_trailing_whitespace', function()
218    local filename = testdir .. pathsep .. 'trim.txt'
219    -- luacheck: push ignore 613
220    local untrimmed = [[
221This line ends in whitespace 
222So does this one    
223And this one         
224But not this one
225]]
226    -- luacheck: pop
227    local trimmed = untrimmed:gsub('%s+\n', '\n')
228
229    t.write_file(filename, untrimmed)
230    command('edit ' .. filename)
231    command('write')
232    command('bdelete')
233    eq(trimmed, t.read_file(filename))
234
235    filename = testdir .. pathsep .. 'no_trim.txt'
236    t.write_file(filename, untrimmed)
237    command('edit ' .. filename)
238    command('write')
239    command('bdelete')
240    eq(untrimmed, t.read_file(filename))
241
242    filename = testdir .. pathsep .. 'skip_trim_when_insert.txt'
243    t.write_file(filename, untrimmed)
244    command('edit ' .. filename)
245    command('startinsert')
246    command('write')
247    command('bdelete')
248    eq(untrimmed, t.read_file(filename))
249
250    filename = testdir .. pathsep .. 'skip_trim_when_replace.txt'
251    t.write_file(filename, untrimmed)
252    command('edit ' .. filename)
253    command('startreplace')
254    command('write')
255    command('bdelete')
256    eq(untrimmed, t.read_file(filename))
257
258    filename = testdir .. pathsep .. 'skip_trim_when_insert_normal.txt'
259    t.write_file(filename, untrimmed)
260    command('edit ' .. filename)
261    command('startinsert')
262    feed('<C-o>:write<CR>')
263    command('bdelete')
264    eq(untrimmed, t.read_file(filename))
265  end)
266
267  it('sets textwidth', function()
268    test_case('max_line_length.txt', { textwidth = 42 })
269  end)
270
271  it('can be disabled globally', function()
272    api.nvim_set_var('editorconfig', false)
273    api.nvim_set_option_value('shiftwidth', 42, {})
274    test_case('3_space.txt', { shiftwidth = 42 })
275  end)
276
277  it('can be disabled per-buffer', function()
278    api.nvim_set_option_value('shiftwidth', 42, {})
279    local bufnr = fn.bufadd(testdir .. pathsep .. '3_space.txt')
280    api.nvim_buf_set_var(bufnr, 'editorconfig', false)
281    test_case('3_space.txt', { shiftwidth = 42 })
282    test_case('4_space.py', { shiftwidth = 4 })
283  end)
284
285  it('does not operate on invalid buffers', function()
286    local ok, err = unpack(n.exec_lua(function()
287      vim.cmd.edit('test.txt')
288      local bufnr = vim.api.nvim_get_current_buf()
289      vim.cmd.bwipeout(bufnr)
290      return { pcall(require('editorconfig').config, bufnr) }
291    end))
292
293    eq(true, ok, err)
294  end)
295
296  it('sets spelllang', function()
297    test_case('short_spelling_language.txt', { spelllang = 'de' })
298    test_case('long_spelling_language.txt', { spelllang = 'en_nz' })
299  end)
300
301  it('set custom properties', function()
302    n.exec_lua(function()
303      local editorconfig = require('editorconfig')
304      editorconfig.properties.property1 = function(bufnr, val, _opts)
305        vim.b[bufnr].property1 = val
306      end
307      editorconfig.properties.property2 = function(bufnr, val, _opts)
308        vim.b[bufnr].property2 = val
309      end
310    end)
311
312    command('edit ' .. testdir .. pathsep .. 'custom_properties.txt')
313    eq('something1', fn.luaeval('vim.b.property1'), 'property1')
314    eq('x[something2]x', fn.luaeval('vim.b.property2'), 'property2')
315  end)
316end)