aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2022-04-06 00:25:50 +0200
committerGitHub <noreply@github.com>2022-04-06 00:25:50 +0200
commit999476c324b26223aaf409a84497215e18d74499 (patch)
tree323cc5b072471e5e78ee94b273157779bbcb9611 /tests
parentfix(taplo): use crate distribution (#576) (diff)
downloadmason-999476c324b26223aaf409a84497215e18d74499.tar
mason-999476c324b26223aaf409a84497215e18d74499.tar.gz
mason-999476c324b26223aaf409a84497215e18d74499.tar.bz2
mason-999476c324b26223aaf409a84497215e18d74499.tar.lz
mason-999476c324b26223aaf409a84497215e18d74499.tar.xz
mason-999476c324b26223aaf409a84497215e18d74499.tar.zst
mason-999476c324b26223aaf409a84497215e18d74499.zip
switch majority of installers to async implementation (#574)
Diffstat (limited to 'tests')
-rw-r--r--tests/core/installer_spec.lua88
-rw-r--r--tests/core/result_spec.lua23
-rw-r--r--tests/helpers/lua/test_helpers.lua21
-rw-r--r--tests/server_spec.lua73
4 files changed, 140 insertions, 65 deletions
diff --git a/tests/core/installer_spec.lua b/tests/core/installer_spec.lua
new file mode 100644
index 00000000..7bddaee6
--- /dev/null
+++ b/tests/core/installer_spec.lua
@@ -0,0 +1,88 @@
+local spy = require "luassert.spy"
+local match = require "luassert.match"
+local fs = require "nvim-lsp-installer.core.fs"
+local installer = require "nvim-lsp-installer.core.installer"
+local InstallContext = require "nvim-lsp-installer.core.installer.context"
+local process = require "nvim-lsp-installer.process"
+local Optional = require "nvim-lsp-installer.core.optional"
+
+describe("installer", function()
+ it(
+ "should call installer",
+ async_test(function()
+ spy.on(fs, "mkdirp")
+ spy.on(fs, "rename")
+ local installer_fn = spy.new(
+ ---@param c InstallContext
+ function(c)
+ c.receipt:with_primary_source(c.receipt.npm "the-pkg")
+ end
+ )
+ local destination_dir = ("%s/installer_spec"):format(os.getenv "INSTALL_ROOT_DIR")
+ local ctx = InstallContext.new {
+ name = "installer_spec_success",
+ destination_dir = destination_dir,
+ boundary_path = os.getenv "INSTALL_ROOT_DIR",
+ stdio_sink = process.empty_sink(),
+ requested_version = Optional.empty(),
+ }
+ local result = installer.execute(ctx, installer_fn)
+ assert.is_nil(result:err_or_nil())
+ assert.spy(installer_fn).was_called(1)
+ assert.spy(installer_fn).was_called_with(match.ref(ctx))
+ assert.spy(fs.mkdirp).was_called(1)
+ assert.spy(fs.mkdirp).was_called_with(destination_dir .. ".tmp")
+ assert.spy(fs.rename).was_called(1)
+ assert.spy(fs.rename).was_called_with(destination_dir .. ".tmp", destination_dir)
+ end)
+ )
+
+ it(
+ "should return failure if installer errors",
+ async_test(function()
+ spy.on(fs, "rmrf")
+ local installer_fn = spy.new(function()
+ error("something went wrong. don't try again.", 4) -- 4 because spy.new callstack
+ end)
+ local destination_dir = ("%s/installer_spec_failure"):format(os.getenv "INSTALL_ROOT_DIR")
+ local ctx = InstallContext.new {
+ name = "installer_spec_failure",
+ destination_dir = destination_dir,
+ boundary_path = os.getenv "INSTALL_ROOT_DIR",
+ stdio_sink = process.empty_sink(),
+ requested_version = Optional.empty(),
+ }
+ local result = installer.execute(ctx, installer_fn)
+ assert.spy(installer_fn).was_called(1)
+ assert.spy(installer_fn).was_called_with(match.ref(ctx))
+ assert.is_true(result:is_failure())
+ assert.equals("something went wrong. don't try again.", result:err_or_nil())
+ assert.spy(fs.rmrf).was_called(2)
+ assert.spy(fs.rmrf).was_called_with(destination_dir .. ".tmp")
+ assert.spy(fs.rmrf).was_not_called_with(destination_dir)
+ end)
+ )
+
+ it(
+ "should write receipt",
+ async_test(function()
+ spy.on(fs, "write_file")
+ local destination_dir = ("%s/installer_spec_receipt"):format(os.getenv "INSTALL_ROOT_DIR")
+ local ctx = InstallContext.new {
+ name = "installer_spec_receipt",
+ destination_dir = destination_dir,
+ boundary_path = os.getenv "INSTALL_ROOT_DIR",
+ stdio_sink = process.empty_sink(),
+ requested_version = Optional.empty(),
+ }
+ installer.execute(ctx, function(c)
+ c.receipt:with_primary_source(c.receipt.npm "my-pkg")
+ end)
+ assert.spy(fs.write_file).was_called(1)
+ assert.spy(fs.write_file).was_called_with(
+ ("%s.tmp/nvim-lsp-installer-receipt.json"):format(destination_dir),
+ match.is_string()
+ )
+ end)
+ )
+end)
diff --git a/tests/core/result_spec.lua b/tests/core/result_spec.lua
index cd10acb5..97c7da06 100644
--- a/tests/core/result_spec.lua
+++ b/tests/core/result_spec.lua
@@ -1,5 +1,6 @@
local Result = require "nvim-lsp-installer.core.result"
local match = require "luassert.match"
+local spy = require "luassert.spy"
describe("result", function()
it("should create success", function()
@@ -117,4 +118,26 @@ describe("result", function()
assert.is_true(result:is_failure())
assert.equals("Oh noes", result:err_or_nil())
end)
+
+ it("should run on_failure if failure", function()
+ local on_success = spy.new()
+ local on_failure = spy.new()
+ local result = Result.failure("Oh noes"):on_failure(on_failure):on_success(on_success)
+ assert.is_true(result:is_failure())
+ assert.equals("Oh noes", result:err_or_nil())
+ assert.spy(on_failure).was_called(1)
+ assert.spy(on_success).was_called(0)
+ assert.spy(on_failure).was_called_with "Oh noes"
+ end)
+
+ it("should run on_success if success", function()
+ local on_success = spy.new()
+ local on_failure = spy.new()
+ local result = Result.success("Oh noes"):on_failure(on_failure):on_success(on_success)
+ assert.is_true(result:is_success())
+ assert.equals("Oh noes", result:get_or_nil())
+ assert.spy(on_failure).was_called(0)
+ assert.spy(on_success).was_called(1)
+ assert.spy(on_success).was_called_with "Oh noes"
+ end)
end)
diff --git a/tests/helpers/lua/test_helpers.lua b/tests/helpers/lua/test_helpers.lua
index 23353fcb..206337e1 100644
--- a/tests/helpers/lua/test_helpers.lua
+++ b/tests/helpers/lua/test_helpers.lua
@@ -6,7 +6,6 @@ local a = require "nvim-lsp-installer.core.async"
local process = require "nvim-lsp-installer.process"
local server = require "nvim-lsp-installer.server"
local Optional = require "nvim-lsp-installer.core.optional"
-local Result = require "nvim-lsp-installer.core.result"
local receipt = require "nvim-lsp-installer.core.receipt"
function async_test(suspend_fn)
@@ -38,18 +37,18 @@ function ServerGenerator(opts)
languages = { "dummylang" },
root_dir = server.get_server_root_path "dummy",
homepage = "https://dummylang.org",
- installer = function(_, callback, ctx)
+ async = true,
+ installer = function(ctx)
ctx.stdio_sink.stdout "Installing dummy!\n"
- callback(true)
end,
}, opts))
end
function FailingServerGenerator(opts)
return ServerGenerator(vim.tbl_deep_extend("force", {
- installer = function(_, callback, ctx)
+ installer = function(ctx)
ctx.stdio_sink.stdout "Installing failing dummy!\n"
- callback(false)
+ error "Failed to do something."
end,
}, opts))
end
@@ -57,16 +56,20 @@ end
function InstallContextGenerator(opts)
---@type InstallContext
local default_opts = {
+ name = "mock",
fs = mock.new {
append_file = mockx.just_runs,
dir_exists = mockx.returns(true),
file_exists = mockx.returns(true),
},
spawn = mock.new {},
- cwd = function()
- return "/tmp/install-dir"
- end,
- promote_cwd = mockx.returns(Result.success()),
+ cwd = mock.new {
+ get = mockx.returns "/tmp/install-dir",
+ set = mockx.just_runs,
+ },
+ destination_dir = "/opt/install-dir",
+ stdio_sink = process.empty_sink(),
+ promote_cwd = mockx.just_runs,
receipt = receipt.InstallReceiptBuilder.new(),
requested_version = Optional.empty(),
}
diff --git a/tests/server_spec.lua b/tests/server_spec.lua
index 41a40750..d1ecbb6a 100644
--- a/tests/server_spec.lua
+++ b/tests/server_spec.lua
@@ -2,8 +2,6 @@ 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()
@@ -55,64 +53,27 @@ describe("server", function()
end)
)
- -- it(
- -- "should be able to run async installer functions",
- -- async_test(function()
- -- local srv = ServerGenerator {
- -- name = "async_installer_fixture",
- -- root_dir = server.get_server_root_path "async_installer_fixture",
- -- async = true,
- -- installer = function()
- -- a.sleep(130)
- -- end,
- -- }
- -- local start = timestamp()
- -- srv:install()
- -- a.sleep(100)
- -- assert.wait_for(function()
- -- assert.is_true(srv:is_installed())
- -- end)
- -- local stop = timestamp()
- -- assert.is_true(stop - start >= 100)
- -- end)
- -- )
-
it(
- "should remove directories upon installation failure",
+ "should be able to run sync installer functions",
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,
- },
+ local srv = ServerGenerator {
+ name = "async_installer_fixture",
+ root_dir = server.get_server_root_path "async_installer_fixture",
+ async = false,
+ installer = function(_, callback)
+ vim.defer_fn(function()
+ callback(true)
+ end, 130)
+ end,
}
+ local start = timestamp()
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()))
+ a.sleep(100)
+ assert.wait_for(function()
+ assert.is_true(srv:is_installed())
+ end)
+ local stop = timestamp()
+ assert.is_true(stop - start >= 100)
end)
)
end)