2023-01-02 03:19:09 +08:00
|
|
|
local Util = require("lazy.util")
|
|
|
|
local Cache = require("lazy.core.cache")
|
|
|
|
local Helpers = require("tests.helpers")
|
|
|
|
|
|
|
|
describe("util", function()
|
2023-01-08 05:24:46 +08:00
|
|
|
local rtp = vim.opt.rtp:get()
|
2023-01-02 03:19:09 +08:00
|
|
|
before_each(function()
|
2023-01-08 05:24:46 +08:00
|
|
|
vim.opt.rtp = rtp
|
|
|
|
for k, v in pairs(package.loaded) do
|
|
|
|
if k:find("^foobar") then
|
|
|
|
package.loaded[k] = nil
|
|
|
|
end
|
|
|
|
end
|
2023-01-02 03:19:09 +08:00
|
|
|
Helpers.fs_rm("")
|
2023-01-08 05:52:49 +08:00
|
|
|
assert(not vim.loop.fs_stat(Helpers.path("")), "fs root should be deleted")
|
2023-01-02 03:19:09 +08:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("lsmod lists all mods in dir", function()
|
2023-01-08 05:24:46 +08:00
|
|
|
vim.opt.rtp:append(Helpers.path(""))
|
2023-01-02 03:19:09 +08:00
|
|
|
local tests = {
|
|
|
|
{
|
2023-01-04 05:31:18 +08:00
|
|
|
root = "lua/foo",
|
|
|
|
mod = "foo",
|
2023-01-02 03:19:09 +08:00
|
|
|
files = { "lua/foo/one.lua", "lua/foo/two.lua", "lua/foo/init.lua" },
|
2023-01-04 05:31:18 +08:00
|
|
|
mods = { "foo.one", "foo.two", "foo" },
|
2023-01-02 03:19:09 +08:00
|
|
|
},
|
|
|
|
{
|
2023-01-04 05:31:18 +08:00
|
|
|
root = "lua/foo",
|
|
|
|
mod = "foo",
|
2023-01-02 03:19:09 +08:00
|
|
|
files = { "lua/foo/one.lua", "lua/foo/two.lua", "lua/foo.lua" },
|
2023-01-04 05:31:18 +08:00
|
|
|
mods = { "foo.one", "foo.two", "foo" },
|
2023-01-02 03:19:09 +08:00
|
|
|
},
|
|
|
|
{
|
2023-01-04 05:31:18 +08:00
|
|
|
root = "lua/foo",
|
|
|
|
mod = "foo",
|
2023-01-02 03:19:09 +08:00
|
|
|
files = { "lua/foo/one.lua", "lua/foo/two.lua" },
|
|
|
|
mods = { "foo.one", "foo.two" },
|
|
|
|
},
|
2023-01-04 05:31:18 +08:00
|
|
|
{
|
|
|
|
root = "lua/load-plugins",
|
|
|
|
mod = "load-plugins",
|
|
|
|
files = { "lua/load-plugins.lua" },
|
|
|
|
mods = { "load-plugins" },
|
|
|
|
},
|
2023-01-02 03:19:09 +08:00
|
|
|
}
|
|
|
|
|
2023-01-04 05:31:18 +08:00
|
|
|
for t, test in ipairs(tests) do
|
|
|
|
local expected = vim.deepcopy(test.mods)
|
|
|
|
table.sort(expected)
|
2023-01-02 03:19:09 +08:00
|
|
|
Helpers.fs_rm("")
|
2023-01-04 05:31:18 +08:00
|
|
|
local files = Helpers.fs_create(test.files)
|
|
|
|
|
|
|
|
-- test with empty cache
|
|
|
|
Cache.cache = {}
|
|
|
|
Cache.indexed = {}
|
|
|
|
Cache.indexed_rtp = false
|
|
|
|
local root = Cache.find_root(test.mod)
|
|
|
|
assert(root, "no root found for " .. test.mod .. " (test " .. t .. ")")
|
|
|
|
assert.same(Helpers.path(test.root), root)
|
2023-01-02 03:19:09 +08:00
|
|
|
local mods = {}
|
2023-01-04 05:31:18 +08:00
|
|
|
Util.lsmod(test.mod, function(modname, modpath)
|
|
|
|
mods[#mods + 1] = modname
|
|
|
|
end)
|
|
|
|
table.sort(mods)
|
|
|
|
assert.same(expected, mods)
|
|
|
|
|
|
|
|
-- fill the cache
|
|
|
|
Cache.cache = {}
|
|
|
|
for i, file in ipairs(files) do
|
|
|
|
Cache.cache[test.mods[i]] = { modpath = file }
|
|
|
|
end
|
|
|
|
Cache.indexed = {}
|
|
|
|
Cache.indexed_rtp = false
|
|
|
|
root = Cache.find_root(test.mod)
|
|
|
|
assert(root, "no root found for " .. test.mod .. " (test " .. t .. ")")
|
|
|
|
assert.same(Helpers.path(test.root), root)
|
|
|
|
mods = {}
|
|
|
|
Util.lsmod(test.mod, function(modname, modpath)
|
2023-01-02 03:19:09 +08:00
|
|
|
mods[#mods + 1] = modname
|
|
|
|
end)
|
|
|
|
table.sort(mods)
|
2023-01-04 05:31:18 +08:00
|
|
|
assert.same(expected, mods)
|
2023-01-02 03:19:09 +08:00
|
|
|
end
|
|
|
|
end)
|
2023-01-08 05:24:46 +08:00
|
|
|
|
|
|
|
it("find the correct root with dels", function()
|
|
|
|
Cache.cache = {}
|
|
|
|
Cache.indexed = {}
|
|
|
|
Cache.indexed_rtp = false
|
|
|
|
vim.opt.rtp:append(Helpers.path("old"))
|
|
|
|
Helpers.fs_create({ "old/lua/foobar/init.lua" })
|
2023-01-08 05:52:49 +08:00
|
|
|
Cache.cache["foobar"] = { modpath = Helpers.path("old/lua/foobar/init.lua") }
|
2023-01-08 05:24:46 +08:00
|
|
|
local root = Cache.find_root("foobar")
|
|
|
|
assert(root, "foobar root not found")
|
|
|
|
assert.same(Helpers.path("old/lua/foobar"), root)
|
|
|
|
|
2023-01-08 05:52:49 +08:00
|
|
|
Helpers.fs_rm("old")
|
|
|
|
assert(not vim.loop.fs_stat(Helpers.path("old/lua/foobar")), "old/lua/foobar should not exist")
|
2023-01-08 05:24:46 +08:00
|
|
|
|
|
|
|
-- vim.opt.rtp = rtp
|
|
|
|
Cache.indexed = {}
|
|
|
|
Cache.indexed_rtp = false
|
|
|
|
vim.opt.rtp:append(Helpers.path("new"))
|
|
|
|
Helpers.fs_create({ "new/lua/foobar/init.lua" })
|
|
|
|
root = Cache.find_root("foobar")
|
|
|
|
assert(root, "foobar root not found")
|
|
|
|
assert.same(Helpers.path("new/lua/foobar"), root)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("find the correct root with mod dels", function()
|
|
|
|
Cache.cache = {}
|
|
|
|
Cache.indexed = {}
|
|
|
|
Cache.indexed_rtp = false
|
|
|
|
Cache.enabled = true
|
|
|
|
vim.opt.rtp:append(Helpers.path("old"))
|
|
|
|
Helpers.fs_create({ "old/lua/foobar/test.lua" })
|
|
|
|
Cache.cache["foobar.test"] = { modpath = Helpers.path("old/lua/foobar/test.lua") }
|
|
|
|
local root = Cache.find_root("foobar")
|
|
|
|
assert(root, "foobar root not found")
|
|
|
|
assert.same(Helpers.path("old/lua/foobar"), root)
|
|
|
|
assert(not Cache.cache["foobar"], "foobar should not be in cache")
|
|
|
|
assert(Cache.cache["foobar.test"], "foobar.test not found in cache")
|
|
|
|
|
2023-01-08 05:52:49 +08:00
|
|
|
Helpers.fs_rm("old")
|
2023-01-08 05:24:46 +08:00
|
|
|
|
|
|
|
-- vim.opt.rtp = rtp
|
|
|
|
Cache.indexed = {}
|
|
|
|
Cache.indexed_rtp = false
|
|
|
|
vim.opt.rtp:append(Helpers.path("new"))
|
|
|
|
Helpers.fs_create({ "new/lua/foobar/test.lua" })
|
|
|
|
root = Cache.find_root("foobar")
|
|
|
|
assert(root, "foobar root not found")
|
|
|
|
assert.same(Helpers.path("new/lua/foobar"), root)
|
|
|
|
end)
|
2023-01-02 03:19:09 +08:00
|
|
|
end)
|