test: fix tests

This commit is contained in:
Folke Lemaitre 2024-06-26 14:29:00 +02:00
parent 0614ca6ca6
commit bbe136bda6
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
3 changed files with 24 additions and 23 deletions

View File

@ -195,7 +195,6 @@ function Task:spawn(cmd, opts)
local on_exit = opts.on_exit local on_exit = opts.on_exit
function opts.on_line(line) function opts.on_line(line)
self:notify(line)
self.status = line self.status = line
if on_line then if on_line then
pcall(on_line, line) pcall(on_line, line)

View File

@ -32,7 +32,8 @@ describe("runner", function()
package.loaded["lazy.manage.task.test"]["async" .. i] = { package.loaded["lazy.manage.task.test"]["async" .. i] = {
---@param task LazyTask ---@param task LazyTask
run = function(task) run = function(task)
task:schedule(function() task:async(function()
coroutine.yield()
table.insert(runs, { plugin = task.plugin.name, task = task.name }) table.insert(runs, { plugin = task.plugin.name, task = task.name })
end) end)
end, end,

View File

@ -4,20 +4,18 @@ local Task = require("lazy.manage.task")
describe("task", function() describe("task", function()
local plugin = { name = "test", _ = {} } local plugin = { name = "test", _ = {} }
local done = false ---@type {done?:boolean, error:string?}
---@type string? local task_result = {}
local error
local opts = { local opts = {
---@param task LazyTask
on_done = function(task) on_done = function(task)
done = true task_result = { done = true, error = task.error }
error = task.error
end, end,
} }
before_each(function() before_each(function()
done = false task_result = {}
error = nil
end) end)
it("simple function", function() it("simple function", function()
@ -25,9 +23,10 @@ describe("task", function()
assert(not task:has_started()) assert(not task:has_started())
assert(not task:is_running()) assert(not task:is_running())
task:start() task:start()
task:wait()
assert(not task:is_running()) assert(not task:is_running())
assert(task:is_done()) assert(task:is_done())
assert(done) assert(task_result.done)
end) end)
it("detects errors", function() it("detects errors", function()
@ -37,18 +36,19 @@ describe("task", function()
assert(not task:has_started()) assert(not task:has_started())
assert(not task:is_running()) assert(not task:is_running())
task:start() task:start()
task:wait()
assert(task:is_done()) assert(task:is_done())
assert(not task:is_running()) assert(not task:is_running())
assert(done) assert(task_result.done)
assert(error) assert(task_result.error)
assert(task.error and task.error:find("test")) assert(task.error and task.error:find("test"))
end) end)
it("schedule", function() it("async", function()
local running = false local running = true
local task = Task.new(plugin, "test", function(task) local task = Task.new(plugin, "test", function(task)
running = true task:async(function()
task:schedule(function() coroutine.yield()
running = false running = false
end) end)
end, opts) end, opts)
@ -56,25 +56,26 @@ describe("task", function()
assert(not task:has_started()) assert(not task:has_started())
task:start() task:start()
assert(running) assert(running)
assert(#task._running == 1)
assert(task:is_running()) assert(task:is_running())
assert(not task:is_done()) assert(not task:is_done())
task:wait() task:wait()
assert(not running)
assert(task:is_done()) assert(task:is_done())
assert(not task:is_running()) assert(not task:is_running())
assert(done) assert(task_result.done)
assert(not task.error) assert(not task.error)
end) end)
it("spawn errors", function() it("spawn errors", function()
local task = Task.new(plugin, "test", function(task) local task = Task.new(plugin, "spawn_errors", function(task)
task:spawn("foobar") task:spawn("foobar")
end, opts) end, opts)
assert(not task:is_running()) assert(not task:is_running())
task:start() task:start()
task:wait()
assert(not task:is_running()) assert(not task:is_running())
assert(done) assert(task_result.done)
assert(task.error and task.error:find("Failed to spawn")) assert(task.error and task.error:find("Failed to spawn"), task.output)
end) end)
it("spawn", function() it("spawn", function()
@ -89,7 +90,7 @@ describe("task", function()
task:wait() task:wait()
assert(task:is_done()) assert(task:is_done())
assert.same(task.output, "foo\n") assert.same(task.output, "foo\n")
assert(done) assert(task_result.done)
assert(not task.error) assert(not task.error)
end) end)
@ -103,7 +104,7 @@ describe("task", function()
assert(task:is_running()) assert(task:is_running())
task:wait() task:wait()
assert(task.output == "foo\nbar\n" or task.output == "bar\nfoo\n", task.output) assert(task.output == "foo\nbar\n" or task.output == "bar\nfoo\n", task.output)
assert(done) assert(task_result.done)
assert(not task.error) assert(not task.error)
end) end)
end) end)