robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
5.1 KiB157 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, setup, teardown, finally =
5  t.describe, t.it, t.before_each, t.setup, t.teardown, t.finally
6local clear = n.clear
7local command = n.command
8local eq = t.eq
9local fn = n.fn
10
11local pathsep = n.get_pathsep()
12local testdir_left = 'Xtest-difftool-left'
13local testdir_right = 'Xtest-difftool-right'
14
15setup(function()
16  n.mkdir_p(testdir_left)
17  n.mkdir_p(testdir_right)
18  t.write_file(testdir_left .. pathsep .. 'file1.txt', 'hello')
19  t.write_file(testdir_left .. pathsep .. 'file2.txt', 'foo')
20  t.write_file(testdir_left .. pathsep .. 'file4 with space.txt', 'hello')
21  t.write_file(testdir_right .. pathsep .. 'file1.txt', 'hello world') -- modified
22  t.write_file(testdir_right .. pathsep .. 'file3.txt', 'bar') -- added
23  t.write_file(testdir_right .. pathsep .. 'file4 with space.txt', 'hello world') -- modified
24end)
25
26teardown(function()
27  n.rmdir(testdir_left)
28  n.rmdir(testdir_right)
29end)
30
31describe('nvim.difftool', function()
32  before_each(function()
33    clear()
34    command('packadd nvim.difftool')
35  end)
36
37  it('shows added, modified, and deleted files in quickfix', function()
38    command(('DiffTool %s %s'):format(testdir_left, testdir_right))
39    local qflist = fn.getqflist()
40    local entries = {}
41    for _, item in ipairs(qflist) do
42      table.insert(entries, { text = item.text, rel = item.user_data and item.user_data.rel })
43    end
44
45    -- Should show:
46    -- file1.txt as modified (M)
47    -- file2.txt as deleted (D)
48    -- file3.txt as added (A)
49    -- file4 with space.txt as modified (M)
50    eq({
51      { text = 'M', rel = 'file1.txt' },
52      { text = 'D', rel = 'file2.txt' },
53      { text = 'A', rel = 'file3.txt' },
54      { text = 'M', rel = 'file4 with space.txt' },
55    }, entries)
56  end)
57
58  it('has consistent split layout', function()
59    command('set nosplitright')
60    command(('DiffTool %s %s'):format(testdir_left, testdir_right))
61    local wins = fn.getwininfo()
62    local left_win_col = wins[1].wincol
63    local right_win_col = wins[2].wincol
64    assert(
65      left_win_col < right_win_col,
66      'Left window should be to the left of right window even with nosplitright set'
67    )
68  end)
69
70  it('handles symlinks', function()
71    -- Create a symlink in right dir pointing to file2.txt in left dir
72    local symlink_path = vim.fs.joinpath(testdir_right, 'file2.txt')
73    local target_path = vim.fs.joinpath('..', testdir_left, 'file2.txt')
74    assert(vim.uv.fs_symlink(target_path, symlink_path) == true)
75    finally(function()
76      os.remove(symlink_path)
77    end)
78    assert(fn.getftype(symlink_path) == 'link')
79
80    -- Run difftool
81    command(('DiffTool %s %s'):format(testdir_left, testdir_right))
82    local qflist = fn.getqflist()
83    local entries = {}
84    for _, item in ipairs(qflist) do
85      table.insert(entries, { text = item.text, rel = item.user_data and item.user_data.rel })
86    end
87
88    -- file2.txt should not be reported as added or deleted anymore
89    eq({
90      { text = 'M', rel = 'file1.txt' },
91      { text = 'A', rel = 'file3.txt' },
92      { text = 'M', rel = 'file4 with space.txt' },
93    }, entries)
94  end)
95
96  it('has autocmds when diff window is opened', function()
97    command(('DiffTool %s %s'):format(testdir_left, testdir_right))
98    local autocmds = fn.nvim_get_autocmds({ group = 'nvim.difftool.events' })
99    assert(#autocmds > 0)
100  end)
101
102  it('cleans up autocmds when diff window is closed', function()
103    command(('DiffTool %s %s'):format(testdir_left, testdir_right))
104    command('q')
105    local ok = pcall(fn.nvim_get_autocmds, { group = 'nvim.difftool.events' })
106    eq(false, ok)
107  end)
108
109  it('does not reset quickfix list when closing quickfix window', function()
110    command(('DiffTool %s %s'):format(testdir_left, testdir_right))
111    local qflist_before = fn.getqflist()
112    assert(#qflist_before > 0, 'quickfix list should not be empty')
113
114    -- Close the quickfix window
115    command('cclose')
116
117    -- Quickfix list should still be intact
118    local qflist_after = fn.getqflist()
119    eq(#qflist_before, #qflist_after)
120
121    -- Autocmds should still be active
122    local autocmds = fn.nvim_get_autocmds({ group = 'nvim.difftool.events' })
123    assert(#autocmds > 0, 'autocmds should still exist after closing quickfix window')
124  end)
125
126  it('opens difftool automatically when started with nvim -d', function()
127    -- Start Neovim with -d flag for directory diff
128    clear({
129      args = {
130        '--cmd',
131        'packadd nvim.difftool',
132        '-d',
133        testdir_left,
134        testdir_right,
135      },
136    })
137
138    -- Wait for difftool to open
139    n.poke_eventloop()
140
141    -- Verify we have 3 windows (left, right, and quickfix)
142    eq(3, #fn.getwininfo())
143
144    -- Verify quickfix list has the expected entries
145    local qflist = fn.getqflist()
146    local entries = {}
147    for _, item in ipairs(qflist) do
148      table.insert(entries, { text = item.text, rel = item.user_data and item.user_data.rel })
149    end
150    eq({
151      { text = 'M', rel = 'file1.txt' },
152      { text = 'D', rel = 'file2.txt' },
153      { text = 'A', rel = 'file3.txt' },
154      { text = 'M', rel = 'file4 with space.txt' },
155    }, entries)
156  end)
157end)