From 3a6d87bf6378a61ab9aad17a5d8f2ef3a1ef76b4 Mon Sep 17 00:00:00 2001 From: tanvirtin Date: Sat, 13 Jul 2024 03:51:59 -0400 Subject: [PATCH] fix(buffer_diff_preview): fix error being thrown on invalid buffers --- .../features/screens/DiffScreen/Store.lua | 2 + tests/unit/core/Buffer_spec.lua | 184 +++++++++--------- tests/unit/core/Namespace_spec.lua | 6 +- 3 files changed, 97 insertions(+), 95 deletions(-) diff --git a/lua/vgit/features/screens/DiffScreen/Store.lua b/lua/vgit/features/screens/DiffScreen/Store.lua index 7bf8864d..ce4abf35 100644 --- a/lua/vgit/features/screens/DiffScreen/Store.lua +++ b/lua/vgit/features/screens/DiffScreen/Store.lua @@ -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 diff --git a/tests/unit/core/Buffer_spec.lua b/tests/unit/core/Buffer_spec.lua index e872c642..52b47d3d 100644 --- a/tests/unit/core/Buffer_spec.lua +++ b/tests/unit/core/Buffer_spec.lua @@ -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) @@ -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) diff --git a/tests/unit/core/Namespace_spec.lua b/tests/unit/core/Namespace_spec.lua index e9d40e4a..b96a96de 100644 --- a/tests/unit/core/Namespace_spec.lua +++ b/tests/unit/core/Namespace_spec.lua @@ -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)