test(utils): add unit test for transform_path (#3013)

This commit is contained in:
James Trew
2024-03-27 23:39:14 -04:00
committed by GitHub
parent c2b8311dfa
commit b22e6f6896
2 changed files with 136 additions and 22 deletions

View File

@@ -15,7 +15,7 @@ local get_status = require("telescope.state").get_status
local utils = {} local utils = {}
local iswin = vim.loop.os_uname().sysname == "Windows_NT" utils.iswin = vim.loop.os_uname().sysname == "Windows_NT"
--- Hybrid of `vim.fn.expand()` and custom `vim.fs.normalize()` --- Hybrid of `vim.fn.expand()` and custom `vim.fs.normalize()`
--- ---
@@ -53,7 +53,7 @@ utils.path_expand = function(path)
path = path:gsub("%$([%w_]+)", vim.loop.os_getenv) path = path:gsub("%$([%w_]+)", vim.loop.os_getenv)
path = path:gsub("/+", "/") path = path:gsub("/+", "/")
if iswin then if utils.iswin then
path = path:gsub("\\+", "\\") path = path:gsub("\\+", "\\")
if path:match "^%w:\\$" then if path:match "^%w:\\$" then
return path return path
@@ -161,14 +161,15 @@ end
utils.path_smart = (function() utils.path_smart = (function()
local paths = {} local paths = {}
local os_sep = utils.get_separator()
return function(filepath) return function(filepath)
local final = filepath local final = filepath
if #paths ~= 0 then if #paths ~= 0 then
local dirs = vim.split(filepath, "/") local dirs = vim.split(filepath, os_sep)
local max = 1 local max = 1
for _, p in pairs(paths) do for _, p in pairs(paths) do
if #p > 0 and p ~= filepath then if #p > 0 and p ~= filepath then
local _dirs = vim.split(p, "/") local _dirs = vim.split(p, os_sep)
for i = 1, math.min(#dirs, #_dirs) do for i = 1, math.min(#dirs, #_dirs) do
if (dirs[i] ~= _dirs[i]) and i > max then if (dirs[i] ~= _dirs[i]) and i > max then
max = i max = i
@@ -184,7 +185,7 @@ utils.path_smart = (function()
final = "" final = ""
for k, v in pairs(dirs) do for k, v in pairs(dirs) do
if k >= max - 1 then if k >= max - 1 then
final = final .. (#final > 0 and "/" or "") .. v final = final .. (#final > 0 and os_sep or "") .. v
end end
end end
end end
@@ -194,7 +195,7 @@ utils.path_smart = (function()
table.insert(paths, filepath) table.insert(paths, filepath)
end end
if final and final ~= filepath then if final and final ~= filepath then
return "../" .. final return ".." .. os_sep .. final
else else
return filepath return filepath
end end
@@ -264,11 +265,11 @@ end
--- this function outside of telescope might yield to undefined behavior and will --- this function outside of telescope might yield to undefined behavior and will
--- not be addressed by us --- not be addressed by us
---@param opts table: The opts the users passed into the picker. Might contains a path_display key ---@param opts table: The opts the users passed into the picker. Might contains a path_display key
---@param path string: The path that should be formatted ---@param path string?: The path that should be formatted
---@return string: The transformed path ready to be displayed ---@return string: The transformed path ready to be displayed
utils.transform_path = function(opts, path) utils.transform_path = function(opts, path)
if path == nil then if path == nil then
return return ""
end end
if utils.is_uri(path) then if utils.is_uri(path) then
return path return path
@@ -288,7 +289,7 @@ utils.transform_path = function(opts, path)
elseif vim.tbl_contains(path_display, "smart") or path_display.smart then elseif vim.tbl_contains(path_display, "smart") or path_display.smart then
transformed_path = utils.path_smart(transformed_path) transformed_path = utils.path_smart(transformed_path)
else else
if not vim.tbl_contains(path_display, "absolute") or path_display.absolute == false then if not vim.tbl_contains(path_display, "absolute") and not path_display.absolute then
local cwd local cwd
if opts.cwd then if opts.cwd then
cwd = opts.cwd cwd = opts.cwd
@@ -306,7 +307,8 @@ utils.transform_path = function(opts, path)
local shorten = path_display["shorten"] local shorten = path_display["shorten"]
transformed_path = Path:new(transformed_path):shorten(shorten.len, shorten.exclude) transformed_path = Path:new(transformed_path):shorten(shorten.len, shorten.exclude)
else else
transformed_path = Path:new(transformed_path):shorten(path_display["shorten"]) local length = type(path_display["shorten"]) == "number" and path_display["shorten"]
transformed_path = Path:new(transformed_path):shorten(length)
end end
end end
if vim.tbl_contains(path_display, "truncate") or path_display.truncate then if vim.tbl_contains(path_display, "truncate") or path_display.truncate then

View File

@@ -1,27 +1,42 @@
local Path = require "plenary.path"
local utils = require "telescope.utils" local utils = require "telescope.utils"
local eq = assert.are.equal
describe("path_expand()", function() describe("path_expand()", function()
it("removes trailing /", function() it("removes trailing os_sep", function()
assert.is.equal("/home/user", utils.path_expand "/home/user/") if utils.iswin then
eq([[C:\Users\a\b]], utils.path_expand [[C:\Users\a\b\]])
else
eq("/home/user", utils.path_expand "/home/user/")
end
end) end)
it("works with /", function() it("works with root dir", function()
assert.is.equal("/", utils.path_expand "/") if utils.iswin then
eq([[C:\]], utils.path_expand [[C:\]])
else
eq("/", utils.path_expand "/")
end
end) end)
it("works with ~", function() it("works with ~", function()
assert.is.equal(vim.loop.os_homedir() .. "/src/foo", utils.path_expand "~/src/foo") eq(vim.loop.os_homedir() .. "/src/foo", utils.path_expand "~/src/foo")
end) end)
it("handles duplicate /", function() it("handles duplicate os_sep", function()
assert.is.equal("/home/user", utils.path_expand "/home///user") if utils.iswin then
eq([[C:\Users\a]], utils.path_expand [[C:\\\Users\\a]])
else
eq("/home/user", utils.path_expand "/home///user")
end
end) end)
it("preserves fake whitespace characters and whitespace", function() it("preserves fake whitespace characters and whitespace", function()
local path_space = "/home/user/hello world" local path_space = "/home/user/hello world"
assert.is.equal(path_space, utils.path_expand(path_space)) eq(path_space, utils.path_expand(path_space))
local path_newline = [[/home/user/hello\nworld]] local path_newline = [[/home/user/hello\nworld]]
assert.is.equal(path_newline, utils.path_expand(path_newline)) eq(path_newline, utils.path_expand(path_newline))
end) end)
end) end)
@@ -140,9 +155,106 @@ describe("__separates_file_path_location", function()
it("separtates file path for " .. suite.input, function() it("separtates file path for " .. suite.input, function()
local file, row, col = utils.__separate_file_path_location(suite.input) local file, row, col = utils.__separate_file_path_location(suite.input)
assert.are.equal(file, suite.file) eq(file, suite.file)
assert.are.equal(row, suite.row) eq(row, suite.row)
assert.are.equal(col, suite.col) eq(col, suite.col)
end) end)
end end
end) end)
describe("transform_path", function()
local cwd = (function()
if utils.iswin then
return [[C:\Users\user\projects\telescope.nvim]]
else
return "/home/user/projects/telescope.nvim"
end
end)()
local function new_relpath(unix_path)
return Path:new(unpack(vim.split(unix_path, "/"))).filename
end
local function assert_path(path_display, path, expect)
local opts = { cwd = cwd, __length = 15 }
if type(path_display) == "string" then
opts.path_display = { path_display }
eq(expect, utils.transform_path(opts, path))
opts.path_display = { [path_display] = true }
eq(expect, utils.transform_path(opts, path))
elseif type(path_display) == "table" then
opts.path_display = path_display
eq(expect, utils.transform_path(opts, path))
elseif path_display == nil then
eq(expect, utils.transform_path(opts, path))
end
end
it("handles nil path", function()
assert_path(nil, nil, "")
end)
it("returns back uri", function()
local uri = [[https://www.example.com/index.html]]
assert_path(nil, uri, uri)
end)
it("handles 'hidden' path_display", function()
eq("", utils.transform_path({ cwd = cwd, path_display = "hidden" }, "foobar"))
assert_path("hidden", "foobar", "")
end)
it("returns relative path for default opts", function()
local relative = Path:new { "lua", "telescope", "init.lua" }
local absolute = Path:new { cwd, relative }
assert_path(nil, absolute.filename, relative.filename)
assert_path(nil, relative.filename, relative.filename)
end)
it("handles 'tail' path_display", function()
local path = new_relpath "lua/telescope/init.lua"
assert_path("tail", path, "init.lua")
end)
it("handles 'smart' path_display", function()
local path1 = new_relpath "lua/telescope/init.lua"
local path2 = new_relpath "lua/telescope/finders.lua"
local path3 = new_relpath "lua/telescope/finders/async_job_finder.lua"
local path4 = new_relpath "plugin/telescope.lua"
assert_path("smart", path1, path1)
assert_path("smart", path2, new_relpath "../telescope/finders.lua")
assert_path("smart", path3, new_relpath "../telescope/finders/async_job_finder.lua")
assert_path("smart", path4, path4)
end)
it("handles 'absolute' path_display", function()
local relative = Path:new { "lua", "telescope", "init.lua" }
local absolute = Path:new { cwd, relative }
-- TODO: feels like 'absolute' should turn relative paths to absolute
-- assert_path("absolute", relative.filename, absolute.filename)
assert_path("absolute", absolute.filename, absolute.filename)
end)
it("handles default 'shorten' path_display", function()
assert_path("shorten", new_relpath "lua/telescope/init.lua", new_relpath "l/t/init.lua")
end)
it("handles 'shorten' with number", function()
assert_path({ shorten = 2 }, new_relpath "lua/telescope/init.lua", new_relpath "lu/te/init.lua")
end)
it("handles 'shorten' with option table", function()
assert_path({ shorten = { len = 2 } }, new_relpath "lua/telescope/init.lua", new_relpath "lu/te/init.lua")
assert_path(
{ shorten = { len = 2, exclude = { 1, 3, -1 } } },
new_relpath "lua/telescope/builtin/init.lua",
new_relpath "lua/te/builtin/init.lua"
)
end)
it("handles default 'truncate' path_display", function()
assert_path({ "truncate" }, new_relpath "lua/telescope/init.lua", new_relpath "…scope/init.lua")
end)
end)