diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..32abdc3 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,26 @@ +name: Run Tests + +on: + push: + branches: ['main'] + pull_request: + branches: ['main'] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + nvim-versions: ['stable', 'nightly'] + name: Run Tests + steps: + - name: checkout + uses: actions/checkout@v3 + + - uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: ${{ matrix.nvim-versions }} + + - name: run tests + run: nvim --clean --headless -u tests/minimal_init.lua -c "PlenaryBustedDirectory tests/ { minimal_init = 'tests/minimal_init.lua' }" diff --git a/tests/handle_spec.lua b/tests/handle_spec.lua new file mode 100644 index 0000000..a66fd58 --- /dev/null +++ b/tests/handle_spec.lua @@ -0,0 +1,165 @@ +local fidget = require("fidget") +local notif = require("fidget.notification") +local handle = require("fidget.progress.handle") + +local eq = assert.are.same +local is_nil = assert.is_nil + +describe("progress handle", function() + before_each(function() + require("fidget").setup({}) + notif.clear() + notif.window.close() + end) + + it("should create a handle", function() + local h = handle.create({ + title = "test", + message = "test message", + }) + assert(h ~= nil) + + eq(h.title, "test") + eq(h.message, "test message") + eq(h.done, false) + + -- defaults + eq(h.lsp_client, { name = "fidget" }) + eq(h.cancellable, true) + end) + + it("should update when report() is called", function() + local h = handle.create({ + title = "test", + message = "test message", + }) + h:report({ + message = "new message", + percentage = 50, + }) + eq(h.message, "new message") + eq(h.percentage, 50) + end) + + it("should complete when finish() is called", function() + local h = handle.create({ + title = "test", + message = "test message", + }) + h:finish() + eq(h.done, true) + is_nil(h.percentage) + end) + + it("should set percentage to 100 when complete", function() + local h = handle.create({ + title = "test", + message = "test message", + percentage = 0, + }) + eq(h.percentage, 0) + eq(h.done, false) + h:report({ + percentage = 50, + }) + eq(h.percentage, 50) + eq(h.done, false) + h:finish() + eq(h.percentage, 100) + eq(h.done, true) + end) + + it("should *not* set percentage when cancel() is called", function() + local h = handle.create({ + title = "test", + message = "test message", + percentage = 0, + }) + h:report({ + percentage = 40, + }) + h:cancel() + eq(h.done, true) + eq(h.percentage, 40) + end) + + it("should *not* initialize percentage when not provided", function() + local h = handle.create({ + title = "test", + message = "test message", + }) + eq(h.percentage, nil) + eq(h.done, false) + h:finish() + eq(h.percentage, nil) + eq(h.done, true) + end) + + it("should show in the fidget window", function() + local buf = notif.window.get_buffer() + local line_count = vim.api.nvim_buf_line_count(buf) + eq(line_count, 1) + + local h = handle.create({ + title = "test", + message = "test message", + lsp_client = { name = "test" }, + }) + eq(h.percentage, nil) + eq(h.done, false) + + eq(2, vim.api.nvim_buf_line_count(buf)) -- title + message and spinner + end) + + it("should update the window/buffer when report() is called", function() + local buf = notif.window.get_buffer() + + local h = handle.create({ + title = "test", + message = "test message", + lsp_client = { name = "test" }, + }) + eq(nil, h.percentage) + eq(false, h.done) + + local changed = false + vim.api.nvim_create_autocmd("TextChanged", { + once = true, + buffer = buf, + callback = function() + changed = true + end, + }) + + h:report({ + message = "new message", + percentage = 50, + }) + + eq(true, changed) + end) + + it("should stop displaying if it's GC'd before it finished", function() + local function create_handle() + ---@diagnostic disable-next-line: discard-returns + handle.create({ + title = "test", + message = "test message", + lsp_client = { name = "test" }, + }) + end + local buf = notif.window.get_buffer() + local line_count = vim.api.nvim_buf_line_count(buf) + eq(line_count, 1) + create_handle() + line_count = vim.api.nvim_buf_line_count(buf) + eq(line_count, 2) + + collectgarbage("collect") + collectgarbage("collect") + collectgarbage("collect") + collectgarbage("collect") + + eq(vim.api.nvim_buf_line_count(buf), 0) + end) +end) diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua new file mode 100644 index 0000000..91ed7d6 --- /dev/null +++ b/tests/minimal_init.lua @@ -0,0 +1,33 @@ +local deps = { + "nvim-lua/plenary.nvim", +} + +local uv = vim.uv or vim.loop + +local tmp = uv.os_tmpdir() +local base = tmp .. "/fidget-test/" + +local waiting = 0 + +for _, dep in ipairs(deps) do + local path = base .. dep:gsub("/", "-") + local stat = vim.loop.fs_stat(path) + if stat == nil or stat.type ~= "directory" then + vim.fn.mkdir(path, "p") + waiting = waiting + 1 + uv.spawn("git", { + args = { "clone", "https://github.com/" .. dep, path }, + }, function() + waiting = waiting - 1 + end) + end + vim.opt.rtp:prepend(path) +end + +vim.wait(10000, function() + return waiting == 0 +end) + +vim.opt.rtp:prepend(uv.cwd()) + +require("plenary.busted")