aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/terminator_spec.lua
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2023-10-11 16:31:50 +0200
committerWilliam Boman <william@redwill.se>2025-02-19 09:22:40 +0100
commit047ec18da56ad8f331e5c6bc7417dc5a9a6e71cc (patch)
treec50c22cd05d3605fc5a1e8eb902ffeb11e339697 /tests/mason-core/terminator_spec.lua
parentrefactor(receipt): change receipt structure and remove old builder APIs (#1521) (diff)
downloadmason-047ec18da56ad8f331e5c6bc7417dc5a9a6e71cc.tar
mason-047ec18da56ad8f331e5c6bc7417dc5a9a6e71cc.tar.gz
mason-047ec18da56ad8f331e5c6bc7417dc5a9a6e71cc.tar.bz2
mason-047ec18da56ad8f331e5c6bc7417dc5a9a6e71cc.tar.lz
mason-047ec18da56ad8f331e5c6bc7417dc5a9a6e71cc.tar.xz
mason-047ec18da56ad8f331e5c6bc7417dc5a9a6e71cc.tar.zst
mason-047ec18da56ad8f331e5c6bc7417dc5a9a6e71cc.zip
refactor!: refactor installer internals and add new Package class methods (#1523)
This contains the following changes: 1) `Package:install()` now accepts a second, optional, callback argument which is called when installation finishes (successfully or not). 2) Adds a `Package:is_installing()` method. This contains the following breaking changes: 1) `Package:install()` will now error when called while an installation is already ongoing. Use the new `Package:is_installing()` method to check whether an installation is already running. This also refactors large portions of the tests by removing test globals, removing async_test, and adding the `mason-test` Lua module instead. Test helpers via globals are problematic to work with due to not being detected through tools like the Lua language server without additional configuration. This has been replaced with a Lua module `mason-test`. `async_test` has also been removed in favour of explicitly making use of the `mason-core.async` API. These changes stands for a significant portion of the diff.
Diffstat (limited to 'tests/mason-core/terminator_spec.lua')
-rw-r--r--tests/mason-core/terminator_spec.lua167
1 files changed, 84 insertions, 83 deletions
diff --git a/tests/mason-core/terminator_spec.lua b/tests/mason-core/terminator_spec.lua
index 24c1ec25..29a3a1dd 100644
--- a/tests/mason-core/terminator_spec.lua
+++ b/tests/mason-core/terminator_spec.lua
@@ -8,112 +8,113 @@ local stub = require "luassert.stub"
local terminator = require "mason-core.terminator"
describe("terminator", function()
- it(
- "should terminate all active handles on nvim exit",
- async_test(function()
- spy.on(InstallHandle, "terminate")
- local dummy = registry.get_package "dummy"
- local dummy2 = registry.get_package "dummy2"
- for _, pkg in ipairs { dummy, dummy2 } do
- stub(pkg.spec.source, "install", function()
- a.sleep(10000)
- end)
- end
+ local snapshot
- local dummy_handle = dummy:install()
- local dummy2_handle = dummy2:install()
+ before_each(function()
+ snapshot = assert.snapshot()
+ end)
- assert.wait_for(function()
- assert.spy(dummy.spec.source.install).was_called()
- assert.spy(dummy2.spec.source.install).was_called()
+ after_each(function()
+ -- wait for scheduled calls to expire
+ a.run_blocking(a.wait, vim.schedule)
+ snapshot:revert()
+ end)
+
+ it("should terminate all active handles on nvim exit", function()
+ spy.on(InstallHandle, "terminate")
+ local dummy = registry.get_package "dummy"
+ local dummy2 = registry.get_package "dummy2"
+ for _, pkg in ipairs { dummy, dummy2 } do
+ stub(pkg.spec.source, "install", function()
+ a.sleep(10000)
end)
+ end
- terminator.terminate(5000)
+ local dummy_handle = dummy:install()
+ local dummy2_handle = dummy2:install()
- assert.spy(InstallHandle.terminate).was_called(2)
- assert.spy(InstallHandle.terminate).was_called_with(match.is_ref(dummy_handle))
- assert.spy(InstallHandle.terminate).was_called_with(match.is_ref(dummy2_handle))
- assert.wait_for(function()
- assert.is_true(dummy_handle:is_closed())
- assert.is_true(dummy2_handle:is_closed())
- end)
+ assert.wait(function()
+ assert.spy(dummy.spec.source.install).was_called()
+ assert.spy(dummy2.spec.source.install).was_called()
end)
- )
- it(
- "should print warning messages",
- async_test(function()
- spy.on(vim.api, "nvim_echo")
- spy.on(vim.api, "nvim_err_writeln")
- spy.on(InstallHandle, "terminate")
- local dummy = registry.get_package "dummy"
- local dummy2 = registry.get_package "dummy2"
- for _, pkg in ipairs { dummy, dummy2 } do
- stub(pkg.spec.source, "install", function()
- a.sleep(10000)
- end)
- end
+ terminator.terminate(5000)
- local dummy_handle = dummy:install()
- local dummy2_handle = dummy2:install()
+ assert.spy(InstallHandle.terminate).was_called(2)
+ assert.spy(InstallHandle.terminate).was_called_with(match.is_ref(dummy_handle))
+ assert.spy(InstallHandle.terminate).was_called_with(match.is_ref(dummy2_handle))
+ assert.wait(function()
+ assert.is_true(dummy_handle:is_closed())
+ assert.is_true(dummy2_handle:is_closed())
+ end)
+ end)
- assert.wait_for(function()
- assert.spy(dummy.spec.source.install).was_called()
- assert.spy(dummy2.spec.source.install).was_called()
+ it("should print warning messages", function()
+ spy.on(vim.api, "nvim_echo")
+ spy.on(vim.api, "nvim_err_writeln")
+ local dummy = registry.get_package "dummy"
+ local dummy2 = registry.get_package "dummy2"
+ for _, pkg in ipairs { dummy, dummy2 } do
+ stub(pkg.spec.source, "install", function()
+ a.sleep(10000)
end)
+ end
- terminator.terminate(5000)
+ local dummy_handle = dummy:install()
+ local dummy2_handle = dummy2:install()
- assert.spy(vim.api.nvim_echo).was_called(1)
- assert.spy(vim.api.nvim_echo).was_called_with({
- {
- "[mason.nvim] Neovim is exiting while packages are still installing. Terminating all installations…",
- "WarningMsg",
- },
- }, true, {})
+ assert.wait(function()
+ assert.spy(dummy.spec.source.install).was_called()
+ assert.spy(dummy2.spec.source.install).was_called()
+ end)
- a.wait(vim.schedule)
+ terminator.terminate(5000)
- assert.spy(vim.api.nvim_err_writeln).was_called(1)
- assert.spy(vim.api.nvim_err_writeln).was_called_with(_.dedent [[
+ assert.spy(vim.api.nvim_echo).was_called(1)
+ assert.spy(vim.api.nvim_echo).was_called_with({
+ {
+ "[mason.nvim] Neovim is exiting while packages are still installing. Terminating all installations…",
+ "WarningMsg",
+ },
+ }, true, {})
+
+ a.run_blocking(a.wait, vim.schedule)
+
+ assert.spy(vim.api.nvim_err_writeln).was_called(1)
+ assert.spy(vim.api.nvim_err_writeln).was_called_with(_.dedent [[
[mason.nvim] Neovim exited while the following packages were installing. Installation was aborted.
- dummy
- dummy2
]])
- assert.wait_for(function()
- assert.is_true(dummy_handle:is_closed())
- assert.is_true(dummy2_handle:is_closed())
- end)
+ assert.wait(function()
+ assert.is_true(dummy_handle:is_closed())
+ assert.is_true(dummy2_handle:is_closed())
end)
- )
+ end)
- it(
- "should send SIGTERM and then SIGKILL after grace period",
- async_test(function()
- spy.on(InstallHandle, "kill")
- local dummy = registry.get_package "dummy"
- stub(dummy.spec.source, "install")
- dummy.spec.source.install.invokes(function(ctx)
- -- your signals have no power here
- ctx.spawn.bash { "-c", "function noop { :; }; trap noop SIGTERM; sleep 999999;" }
- end)
+ it("should send SIGTERM and then SIGKILL after grace period", function()
+ spy.on(InstallHandle, "kill")
+ local dummy = registry.get_package "dummy"
+ stub(dummy.spec.source, "install", function(ctx)
+ -- your signals have no power here
+ ctx.spawn.bash { "-c", "function noop { :; }; trap noop SIGTERM; sleep 999999;" }
+ end)
- local handle = dummy:install()
+ local handle = dummy:install()
- assert.wait_for(function()
- assert.spy(dummy.spec.source.install).was_called()
- end)
- terminator.terminate(50)
+ assert.wait(function()
+ assert.spy(dummy.spec.source.install).was_called()
+ end)
+ terminator.terminate(50)
- assert.wait_for(function()
- assert.spy(InstallHandle.kill).was_called(2)
- assert.spy(InstallHandle.kill).was_called_with(match.is_ref(handle), 15) -- SIGTERM
- assert.spy(InstallHandle.kill).was_called_with(match.is_ref(handle), 9) -- SIGKILL
- end)
+ assert.wait(function()
+ assert.spy(InstallHandle.kill).was_called(2)
+ assert.spy(InstallHandle.kill).was_called_with(match.is_ref(handle), 15) -- SIGTERM
+ assert.spy(InstallHandle.kill).was_called_with(match.is_ref(handle), 9) -- SIGKILL
+ end)
- assert.wait_for(function()
- assert.is_true(handle:is_closed())
- end)
+ assert.wait(function()
+ assert.is_true(handle:is_closed())
end)
- )
+ end)
end)