refactor: async processes

This commit is contained in:
Folke Lemaitre 2024-06-28 16:08:26 +02:00
parent 4319846b8c
commit a36ebd2a75
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
12 changed files with 394 additions and 379 deletions

View file

@ -0,0 +1,20 @@
---@module 'luassert'
local Async = require("lazy.async")
local Process = require("lazy.manage.process")
describe("process", function()
it("runs sync", function()
local lines = Process.exec({ "echo", "-n", "hello" })
assert.are.same({ "hello" }, lines)
end)
it("runs sync from async context", function()
local lines ---@type string[]
local async = Async.new(function()
lines = Process.exec({ "echo", "-n", "hello" })
end)
async:wait()
assert.are.same({ "hello" }, lines)
end)
end)

View file

@ -21,9 +21,9 @@ describe("task", function()
it("simple function", function()
local task = Task.new(plugin, "test", function() end, opts)
assert(task:is_running())
assert(task:running())
task:wait()
assert(not task:is_running())
assert(not task:running())
assert(task_result.done)
end)
@ -31,9 +31,9 @@ describe("task", function()
local task = Task.new(plugin, "test", function()
error("test")
end, opts)
assert(task:is_running())
assert(task:running())
task:wait()
assert(not task:is_running())
assert(not task:running())
assert(task_result.done)
assert(task_result.error)
assert(task:has_errors() and task:output(vim.log.levels.ERROR):find("test"))
@ -46,12 +46,12 @@ describe("task", function()
coroutine.yield()
running = false
end, opts)
assert(task:is_running())
assert(task:running())
assert(running)
assert(task:is_running())
assert(task:running())
task:wait()
assert(not running)
assert(not task:is_running())
assert(not task:running())
assert(task_result.done)
assert(not task:has_errors())
end)
@ -60,19 +60,19 @@ describe("task", function()
local task = Task.new(plugin, "spawn_errors", function(task)
task:spawn("foobar")
end, opts)
assert(task:is_running())
assert(task:running())
task:wait()
assert(not task:is_running())
assert(not task:running())
assert(task_result.done)
assert(task:has_errors() and task:output(vim.log.levels.ERROR):find("Failed to spawn"), task.output)
assert(task:has_errors() and task:output(vim.log.levels.ERROR):find("Failed to spawn"), task:output())
end)
it("spawn", function()
local task = Task.new(plugin, "test", function(task)
task:spawn("echo", { args = { "foo" } })
end, opts)
assert(task:is_running())
assert(task:is_running())
assert(task:running())
assert(task:running())
task:wait()
assert.same(task:output(), "foo")
assert(task_result.done)
@ -84,8 +84,8 @@ describe("task", function()
task:spawn("echo", { args = { "foo" } })
task:spawn("echo", { args = { "bar" } })
end, opts)
assert(task:is_running())
assert(task:is_running())
assert(task:running())
assert(task:running())
task:wait()
assert(task:output() == "foo\nbar" or task:output() == "bar\nfoo", task:output())
assert(task_result.done)