aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2022-03-05 22:22:54 +0100
committerGitHub <noreply@github.com>2022-03-05 22:22:54 +0100
commit20e5dc0d10bb4d92d6f0c80a6057924ca2ce1e51 (patch)
tree6d004b451872a30015e090dda4dabb888a18ac19
parentremove npm install --production from ansiblels (#518) (diff)
downloadmason-20e5dc0d10bb4d92d6f0c80a6057924ca2ce1e51.tar
mason-20e5dc0d10bb4d92d6f0c80a6057924ca2ce1e51.tar.gz
mason-20e5dc0d10bb4d92d6f0c80a6057924ca2ce1e51.tar.bz2
mason-20e5dc0d10bb4d92d6f0c80a6057924ca2ce1e51.tar.lz
mason-20e5dc0d10bb4d92d6f0c80a6057924ca2ce1e51.tar.xz
mason-20e5dc0d10bb4d92d6f0c80a6057924ca2ce1e51.tar.zst
mason-20e5dc0d10bb4d92d6f0c80a6057924ca2ce1e51.zip
async: raise errors instead of returning pcall-style (#521)
-rw-r--r--lua/nvim-lsp-installer/core/async/init.lua8
-rw-r--r--lua/nvim-lsp-installer/process.lua2
-rw-r--r--scripts/autogen_metadata.lua8
-rw-r--r--tests/core/async_spec.lua46
-rw-r--r--tests/core/process_spec.lua2
5 files changed, 51 insertions, 15 deletions
diff --git a/lua/nvim-lsp-installer/core/async/init.lua b/lua/nvim-lsp-installer/core/async/init.lua
index 1dd2c138..e9c753d2 100644
--- a/lua/nvim-lsp-installer/core/async/init.lua
+++ b/lua/nvim-lsp-installer/core/async/init.lua
@@ -17,7 +17,7 @@ function Promise:_wrap_resolver_cb(success, cb)
return
end
self.has_resolved = true
- cb(success, ...)
+ cb(success, { ... })
end
end
@@ -26,7 +26,11 @@ function Promise:__call(callback)
end
local function await(resolver)
- return co.yield(Promise.new(resolver))
+ local ok, value = co.yield(Promise.new(resolver))
+ if not ok then
+ error(value[1], 2)
+ end
+ return unpack(value)
end
local function table_pack(...)
diff --git a/lua/nvim-lsp-installer/process.lua b/lua/nvim-lsp-installer/process.lua
index 87a4b950..07d51c32 100644
--- a/lua/nvim-lsp-installer/process.lua
+++ b/lua/nvim-lsp-installer/process.lua
@@ -143,7 +143,7 @@ function M.spawn(cmd, opts, callback)
end
end
check:stop()
- callback(successful)
+ callback(successful, exit_code)
end)
log.fmt_debug("Job pid=%s exited with exit_code=%s, signal=%s", pid_or_err, exit_code, signal)
diff --git a/scripts/autogen_metadata.lua b/scripts/autogen_metadata.lua
index cca9a410..7ff2dc24 100644
--- a/scripts/autogen_metadata.lua
+++ b/scripts/autogen_metadata.lua
@@ -151,18 +151,16 @@ end
local function create_setting_schema_files()
local available_servers = servers.get_available_servers()
- local gist_ok, gist_err, gist_response =
+ local gist_err, gist_data =
a.promisify(fetch) "https://gist.githubusercontent.com/williamboman/a01c3ce1884d4b57cc93422e7eae7702/raw/lsp-packages.json"
- assert(gist_ok, "Failed to fetch gist.")
assert(not gist_err, "Failed to fetch gist.")
- local package_json_mappings = vim.json.decode(gist_response)
+ local package_json_mappings = vim.json.decode(gist_data)
for _, server in pairs(available_servers) do
local package_json_url = package_json_mappings[server.name]
if package_json_url then
print(("Fetching %q..."):format(package_json_url))
- local ok, err, response = a.promisify(fetch)(package_json_url)
- assert(ok, "Failed to fetch.")
+ local err, response = a.promisify(fetch)(package_json_url)
assert(not err, "Failed to fetch package.json for " .. server.name)
local schema = vim.json.decode(response)
if schema.contributes and schema.contributes.configuration then
diff --git a/tests/core/async_spec.lua b/tests/core/async_spec.lua
index 7d965e9b..88af14af 100644
--- a/tests/core/async_spec.lua
+++ b/tests/core/async_spec.lua
@@ -24,18 +24,30 @@ describe("async", function()
"should wrap callback-style async functions",
async_test(function()
local stdio = process.in_memory_sink()
- local ok, success = a.promisify(process.spawn)("env", {
+ local success, exit_code = a.promisify(process.spawn)("env", {
args = {},
env = { "FOO=BAR", "BAR=BAZ" },
stdio_sink = stdio.sink,
})
- assert.is_true(ok)
assert.is_true(success)
+ assert.equals(0, exit_code)
assert.equals("FOO=BAR\nBAR=BAZ\n", table.concat(stdio.buffers.stdout, ""))
end)
)
it(
+ "should return all values",
+ async_test(function()
+ local val1, val2, val3 = a.wait(function(resolve)
+ resolve(1, 2, 3)
+ end)
+ assert.equals(1, val1)
+ assert.equals(2, val2)
+ assert.equals(3, val3)
+ end)
+ )
+
+ it(
"should cancel coroutine",
async_test(function()
local james_bond = spy.new()
@@ -50,13 +62,35 @@ describe("async", function()
)
it(
- "should reject if async function raises error",
+ "should raise error if async function raises error",
async_test(function()
- local ok, err = a.promisify(function()
+ local err = assert.has.errors(a.promisify(function()
error "something went wrong"
- end)()
- assert.is_false(ok)
+ end))
assert.is_true(match.has_match "something went wrong$"(err))
end)
)
+
+ it(
+ "should raise error if async function rejects",
+ async_test(function()
+ local err = assert.has.errors(function()
+ a.wait(function(_, reject)
+ reject "This is an error"
+ end)
+ end)
+ assert.equals("This is an error", err)
+ end)
+ )
+
+ it(
+ "should pass nil arguments to promisified functions",
+ async_test(function()
+ local fn = spy.new(function(_, _, _, _, _, _, _, cb)
+ cb()
+ end)
+ a.promisify(fn)(nil, 2, nil, 4, nil, nil, 7)
+ assert.spy(fn).was_called_with(nil, 2, nil, 4, nil, nil, 7, match.is_function())
+ end)
+ )
end)
diff --git a/tests/core/process_spec.lua b/tests/core/process_spec.lua
index 2db42865..3c76b462 100644
--- a/tests/core/process_spec.lua
+++ b/tests/core/process_spec.lua
@@ -79,7 +79,7 @@ describe("process.spawn", function()
assert.wait_for(function()
assert.spy(callback).was_called(1)
- assert.spy(callback).was_called_with(true)
+ assert.spy(callback).was_called_with(true, 0)
assert.equal(table.concat(stdio.buffers.stdout, ""), "HELLO=world\nMY_ENV=var\n")
end)
end)