diff options
| author | William Boman <william@redwill.se> | 2023-11-07 00:29:18 +0100 |
|---|---|---|
| committer | William Boman <william@redwill.se> | 2025-02-19 12:15:48 +0100 |
| commit | 6a7662760c515c74f2c37fc825776ead65d307f9 (patch) | |
| tree | 0f4496d0678c7029b10236cbf48cc0f5ff63c1dc /tests/mason-core/package/package_spec.lua | |
| parent | fix(pypi): remove -U flag and fix log message (diff) | |
| download | mason-6a7662760c515c74f2c37fc825776ead65d307f9.tar mason-6a7662760c515c74f2c37fc825776ead65d307f9.tar.gz mason-6a7662760c515c74f2c37fc825776ead65d307f9.tar.bz2 mason-6a7662760c515c74f2c37fc825776ead65d307f9.tar.lz mason-6a7662760c515c74f2c37fc825776ead65d307f9.tar.xz mason-6a7662760c515c74f2c37fc825776ead65d307f9.tar.zst mason-6a7662760c515c74f2c37fc825776ead65d307f9.zip | |
refactor!: change Package API
This changes the following public APIs:
**(_breaking_) Events on the `Package` class**
The `uninstall:success` event on the `Package` class now receives an `InstallReceipt` as argument, instead of an
`InstallHandle`. This receipt is an in-memory representation of what was uninstalled. There's also a new
`uninstall:failed` event for situations where uninstallation for some
reason fails. Note: this also applies to the registry events (i.e.
`package:uninstall:success` and `package:uninstall:failed`).
---
**(_breaking_) `Package:uninstall()` is now asynchronous and receives two new arguments, similarly to `Package:install()`**
While package uninstallations remain synchronous under the hood, the public API has been changed from synchronous ->
asynchronous. Users of this method are recommended to provide a callback in situations where code needs to execute after
uninstallation fully completes.
---
**(_breaking_) `Package:get_install_path()` has been removed.
---
**`Package:install()` now takes an optional callback**
This callback allows consumers to be informed whether installation was successful or not without having to go through a
different, low-level, API. See below for a comparison between the old and new APIs:
```lua
-- before
local handle = pkg:install()
handle:once("closed", function ()
-- ...
end)
-- after
pkg:install({}, function (success, result)
-- ...
end)
```
Diffstat (limited to 'tests/mason-core/package/package_spec.lua')
| -rw-r--r-- | tests/mason-core/package/package_spec.lua | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/tests/mason-core/package/package_spec.lua b/tests/mason-core/package/package_spec.lua index b9b15d04..5f69ea4e 100644 --- a/tests/mason-core/package/package_spec.lua +++ b/tests/mason-core/package/package_spec.lua @@ -2,26 +2,27 @@ local Pkg = require "mason-core.package" local a = require "mason-core.async" local match = require "luassert.match" local mock = require "luassert.mock" +local receipt = require "mason-core.receipt" local registry = require "mason-registry" local spy = require "luassert.spy" local stub = require "luassert.stub" +local test_helpers = require "mason-test.helpers" -describe("package", function() +describe("Package ::", function() local snapshot before_each(function() snapshot = assert.snapshot() + local dummy = registry.get_package "dummy" + if dummy:is_installed() then + test_helpers.sync_uninstall(dummy) + end end) after_each(function() snapshot:revert() end) - before_each(function() - registry.get_package("dummy"):uninstall() - package.loaded["dummy_package"] = nil - end) - it("should parse package specifiers", function() local function parse(str) local name, version = Pkg.Parse(str) @@ -91,28 +92,27 @@ describe("package", function() it("should create new handle", function() local dummy = registry.get_package "dummy" - -- yo dawg - local handle_handler = spy.new() - dummy:once("handle", handle_handler) - local handle = dummy:new_handle() - assert.spy(handle_handler).was_called(1) - assert.spy(handle_handler).was_called_with(match.ref(handle)) + local callback = spy.new() + dummy:once("install:handle", callback) + local handle = dummy:new_install_handle() + assert.spy(callback).was_called(1) + assert.spy(callback).was_called_with(match.ref(handle)) handle:close() end) it("should not create new handle if one already exists", function() local dummy = registry.get_package "dummy" - dummy.handle = mock.new { + dummy.install_handle = mock.new { is_closed = mockx.returns(false), } local handle_handler = spy.new() - dummy:once("handle", handle_handler) + dummy:once("install:handle", handle_handler) local err = assert.has_error(function() - dummy:new_handle() + dummy:new_install_handle() end) - assert.equals("Cannot create new handle because existing handle is not closed.", err) + assert.equals("Cannot create new install handle because existing handle is not closed.", err) assert.spy(handle_handler).was_called(0) - dummy.handle = nil + dummy.install_handle = nil end) it("should successfully install package", function() @@ -135,9 +135,11 @@ describe("package", function() assert.wait(function() assert.spy(install_success_handler).was_called(1) - assert.spy(install_success_handler).was_called_with(match.is_ref(handle)) + assert.spy(install_success_handler).was_called_with(match.instanceof(receipt.InstallReceipt)) assert.spy(package_install_success_handler).was_called(1) - assert.spy(package_install_success_handler).was_called_with(match.is_ref(dummy), match.is_ref(handle)) + assert + .spy(package_install_success_handler) + .was_called_with(match.is_ref(dummy), match.instanceof(receipt.InstallReceipt)) assert.spy(package_install_failed_handler).was_called(0) assert.spy(install_failed_handler).was_called(0) end) @@ -166,11 +168,11 @@ describe("package", function() assert.wait(function() assert.spy(install_failed_handler).was_called(1) - assert.spy(install_failed_handler).was_called_with(match.is_ref(handle), "I simply refuse to be installed.") + assert.spy(install_failed_handler).was_called_with "I simply refuse to be installed." assert.spy(package_install_failed_handler).was_called(1) assert .spy(package_install_failed_handler) - .was_called_with(match.is_ref(dummy), match.is_ref(handle), "I simply refuse to be installed.") + .was_called_with(match.is_ref(dummy), "I simply refuse to be installed.") assert.spy(package_install_success_handler).was_called(0) assert.spy(install_success_handler).was_called(0) end) @@ -200,7 +202,7 @@ describe("package", function() local dummy = registry.get_package "registry" -- Move outside the main loop - a.run_blocking(function () + a.run_blocking(function() a.wait(function(resolve) local timer = vim.loop.new_timer() timer:start(0, 0, function() |
