loader_spec.lua 2.4 KB

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