diff options
| author | William Boman <william@redwill.se> | 2023-02-23 07:38:08 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-23 07:38:08 +0100 |
| commit | 11e34da70dc531e5d917fd4303db2d81258f6ada (patch) | |
| tree | 5f43adfff3d89093174ff7af7e3681992b482d05 | |
| parent | chore: autogenerate (#1022) (diff) | |
| download | mason-11e34da70dc531e5d917fd4303db2d81258f6ada.tar mason-11e34da70dc531e5d917fd4303db2d81258f6ada.tar.gz mason-11e34da70dc531e5d917fd4303db2d81258f6ada.tar.bz2 mason-11e34da70dc531e5d917fd4303db2d81258f6ada.tar.lz mason-11e34da70dc531e5d917fd4303db2d81258f6ada.tar.xz mason-11e34da70dc531e5d917fd4303db2d81258f6ada.tar.zst mason-11e34da70dc531e5d917fd4303db2d81258f6ada.zip | |
tests: add LuaRegistrySource spec (#1027)
| -rw-r--r-- | lua/mason-registry/init.lua | 2 | ||||
| -rw-r--r-- | lua/mason-registry/sources/lua.lua | 5 | ||||
| -rw-r--r-- | tests/mason-registry/sources/lua_spec.lua | 50 | ||||
| -rw-r--r-- | vim.yml | 3 |
4 files changed, 58 insertions, 2 deletions
diff --git a/lua/mason-registry/init.lua b/lua/mason-registry/init.lua index a7cd4da7..f897095c 100644 --- a/lua/mason-registry/init.lua +++ b/lua/mason-registry/init.lua @@ -7,7 +7,7 @@ local EventEmitter = require "mason-core.EventEmitter" local sources = require "mason-registry.sources" ---@class RegistrySource ----@field get_package fun(self: RegistrySource, pkg_name: string): Package +---@field get_package fun(self: RegistrySource, pkg_name: string): Package? ---@field get_all_package_names fun(self: RegistrySource): string[] ---@field is_installed fun(self: RegistrySource): boolean ---@field install async fun(self: RegistrySource): Result diff --git a/lua/mason-registry/sources/lua.lua b/lua/mason-registry/sources/lua.lua index 94b4e4f8..d2032cc0 100644 --- a/lua/mason-registry/sources/lua.lua +++ b/lua/mason-registry/sources/lua.lua @@ -14,6 +14,8 @@ function LuaRegistrySource.new(spec) }, LuaRegistrySource) end +---@param pkg_name string +---@return Package? function LuaRegistrySource:get_package(pkg_name) local index = require(self.spec.mod) if index[pkg_name] then @@ -21,6 +23,7 @@ function LuaRegistrySource:get_package(pkg_name) end end +---@return string[] function LuaRegistrySource:get_all_package_names() local index = require(self.spec.mod) return vim.tbl_keys(index) @@ -37,7 +40,7 @@ function LuaRegistrySource:install() end function LuaRegistrySource:__tostring() - return ("LuaRegistry(mod=%s)"):format(self.spec.mod) + return ("LuaRegistrySource(mod=%s)"):format(self.spec.mod) end return LuaRegistrySource diff --git a/tests/mason-registry/sources/lua_spec.lua b/tests/mason-registry/sources/lua_spec.lua new file mode 100644 index 00000000..27a351d7 --- /dev/null +++ b/tests/mason-registry/sources/lua_spec.lua @@ -0,0 +1,50 @@ +local LuaRegistrySource = require "mason-registry.sources.lua" + +describe("Lua registry source", function() + it("should get package", function() + package.loaded["pkg-index"] = { + ["my-pkg"] = "pkg-index.my-pkg", + } + package.loaded["pkg-index.my-pkg"] = {} + local source = LuaRegistrySource.new { + mod = "pkg-index", + } + assert.is_not_nil(source:get_package "my-pkg") + assert.is_nil(source:get_package "non-existent") + end) + + it("should get all package names", function() + package.loaded["pkg-index"] = { + ["my-pkg"] = "pkg-index.my-pkg", + ["rust-analyzer"] = "pkg-index.rust-analyzer", + ["typescript-language-server"] = "pkg-index.typescript-language-server", + } + local source = LuaRegistrySource.new { + mod = "pkg-index", + } + local package_names = source:get_all_package_names() + table.sort(package_names) + assert.same({ + "my-pkg", + "rust-analyzer", + "typescript-language-server", + }, package_names) + end) + + it("should check if is installed", function() + package.loaded["pkg-index"] = {} + local installed_source = LuaRegistrySource.new { + mod = "pkg-index", + } + local uninstalled_source = LuaRegistrySource.new { + mod = "non-existent", + } + + assert.is_true(installed_source:is_installed()) + assert.is_false(uninstalled_source:is_installed()) + end) + + it("should stringify instances", function() + assert.equals("LuaRegistrySource(mod=pkg-index)", tostring(LuaRegistrySource.new { mod = "pkg-index" })) + end) +end) @@ -66,6 +66,9 @@ globals: assert.is_nil: args: - type: any + assert.is_not_nil: + args: + - type: any assert.spy: any: true assert.snapshot: |
