Skip to content

Commit 953c279

Browse files
committed
fix(util): made Util.lsmod more robust. See #298
1 parent f36c7cb commit 953c279

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

lua/lazy/core/cache.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,14 @@ function M.get_topmods(path)
257257
end
258258

259259
---@param modname string
260-
---@return string?, string?
261-
function M.find_dir(modname)
260+
---@return string?
261+
function M.find_root(modname)
262262
if M.cache[modname] then
263263
-- check if modname is in cache
264264
local modpath = M.cache[modname].modpath
265265
if M.check_path(modname, modpath) then
266266
local root = modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
267-
return root, modpath
267+
return root
268268
end
269269
else
270270
-- in case modname is just a directory and not a real mod,
@@ -286,7 +286,7 @@ function M.find_dir(modname)
286286
local modpath = M.find(modname, { patterns = { "" } })
287287
if modpath then
288288
local root = modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
289-
return root, (modpath ~= root and modpath or nil)
289+
return root
290290
end
291291
end
292292

lua/lazy/core/util.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,13 @@ end
195195
---@param fn fun(modname:string, modpath:string)
196196
function M.lsmod(modname, fn)
197197
local Cache = require("lazy.core.cache")
198-
local root, modpath = Cache.find_dir(modname)
198+
local root = Cache.find_root(modname)
199199
if not root then
200200
return
201201
end
202202

203-
if modpath and not modpath:find("/init%.lua$") and vim.loop.fs_stat(modpath) then
204-
fn(modname, modpath)
203+
if vim.loop.fs_stat(root .. ".lua") then
204+
fn(modname, root .. ".lua")
205205
end
206206

207207
M.ls(root, function(path, name, type)

tests/core/util_spec.lua

+45-8
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,68 @@ describe("util", function()
1010
it("lsmod lists all mods in dir", function()
1111
local tests = {
1212
{
13+
root = "lua/foo",
14+
mod = "foo",
1315
files = { "lua/foo/one.lua", "lua/foo/two.lua", "lua/foo/init.lua" },
14-
mods = { "foo", "foo.one", "foo.two" },
16+
mods = { "foo.one", "foo.two", "foo" },
1517
},
1618
{
19+
root = "lua/foo",
20+
mod = "foo",
1721
files = { "lua/foo/one.lua", "lua/foo/two.lua", "lua/foo.lua" },
18-
mods = { "foo", "foo.one", "foo.two" },
22+
mods = { "foo.one", "foo.two", "foo" },
1923
},
2024
{
25+
root = "lua/foo",
26+
mod = "foo",
2127
files = { "lua/foo/one.lua", "lua/foo/two.lua" },
2228
mods = { "foo.one", "foo.two" },
2329
},
30+
{
31+
root = "lua/load-plugins",
32+
mod = "load-plugins",
33+
files = { "lua/load-plugins.lua" },
34+
mods = { "load-plugins" },
35+
},
2436
}
2537

2638
vim.opt.rtp:append(Helpers.path(""))
27-
for _, test in ipairs(tests) do
28-
Cache.cache = {}
29-
table.sort(test.mods)
39+
for t, test in ipairs(tests) do
40+
local expected = vim.deepcopy(test.mods)
41+
table.sort(expected)
3042
Helpers.fs_rm("")
31-
Helpers.fs_create(test.files)
43+
local files = Helpers.fs_create(test.files)
44+
45+
-- test with empty cache
46+
Cache.cache = {}
47+
Cache.indexed = {}
48+
Cache.indexed_rtp = false
49+
local root = Cache.find_root(test.mod)
50+
assert(root, "no root found for " .. test.mod .. " (test " .. t .. ")")
51+
assert.same(Helpers.path(test.root), root)
3252
local mods = {}
33-
Util.lsmod("foo", function(modname, modpath)
53+
Util.lsmod(test.mod, function(modname, modpath)
54+
mods[#mods + 1] = modname
55+
end)
56+
table.sort(mods)
57+
assert.same(expected, mods)
58+
59+
-- fill the cache
60+
Cache.cache = {}
61+
for i, file in ipairs(files) do
62+
Cache.cache[test.mods[i]] = { modpath = file }
63+
end
64+
Cache.indexed = {}
65+
Cache.indexed_rtp = false
66+
root = Cache.find_root(test.mod)
67+
assert(root, "no root found for " .. test.mod .. " (test " .. t .. ")")
68+
assert.same(Helpers.path(test.root), root)
69+
mods = {}
70+
Util.lsmod(test.mod, function(modname, modpath)
3471
mods[#mods + 1] = modname
3572
end)
3673
table.sort(mods)
37-
assert.same(test.mods, mods)
74+
assert.same(expected, mods)
3875
end
3976
end)
4077
end)

0 commit comments

Comments
 (0)