From 352dbadcb68beda35450cc7d2619061e843f78a9 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Mon, 28 Nov 2022 11:04:44 +0100 Subject: [PATCH] test: added tests for runner --- tests/manage/runner_spec.lua | 40 +++++++++++++ tests/manage/task_spec.lua | 107 +++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 tests/manage/runner_spec.lua create mode 100644 tests/manage/task_spec.lua diff --git a/tests/manage/runner_spec.lua b/tests/manage/runner_spec.lua new file mode 100644 index 0000000..b8fdc9e --- /dev/null +++ b/tests/manage/runner_spec.lua @@ -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) diff --git a/tests/manage/task_spec.lua b/tests/manage/task_spec.lua new file mode 100644 index 0000000..090d76a --- /dev/null +++ b/tests/manage/task_spec.lua @@ -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)