|
| 1 | +local utils = {} |
| 2 | + |
| 3 | +local Path = require("plenary.path") |
| 4 | +local testdir = Path:new(vim.env.TMPDIR or "/tmp", "neo-tree-testing"):absolute() |
| 5 | + |
| 6 | +local function rm_test_dir() |
| 7 | + if vim.fn.isdirectory(testdir) == 1 then |
| 8 | + vim.fn.delete(testdir, "rf") |
| 9 | + end |
| 10 | +end |
| 11 | + |
| 12 | +utils.setup_test_fs = function() |
| 13 | + rm_test_dir() |
| 14 | + |
| 15 | + -- Need a list-style map here to ensure that things happen in the correct order. |
| 16 | + -- |
| 17 | + -- When/if editing this, be cautious as (for now) other tests might be accessing |
| 18 | + -- files from within this array by index |
| 19 | + local fs = { |
| 20 | + basedir = testdir, |
| 21 | + content = { |
| 22 | + { |
| 23 | + name = "foo", |
| 24 | + type = "dir", |
| 25 | + content = { |
| 26 | + { name = "foofile1.txt", type = "file" }, |
| 27 | + { name = "foofile2.txt", type = "file" }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + { name = "bar", type = "dir" }, |
| 31 | + { name = "topfile1.txt", type = "file" }, |
| 32 | + }, |
| 33 | + } |
| 34 | + local function makefs(content, basedir) |
| 35 | + for _, info in ipairs(content) do |
| 36 | + if info.type == "dir" then |
| 37 | + info.abspath = Path:new(basedir, info.name):absolute() |
| 38 | + vim.fn.mkdir(info.abspath, "p") |
| 39 | + if info.content then |
| 40 | + makefs(info.content, info.abspath) |
| 41 | + end |
| 42 | + elseif info.type == "file" then |
| 43 | + info.abspath = Path:new(basedir, info.name):absolute() |
| 44 | + vim.fn.writefile({}, info.abspath) |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + makefs(fs.content, testdir) |
| 49 | + vim.cmd("tcd " .. testdir) |
| 50 | + return fs |
| 51 | +end |
| 52 | + |
| 53 | +utils.teardown_test_fs = function() |
| 54 | + rm_test_dir() |
| 55 | +end |
| 56 | + |
| 57 | +utils.clear_test_state = function() |
| 58 | + -- TODO: Clear internal state? |
| 59 | + vim.cmd("top new | wincmd o") |
| 60 | + local keepbufnr = vim.api.nvim_get_current_buf() |
| 61 | + for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do |
| 62 | + if bufnr ~= keepbufnr then |
| 63 | + vim.api.nvim_buf_delete(bufnr, { force = true }) |
| 64 | + end |
| 65 | + end |
| 66 | + assert(#vim.api.nvim_tabpage_list_wins(0) == 1, "Failed to properly clear tab") |
| 67 | + assert(#vim.api.nvim_list_bufs() == 1, "Failed to properly clear buffers") |
| 68 | +end |
| 69 | + |
| 70 | +utils.editfile = function(testfile) |
| 71 | + vim.cmd("e " .. testfile) |
| 72 | + assert.are.same(vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":p"), vim.fn.fnamemodify(testfile, ":p")) |
| 73 | +end |
| 74 | + |
| 75 | +return utils |
0 commit comments