|
| 1 | +local assert = require('luassert') |
| 2 | +local event_handling = require('yazi.event_handling') |
| 3 | + |
| 4 | +describe('get_buffers_that_need_renaming_after_yazi_exited', function() |
| 5 | + before_each(function() |
| 6 | + -- clear all buffers |
| 7 | + for _, buf in ipairs(vim.api.nvim_list_bufs()) do |
| 8 | + vim.api.nvim_buf_delete(buf, { force = true }) |
| 9 | + end |
| 10 | + end) |
| 11 | + |
| 12 | + it('can detect moves to files whose names match exactly', function() |
| 13 | + ---@type YaziEventDataRenameOrMove |
| 14 | + local move_event = { |
| 15 | + from = '/my-tmp/file1', |
| 16 | + to = '/my-tmp/file2', |
| 17 | + } |
| 18 | + |
| 19 | + -- simulate buffers being opened |
| 20 | + vim.fn.bufadd('/my-tmp/file1') |
| 21 | + vim.fn.bufadd('/my-tmp/file_A') |
| 22 | + |
| 23 | + local instructions = |
| 24 | + event_handling.get_buffers_that_need_renaming_after_yazi_exited( |
| 25 | + move_event |
| 26 | + ) |
| 27 | + |
| 28 | + assert.is_equal(vim.tbl_count(instructions), 1) |
| 29 | + |
| 30 | + local result1 = instructions[1] |
| 31 | + assert.is_equal('/my-tmp/file2', result1.path.filename) |
| 32 | + assert.is_number(result1.bufnr) |
| 33 | + end) |
| 34 | + |
| 35 | + it( |
| 36 | + 'can detect moves to buffers open in a directory that was moved', |
| 37 | + function() |
| 38 | + ---@type YaziEventDataRenameOrMove |
| 39 | + local move_event = { |
| 40 | + from = '/my-tmp/dir1', |
| 41 | + to = '/my-tmp/dir2', |
| 42 | + } |
| 43 | + |
| 44 | + -- simulate the buffer being opened |
| 45 | + vim.fn.bufadd('/my-tmp/dir1/file') |
| 46 | + |
| 47 | + local instructions = |
| 48 | + event_handling.get_buffers_that_need_renaming_after_yazi_exited( |
| 49 | + move_event |
| 50 | + ) |
| 51 | + |
| 52 | + assert.is_equal(vim.tbl_count(instructions), 1) |
| 53 | + local result = instructions[1] |
| 54 | + assert.is_equal('/my-tmp/dir2/file', result.path.filename) |
| 55 | + end |
| 56 | + ) |
| 57 | + |
| 58 | + it("doesn't move a buffer that was not moved in yazi", function() |
| 59 | + ---@type YaziEventDataRenameOrMove |
| 60 | + local move_event = { |
| 61 | + from = '/my-tmp/not-opened-file', |
| 62 | + to = '/my-tmp/not-opened-file-moved', |
| 63 | + } |
| 64 | + |
| 65 | + -- simulate the buffer being opened |
| 66 | + vim.fn.bufadd('/my-tmp/dir1/file') |
| 67 | + |
| 68 | + local instructions = |
| 69 | + event_handling.get_buffers_that_need_renaming_after_yazi_exited( |
| 70 | + move_event |
| 71 | + ) |
| 72 | + |
| 73 | + assert.is_equal(vim.tbl_count(instructions), 0) |
| 74 | + end) |
| 75 | +end) |
0 commit comments