summaryrefslogtreecommitdiffstatshomepage
path: root/test/functional/plugin/spellfile_spec.lua
blob: 38ffb6e6c09627a3212ac0cefa59bbba5235737a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
local n = require('test.functional.testnvim')()
local t = require('test.testutil')

local eq = t.eq
local neq = t.neq
local exec_lua = n.exec_lua

describe('nvim.spellfile', function()
  local data_root = 'Xtest_data_spellfile'
  local rtp_dir = 'Xtest_rtp_spellfile'

  before_each(function()
    n.clear()
    n.exec('set runtimepath+=' .. rtp_dir)
  end)
  after_each(function()
    n.rmdir(data_root)
    n.rmdir(rtp_dir)
  end)

  it('no-op when .spl and .sug already exist on runtimepath', function()
    local my_spell = vim.fs.joinpath(vim.fs.abspath(rtp_dir), 'spell')
    n.mkdir_p(my_spell)
    t.retry(nil, nil, function()
      assert(vim.uv.fs_stat(my_spell))
    end)
    t.write_file(my_spell .. '/en_gb.utf-8.spl', 'dummy')
    t.write_file(my_spell .. '/en_gb.utf-8.sug', 'dummy')

    local out = exec_lua(
      [[
      local rtp_dir = ...
      local s = require('nvim.spellfile')
      local my_spell = vim.fs.joinpath(vim.fs.abspath(rtp_dir), 'spell')

      vim.uv.fs_access = function(p, mode)
        return p == my_spell
      end

      local prompted = false
      vim.fn.input = function() prompted = true; return 'n' end

      local requests = 0
      vim.net.request = function(_, _, cb)
        requests = requests + 1
        cb()
      end

      s.get('en_gb')

      return { prompted = prompted, requests = requests }
    ]],
      rtp_dir
    )

    eq(false, out.prompted)
    eq(0, out.requests)
  end)

  it('downloads .spl to stdpath(data)/site/spell, .sug 404 is non-fatal, reloads', function()
    n.mkdir_p(rtp_dir)

    local out = exec_lua(
      [[
        local data_root = ...
        local s = require('nvim.spellfile')

        vim.fn.stdpath = function(k)
          assert(k == 'data')
          return data_root
        end

        vim.fn.input = function() return 'y' end

        local did_reload = false
        local orig_cmd = vim.cmd
        vim.cmd = function(cmd)
          if cmd:match('setlocal%s+spell!') then
            did_reload = true
          end
          return orig_cmd(cmd)
        end

        vim.net.request = function(url, opts, cb)
          local name = url:match('/([^/]+)$')
          if name and name:find('%.spl$') then
            vim.fn.mkdir(vim.fs.dirname(opts.outpath), 'p')
            vim.fn.writefile({'ok'}, opts.outpath)
            cb(nil, { status = 200 })
          else
            cb(nil, { status = 404 })
          end
        end

        s.get('en_gb')

        local spl = vim.fs.joinpath(data_root, 'site/spell/en_gb.utf-8.spl')
        local sug = vim.fs.joinpath(data_root, 'site/spell/en_gb.utf-8.sug')

        return {
          has_spl = vim.uv.fs_stat(spl) ~= nil,
          has_sug = vim.uv.fs_stat(sug) ~= nil,
          did_reload = did_reload,
        }
      ]],
      data_root
    )

    eq(true, out.has_spl)
    eq(false, out.has_sug)
    eq(true, out.did_reload)
  end)

  it('failure mode: 404 for all files => warn once, mark done, no reload', function()
    local out = exec_lua(
      [[
      local data_root = ...
      local s = require('nvim.spellfile')

      vim.fn.stdpath = function(k)
        assert(k == 'data')
        return data_root
      end

      vim.fn.input = function() return 'y' end

      local warns = 0
      vim.notify = function(_, lvl)
        if lvl and lvl >= vim.log.levels.WARN then warns = warns + 1 end
      end

      local did_reload = false
      local orig_cmd = vim.cmd
      vim.cmd = function(c)
        if c:match('setlocal%s+spell!') then
          did_reload = true
        end
        return orig_cmd(c)
      end

      vim.net.request = function(_, _, cb) cb(nil, { status = 404 }) end

      local info = s.get('zz')
      local done = s._done[info.key] == true

      return { warns = warns, done = done, did_reload = did_reload }
    ]],
      data_root
    )

    eq(1, out.warns)
    eq(true, out.done)
    eq(false, out.did_reload)
  end)

  it('no confirmation when using confirm = false', function()
    local out = exec_lua(
      [[
      local rtp_dir = ...
      local s = require('nvim.spellfile')

      vim.fn.input = function(...)
        error('prompt was triggered')
        return 'n'
      end

      local requests = 0
      vim.net.request = function(_, _, cb)
        requests = requests + 1
        cb()
      end

      s.config({ confirm = false })
      s.get('en_gb')

      -- Reset value
      s.config({ confirm = true })

      return { requests = requests }
    ]],
      rtp_dir
    )

    neq(0, out.requests)
  end)
end)