2024-06-26 16:06:56 +01:00
|
|
|
---@module 'luassert'
|
2022-12-29 22:12:38 +00:00
|
|
|
--# selene:allow(incorrect_standard_library_use)
|
2022-11-28 10:04:44 +00:00
|
|
|
local Task = require("lazy.manage.task")
|
|
|
|
|
|
|
|
describe("task", function()
|
2022-11-28 12:10:52 +00:00
|
|
|
local plugin = { name = "test", _ = {} }
|
2022-11-28 10:04:44 +00:00
|
|
|
|
2024-06-26 13:29:00 +01:00
|
|
|
---@type {done?:boolean, error:string?}
|
|
|
|
local task_result = {}
|
2022-11-28 10:04:44 +00:00
|
|
|
|
|
|
|
local opts = {
|
2024-06-26 13:29:00 +01:00
|
|
|
---@param task LazyTask
|
2022-11-28 10:04:44 +00:00
|
|
|
on_done = function(task)
|
2024-06-26 13:29:00 +01:00
|
|
|
task_result = { done = true, error = task.error }
|
2022-11-28 10:04:44 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
before_each(function()
|
2024-06-26 13:29:00 +01:00
|
|
|
task_result = {}
|
2022-11-28 10:04:44 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("simple function", function()
|
|
|
|
local task = Task.new(plugin, "test", function() end, opts)
|
2024-06-26 16:06:56 +01:00
|
|
|
assert(task:is_running())
|
2024-06-26 13:29:00 +01:00
|
|
|
task:wait()
|
2022-11-28 10:04:44 +00:00
|
|
|
assert(not task:is_running())
|
2024-06-26 13:29:00 +01:00
|
|
|
assert(task_result.done)
|
2022-11-28 10:04:44 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("detects errors", function()
|
|
|
|
local task = Task.new(plugin, "test", function()
|
|
|
|
error("test")
|
|
|
|
end, opts)
|
2024-06-26 16:06:56 +01:00
|
|
|
assert(task:is_running())
|
2024-06-26 13:29:00 +01:00
|
|
|
task:wait()
|
2022-11-28 10:04:44 +00:00
|
|
|
assert(not task:is_running())
|
2024-06-26 13:29:00 +01:00
|
|
|
assert(task_result.done)
|
|
|
|
assert(task_result.error)
|
2024-06-26 17:42:52 +01:00
|
|
|
assert(task:has_errors() and task:output(vim.log.levels.ERROR):find("test"))
|
2022-11-28 10:04:44 +00:00
|
|
|
end)
|
|
|
|
|
2024-06-26 13:29:00 +01:00
|
|
|
it("async", function()
|
|
|
|
local running = true
|
2024-06-26 16:06:56 +01:00
|
|
|
---@async
|
|
|
|
local task = Task.new(plugin, "test", function()
|
|
|
|
coroutine.yield()
|
|
|
|
running = false
|
2022-11-28 10:04:44 +00:00
|
|
|
end, opts)
|
2024-06-26 16:06:56 +01:00
|
|
|
assert(task:is_running())
|
2022-11-28 10:04:44 +00:00
|
|
|
assert(running)
|
|
|
|
assert(task:is_running())
|
|
|
|
task:wait()
|
2024-06-26 13:29:00 +01:00
|
|
|
assert(not running)
|
2022-11-28 10:04:44 +00:00
|
|
|
assert(not task:is_running())
|
2024-06-26 13:29:00 +01:00
|
|
|
assert(task_result.done)
|
2024-06-26 17:42:52 +01:00
|
|
|
assert(not task:has_errors())
|
2022-11-28 10:04:44 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("spawn errors", function()
|
2024-06-26 13:29:00 +01:00
|
|
|
local task = Task.new(plugin, "spawn_errors", function(task)
|
2022-11-28 10:04:44 +00:00
|
|
|
task:spawn("foobar")
|
|
|
|
end, opts)
|
2024-06-26 16:06:56 +01:00
|
|
|
assert(task:is_running())
|
2024-06-26 13:29:00 +01:00
|
|
|
task:wait()
|
2022-11-28 10:04:44 +00:00
|
|
|
assert(not task:is_running())
|
2024-06-26 13:29:00 +01:00
|
|
|
assert(task_result.done)
|
2024-06-26 17:42:52 +01:00
|
|
|
assert(task:has_errors() and task:output(vim.log.levels.ERROR):find("Failed to spawn"), task.output)
|
2022-11-28 10:04:44 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("spawn", function()
|
|
|
|
local task = Task.new(plugin, "test", function(task)
|
|
|
|
task:spawn("echo", { args = { "foo" } })
|
|
|
|
end, opts)
|
2024-06-26 16:06:56 +01:00
|
|
|
assert(task:is_running())
|
2022-11-28 10:04:44 +00:00
|
|
|
assert(task:is_running())
|
|
|
|
task:wait()
|
2024-06-26 17:42:52 +01:00
|
|
|
assert.same(task:output(), "foo")
|
2024-06-26 13:29:00 +01:00
|
|
|
assert(task_result.done)
|
2024-06-26 17:42:52 +01:00
|
|
|
assert(not task:has_errors())
|
2022-11-28 10:04:44 +00:00
|
|
|
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)
|
2024-06-26 16:06:56 +01:00
|
|
|
assert(task:is_running())
|
2022-11-28 10:04:44 +00:00
|
|
|
assert(task:is_running())
|
|
|
|
task:wait()
|
2024-06-26 17:42:52 +01:00
|
|
|
assert(task:output() == "foo\nbar" or task:output() == "bar\nfoo", task:output())
|
2024-06-26 13:29:00 +01:00
|
|
|
assert(task_result.done)
|
2024-06-26 17:42:52 +01:00
|
|
|
assert(not task:has_errors())
|
2022-11-28 10:04:44 +00:00
|
|
|
end)
|
|
|
|
end)
|