Skip to content

Commit

Permalink
fix(buffer_diff_preview): fix error being thrown on invalid buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvirtin committed Jul 13, 2024
1 parent 588af83 commit 3a6d87b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 95 deletions.
2 changes: 2 additions & 0 deletions lua/vgit/features/screens/DiffScreen/Store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ end
function Store:fetch(shape, filename, opts)
opts = opts or {}

if not fs.exists(filename) then return nil, { 'Buffer has no diff associated with it' } end

self:reset()

self.shape = shape
Expand Down
184 changes: 92 additions & 92 deletions tests/unit/core/Buffer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ describe('Buffer', function()
it('should transpose virtual line number in buffer', function()
buffer:set_lines({ 'Hello World', 'sup' })
local ok, extmark_id = buffer:transpose_virtual_line_number({
text = 'virtual',
hl = 'Error',
row = 1
text = 'virtual',
hl = 'Error',
row = 1,
})
assert.is_true(ok)
assert.is_not_nil(extmark_id)
Expand Down Expand Up @@ -202,116 +202,116 @@ describe('Buffer', function()
end)
end)

describe('create', function()
it('should create a new buffer', function()
buffer = Buffer():create(false, true)
assert.is_not_nil(buffer.bufnr)
assert.is_not.same(buffer.bufnr, bufnr)
end)
describe('create', function()
it('should create a new buffer', function()
buffer = Buffer():create(false, true)
assert.is_not_nil(buffer.bufnr)
assert.is_not.same(buffer.bufnr, bufnr)
end)
end)

describe('is_current', function()
it('should return if buffer is current', function()
local other_buffer = Buffer():create(false, true)
local window = Window(0):open(buffer, {
relative = 'editor',
width = 20,
height = 10,
row = 5,
col = 5,
})
window:focus()
assert.is_true(buffer:is_current())
assert.is_false(other_buffer:is_current())
end)
describe('is_current', function()
it('should return if buffer is current', function()
local other_buffer = Buffer():create(false, true)
local window = Window(0):open(buffer, {
relative = 'editor',
width = 20,
height = 10,
row = 5,
col = 5,
})
window:focus()
assert.is_true(buffer:is_current())
assert.is_false(other_buffer:is_current())
end)
end)

describe('is_valid', function()
it('should return if buffer is valid', function()
assert.is_true(buffer:is_valid())
vim.api.nvim_buf_delete(buffer.bufnr, { force = true })
assert.is_false(buffer:is_valid())
end)
describe('is_valid', function()
it('should return if buffer is valid', function()
assert.is_true(buffer:is_valid())
vim.api.nvim_buf_delete(buffer.bufnr, { force = true })
assert.is_false(buffer:is_valid())
end)
end)

describe('delete', function()
it('should delete buffer', function()
assert.is_true(buffer:is_valid())
vim.api.nvim_buf_delete(buffer.bufnr, { force = true })
assert.is_false(buffer:is_valid())
end)
describe('delete', function()
it('should delete buffer', function()
assert.is_true(buffer:is_valid())
vim.api.nvim_buf_delete(buffer.bufnr, { force = true })
assert.is_false(buffer:is_valid())
end)
end)

describe('get_lines', function()
it('should get buffer lines', function()
vim.api.nvim_buf_set_lines(buffer.bufnr, 0, -1, false, { 'line1', 'line2' })
assert.are.same(buffer:get_lines(), { 'line1', 'line2' })
end)
describe('get_lines', function()
it('should get buffer lines', function()
vim.api.nvim_buf_set_lines(buffer.bufnr, 0, -1, false, { 'line1', 'line2' })
assert.are.same(buffer:get_lines(), { 'line1', 'line2' })
end)
end)

describe('set_lines', function()
it('should set buffer lines', function()
buffer:set_lines({ 'line1', 'line2' })
assert.are.same(buffer:get_lines(), { 'line1', 'line2' })
end)
describe('set_lines', function()
it('should set buffer lines', function()
buffer:set_lines({ 'line1', 'line2' })
assert.are.same(buffer:get_lines(), { 'line1', 'line2' })
end)
end)

describe('set_option', function()
it('should set buffer option', function()
vim.api.nvim_buf_set_option(buffer.bufnr, 'ft', 'lua')
assert.equals(buffer:get_option('ft'), 'lua')
end)
describe('set_option', function()
it('should set buffer option', function()
vim.api.nvim_buf_set_option(buffer.bufnr, 'ft', 'lua')
assert.equals(buffer:get_option('ft'), 'lua')
end)
end)

describe('get_option', function()
it('should get buffer option', function()
buffer:set_option('ft', 'lua')
assert.equals(buffer:get_option('ft'), 'lua')
end)
describe('get_option', function()
it('should get buffer option', function()
buffer:set_option('ft', 'lua')
assert.equals(buffer:get_option('ft'), 'lua')
end)
end)

describe('assign_options', function()
it('should assign multiple buffer options', function()
buffer:assign_options({
ft = 'lua',
bufhidden = 'wipe'
})
assert.equals(buffer:get_option('ft'), 'lua')
assert.equals(buffer:get_option('bufhidden'), 'wipe')
end)
describe('assign_options', function()
it('should assign multiple buffer options', function()
buffer:assign_options({
ft = 'lua',
bufhidden = 'wipe',
})
assert.equals(buffer:get_option('ft'), 'lua')
assert.equals(buffer:get_option('bufhidden'), 'wipe')
end)
end)

describe('get_line_count', function()
it('should get buffer line count', function()
buffer:set_lines({ 'line1', 'line2' })
assert.equals(buffer:get_line_count(), 2)
end)
describe('get_line_count', function()
it('should get buffer line count', function()
buffer:set_lines({ 'line1', 'line2' })
assert.equals(buffer:get_line_count(), 2)
end)
end)

describe('list', function()
it('should list all buffers', function()
local buffers = buffer:list()
assert.is_true(#buffers > 0)
for i = 1, #buffers do
buffer = buffers[i]
buffer:delete()
end
assert.are.same(#buffer:list(), 1)
end)
describe('list', function()
it('should list all buffers', function()
local buffers = buffer:list()
assert.is_true(#buffers > 0)
for i = 1, #buffers do
buffer = buffers[i]
buffer:delete()
end
assert.are.same(#buffer:list(), 1)
end)
end)

describe('set_var', function()
it('should set buffer variable', function()
buffer:set_var('vgit_status', {
added = 0,
changed = 0,
removed = 0
})
assert.are.same(vim.api.nvim_buf_get_var(buffer.bufnr, 'vgit_status'), {
added = 0,
changed = 0,
removed = 0
})
end)
describe('set_var', function()
it('should set buffer variable', function()
buffer:set_var('vgit_status', {
added = 0,
changed = 0,
removed = 0,
})
assert.are.same(vim.api.nvim_buf_get_var(buffer.bufnr, 'vgit_status'), {
added = 0,
changed = 0,
removed = 0,
})
end)
end)
end)
6 changes: 3 additions & 3 deletions tests/unit/core/Namespace_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ describe('Namespace', function()
buffer:set_lines({ 'Hello World', 'sup' })
local opts = {}
local success, extmark_id = namespace:transpose_virtual_line_number(buffer, {
text = 'virtual',
hl = 'Error',
row = 1
text = 'virtual',
hl = 'Error',
row = 1,
})
assert.is_true(success)
assert.is_number(extmark_id)
Expand Down

0 comments on commit 3a6d87b

Please sign in to comment.