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