lazy.nvim/tests/manage/task_spec.lua

95 lines
2.5 KiB
Lua
Raw Permalink Normal View History

2022-12-29 22:12:38 +00:00
--# selene:allow(incorrect_standard_library_use)
2024-06-30 12:35:11 +01:00
local Async = require("lazy.async")
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)
2024-06-28 15:08:26 +01:00
assert(task:running())
2024-06-26 13:29:00 +01:00
task:wait()
2024-06-28 15:08:26 +01:00
assert(not task: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-28 15:08:26 +01:00
assert(task:running())
2024-06-26 13:29:00 +01:00
task:wait()
2024-06-28 15:08:26 +01:00
assert(not task: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
---@async
local task = Task.new(plugin, "test", function()
2024-06-30 12:35:11 +01:00
Async.yield()
running = false
2022-11-28 10:04:44 +00:00
end, opts)
2024-06-28 15:08:26 +01:00
assert(task:running())
2022-11-28 10:04:44 +00:00
assert(running)
2024-06-28 15:08:26 +01:00
assert(task:running())
2022-11-28 10:04:44 +00:00
task:wait()
2024-06-26 13:29:00 +01:00
assert(not running)
2024-06-28 15:08:26 +01:00
assert(not task: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-28 15:08:26 +01:00
assert(task:running())
2024-06-26 13:29:00 +01:00
task:wait()
2024-06-28 15:08:26 +01:00
assert(not task:running())
2024-06-26 13:29:00 +01:00
assert(task_result.done)
2024-06-28 15:08:26 +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-28 15:08:26 +01:00
assert(task:running())
assert(task:running())
2022-11-28 10:04:44 +00:00
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-28 15:08:26 +01:00
assert(task:running())
assert(task:running())
2022-11-28 10:04:44 +00:00
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)