aboutsummaryrefslogtreecommitdiff
path: root/test/functional/autocmd/dirchanged_spec.lua
blob: 15196dbd442a7ef590a49b65ee95d08c389ebf8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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

describe('autocmd DirChanged', function()
  local curdir = string.gsub(lfs.currentdir(), '\\', '/')
  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()
    command('autocmd DirChanged * let [g:event, g:scope, g:cdcount] = [copy(v:event), expand("<amatch>"), 1 + get(g:, "cdcount", 0)]')
  end)

  it('sets v:event', function()
    command('lcd '..dirs[1])
    eq({cwd=dirs[1], scope='window'}, eval('g:event'))
    eq(1, eval('g:cdcount'))

    command('tcd '..dirs[2])
    eq({cwd=dirs[2], scope='tab'}, eval('g:event'))
    eq(2, eval('g:cdcount'))

    command('cd '..dirs[3])
    eq({cwd=dirs[3], scope='global'}, eval('g:event'))
    eq(3, eval('g:cdcount'))
  end)

  it('disallows recursion', function()
    command('set shellslash')
    -- Set up a _nested_ handler.
    command('autocmd DirChanged * nested lcd '..dirs[3])
    command('lcd '..dirs[1])
    eq({cwd=dirs[1], scope='window'}, eval('g:event'))
    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])
    eq('window', eval('g:scope'))

    command('tcd '..dirs[2])
    eq('tab', eval('g:scope'))

    command('cd '..dirs[3])
    eq('global', eval('g:scope'))
  end)

  it('does not trigger if :cd fails', function()
    command('let g:event = {}')

    local status1, err1 = pcall(function()
      command('lcd '..dirs[1] .. '/doesnotexist')
    end)
    eq({}, eval('g:event'))

    local status2, err2 = pcall(function()
      command('lcd '..dirs[2] .. '/doesnotexist')
    end)
    eq({}, eval('g:event'))

    local status3, err3 = pcall(function()
      command('lcd '..dirs[3] .. '/doesnotexist')
    end)
    eq({}, eval('g:event'))

    eq(false, status1)
    eq(false, status2)
    eq(false, status3)

    eq('E344:', string.match(err1, "E%d*:"))
    eq('E344:', string.match(err2, "E%d*:"))
    eq('E344:', string.match(err3, "E%d*:"))
  end)

  it("is triggered by 'autochdir'", function()
    command('set autochdir')

    command('split '..dirs[1]..'/foo')
    eq({cwd=dirs[1], scope='window'}, eval('g:event'))

    command('split '..dirs[2]..'/bar')
    eq({cwd=dirs[2], scope='window'}, eval('g:event'))
  end)

  it('is triggered by nvim_set_current_dir()', function()
    request('nvim_set_current_dir', dirs[1])
    eq({cwd=dirs[1], scope='global'}, eval('g:event'))

    request('nvim_set_current_dir', dirs[2])
    eq({cwd=dirs[2], scope='global'}, eval('g:event'))

    local status, err = pcall(function()
      request('nvim_set_current_dir', '/doesnotexist')
    end)
    eq(false, status)
    eq('Failed to change directory', string.match(err, ': (.*)'))
    eq({cwd=dirs[2], scope='global'}, eval('g:event'))
  end)
end)