2023-01-01 19:19:09 +00:00
|
|
|
local Cache = require("lazy.core.cache")
|
|
|
|
local Helpers = require("tests.helpers")
|
2023-10-09 10:25:42 +01:00
|
|
|
local Util = require("lazy.util")
|
2023-01-01 19:19:09 +00:00
|
|
|
|
|
|
|
describe("util", function()
|
2023-01-07 21:24:46 +00:00
|
|
|
local rtp = vim.opt.rtp:get()
|
2023-01-01 19:19:09 +00:00
|
|
|
before_each(function()
|
2024-06-04 05:58:19 +01:00
|
|
|
---@type vim.Option
|
2023-01-07 21:24:46 +00: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-01 19:19:09 +00:00
|
|
|
Helpers.fs_rm("")
|
2024-03-22 07:58:36 +00:00
|
|
|
assert(not vim.uv.fs_stat(Helpers.path("")), "fs root should be deleted")
|
2023-01-01 19:19:09 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("lsmod lists all mods in dir", function()
|
2023-01-07 21:24:46 +00:00
|
|
|
vim.opt.rtp:append(Helpers.path(""))
|
2023-01-01 19:19:09 +00:00
|
|
|
local tests = {
|
|
|
|
{
|
2023-01-03 21:31:18 +00:00
|
|
|
root = "lua/foo",
|
|
|
|
mod = "foo",
|
2023-01-01 19:19:09 +00:00
|
|
|
files = { "lua/foo/one.lua", "lua/foo/two.lua", "lua/foo/init.lua" },
|
2023-01-03 21:31:18 +00:00
|
|
|
mods = { "foo.one", "foo.two", "foo" },
|
2023-01-01 19:19:09 +00:00
|
|
|
},
|
|
|
|
{
|
2023-01-03 21:31:18 +00:00
|
|
|
root = "lua/foo",
|
|
|
|
mod = "foo",
|
2023-01-01 19:19:09 +00:00
|
|
|
files = { "lua/foo/one.lua", "lua/foo/two.lua", "lua/foo.lua" },
|
2023-01-03 21:31:18 +00:00
|
|
|
mods = { "foo.one", "foo.two", "foo" },
|
2023-01-01 19:19:09 +00:00
|
|
|
},
|
|
|
|
{
|
2023-01-03 21:31:18 +00:00
|
|
|
root = "lua/foo",
|
|
|
|
mod = "foo",
|
2023-01-01 19:19:09 +00:00
|
|
|
files = { "lua/foo/one.lua", "lua/foo/two.lua" },
|
|
|
|
mods = { "foo.one", "foo.two" },
|
|
|
|
},
|
2023-01-03 21:31:18 +00:00
|
|
|
{
|
|
|
|
root = "lua/load-plugins",
|
|
|
|
mod = "load-plugins",
|
|
|
|
files = { "lua/load-plugins.lua" },
|
|
|
|
mods = { "load-plugins" },
|
|
|
|
},
|
2023-01-01 19:19:09 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 21:31:18 +00:00
|
|
|
for t, test in ipairs(tests) do
|
|
|
|
local expected = vim.deepcopy(test.mods)
|
|
|
|
table.sort(expected)
|
2023-01-01 19:19:09 +00:00
|
|
|
Helpers.fs_rm("")
|
2023-01-03 21:31:18 +00:00
|
|
|
local files = Helpers.fs_create(test.files)
|
|
|
|
|
|
|
|
-- test with empty cache
|
2023-04-16 06:39:39 +01:00
|
|
|
Cache.reset()
|
2023-02-13 11:01:56 +00:00
|
|
|
local root = Util.find_root(test.mod)
|
2023-01-03 21:31:18 +00:00
|
|
|
assert(root, "no root found for " .. test.mod .. " (test " .. t .. ")")
|
|
|
|
assert.same(Helpers.path(test.root), root)
|
2023-01-01 19:19:09 +00:00
|
|
|
local mods = {}
|
2023-01-03 21:31:18 +00:00
|
|
|
Util.lsmod(test.mod, function(modname, modpath)
|
|
|
|
mods[#mods + 1] = modname
|
|
|
|
end)
|
|
|
|
table.sort(mods)
|
|
|
|
assert.same(expected, mods)
|
|
|
|
|
|
|
|
-- fill the cache
|
2023-04-16 06:39:39 +01:00
|
|
|
Cache.reset()
|
2023-02-13 11:01:56 +00:00
|
|
|
root = Util.find_root(test.mod)
|
2023-01-03 21:31:18 +00:00
|
|
|
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-01 19:19:09 +00:00
|
|
|
mods[#mods + 1] = modname
|
|
|
|
end)
|
|
|
|
table.sort(mods)
|
2023-01-03 21:31:18 +00:00
|
|
|
assert.same(expected, mods)
|
2023-01-01 19:19:09 +00:00
|
|
|
end
|
|
|
|
end)
|
2023-01-07 21:24:46 +00:00
|
|
|
|
|
|
|
it("find the correct root with dels", function()
|
2023-04-16 06:39:39 +01:00
|
|
|
Cache.reset()
|
2023-01-07 21:24:46 +00:00
|
|
|
vim.opt.rtp:append(Helpers.path("old"))
|
|
|
|
Helpers.fs_create({ "old/lua/foobar/init.lua" })
|
2023-02-13 11:01:56 +00:00
|
|
|
local root = Util.find_root("foobar")
|
2023-01-07 21:24:46 +00:00
|
|
|
assert(root, "foobar root not found")
|
|
|
|
assert.same(Helpers.path("old/lua/foobar"), root)
|
|
|
|
|
2023-01-07 21:52:49 +00:00
|
|
|
Helpers.fs_rm("old")
|
2024-03-22 07:58:36 +00:00
|
|
|
assert(not vim.uv.fs_stat(Helpers.path("old/lua/foobar")), "old/lua/foobar should not exist")
|
2023-01-07 21:24:46 +00:00
|
|
|
|
|
|
|
-- vim.opt.rtp = rtp
|
|
|
|
vim.opt.rtp:append(Helpers.path("new"))
|
|
|
|
Helpers.fs_create({ "new/lua/foobar/init.lua" })
|
2023-02-13 11:01:56 +00:00
|
|
|
root = Util.find_root("foobar")
|
2023-01-07 21:24:46 +00:00
|
|
|
assert(root, "foobar root not found")
|
|
|
|
assert.same(Helpers.path("new/lua/foobar"), root)
|
|
|
|
end)
|
|
|
|
|
2023-01-10 06:32:28 +00:00
|
|
|
it("merges correctly", function()
|
|
|
|
local tests = {
|
|
|
|
{
|
|
|
|
input = { { a = 1 }, { b = 2 } },
|
|
|
|
output = { a = 1, b = 2 },
|
|
|
|
},
|
2023-10-13 10:37:52 +01:00
|
|
|
{
|
|
|
|
input = { nil, { a = 1 }, { b = 2 } },
|
|
|
|
output = { a = 1, b = 2 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input = { { a = 1 }, { b = 2 }, nil },
|
|
|
|
output = { a = 1, b = 2 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input = { { a = 1 }, nil, { b = 2 } },
|
|
|
|
output = { a = 1, b = 2 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input = { nil, { a = 1 }, nil, { b = 2 }, nil },
|
|
|
|
output = { a = 1, b = 2 },
|
|
|
|
},
|
2023-01-10 06:32:28 +00:00
|
|
|
{
|
|
|
|
input = { { a = 1 }, { a = 2 } },
|
|
|
|
output = { a = 2 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input = { { a = { 1, 2 } }, { a = { 3 } } },
|
|
|
|
output = { a = { 3 } },
|
|
|
|
},
|
|
|
|
{
|
2023-01-12 12:16:23 +00:00
|
|
|
input = { { b = { 1, 2 } }, { a = { 3 }, b = vim.NIL } },
|
2023-01-10 06:32:28 +00:00
|
|
|
output = { a = { 3 } },
|
|
|
|
},
|
|
|
|
{
|
2023-01-12 12:16:23 +00:00
|
|
|
input = { { a = 1 }, { b = 2, a = vim.NIL } },
|
2023-01-10 06:32:28 +00:00
|
|
|
output = { b = 2 },
|
|
|
|
},
|
2024-06-06 22:23:49 +01:00
|
|
|
{
|
|
|
|
input = { { 1, 2, __extend = true }, { 3, 4 } },
|
|
|
|
output = { 1, 2, 3, 4, __extend = true },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input = { { 1, 2, __extend = true }, { __extend = true, 3, 4 } },
|
|
|
|
output = { 1, 2, 3, 4, __extend = true },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input = { { 1, 2 }, { 3, 4, __extend = true } },
|
|
|
|
output = { 1, 2, 3, 4, __extend = true },
|
|
|
|
},
|
2023-01-10 06:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test in ipairs(tests) do
|
2023-10-13 10:37:52 +01:00
|
|
|
local n = 0
|
|
|
|
for i in pairs(test.input) do
|
|
|
|
n = math.max(n, i)
|
|
|
|
end
|
|
|
|
assert.same(test.output, Util.merge(unpack(test.input, 1, n)))
|
2023-01-10 06:32:28 +00:00
|
|
|
end
|
|
|
|
end)
|
2023-01-01 19:19:09 +00:00
|
|
|
end)
|