loader_spec.lua 2.6 KB

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