test: added tests for runner

This commit is contained in:
Folke Lemaitre 2022-11-28 11:04:44 +01:00
parent ab1b512545
commit 352dbadcb6
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 147 additions and 0 deletions

View File

@ -0,0 +1,40 @@
local Runner = require("lazy.manage.runner")
describe("runner", function()
local plugins = { { name = "plugin1" }, { name = "plugin2" } }
---@type {plugin:string, task:string}[]
local runs = {}
before_each(function()
runs = {}
end)
package.loaded["lazy.manage.task.test"] = {}
for i = 1, 10 do
package.loaded["lazy.manage.task.test"]["test" .. i] = {
---@param task LazyTask
run = function(task)
table.insert(runs, { plugin = task.plugin.name, task = task.type })
end,
}
package.loaded["lazy.manage.task.test"]["error" .. i] = {
---@param task LazyTask
run = function(task)
table.insert(runs, { plugin = task.plugin.name, task = task.type })
error("error" .. i)
end,
}
end
it("runs the pipeline", function()
local runner = Runner.new({ plugins = plugins, pipeline = { "test.test1", "test.test2" } })
runner:start()
assert.equal(4, #runs)
end)
it("aborts on error", function()
local runner = Runner.new({ plugins = plugins, pipeline = { "test.test1", "test.error1", "test.test2" } })
runner:start()
assert.equal(4, #runs)
end)
end)

107
tests/manage/task_spec.lua Normal file
View File

@ -0,0 +1,107 @@
local Task = require("lazy.manage.task")
describe("task", function()
local plugin = { name = "test" }
local done = false
local error = nil
local opts = {
on_done = function(task)
done = true
error = task.error
end,
}
before_each(function()
done = false
error = nil
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()
assert(not task:is_running())
assert(task:is_done())
assert(done)
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()
assert(task:is_done())
assert(not task:is_running())
assert(done)
assert(error)
assert(task.error and task.error:find("test"))
end)
it("schedule", function()
local running = false
local task = Task.new(plugin, "test", function(task)
running = true
task:schedule(function()
running = false
end)
end, opts)
assert(not task:is_running())
assert(not task:has_started())
task:start()
assert(running)
assert(#task._running == 1)
assert(task:is_running())
assert(not task:is_done())
task:wait()
assert(task:is_done())
assert(not task:is_running())
assert(done)
assert(not task.error)
end)
it("spawn errors", function()
local task = Task.new(plugin, "test", function(task)
task:spawn("foobar")
end, opts)
assert(not task:is_running())
task:start()
assert(not task:is_running())
assert(done)
assert(task.error and task.error:find("Failed to spawn"))
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")
assert(done)
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()
assert.same(task.output, "foo\nbar\n")
assert(done)
assert(not task.error)
end)
end)