-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: compatible paths for windows and unix
- Loading branch information
Showing
2 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |