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