diff options
| -rw-r--r-- | lua/mason-registry/init.lua | 22 | ||||
| -rw-r--r-- | lua/mason/settings.lua | 13 | ||||
| -rw-r--r-- | tests/mason-registry/registry_spec.lua | 48 |
3 files changed, 75 insertions, 8 deletions
diff --git a/lua/mason-registry/init.lua b/lua/mason-registry/init.lua index 1e59175c..523a1256 100644 --- a/lua/mason-registry/init.lua +++ b/lua/mason-registry/init.lua @@ -2,6 +2,7 @@ local EventEmitter = require "mason-core.EventEmitter" local InstallLocation = require "mason-core.installer.InstallLocation" local log = require "mason-core.log" local path = require "mason-core.path" +local settings = require "mason.settings" local uv = vim.loop local LazySourceCollection = require "mason-registry.sources" @@ -165,34 +166,39 @@ function Registry.update(callback) a.run(update, callback or noop, Registry.sources) end -local REGISTRY_STORE_TTL = 86400 -- 24 hrs - ---@param sources LazySourceCollection ---@param callback? RegistryUpdateCallback local function refresh(sources, callback) + if not settings.current.registry_cache.refresh then + log.debug "Not performing a registry refresh as it's disabled in settings." + if callback then + return callback(true, {}) + end + return true, {} + end local a = require "mason-core.async" local state = sources:get_install_state() if state and sources:is_all_installed() then local registry_age = os.time() - state.timestamp - if registry_age <= REGISTRY_STORE_TTL and state.checksum == sources:checksum() then + if registry_age <= settings.current.registry_cache.duration and state.checksum == sources:checksum() then log.fmt_debug( "Registry refresh is not necessary yet. Registry age=%d, checksum=%s", registry_age, state.checksum ) if callback then - callback(true, {}) + return callback(true, {}) end - return + return true, {} end end if not callback then -- We don't want to error in the synchronous version because of how this function is recommended to be used in -- 3rd party code. If accessing the update result is required, users are recommended to pass a callback. - pcall(a.run_blocking, update, sources) + return pcall(a.run_blocking, update, sources) else a.run(update, callback, sources) end @@ -201,13 +207,13 @@ end ---@param callback? RegistryUpdateCallback function Registry.refresh(callback) log.debug "Refreshing the registry." - refresh(Registry.sources, callback) + return refresh(Registry.sources, callback) end ---@param callback? RegistryUpdateCallback function Registry.refresh_system(callback) log.debug "Refreshing the system registry." - refresh(Registry.system_sources, callback) + return refresh(Registry.system_sources, callback) end return Registry diff --git a/lua/mason/settings.lua b/lua/mason/settings.lua index 1788c035..1c72756b 100644 --- a/lua/mason/settings.lua +++ b/lua/mason/settings.lua @@ -42,6 +42,19 @@ local DEFAULT_SETTINGS = { "github:mason-org/mason-system-registry", }, + registry_cache = { + ---@since 2.3.0 + -- [Advanced setting] + -- Whether Mason should automatically refresh the registry when needed. If false, the registry will have to be + -- updated manually via :MasonUpdate or the :Mason UI. + refresh = true, + + ---@since 2.3.0 + -- Amount of seconds before the local registry cache is considered stale. + -- Note that this setting has no effect if refresh is set to false. + duration = 24 * 60 * 60, -- 24 hours + }, + ---@since 1.0.0 -- The provider implementations to use for resolving supplementary package metadata (e.g., all available versions). -- Accepts multiple entries, where later entries will be used as fallback should prior providers fail. diff --git a/tests/mason-registry/registry_spec.lua b/tests/mason-registry/registry_spec.lua index daa8fc9f..cf909667 100644 --- a/tests/mason-registry/registry_spec.lua +++ b/tests/mason-registry/registry_spec.lua @@ -1,5 +1,7 @@ local Pkg = require "mason-core.package" +local match = require "luassert.match" local registry = require "mason-registry" +local spy = require "luassert.spy" local test_helpers = require "mason-test.helpers" describe("mason-registry", function() @@ -33,4 +35,50 @@ describe("mason-registry", function() test_helpers.sync_install(dummy) assert.is_true(registry.is_installed "dummy") end) + + describe("refresh/update", function() + local a = require "mason-core.async" + local settings = require "mason.settings" + local installer = require "mason-registry.installer" + + after_each(function() + settings.set(settings._DEFAULT_SETTINGS) + end) + + it("should refresh registry synchronously", function() + local ok, updated_registries = registry.refresh() + assert.is_true(ok) + assert.same({}, updated_registries) + end) + + it("should call registry.refresh callback", function() + local spy = spy.new() + registry.refresh(spy) + assert.wait(function() + assert.spy(spy).was_called(1) + assert.spy(spy).was_called_with(true, {}) + end) + end) + + it("should call registry.update callback", function() + local spy = spy.new() + registry.update(spy) + assert.wait(function() + assert.spy(spy).was_called(1) + assert.spy(spy).was_called_with(true, match.is_table()) + end) + end) + + it("should immediately return if refresh is disabled", function() + settings.current.registry_cache.refresh = false + local ok, registries = registry.refresh() + assert.is_true(ok) + assert.same({}, registries) + + local spy = spy.new() + registry.refresh(spy) + assert.spy(spy).was_called(1) + assert.spy(spy).was_called_with(true, {}) + end) + end) end) |
