dirchanged_spec.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. local lfs = require('lfs')
  2. local h = require('test.functional.helpers')(after_each)
  3. local clear = h.clear
  4. local command = h.command
  5. local eq = h.eq
  6. local eval = h.eval
  7. local request = h.request
  8. describe('autocmd DirChanged', function()
  9. local curdir = string.gsub(lfs.currentdir(), '\\', '/')
  10. local dirs = {
  11. curdir .. '/Xtest-functional-autocmd-dirchanged.dir1',
  12. curdir .. '/Xtest-functional-autocmd-dirchanged.dir2',
  13. curdir .. '/Xtest-functional-autocmd-dirchanged.dir3',
  14. }
  15. setup(function() for _, dir in pairs(dirs) do h.mkdir(dir) end end)
  16. teardown(function() for _, dir in pairs(dirs) do h.rmdir(dir) end end)
  17. before_each(function()
  18. clear()
  19. command('autocmd DirChanged * let [g:getcwd, g:ev, g:amatch, g:cdcount] '
  20. ..' = [getcwd(), copy(v:event), expand("<amatch>"), 1 + get(g:, "cdcount", 0)]')
  21. -- Normalize path separators.
  22. command([[autocmd DirChanged * let g:ev['cwd'] = substitute(g:ev['cwd'], '\\', '/', 'g')]])
  23. command([[autocmd DirChanged * let g:getcwd = substitute(g:getcwd, '\\', '/', 'g')]])
  24. end)
  25. it('sets v:event', function()
  26. command('lcd '..dirs[1])
  27. eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
  28. eq(1, eval('g:cdcount'))
  29. command('tcd '..dirs[2])
  30. eq({cwd=dirs[2], scope='tab'}, eval('g:ev'))
  31. eq(2, eval('g:cdcount'))
  32. command('cd '..dirs[3])
  33. eq({cwd=dirs[3], scope='global'}, eval('g:ev'))
  34. eq(3, eval('g:cdcount'))
  35. end)
  36. it('sets getcwd() during event #6260', function()
  37. command('lcd '..dirs[1])
  38. eq(dirs[1], eval('g:getcwd'))
  39. command('tcd '..dirs[2])
  40. eq(dirs[2], eval('g:getcwd'))
  41. command('cd '..dirs[3])
  42. eq(dirs[3], eval('g:getcwd'))
  43. end)
  44. it('disallows recursion', function()
  45. command('set shellslash')
  46. -- Set up a _nested_ handler.
  47. command('autocmd DirChanged * nested lcd '..dirs[3])
  48. command('lcd '..dirs[1])
  49. eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
  50. eq(1, eval('g:cdcount'))
  51. -- autocmd changed to dirs[3], but did NOT trigger another DirChanged.
  52. eq(dirs[3], eval('getcwd()'))
  53. end)
  54. it('sets <amatch> to CWD "scope"', function()
  55. command('lcd '..dirs[1])
  56. eq('window', eval('g:amatch'))
  57. command('tcd '..dirs[2])
  58. eq('tab', eval('g:amatch'))
  59. command('cd '..dirs[3])
  60. eq('global', eval('g:amatch'))
  61. end)
  62. it('does not trigger if :cd fails', function()
  63. command('let g:ev = {}')
  64. local status1, err1 = pcall(function()
  65. command('lcd '..dirs[1] .. '/doesnotexist')
  66. end)
  67. eq({}, eval('g:ev'))
  68. local status2, err2 = pcall(function()
  69. command('lcd '..dirs[2] .. '/doesnotexist')
  70. end)
  71. eq({}, eval('g:ev'))
  72. local status3, err3 = pcall(function()
  73. command('lcd '..dirs[3] .. '/doesnotexist')
  74. end)
  75. eq({}, eval('g:ev'))
  76. eq(false, status1)
  77. eq(false, status2)
  78. eq(false, status3)
  79. eq('E344:', string.match(err1, "E%d*:"))
  80. eq('E344:', string.match(err2, "E%d*:"))
  81. eq('E344:', string.match(err3, "E%d*:"))
  82. end)
  83. it("is triggered by 'autochdir'", function()
  84. command('set autochdir')
  85. command('split '..dirs[1]..'/foo')
  86. eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
  87. command('split '..dirs[2]..'/bar')
  88. eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
  89. eq(2, eval('g:cdcount'))
  90. end)
  91. it("is triggered by switching to win/tab with different CWD #6054", function()
  92. command('lcd '..dirs[3]) -- window 3
  93. command('split '..dirs[2]..'/foo') -- window 2
  94. command('lcd '..dirs[2])
  95. command('split '..dirs[1]..'/bar') -- window 1
  96. command('lcd '..dirs[1])
  97. command('2wincmd w') -- window 2
  98. eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
  99. eq(4, eval('g:cdcount'))
  100. command('tabnew') -- tab 2 (tab-local CWD)
  101. eq(4, eval('g:cdcount')) -- same CWD, no DirChanged event
  102. command('tcd '..dirs[3])
  103. command('tabnext') -- tab 1 (no tab-local CWD)
  104. eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
  105. command('tabnext') -- tab 2
  106. eq({cwd=dirs[3], scope='tab'}, eval('g:ev'))
  107. eq(7, eval('g:cdcount'))
  108. command('tabnext') -- tab 1
  109. command('3wincmd w') -- window 3
  110. eq(9, eval('g:cdcount'))
  111. command('tabnext') -- tab 2 (has the *same* CWD)
  112. eq(9, eval('g:cdcount')) -- same CWD, no DirChanged event
  113. end)
  114. it('is triggered by nvim_set_current_dir()', function()
  115. request('nvim_set_current_dir', dirs[1])
  116. eq({cwd=dirs[1], scope='global'}, eval('g:ev'))
  117. request('nvim_set_current_dir', dirs[2])
  118. eq({cwd=dirs[2], scope='global'}, eval('g:ev'))
  119. local status, err = pcall(function()
  120. request('nvim_set_current_dir', '/doesnotexist')
  121. end)
  122. eq(false, status)
  123. eq('Failed to change directory', string.match(err, ': (.*)'))
  124. eq({cwd=dirs[2], scope='global'}, eval('g:ev'))
  125. end)
  126. it('works when local to buffer', function()
  127. command('let g:triggered = 0')
  128. command('autocmd DirChanged <buffer> let g:triggered = 1')
  129. command('cd '..dirs[1])
  130. eq(1, eval('g:triggered'))
  131. end)
  132. end)