lazy.nvim/tests/manage/task_spec.lua

111 lines
2.8 KiB
Lua
Raw Normal View History

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()
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)
assert(not task:has_started())
assert(not task:is_running())
task:start()
2024-06-26 13:29:00 +01:00
task:wait()
2022-11-28 10:04:44 +00:00
assert(not task:is_running())
assert(task:is_done())
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)
assert(not task:has_started())
assert(not task:is_running())
task:start()
2024-06-26 13:29:00 +01:00
task:wait()
2022-11-28 10:04:44 +00:00
assert(task:is_done())
assert(not task:is_running())
2024-06-26 13:29:00 +01:00
assert(task_result.done)
assert(task_result.error)
2022-11-28 10:04:44 +00:00
assert(task.error and task.error:find("test"))
end)
2024-06-26 13:29:00 +01:00
it("async", function()
local running = true
2022-11-28 10:04:44 +00:00
local task = Task.new(plugin, "test", function(task)
2024-06-26 13:29:00 +01:00
task:async(function()
coroutine.yield()
2022-11-28 10:04:44 +00:00
running = false
end)
end, opts)
assert(not task:is_running())
assert(not task:has_started())
task:start()
assert(running)
assert(task:is_running())
assert(not task:is_done())
task:wait()
2024-06-26 13:29:00 +01:00
assert(not running)
2022-11-28 10:04:44 +00:00
assert(task:is_done())
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
assert(not task.error)
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)
assert(not task:is_running())
task:start()
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.error and task.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)
assert(not task:is_running())
assert(not task:has_started())
task:start()
assert(task:has_started())
assert(task:is_running())
task:wait()
assert(task:is_done())
assert.same(task.output, "foo\n")
2024-06-26 13:29:00 +01:00
assert(task_result.done)
2022-11-28 10:04:44 +00:00
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(not task:is_running())
task:start()
assert(task:is_running())
task:wait()
2024-06-24 18:55:09 +01:00
assert(task.output == "foo\nbar\n" or task.output == "bar\nfoo\n", task.output)
2024-06-26 13:29:00 +01:00
assert(task_result.done)
2022-11-28 10:04:44 +00:00
assert(not task.error)
end)
end)