aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2022-03-06 21:48:29 +0100
committerGitHub <noreply@github.com>2022-03-06 21:48:29 +0100
commitdc39ce90f99a77699317bd31d95ce970690a4624 (patch)
tree901e89bacca9b0d370c694fcd5a88cf2e1ae768e /tests
parentfix(fetch): shift args to put callback arg last (diff)
downloadmason-dc39ce90f99a77699317bd31d95ce970690a4624.tar
mason-dc39ce90f99a77699317bd31d95ce970690a4624.tar.gz
mason-dc39ce90f99a77699317bd31d95ce970690a4624.tar.bz2
mason-dc39ce90f99a77699317bd31d95ce970690a4624.tar.lz
mason-dc39ce90f99a77699317bd31d95ce970690a4624.tar.xz
mason-dc39ce90f99a77699317bd31d95ce970690a4624.tar.zst
mason-dc39ce90f99a77699317bd31d95ce970690a4624.zip
run server installation in async execution context (#525)
Diffstat (limited to 'tests')
-rw-r--r--tests/core/async/async_spec.lua (renamed from tests/core/async_spec.lua)0
-rw-r--r--tests/core/async/spawn_spec.lua47
-rw-r--r--tests/core/result_spec.lua88
-rw-r--r--tests/server_spec.lua46
4 files changed, 181 insertions, 0 deletions
diff --git a/tests/core/async_spec.lua b/tests/core/async/async_spec.lua
index 88af14af..88af14af 100644
--- a/tests/core/async_spec.lua
+++ b/tests/core/async/async_spec.lua
diff --git a/tests/core/async/spawn_spec.lua b/tests/core/async/spawn_spec.lua
new file mode 100644
index 00000000..721e4d08
--- /dev/null
+++ b/tests/core/async/spawn_spec.lua
@@ -0,0 +1,47 @@
+local spawn = require "nvim-lsp-installer.core.async.spawn"
+local process = require "nvim-lsp-installer.process"
+
+describe("async spawn", function()
+ it(
+ "should spawn commands and return stdout & stderr",
+ async_test(function()
+ local result = spawn.env {
+ env = { "FOO=bar" },
+ }
+ assert.is_true(result:is_success())
+ assert.equals("FOO=bar\n", result:get_or_nil().stdout)
+ assert.equals("", result:get_or_nil().stderr)
+ end)
+ )
+
+ it(
+ "should use provided stdio_sink",
+ async_test(function()
+ local stdio = process.in_memory_sink()
+ local result = spawn.env {
+ env = { "FOO=bar" },
+ stdio_sink = stdio.sink,
+ }
+ assert.is_true(result:is_success())
+ assert.equals(nil, result:get_or_nil().stdout)
+ assert.equals(nil, result:get_or_nil().stderr)
+ assert.equals("FOO=bar\n", table.concat(stdio.buffers.stdout, ""))
+ assert.equals("", table.concat(stdio.buffers.stderr, ""))
+ end)
+ )
+
+ it(
+ "should pass command arguments",
+ async_test(function()
+ local result = spawn.bash {
+ "-c",
+ 'echo "Hello $VAR"',
+ env = { "VAR=world" },
+ }
+
+ assert.is_true(result:is_success())
+ assert.equals("Hello world\n", result:get_or_nil().stdout)
+ assert.equals("", result:get_or_nil().stderr)
+ end)
+ )
+end)
diff --git a/tests/core/result_spec.lua b/tests/core/result_spec.lua
new file mode 100644
index 00000000..725041de
--- /dev/null
+++ b/tests/core/result_spec.lua
@@ -0,0 +1,88 @@
+local Result = require "nvim-lsp-installer.core.result"
+local match = require "luassert.match"
+
+describe("result", function()
+ it("should create success", function()
+ local result = Result.success "Hello!"
+ assert.is_true(result:is_success())
+ assert.is_false(result:is_failure())
+ assert.equals("Hello!", result:get_or_nil())
+ end)
+
+ it("should create failure", function()
+ local result = Result.failure "Hello!"
+ assert.is_true(result:is_failure())
+ assert.is_false(result:is_success())
+ assert.equals("Hello!", result:err_or_nil())
+ end)
+
+ it("should return value on get_or_throw()", function()
+ local result = Result.success "Hello!"
+ local val = result:get_or_throw()
+ assert.equals("Hello!", val)
+ end)
+
+ it("should throw failure on get_or_throw()", function()
+ local result = Result.failure "Hello!"
+ local err = assert.has_error(function()
+ result:get_or_throw()
+ end)
+ assert.equals("Hello!", err)
+ end)
+
+ it("should map() success", function()
+ local result = Result.success "Hello"
+ local mapped = result:map(function(x)
+ return x .. " World!"
+ end)
+ assert.equals("Hello World!", mapped:get_or_nil())
+ assert.is_nil(mapped:err_or_nil())
+ end)
+
+ it("should not map() failure", function()
+ local result = Result.failure "Hello"
+ local mapped = result:map(function(x)
+ return x .. " World!"
+ end)
+ assert.equals("Hello", mapped:err_or_nil())
+ assert.is_nil(mapped:get_or_nil())
+ end)
+
+ it("should raise exceptions in map()", function()
+ local result = Result.success "failure"
+ local err = assert.has_error(function()
+ result:map(function()
+ error "error"
+ end)
+ end)
+ assert.equals("error", err)
+ end)
+
+ it("should map_catching() success", function()
+ local result = Result.success "Hello"
+ local mapped = result:map_catching(function(x)
+ return x .. " World!"
+ end)
+ assert.equals("Hello World!", mapped:get_or_nil())
+ assert.is_nil(mapped:err_or_nil())
+ end)
+
+ it("should not map_catching() failure", function()
+ local result = Result.failure "Hello"
+ local mapped = result:map_catching(function(x)
+ return x .. " World!"
+ end)
+ assert.equals("Hello", mapped:err_or_nil())
+ assert.is_nil(mapped:get_or_nil())
+ end)
+
+ it("should catch errors in map_catching()", function()
+ local result = Result.success "value"
+ local mapped = result:map_catching(function()
+ error "This is an error"
+ end)
+ assert.is_false(mapped:is_success())
+ assert.is_true(mapped:is_failure())
+ assert.is_true(match.has_match "This is an error$"(mapped:err_or_nil()))
+ end)
+end)
diff --git a/tests/server_spec.lua b/tests/server_spec.lua
index 744b4cf8..81ae83b0 100644
--- a/tests/server_spec.lua
+++ b/tests/server_spec.lua
@@ -2,6 +2,13 @@ local spy = require "luassert.spy"
local lsp_installer = require "nvim-lsp-installer"
local server = require "nvim-lsp-installer.server"
local a = require "nvim-lsp-installer.core.async"
+local context = require "nvim-lsp-installer.installers.context"
+local fs = require "nvim-lsp-installer.fs"
+
+local function timestamp()
+ local seconds, microseconds = vim.loop.gettimeofday()
+ return (seconds * 1000) + math.floor(microseconds / 1000)
+end
describe("server", function()
it(
@@ -47,4 +54,43 @@ describe("server", function()
assert.is_false(srv:is_installed())
end)
)
+
+ it(
+ "should remove directories upon installation failure",
+ async_test(function()
+ local srv = FailingServerGenerator {
+ name = "remove_dirs_failure",
+ root_dir = server.get_server_root_path "remove_dirs_failure",
+ installer = {
+ -- 1. sleep 500ms
+ function(_, callback)
+ vim.defer_fn(function()
+ callback(true)
+ end, 500)
+ end,
+ -- 2. promote install dir
+ context.promote_install_dir(),
+ -- 3. fail
+ function(_, callback)
+ callback(false)
+ end,
+ },
+ }
+ srv:install()
+
+ -- 1. installation started
+ a.sleep(50)
+ assert.is_true(fs.dir_exists(srv:get_tmp_install_dir()))
+
+ -- 2. install dir promoted
+ a.sleep(500)
+ assert.is_false(fs.dir_exists(srv:get_tmp_install_dir()))
+
+ -- 3. installation failed
+ a.sleep(200)
+
+ assert.is_false(srv:is_installed())
+ assert.is_false(fs.dir_exists(srv:get_tmp_install_dir()))
+ end)
+ )
end)