012_directory_spec.lua 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. -- Tests for 'directory' option.
  2. -- - ".", in same dir as file
  3. -- - "./dir", in directory relative to file
  4. -- - "dir", in directory relative to current dir
  5. local t = require('test.testutil')
  6. local n = require('test.functional.testnvim')()
  7. local eq = t.eq
  8. local neq = t.neq
  9. local poke_eventloop = n.poke_eventloop
  10. local fn = n.fn
  11. local api = n.api
  12. local clear = n.clear
  13. local insert = n.insert
  14. local command = n.command
  15. local write_file = t.write_file
  16. local mkdir = t.mkdir
  17. local function ls_dir_sorted(dirname)
  18. local files = {}
  19. for f in vim.fs.dir(dirname) do
  20. if f ~= '.' and f ~= '..' then
  21. table.insert(files, f)
  22. end
  23. end
  24. table.sort(files)
  25. return files
  26. end
  27. describe("'directory' option", function()
  28. setup(function()
  29. local text = [[
  30. start of testfile
  31. line 2 Abcdefghij
  32. line 3 Abcdefghij
  33. end of testfile
  34. ]]
  35. write_file('Xtest1', text)
  36. mkdir('Xtest.je')
  37. mkdir('Xtest2')
  38. write_file('Xtest2/Xtest3', text)
  39. clear()
  40. end)
  41. teardown(function()
  42. command('%bwipe!')
  43. api.nvim_set_option_value('swapfile', false, {})
  44. api.nvim_set_option_value('directory', '.', {})
  45. n.rmdir('Xtest.je')
  46. n.rmdir('Xtest2')
  47. os.remove('Xtest1')
  48. end)
  49. it('is working', function()
  50. insert([[
  51. start of testfile
  52. line 2 Abcdefghij
  53. line 3 Abcdefghij
  54. end of testfile]])
  55. api.nvim_set_option_value('swapfile', true, {})
  56. api.nvim_set_option_value('directory', '.', {})
  57. -- sanity check: files should not exist yet.
  58. eq(nil, vim.uv.fs_stat('.Xtest1.swp'))
  59. command('edit! Xtest1')
  60. poke_eventloop()
  61. eq('Xtest1', fn.buffer_name('%'))
  62. -- Verify that the swapfile exists. In the legacy test this was done by
  63. -- reading the output from :!ls.
  64. neq(nil, vim.uv.fs_stat('.Xtest1.swp'))
  65. api.nvim_set_option_value('directory', './Xtest2,.', {})
  66. command('edit Xtest1')
  67. poke_eventloop()
  68. -- swapfile should no longer exist in CWD.
  69. eq(nil, vim.uv.fs_stat('.Xtest1.swp'))
  70. eq({ 'Xtest1.swp', 'Xtest3' }, ls_dir_sorted('Xtest2'))
  71. api.nvim_set_option_value('directory', 'Xtest.je', {})
  72. command('bdelete')
  73. command('edit Xtest2/Xtest3')
  74. eq(true, api.nvim_get_option_value('swapfile', {}))
  75. poke_eventloop()
  76. eq({ 'Xtest3' }, ls_dir_sorted('Xtest2'))
  77. eq({ 'Xtest3.swp' }, ls_dir_sorted('Xtest.je'))
  78. end)
  79. end)