neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

spellfile_spec.lua (4537B)


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