diff --git a/.github/workflows/docs.yaml b/.github/workflows/docs.yaml index d2a5a4b..95fd3f1 100644 --- a/.github/workflows/docs.yaml +++ b/.github/workflows/docs.yaml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest name: Generate Fidget documentation steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Install luajit run: sudo apt install -y luajit diff --git a/.github/workflows/luarocks.yml b/.github/workflows/luarocks.yml index 0427d58..8054129 100644 --- a/.github/workflows/luarocks.yml +++ b/.github/workflows/luarocks.yml @@ -14,7 +14,7 @@ jobs: luarocks-upload: runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 # Required to count the commits - name: Get Version diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..4a05dbc --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,27 @@ +name: Run Tests + +on: + push: + branches: ['main'] + pull_request: + branches: ['main'] + +jobs: + test: + strategy: + matrix: + nvim-version: ['stable', 'nightly'] + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} + name: Run Tests + steps: + - uses: actions/checkout@v4 + + - name: Install Neovim + uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: ${{ matrix.nvim-version }} + + - 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..b5dfd50 --- /dev/null +++ b/tests/handle_spec.lua @@ -0,0 +1,97 @@ +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) +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")