Skip to content

Commit

Permalink
fix: compatible paths for windows and unix
Browse files Browse the repository at this point in the history
  • Loading branch information
rcasia committed Oct 8, 2024
1 parent 03c415f commit f9f8731
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lua/neotest-java/util/compatible_path.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
local Path = require("plenary.path")

---@param path string
---@return string compatible_path
local function compatible_path(path)
return Path:new(path).filename
local has_win = vim.fn.has("win64") == 1 or vim.fn.has("win32") == 1
print(vim.inspect(has_win))
local sep = has_win and "\\" or "/"
path = path
--
:gsub("%./", sep)
--
:gsub("%.\\", sep)
--
:gsub("/", sep)
--
:gsub("\\", sep)
--
:gsub(sep .. "+", sep)

return vim.fs.normalize(path)
end

return compatible_path
78 changes: 78 additions & 0 deletions tests/util/compatible_path_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
local compatible_path = require("neotest-java.util.compatible_path")

describe("compatible_path", function()
local original_vim_fn_has

before_each(function()
-- save original function
original_vim_fn_has = vim.fn.has -- luacheck: ignore 122 Setting a read-only field of a global variable
end)

after_each(function()
-- reset
vim.fn.has = original_vim_fn_has -- luacheck: ignore 122 Setting a read-only field of a global variable
end)

-- should create compatible path for unix
do
local testcases = {
{
description = "basic case",
input = "\\PP\\expense-app-v3\\expenses-domain\\target",
expected = "/PP/expense-app-v3/expenses-domain/target",
},
{
description = "with a dot between file separators",
input = "\\PP\\expense-app-v3\\expenses-domain\\.\\target",
expected = "/PP/expense-app-v3/expenses-domain/target",
},
{
description = "mixed file separators",
input = "\\PP\\expense-app-v3/expenses-domain\\./target",
expected = "/PP/expense-app-v3/expenses-domain/target",
},
}

for _, case in ipairs(testcases) do
it(("-> case: %s -> %s"):format(case.description, case.input), function()
assert.same(case.expected, compatible_path(case.input))
end)
end
end

-- should create compatible path for windows
do
local testcases = {
{
description = "basic case for win",
input = "/PP/expense-app-v3/expenses-domain/target",
expected = "\\PP\\expense-app-v3\\expenses-domain\\target",
},
{
description = "with a dot between file separators",
input = "/PP/expense-app-v3/expenses-domain/./target",
expected = "\\PP\\expense-app-v3\\expenses-domain\\target",
},
{
description = "mixed file separators",
input = "/PP/expense-app-v3\\expenses-domain\\./target",
expected = "\\PP\\expense-app-v3\\expenses-domain\\target",
},
}

for _, case in ipairs(testcases) do
-- mock vim.fn.has to simulate to be on windows
vim.fn.has = function(arg) -- luacheck: ignore 122 Setting a read-only field of a global variable
if arg == "win64" or arg == "win32" then
return 1
else
return 0
end
end
it(("(windows) case: %s -> %s "):format(case.description, case.input), function()
local result = compatible_path(case.input)
assert.same(case.expected, result)
end)
end
end
end)

0 comments on commit f9f8731

Please sign in to comment.