robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
4.5 KiB187 linesraw
1local n = require('test.functional.testnvim')()
2local t = require('test.testutil')
3
4local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
5local eq = t.eq
6local neq = t.neq
7local exec_lua = n.exec_lua
8
9describe('nvim.spellfile', function()
10  local data_root = 'Xtest_data_spellfile'
11  local rtp_dir = 'Xtest_rtp_spellfile'
12
13  before_each(function()
14    n.clear()
15    n.exec('set runtimepath+=' .. rtp_dir)
16  end)
17  after_each(function()
18    n.rmdir(data_root)
19    n.rmdir(rtp_dir)
20  end)
21
22  it('no-op when .spl and .sug already exist on runtimepath', function()
23    local my_spell = vim.fs.joinpath(vim.fs.abspath(rtp_dir), 'spell')
24    n.mkdir_p(my_spell)
25    t.retry(nil, nil, function()
26      assert(vim.uv.fs_stat(my_spell))
27    end)
28    t.write_file(my_spell .. '/en_gb.utf-8.spl', 'dummy')
29    t.write_file(my_spell .. '/en_gb.utf-8.sug', 'dummy')
30
31    local out = exec_lua(
32      [[
33      local rtp_dir = ...
34      local s = require('nvim.spellfile')
35      local my_spell = vim.fs.joinpath(vim.fs.abspath(rtp_dir), 'spell')
36
37      vim.uv.fs_access = function(p, mode)
38        return p == my_spell
39      end
40
41      local prompted = false
42      vim.fn.input = function() prompted = true; return 'n' end
43
44      local requests = 0
45      vim.net.request = function(_, _, cb)
46        requests = requests + 1
47        cb()
48      end
49
50      s.get('en_gb')
51
52      return { prompted = prompted, requests = requests }
53    ]],
54      rtp_dir
55    )
56
57    eq(false, out.prompted)
58    eq(0, out.requests)
59  end)
60
61  it('downloads .spl to stdpath(data)/site/spell, .sug 404 is non-fatal, reloads', function()
62    n.mkdir_p(rtp_dir)
63
64    local out = exec_lua(
65      [[
66        local data_root = ...
67        local s = require('nvim.spellfile')
68
69        vim.fn.stdpath = function(k)
70          assert(k == 'data')
71          return data_root
72        end
73
74        vim.fn.input = function() return 'y' end
75
76        local did_reload = false
77        local orig_cmd = vim.cmd
78        vim.cmd = function(cmd)
79          if cmd:match('setlocal%s+spell!') then
80            did_reload = true
81          end
82          return orig_cmd(cmd)
83        end
84
85        vim.net.request = function(url, opts, cb)
86          local name = url:match('/([^/]+)$')
87          if name and name:find('%.spl$') then
88            vim.fn.mkdir(vim.fs.dirname(opts.outpath), 'p')
89            vim.fn.writefile({'ok'}, opts.outpath)
90            cb(nil, { status = 200 })
91          else
92            cb(nil, { status = 404 })
93          end
94        end
95
96        s.get('en_gb')
97
98        local spl = vim.fs.joinpath(data_root, 'site/spell/en_gb.utf-8.spl')
99        local sug = vim.fs.joinpath(data_root, 'site/spell/en_gb.utf-8.sug')
100
101        return {
102          has_spl = vim.uv.fs_stat(spl) ~= nil,
103          has_sug = vim.uv.fs_stat(sug) ~= nil,
104          did_reload = did_reload,
105        }
106      ]],
107      data_root
108    )
109
110    eq(true, out.has_spl)
111    eq(false, out.has_sug)
112    eq(true, out.did_reload)
113  end)
114
115  it('failure mode: 404 for all files => warn once, mark done, no reload', function()
116    local out = exec_lua(
117      [[
118      local data_root = ...
119      local s = require('nvim.spellfile')
120
121      vim.fn.stdpath = function(k)
122        assert(k == 'data')
123        return data_root
124      end
125
126      vim.fn.input = function() return 'y' end
127
128      local warns = 0
129      vim.notify = function(_, lvl)
130        if lvl and lvl >= vim.log.levels.WARN then warns = warns + 1 end
131      end
132
133      local did_reload = false
134      local orig_cmd = vim.cmd
135      vim.cmd = function(c)
136        if c:match('setlocal%s+spell!') then
137          did_reload = true
138        end
139        return orig_cmd(c)
140      end
141
142      vim.net.request = function(_, _, cb) cb(nil, { status = 404 }) end
143
144      local info = s.get('zz')
145      local done = s._done[info.key] == true
146
147      return { warns = warns, done = done, did_reload = did_reload }
148    ]],
149      data_root
150    )
151
152    eq(1, out.warns)
153    eq(true, out.done)
154    eq(false, out.did_reload)
155  end)
156
157  it('no confirmation when using confirm = false', function()
158    local out = exec_lua(
159      [[
160      local rtp_dir = ...
161      local s = require('nvim.spellfile')
162
163      vim.fn.input = function(...)
164        error('prompt was triggered')
165        return 'n'
166      end
167
168      local requests = 0
169      vim.net.request = function(_, _, cb)
170        requests = requests + 1
171        cb()
172      end
173
174      s.config({ confirm = false })
175      s.get('en_gb')
176
177      -- Reset value
178      s.config({ confirm = true })
179
180      return { requests = requests }
181    ]],
182      rtp_dir
183    )
184
185    neq(0, out.requests)
186  end)
187end)