2017-01-11 10:09:30 -07:00
|
|
|
local lfs = require('lfs')
|
|
|
|
local h = require('test.functional.helpers')(after_each)
|
|
|
|
|
|
|
|
local clear = h.clear
|
|
|
|
local command = h.command
|
|
|
|
local eq = h.eq
|
|
|
|
local eval = h.eval
|
|
|
|
local request = h.request
|
|
|
|
|
2017-01-17 02:47:20 -07:00
|
|
|
describe('autocmd DirChanged', function()
|
|
|
|
local curdir = string.gsub(lfs.currentdir(), '\\', '/')
|
2017-01-11 10:09:30 -07:00
|
|
|
local dirs = {
|
|
|
|
curdir .. '/Xtest-functional-autocmd-dirchanged.dir1',
|
|
|
|
curdir .. '/Xtest-functional-autocmd-dirchanged.dir2',
|
|
|
|
curdir .. '/Xtest-functional-autocmd-dirchanged.dir3',
|
|
|
|
}
|
|
|
|
|
|
|
|
setup(function() for _, dir in pairs(dirs) do h.mkdir(dir) end end)
|
|
|
|
teardown(function() for _, dir in pairs(dirs) do h.rmdir(dir) end end)
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
2017-03-12 04:12:26 -07:00
|
|
|
command('autocmd DirChanged * let [g:getcwd, g:ev, g:amatch, g:cdcount] '
|
|
|
|
..' = [getcwd(), copy(v:event), expand("<amatch>"), 1 + get(g:, "cdcount", 0)]')
|
|
|
|
-- Normalize path separators.
|
|
|
|
command([[autocmd DirChanged * let g:ev['cwd'] = substitute(g:ev['cwd'], '\\', '/', 'g')]])
|
|
|
|
command([[autocmd DirChanged * let g:getcwd = substitute(g:getcwd, '\\', '/', 'g')]])
|
2017-01-11 10:09:30 -07:00
|
|
|
end)
|
|
|
|
|
2017-01-17 02:47:20 -07:00
|
|
|
it('sets v:event', function()
|
2017-01-11 10:09:30 -07:00
|
|
|
command('lcd '..dirs[1])
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
|
2017-01-17 02:47:20 -07:00
|
|
|
eq(1, eval('g:cdcount'))
|
2017-01-11 10:09:30 -07:00
|
|
|
|
|
|
|
command('tcd '..dirs[2])
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[2], scope='tab'}, eval('g:ev'))
|
2017-01-17 02:47:20 -07:00
|
|
|
eq(2, eval('g:cdcount'))
|
2017-01-11 10:09:30 -07:00
|
|
|
|
|
|
|
command('cd '..dirs[3])
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[3], scope='global'}, eval('g:ev'))
|
2017-01-17 02:47:20 -07:00
|
|
|
eq(3, eval('g:cdcount'))
|
2017-01-11 10:09:30 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-11 14:46:20 -07:00
|
|
|
it('sets getcwd() during event #6260', function()
|
|
|
|
command('lcd '..dirs[1])
|
2017-03-12 04:12:26 -07:00
|
|
|
eq(dirs[1], eval('g:getcwd'))
|
2017-03-11 14:46:20 -07:00
|
|
|
|
|
|
|
command('tcd '..dirs[2])
|
2017-03-12 04:12:26 -07:00
|
|
|
eq(dirs[2], eval('g:getcwd'))
|
2017-03-11 14:46:20 -07:00
|
|
|
|
|
|
|
command('cd '..dirs[3])
|
2017-03-12 04:12:26 -07:00
|
|
|
eq(dirs[3], eval('g:getcwd'))
|
2017-03-11 14:46:20 -07:00
|
|
|
end)
|
|
|
|
|
2017-01-17 02:47:20 -07:00
|
|
|
it('disallows recursion', function()
|
|
|
|
command('set shellslash')
|
|
|
|
-- Set up a _nested_ handler.
|
|
|
|
command('autocmd DirChanged * nested lcd '..dirs[3])
|
|
|
|
command('lcd '..dirs[1])
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
|
2017-01-17 02:47:20 -07:00
|
|
|
eq(1, eval('g:cdcount'))
|
|
|
|
-- autocmd changed to dirs[3], but did NOT trigger another DirChanged.
|
|
|
|
eq(dirs[3], eval('getcwd()'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('sets <amatch> to CWD "scope"', function()
|
|
|
|
command('lcd '..dirs[1])
|
2017-03-11 14:46:20 -07:00
|
|
|
eq('window', eval('g:amatch'))
|
2017-01-17 02:47:20 -07:00
|
|
|
|
|
|
|
command('tcd '..dirs[2])
|
2017-03-11 14:46:20 -07:00
|
|
|
eq('tab', eval('g:amatch'))
|
2017-01-17 02:47:20 -07:00
|
|
|
|
|
|
|
command('cd '..dirs[3])
|
2017-03-11 14:46:20 -07:00
|
|
|
eq('global', eval('g:amatch'))
|
2017-01-17 02:47:20 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('does not trigger if :cd fails', function()
|
2017-03-12 04:12:26 -07:00
|
|
|
command('let g:ev = {}')
|
2017-01-11 10:09:30 -07:00
|
|
|
|
|
|
|
local status1, err1 = pcall(function()
|
|
|
|
command('lcd '..dirs[1] .. '/doesnotexist')
|
|
|
|
end)
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({}, eval('g:ev'))
|
2017-01-11 10:09:30 -07:00
|
|
|
|
|
|
|
local status2, err2 = pcall(function()
|
|
|
|
command('lcd '..dirs[2] .. '/doesnotexist')
|
|
|
|
end)
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({}, eval('g:ev'))
|
2017-01-11 10:09:30 -07:00
|
|
|
|
|
|
|
local status3, err3 = pcall(function()
|
|
|
|
command('lcd '..dirs[3] .. '/doesnotexist')
|
|
|
|
end)
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({}, eval('g:ev'))
|
2017-01-11 10:09:30 -07:00
|
|
|
|
|
|
|
eq(false, status1)
|
|
|
|
eq(false, status2)
|
|
|
|
eq(false, status3)
|
|
|
|
|
2017-01-17 02:47:20 -07:00
|
|
|
eq('E344:', string.match(err1, "E%d*:"))
|
|
|
|
eq('E344:', string.match(err2, "E%d*:"))
|
|
|
|
eq('E344:', string.match(err3, "E%d*:"))
|
2017-01-11 10:09:30 -07:00
|
|
|
end)
|
|
|
|
|
2017-01-17 02:47:20 -07:00
|
|
|
it("is triggered by 'autochdir'", function()
|
2017-01-11 10:09:30 -07:00
|
|
|
command('set autochdir')
|
|
|
|
|
|
|
|
command('split '..dirs[1]..'/foo')
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
|
2017-01-11 10:09:30 -07:00
|
|
|
|
|
|
|
command('split '..dirs[2]..'/bar')
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
|
|
|
|
|
|
|
|
eq(2, eval('g:cdcount'))
|
2017-01-11 10:09:30 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-12 04:11:10 -07:00
|
|
|
it("is triggered by switching to win/tab with different CWD #6054", function()
|
|
|
|
command('lcd '..dirs[3]) -- window 3
|
|
|
|
command('split '..dirs[2]..'/foo') -- window 2
|
|
|
|
command('lcd '..dirs[2])
|
|
|
|
command('split '..dirs[1]..'/bar') -- window 1
|
|
|
|
command('lcd '..dirs[1])
|
|
|
|
|
|
|
|
command('2wincmd w') -- window 2
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
|
2017-03-12 04:11:10 -07:00
|
|
|
|
|
|
|
eq(4, eval('g:cdcount'))
|
|
|
|
command('tabnew') -- tab 2 (tab-local CWD)
|
|
|
|
eq(4, eval('g:cdcount')) -- same CWD, no DirChanged event
|
|
|
|
command('tcd '..dirs[3])
|
|
|
|
command('tabnext') -- tab 1 (no tab-local CWD)
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
|
2017-03-12 04:11:10 -07:00
|
|
|
command('tabnext') -- tab 2
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[3], scope='tab'}, eval('g:ev'))
|
2017-03-12 04:11:10 -07:00
|
|
|
eq(7, eval('g:cdcount'))
|
|
|
|
|
|
|
|
command('tabnext') -- tab 1
|
|
|
|
command('3wincmd w') -- window 3
|
|
|
|
eq(9, eval('g:cdcount'))
|
|
|
|
command('tabnext') -- tab 2 (has the *same* CWD)
|
|
|
|
eq(9, eval('g:cdcount')) -- same CWD, no DirChanged event
|
|
|
|
end)
|
|
|
|
|
2017-01-17 02:47:20 -07:00
|
|
|
it('is triggered by nvim_set_current_dir()', function()
|
2017-01-11 10:09:30 -07:00
|
|
|
request('nvim_set_current_dir', dirs[1])
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[1], scope='global'}, eval('g:ev'))
|
2017-01-11 10:09:30 -07:00
|
|
|
|
|
|
|
request('nvim_set_current_dir', dirs[2])
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[2], scope='global'}, eval('g:ev'))
|
2017-01-11 10:09:30 -07:00
|
|
|
|
|
|
|
local status, err = pcall(function()
|
|
|
|
request('nvim_set_current_dir', '/doesnotexist')
|
|
|
|
end)
|
|
|
|
eq(false, status)
|
|
|
|
eq('Failed to change directory', string.match(err, ': (.*)'))
|
2017-03-12 04:12:26 -07:00
|
|
|
eq({cwd=dirs[2], scope='global'}, eval('g:ev'))
|
2017-01-11 10:09:30 -07:00
|
|
|
end)
|
2018-03-15 23:29:20 -07:00
|
|
|
|
|
|
|
it('works when local to buffer', function()
|
|
|
|
command('let g:triggered = 0')
|
|
|
|
command('autocmd DirChanged <buffer> let g:triggered = 1')
|
|
|
|
command('cd '..dirs[1])
|
|
|
|
eq(1, eval('g:triggered'))
|
|
|
|
end)
|
2017-01-11 10:09:30 -07:00
|
|
|
end)
|