loader_spec.lua (2653B)
1 -- Test suite for testing interactions with API bindings 2 local t = require('test.testutil') 3 local n = require('test.functional.testnvim')() 4 5 local exec_lua = n.exec_lua 6 local command = n.command 7 local clear = n.clear 8 local eq = t.eq 9 10 describe('vim.loader', function() 11 before_each(clear) 12 13 it('can be disabled', function() 14 exec_lua(function() 15 local orig_loader = _G.loadfile 16 vim.loader.enable() 17 assert(orig_loader ~= _G.loadfile) 18 vim.loader.enable(false) 19 assert(orig_loader == _G.loadfile) 20 end) 21 end) 22 23 it('works with --luamod-dev #27413', function() 24 clear({ args = { '--luamod-dev' } }) 25 exec_lua(function() 26 vim.loader.enable() 27 28 require('vim.fs') 29 30 -- try to load other vim submodules as well (Nvim Lua stdlib) 31 for key, _ in pairs(vim._submodules) do 32 local modname = 'vim.' .. key -- e.g. "vim.fs" 33 34 local lhs = vim[key] 35 local rhs = require(modname) 36 assert( 37 lhs == rhs, 38 ('%s != require("%s"), %s != %s'):format(modname, modname, tostring(lhs), tostring(rhs)) 39 ) 40 end 41 end) 42 end) 43 44 it('handles changing files #23027', function() 45 exec_lua(function() 46 vim.loader.enable() 47 end) 48 49 local tmp = t.tmpname() 50 command('edit ' .. tmp) 51 52 eq( 53 1, 54 exec_lua(function() 55 vim.api.nvim_buf_set_lines(0, 0, -1, true, { '_G.TEST=1' }) 56 vim.cmd.write() 57 loadfile(tmp)() 58 return _G.TEST 59 end) 60 ) 61 62 -- fs latency 63 vim.uv.sleep(10) 64 65 eq( 66 2, 67 exec_lua(function() 68 vim.api.nvim_buf_set_lines(0, 0, -1, true, { '_G.TEST=2' }) 69 vim.cmd.write() 70 loadfile(tmp)() 71 return _G.TEST 72 end) 73 ) 74 end) 75 76 it('handles % signs in modpath #24491', function() 77 exec_lua [[ 78 vim.loader.enable() 79 ]] 80 81 local tmp = t.tmpname(false) 82 assert(t.mkdir(tmp)) 83 assert(t.mkdir(tmp .. '/%')) 84 local tmp1 = tmp .. '/%/x' 85 local tmp2 = tmp .. '/%%x' 86 87 t.write_file(tmp1, 'return 1', true) 88 t.write_file(tmp2, 'return 2', true) 89 vim.uv.fs_utime(tmp1, 0, 0) 90 vim.uv.fs_utime(tmp2, 0, 0) 91 eq(1, exec_lua('return loadfile(...)()', tmp1)) 92 eq(2, exec_lua('return loadfile(...)()', tmp2)) 93 end) 94 95 it('indents error message #29809', function() 96 local errmsg = exec_lua [[ 97 vim.loader.enable() 98 local _, errmsg = pcall(require, 'non_existent_module') 99 return errmsg 100 ]] 101 local errors = vim.split(errmsg, '\n') 102 eq("\tcache_loader: module 'non_existent_module' not found", errors[3]) 103 eq("\tcache_loader_lib: module 'non_existent_module' not found", errors[4]) 104 end) 105 end)