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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local clear = n.clear
local command = n.command
local eq = t.eq
local fn = n.fn
local pathsep = n.get_pathsep()
local testdir_left = 'Xtest-difftool-left'
local testdir_right = 'Xtest-difftool-right'
setup(function()
n.mkdir_p(testdir_left)
n.mkdir_p(testdir_right)
t.write_file(testdir_left .. pathsep .. 'file1.txt', 'hello')
t.write_file(testdir_left .. pathsep .. 'file2.txt', 'foo')
t.write_file(testdir_right .. pathsep .. 'file1.txt', 'hello world') -- modified
t.write_file(testdir_right .. pathsep .. 'file3.txt', 'bar') -- added
end)
teardown(function()
n.rmdir(testdir_left)
n.rmdir(testdir_right)
end)
describe('nvim.difftool', function()
before_each(function()
clear()
command('packadd nvim.difftool')
end)
it('shows added, modified, and deleted files in quickfix', function()
command(('DiffTool %s %s'):format(testdir_left, testdir_right))
local qflist = fn.getqflist()
local entries = {}
for _, item in ipairs(qflist) do
table.insert(entries, { text = item.text, rel = item.user_data and item.user_data.rel })
end
-- Should show:
-- file1.txt as modified (M)
-- file2.txt as deleted (D)
-- file3.txt as added (A)
eq({
{ text = 'M', rel = 'file1.txt' },
{ text = 'D', rel = 'file2.txt' },
{ text = 'A', rel = 'file3.txt' },
}, entries)
end)
it('has consistent split layout', function()
command('set nosplitright')
command(('DiffTool %s %s'):format(testdir_left, testdir_right))
local wins = fn.getwininfo()
local left_win_col = wins[1].wincol
local right_win_col = wins[2].wincol
assert(
left_win_col < right_win_col,
'Left window should be to the left of right window even with nosplitright set'
)
end)
it('handles symlinks', function()
-- Create a symlink in right dir pointing to file2.txt in left dir
local symlink_path = vim.fs.joinpath(testdir_right, 'file2.txt')
local target_path = vim.fs.joinpath('..', testdir_left, 'file2.txt')
assert(vim.uv.fs_symlink(target_path, symlink_path) == true)
finally(function()
os.remove(symlink_path)
end)
assert(fn.getftype(symlink_path) == 'link')
-- Run difftool
command(('DiffTool %s %s'):format(testdir_left, testdir_right))
local qflist = fn.getqflist()
local entries = {}
for _, item in ipairs(qflist) do
table.insert(entries, { text = item.text, rel = item.user_data and item.user_data.rel })
end
-- file2.txt should not be reported as added or deleted anymore
eq({
{ text = 'M', rel = 'file1.txt' },
{ text = 'A', rel = 'file3.txt' },
}, entries)
end)
it('has autocmds when diff window is opened', function()
command(('DiffTool %s %s'):format(testdir_left, testdir_right))
local autocmds = fn.nvim_get_autocmds({ group = 'nvim.difftool.events' })
assert(#autocmds > 0)
end)
it('cleans up autocmds when diff window is closed', function()
command(('DiffTool %s %s'):format(testdir_left, testdir_right))
command('q')
local ok = pcall(fn.nvim_get_autocmds, { group = 'nvim.difftool.events' })
eq(false, ok)
end)
it('does not reset quickfix list when closing quickfix window', function()
command(('DiffTool %s %s'):format(testdir_left, testdir_right))
local qflist_before = fn.getqflist()
assert(#qflist_before > 0, 'quickfix list should not be empty')
-- Close the quickfix window
command('cclose')
-- Quickfix list should still be intact
local qflist_after = fn.getqflist()
eq(#qflist_before, #qflist_after)
-- Autocmds should still be active
local autocmds = fn.nvim_get_autocmds({ group = 'nvim.difftool.events' })
assert(#autocmds > 0, 'autocmds should still exist after closing quickfix window')
end)
it('opens difftool automatically when started with nvim -d', function()
-- Start Neovim with -d flag for directory diff
clear({
args = {
'--cmd',
'packadd nvim.difftool',
'-d',
testdir_left,
testdir_right,
},
})
-- Wait for difftool to open
n.poke_eventloop()
-- Verify we have 3 windows (left, right, and quickfix)
eq(3, #fn.getwininfo())
-- Verify quickfix list has the expected entries
local qflist = fn.getqflist()
local entries = {}
for _, item in ipairs(qflist) do
table.insert(entries, { text = item.text, rel = item.user_data and item.user_data.rel })
end
eq({
{ text = 'M', rel = 'file1.txt' },
{ text = 'D', rel = 'file2.txt' },
{ text = 'A', rel = 'file3.txt' },
}, entries)
end)
end)
|