diff --git a/lua/neotest-java/util/compatible_path.lua b/lua/neotest-java/util/compatible_path.lua index ca8519f..9bf5bc9 100644 --- a/lua/neotest-java/util/compatible_path.lua +++ b/lua/neotest-java/util/compatible_path.lua @@ -1,9 +1,23 @@ -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 diff --git a/tests/util/compatible_path_spec.lua b/tests/util/compatible_path_spec.lua new file mode 100644 index 0000000..5de6c9b --- /dev/null +++ b/tests/util/compatible_path_spec.lua @@ -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) + 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)