012_directory_spec.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 helpers = require('test.functional.helpers')(after_each)
  6. local lfs = require('lfs')
  7. local eq = helpers.eq
  8. local neq = helpers.neq
  9. local poke_eventloop = helpers.poke_eventloop
  10. local funcs = helpers.funcs
  11. local meths = helpers.meths
  12. local clear = helpers.clear
  13. local insert = helpers.insert
  14. local command = helpers.command
  15. local write_file = helpers.write_file
  16. local curbufmeths = helpers.curbufmeths
  17. local function ls_dir_sorted(dirname)
  18. local files = {}
  19. for f in lfs.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. lfs.mkdir('Xtest.je')
  37. lfs.mkdir('Xtest2')
  38. write_file('Xtest2/Xtest3', text)
  39. clear()
  40. end)
  41. teardown(function()
  42. command('qall!')
  43. helpers.rmdir('Xtest.je')
  44. helpers.rmdir('Xtest2')
  45. os.remove('Xtest1')
  46. end)
  47. it('is working', function()
  48. insert([[
  49. start of testfile
  50. line 2 Abcdefghij
  51. line 3 Abcdefghij
  52. end of testfile]])
  53. meths.set_option('swapfile', true)
  54. curbufmeths.set_option('swapfile', true)
  55. meths.set_option('directory', '.')
  56. -- sanity check: files should not exist yet.
  57. eq(nil, lfs.attributes('.Xtest1.swp'))
  58. command('edit! Xtest1')
  59. poke_eventloop()
  60. eq('Xtest1', funcs.buffer_name('%'))
  61. -- Verify that the swapfile exists. In the legacy test this was done by
  62. -- reading the output from :!ls.
  63. neq(nil, lfs.attributes('.Xtest1.swp'))
  64. meths.set_option('directory', './Xtest2,.')
  65. command('edit Xtest1')
  66. poke_eventloop()
  67. -- swapfile should no longer exist in CWD.
  68. eq(nil, lfs.attributes('.Xtest1.swp'))
  69. eq({ "Xtest1.swp", "Xtest3" }, ls_dir_sorted("Xtest2"))
  70. meths.set_option('directory', 'Xtest.je')
  71. command('edit Xtest2/Xtest3')
  72. eq(true, curbufmeths.get_option('swapfile'))
  73. poke_eventloop()
  74. eq({ "Xtest3" }, ls_dir_sorted("Xtest2"))
  75. eq({ "Xtest3.swp" }, ls_dir_sorted("Xtest.je"))
  76. end)
  77. end)