robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
8.9 KiB192 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
5local eq = t.eq
6local pcall_err = t.pcall_err
7local clear = n.clear
8local api = n.api
9local fn = n.fn
10local rmdir = n.rmdir
11local write_file = t.write_file
12local mkdir = t.mkdir
13
14local testdir = 'Xtest-functional-spell-spellfile.d'
15
16describe('spellfile', function()
17  before_each(function()
18    clear({ env = { XDG_DATA_HOME = testdir .. '/xdg_data' } })
19    rmdir(testdir)
20    mkdir(testdir)
21    mkdir(testdir .. '/spell')
22  end)
23  after_each(function()
24    rmdir(testdir)
25  end)
26  --                   ┌ Magic string (#VIMSPELLMAGIC)
27  --                   │       ┌ Spell file version (#VIMSPELLVERSION)
28  local spellheader = 'VIMspell\050'
29  it('errors out when prefcond section is truncated', function()
30    api.nvim_set_option_value('runtimepath', testdir, {})
31    -- stylua: ignore
32    write_file(testdir .. '/spell/en.ascii.spl',
33    --                         ┌ Section identifier (#SN_PREFCOND)
34    --                         │   ┌ Section flags (#SNF_REQUIRED or zero)
35    --                         │   │   ┌ Section length (4 bytes, MSB first)
36               spellheader .. '\003\001\000\000\000\003'
37    --             ┌ Number of regexes in section (2 bytes, MSB first)
38    --             │       ┌ Condition length (1 byte)
39    --             │       │   ┌ Condition regex (missing!)
40               .. '\000\001\001')
41    api.nvim_set_option_value('spelllang', 'en', {})
42    t.matches('Vim%(set%):E758: Truncated spell file', pcall_err(n.command, 'set spell'))
43  end)
44  it('errors out when prefcond regexp contains NUL byte', function()
45    api.nvim_set_option_value('runtimepath', testdir, {})
46    -- stylua: ignore
47    write_file(testdir .. '/spell/en.ascii.spl',
48    --                         ┌ Section identifier (#SN_PREFCOND)
49    --                         │   ┌ Section flags (#SNF_REQUIRED or zero)
50    --                         │   │   ┌ Section length (4 bytes, MSB first)
51               spellheader .. '\003\001\000\000\000\008'
52    --             ┌ Number of regexes in section (2 bytes, MSB first)
53    --             │       ┌ Condition length (1 byte)
54    --             │       │   ┌ Condition regex
55    --             │       │   │       ┌ End of sections marker
56               .. '\000\001\005ab\000cd\255'
57    --             ┌ LWORDTREE tree length (4 bytes)
58    --             │               ┌ KWORDTREE tree length (4 bytes)
59    --             │               │               ┌ PREFIXTREE tree length
60               .. '\000\000\000\000\000\000\000\000\000\000\000\000')
61    api.nvim_set_option_value('spelllang', 'en', {})
62    t.matches('Vim%(set%):E759: Format error in spell file', pcall_err(n.command, 'set spell'))
63  end)
64  it('errors out when region contains NUL byte', function()
65    api.nvim_set_option_value('runtimepath', testdir, {})
66    -- stylua: ignore
67    write_file(testdir .. '/spell/en.ascii.spl',
68    --                         ┌ Section identifier (#SN_REGION)
69    --                         │   ┌ Section flags (#SNF_REQUIRED or zero)
70    --                         │   │   ┌ Section length (4 bytes, MSB first)
71               spellheader .. '\000\001\000\000\000\008'
72    --             ┌ Regions  ┌ End of sections marker
73               .. '01234\00067\255'
74    --             ┌ LWORDTREE tree length (4 bytes)
75    --             │               ┌ KWORDTREE tree length (4 bytes)
76    --             │               │               ┌ PREFIXTREE tree length
77               .. '\000\000\000\000\000\000\000\000\000\000\000\000')
78    api.nvim_set_option_value('spelllang', 'en', {})
79    t.matches('Vim%(set%):E759: Format error in spell file', pcall_err(n.command, 'set spell'))
80  end)
81  it('errors out when SAL section contains NUL byte', function()
82    api.nvim_set_option_value('runtimepath', testdir, {})
83    -- stylua: ignore
84    write_file(testdir .. '/spell/en.ascii.spl',
85    --                         ┌ Section identifier (#SN_SAL)
86    --                         │   ┌ Section flags (#SNF_REQUIRED or zero)
87    --                         │   │   ┌ Section length (4 bytes, MSB first)
88               spellheader .. '\005\001\000\000\000\008'
89    --             ┌ salflags
90    --             │   ┌ salcount (2 bytes, MSB first)
91    --             │   │       ┌ salfromlen (1 byte)
92    --             │   │       │   ┌ Special character
93    --             │   │       │   │┌ salfrom (should not contain NUL)
94    --             │   │       │   ││   ┌ saltolen
95    --             │   │       │   ││   │   ┌ salto
96    --             │   │       │   ││   │   │┌ End of sections marker
97               .. '\000\000\001\0024\000\0017\255'
98    --             ┌ LWORDTREE tree length (4 bytes)
99    --             │               ┌ KWORDTREE tree length (4 bytes)
100    --             │               │               ┌ PREFIXTREE tree length
101               .. '\000\000\000\000\000\000\000\000\000\000\000\000')
102    api.nvim_set_option_value('spelllang', 'en', {})
103    t.matches('Vim%(set%):E759: Format error in spell file', pcall_err(n.command, 'set spell'))
104  end)
105  it('errors out when spell header contains NUL bytes', function()
106    api.nvim_set_option_value('runtimepath', testdir, {})
107    write_file(testdir .. '/spell/en.ascii.spl', spellheader:sub(1, -3) .. '\000\000')
108    api.nvim_set_option_value('spelllang', 'en', {})
109    t.matches(
110      'Vim%(set%):E757: This does not look like a spell file',
111      pcall_err(n.command, 'set spell')
112    )
113  end)
114
115  it('can be set to a relative path', function()
116    local fname = testdir .. '/spell/spell.add'
117    api.nvim_set_option_value('spellfile', fname, {})
118  end)
119
120  it('can be set to an absolute path', function()
121    local fname = fn.fnamemodify(testdir .. '/spell/spell.add', ':p')
122    api.nvim_set_option_value('spellfile', fname, {})
123  end)
124
125  it('uses the current mkspellmem limits for each build', function()
126    local words = testdir .. '/words'
127    write_file(words, 'one\ntwo\nthree\n')
128    for _, case in ipairs({
129      { '16,16,1', 2 }, -- Compress while reading and at the end.
130      { '1000,50,10', 1 }, -- Compress only at the end.
131    }) do
132      api.nvim_set_option_value('mkspellmem', case[1], {})
133      local output = n.exec_capture('mkspell! ' .. testdir .. '/spell/test.spl ' .. words)
134      local _, count = output:gsub('Compressed case%-folded:', '')
135      eq(case[2], count)
136    end
137  end)
138
139  describe('default location', function()
140    it("is stdpath('data')/site/spell/en.utf-8.add", function()
141      n.command('set spell')
142      n.insert('abc')
143      n.feed('zg')
144      eq(
145        ('%s/site/spell/en.utf-8.add'):format(fn.stdpath('data')),
146        api.nvim_get_option_value('spellfile', {})
147      )
148    end)
149
150    it("is not set if stdpath('data') is not writable", function()
151      n.command('set spell')
152      fn.writefile({ '' }, testdir .. '/xdg_data')
153      n.insert('abc')
154      eq("Vim(normal):E764: Option 'spellfile' is not set", pcall_err(n.command, 'normal! zg'))
155    end)
156
157    it("is not set if 'spelllang' is not set", function()
158      n.command('set spell spelllang=')
159      n.insert('abc')
160      eq("Vim(normal):E764: Option 'spellfile' is not set", pcall_err(n.command, 'normal! zg'))
161    end)
162
163    it('accepts overwrites on midword and syllable section, errors on memory leak', function()
164      api.nvim_set_option_value('runtimepath', testdir, {})
165      -- stylua: ignore
166
167      -- Set SN_MIDWORD and SN_SYLLABLE twice so the loader overwrites lp->sl_midword. Should
168      -- not leak memory.
169      write_file(testdir .. '/spell/en.ascii.spl',
170                 spellheader
171    --            ┌ Section identifier (#SN_MIDWORD)
172    --            │   ┌ Section flags (#SNF_REQUIRED or zero)
173    --            │   │   ┌ Section length (4 bytes, MSB first)
174              .. '\002\001\000\000\000\003abc'  -- SN_MIDWORD (midword = "abc")
175              .. '\002\001\000\000\000\003def'  -- Overwrites SN_MIDWORD (midword = "def")
176    --            ┌ Section identifier (#SN_SYLLABLE)
177    --            │   ┌ Section flags (#SNF_REQUIRED or zero)
178    --            │   │   ┌ Section length (4 bytes, MSB first)
179              .. '\009\001\000\000\000\003ghi'  -- SN_SYLLABLE (syllable = "ghi")
180              .. '\009\001\000\000\000\003jib'  -- Overwrites SN_SYLLABLE (syllable = "jib")
181              .. '\255'                         -- End of sections marker
182              .. '\000\000\000\000'             -- LWORDTREE len
183              .. '\000\000\000\000'             -- KWORDTREE len
184              .. '\000\000\000\000'             -- PREFIXTREE len
185      )
186
187      api.nvim_set_option_value('spelllang', 'en', {})
188      n.command('set spell') -- Should not error
189      eq(true, api.nvim_get_option_value('spell', {}))
190    end)
191  end)
192end)