Skip to content

Commit a7c7504

Browse files
committed
test: add tests for diff_opts.on_unsaved_changes changes option
1 parent cf7632f commit a7c7504

File tree

2 files changed

+121
-50
lines changed

2 files changed

+121
-50
lines changed

tests/unit/config_spec.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ describe("Configuration", function()
8888
layout = "vertical",
8989
open_in_new_tab = false,
9090
keep_terminal_focus = false,
91+
on_unsaved_changes = "error",
9192
},
9293
models = {}, -- Empty models array should be rejected
9394
}
@@ -110,6 +111,7 @@ describe("Configuration", function()
110111
layout = "vertical",
111112
open_in_new_tab = false,
112113
keep_terminal_focus = false,
114+
on_unsaved_changes = "error",
113115
},
114116
models = {
115117
{ name = "Test Model" }, -- Missing value field
@@ -152,6 +154,7 @@ describe("Configuration", function()
152154
layout = "vertical",
153155
open_in_new_tab = false,
154156
keep_terminal_focus = true,
157+
on_unsaved_changes = "error",
155158
},
156159
env = {},
157160
models = {
@@ -177,6 +180,7 @@ describe("Configuration", function()
177180
layout = "vertical",
178181
open_in_new_tab = false,
179182
keep_terminal_focus = "invalid", -- Should be boolean
183+
on_unsaved_changes = "error",
180184
},
181185
env = {},
182186
models = {
@@ -206,6 +210,7 @@ describe("Configuration", function()
206210
show_diff_stats = true,
207211
vertical_split = true,
208212
open_in_current_tab = true,
213+
on_unsaved_changes = "error",
209214
},
210215
env = {},
211216
models = {
@@ -243,6 +248,7 @@ describe("Configuration", function()
243248
show_diff_stats = true,
244249
vertical_split = true,
245250
open_in_current_tab = true,
251+
on_unsaved_changes = "error",
246252
},
247253
env = {},
248254
models = {
@@ -278,6 +284,7 @@ describe("Configuration", function()
278284
show_diff_stats = true,
279285
vertical_split = true,
280286
open_in_current_tab = true,
287+
on_unsaved_changes = "error",
281288
},
282289
env = {},
283290
models = {

tests/unit/diff_spec.lua

Lines changed: 114 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -376,56 +376,6 @@ describe("Diff Module", function()
376376
rawset(io, "open", old_io_open)
377377
end)
378378

379-
it("should detect dirty buffer and throw error", function()
380-
-- Mock vim.fn.bufnr to return a valid buffer number
381-
local old_bufnr = _G.vim.fn.bufnr
382-
_G.vim.fn.bufnr = function(path)
383-
if path == "/path/to/dirty.lua" then
384-
return 2
385-
end
386-
return -1
387-
end
388-
389-
-- Mock vim.api.nvim_buf_get_option to return modified
390-
local old_get_option = _G.vim.api.nvim_buf_get_option
391-
_G.vim.api.nvim_buf_get_option = function(bufnr, option)
392-
if bufnr == 2 and option == "modified" then
393-
return true -- Buffer is dirty
394-
end
395-
return nil
396-
end
397-
398-
local dirty_params = {
399-
tab_name = "test_dirty",
400-
old_file_path = "/path/to/dirty.lua",
401-
new_file_path = "/path/to/dirty.lua",
402-
content = "test content",
403-
}
404-
405-
-- Mock file operations
406-
_G.vim.fn.filereadable = function()
407-
return 1
408-
end
409-
410-
-- This should throw an error for dirty buffer
411-
local success, err = pcall(function()
412-
diff._setup_blocking_diff(dirty_params, function() end)
413-
end)
414-
415-
expect(success).to_be_false()
416-
expect(err).to_be_table()
417-
expect(err.code).to_be(-32000)
418-
expect(err.message).to_be("Diff setup failed")
419-
expect(err.data).to_be_string()
420-
-- For now, let's just verify the basic error structure
421-
-- The important thing is that it fails when buffer is dirty, not the exact message
422-
expect(#err.data > 0).to_be_true()
423-
424-
-- Restore mocks
425-
_G.vim.fn.bufnr = old_bufnr
426-
_G.vim.api.nvim_buf_get_option = old_get_option
427-
end)
428-
429379
it("should handle non-existent buffer", function()
430380
-- Mock vim.fn.bufnr to return -1 (buffer not found)
431381
local old_bufnr = _G.vim.fn.bufnr
@@ -535,6 +485,120 @@ describe("Diff Module", function()
535485

536486
rawset(io, "open", old_io_open)
537487
end)
488+
489+
it("should detect dirty buffer and discard changes when on_unsaved_changes is 'discard'", function()
490+
diff.setup({
491+
diff_opts = {
492+
on_unsaved_changes = "discard",
493+
},
494+
})
495+
496+
local old_bufnr = _G.vim.fn.bufnr
497+
_G.vim.fn.bufnr = function(path)
498+
if path == "/path/to/discard.lua" then
499+
return 2
500+
end
501+
return -1
502+
end
503+
504+
-- Mock vim.api.nvim_buf_get_option to return modified
505+
local old_get_option = _G.vim.api.nvim_buf_get_option
506+
_G.vim.api.nvim_buf_get_option = function(bufnr, option)
507+
if bufnr == 2 and option == "modified" then
508+
return true -- Buffer is dirty
509+
end
510+
return nil
511+
end
512+
513+
-- Test the is_buffer_dirty function indirectly through _setup_blocking_diff
514+
local discard_params = {
515+
tab_name = "test_clean",
516+
old_file_path = "/path/to/discard.lua",
517+
new_file_path = "/path/to/discard.lua",
518+
new_file_contents = "test content",
519+
}
520+
521+
-- Mock file operations
522+
_G.vim.fn.filereadable = function()
523+
return 1
524+
end
525+
_G.vim.api.nvim_list_wins = function()
526+
return { 1 }
527+
end
528+
_G.vim.api.nvim_buf_call = function(bufnr, callback)
529+
callback() -- Execute the callback so vim.cmd gets called
530+
end
531+
532+
spy.on(_G.vim, "cmd")
533+
534+
-- This should not throw an error for dirty buffer since we discard changes
535+
local success, err = pcall(function()
536+
diff._setup_blocking_diff(discard_params, function() end)
537+
end)
538+
539+
expect(err).to_be_nil()
540+
expect(success).to_be_true()
541+
542+
local edit_called = false
543+
local cmd_calls = _G.vim.cmd.calls or {}
544+
545+
for _, call in ipairs(cmd_calls) do
546+
if call.vals[1]:find("edit!", 1, true) then
547+
edit_called = true
548+
break
549+
end
550+
end
551+
expect(edit_called).to_be_true()
552+
553+
-- Restore mocks
554+
_G.vim.fn.bufnr = old_bufnr
555+
_G.vim.api.nvim_buf_get_option = old_get_option
556+
end)
557+
558+
it("should detect dirty buffer and throw error when on_unsaved_changes is 'error'", function()
559+
diff.setup({
560+
diff_opts = {
561+
on_unsaved_changes = "error",
562+
},
563+
})
564+
565+
local old_bufnr = _G.vim.fn.bufnr
566+
_G.vim.fn.bufnr = function(path)
567+
if path == "/path/to/dirty.lua" then
568+
return 2
569+
end
570+
return -1
571+
end
572+
573+
local old_get_option = _G.vim.api.nvim_buf_get_option
574+
_G.vim.api.nvim_buf_get_option = function(bufnr, option)
575+
if bufnr == 2 and option == "modified" then
576+
return true
577+
end
578+
return nil
579+
end
580+
581+
-- this should throw an error for dirty buffer
582+
local success, err = pcall(function()
583+
diff._setup_blocking_diff({
584+
tab_name = "test_error",
585+
old_file_path = "/path/to/dirty.lua",
586+
new_file_path = "/path/to/dirty.lua",
587+
content = "test content",
588+
}, function() end)
589+
end)
590+
591+
expect(success).to_be_false()
592+
expect(err.code).to_be(-32000)
593+
expect(err.message).to_be("Diff setup failed")
594+
expect(err.data).to_be_string()
595+
-- For now, let's just verify the basic error structure
596+
-- The important thing is that it fails when buffer is dirty, not the exact message
597+
expect(#err.data > 0).to_be_true()
598+
599+
_G.vim.fn.bufnr = old_bufnr
600+
_G.vim.api.nvim_buf_get_option = old_get_option
601+
end)
538602
end)
539603

540604
teardown()

0 commit comments

Comments
 (0)