lazy.nvim/tests/manage/task_spec.lua

102 lines
2.6 KiB
Lua

---@module 'luassert'
--# selene:allow(incorrect_standard_library_use)
local Task = require("lazy.manage.task")
describe("task", function()
local plugin = { name = "test", _ = {} }
---@type {done?:boolean, error:string?}
local task_result = {}
local opts = {
---@param task LazyTask
on_done = function(task)
task_result = { done = true, error = task.error }
end,
}
before_each(function()
task_result = {}
end)
it("simple function", function()
local task = Task.new(plugin, "test", function() end, opts)
assert(task:has_started())
assert(task:is_running())
task:wait()
assert(not task:is_running())
assert(task_result.done)
end)
it("detects errors", function()
local task = Task.new(plugin, "test", function()
error("test")
end, opts)
assert(task:has_started())
assert(task:is_running())
task:wait()
assert(not task:is_running())
assert(task_result.done)
assert(task_result.error)
assert(task.error and task.error:find("test"))
end)
it("async", function()
local running = true
---@async
local task = Task.new(plugin, "test", function()
coroutine.yield()
running = false
end, opts)
assert(task:has_started())
assert(task:is_running())
assert(running)
assert(task:is_running())
task:wait()
assert(not running)
assert(not task:is_running())
assert(task_result.done)
assert(not task.error)
end)
it("spawn errors", function()
local task = Task.new(plugin, "spawn_errors", function(task)
task:spawn("foobar")
end, opts)
assert(task:has_started())
assert(task:is_running())
task:wait()
assert(not task:is_running())
assert(task_result.done)
assert(task.error and task.error:find("Failed to spawn"), task.output)
end)
it("spawn", function()
local task = Task.new(plugin, "test", function(task)
task:spawn("echo", { args = { "foo" } })
end, opts)
assert(task:has_started())
assert(task:is_running())
assert(task:has_started())
assert(task:is_running())
task:wait()
assert.same(task.output, "foo\n")
assert(task_result.done)
assert(not task.error)
end)
it("spawn 2x", function()
local task = Task.new(plugin, "test", function(task)
task:spawn("echo", { args = { "foo" } })
task:spawn("echo", { args = { "bar" } })
end, opts)
assert(task:has_started())
assert(task:is_running())
assert(task:is_running())
task:wait()
assert(task.output == "foo\nbar\n" or task.output == "bar\nfoo\n", task.output)
assert(task_result.done)
assert(not task.error)
end)
end)