Skip to content

Commit

Permalink
fix(command): run :MasonUpdate synchronously in headless mode (#1347)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamboman authored Jun 12, 2023
1 parent 5e12190 commit 0276793
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ INSTALL_ROOT_DIR:=$(shell pwd)/tests/fixtures/mason
NVIM_HEADLESS:=nvim --headless --noplugin -u tests/minimal_init.vim

dependencies:
git clone --depth 1 https://github.com/williamboman/mason-lspconfig.nvim dependencies/pack/vendor/start/mason-lspconfig.nvim
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim dependencies/pack/vendor/start/plenary.nvim
git clone --depth 1 https://github.com/nvim-neotest/neotest dependencies/pack/vendor/start/neotest

Expand Down
22 changes: 18 additions & 4 deletions lua/mason/api/command.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local _ = require "mason-core.functional"
local platform = require "mason-core.platform"

local function Mason()
require("mason.ui").open()
Expand Down Expand Up @@ -83,7 +84,6 @@ local function MasonInstall(package_specifiers, opts)
opts = opts or {}
local Package = require "mason-core.package"
local registry = require "mason-registry"
local is_headless = #vim.api.nvim_list_uis() == 0

local install_packages = _.map(function(pkg_specifier)
local package_name, version = Package.Parse(pkg_specifier)
Expand All @@ -96,7 +96,7 @@ local function MasonInstall(package_specifiers, opts)
}
end)

if is_headless then
if platform.is_headless then
registry.refresh()
local valid_packages = filter_valid_packages(package_specifiers)
if #valid_packages ~= #package_specifiers then
Expand Down Expand Up @@ -185,14 +185,28 @@ local function MasonUpdate()
local notify = require "mason-core.notify"
local registry = require "mason-registry"
notify "Updating registries…"
registry.update(vim.schedule_wrap(function(success, updated_registries)

---@param success boolean
---@param updated_registries RegistrySource[]
local function handle_result(success, updated_registries)
if success then
local count = #updated_registries
notify(("Successfully updated %d %s."):format(count, count == 1 and "registry" or "registries"))
else
notify(("Failed to update registries: %s"):format(updated_registries), vim.log.levels.ERROR)
end
end))
end

if platform.is_headless then
local a = require "mason-core.async"
a.run_blocking(function()
local success, updated_registries = a.wait(registry.update)
a.scheduler()
handle_result(success, updated_registries)
end)
else
registry.update(_.scheduler_wrap(handle_result))
end
end

vim.api.nvim_create_user_command("MasonUpdate", MasonUpdate, {
Expand Down
6 changes: 2 additions & 4 deletions tests/mason/api/command_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ describe(":MasonUpdate", function()
end)
spy.on(vim, "notify")
api.MasonUpdate()
assert.spy(vim.notify).was_called(1)
assert.spy(vim.notify).was_called(2)
assert.spy(vim.notify).was_called_with("Updating registries…", vim.log.levels.INFO, {
title = "mason.nvim",
})
a.wait(vim.schedule)
assert.spy(vim.notify).was_called_with("Successfully updated 1 registry.", vim.log.levels.INFO, {
title = "mason.nvim",
})
Expand All @@ -118,11 +117,10 @@ describe(":MasonUpdate", function()
end)
spy.on(vim, "notify")
api.MasonUpdate()
assert.spy(vim.notify).was_called(1)
assert.spy(vim.notify).was_called(2)
assert.spy(vim.notify).was_called_with("Updating registries…", vim.log.levels.INFO, {
title = "mason.nvim",
})
a.wait(vim.schedule)
assert.spy(vim.notify).was_called_with("Failed to update registries: Some error.", vim.log.levels.ERROR, {
title = "mason.nvim",
})
Expand Down

0 comments on commit 0276793

Please # to comment.