From ae2c745dbe079e0d6f8793952db161a12be50408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Cas=C3=ADa?= <31012661+rcasia@users.noreply.github.com> Date: Fri, 5 Dec 2025 00:23:41 +0100 Subject: [PATCH 1/2] refactor: use Path for Module pseudoclass --- lua/neotest-java/types/module.lua | 10 ++++------ lua/neotest-java/types/project.lua | 9 +++------ lua/neotest-java/util/path.lua | 5 +++++ tests/types/project_spec.lua | 2 +- tests/util/path_spec.lua | 23 +++++++++++++++++++++++ 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/lua/neotest-java/types/module.lua b/lua/neotest-java/types/module.lua index 3fceba97..28894be7 100644 --- a/lua/neotest-java/types/module.lua +++ b/lua/neotest-java/types/module.lua @@ -1,26 +1,24 @@ -local LAST_PATH_SEGMENT_REGEX = "([^/\\]+)$" - ---@class neotest-java.Module ----@field base_dir string +---@field base_dir neotest-java.Path ---@field _build_tool neotest-java.BuildTool ---@field name string ---@field module_dependencies string[] local Module = {} Module.__index = Module ----@param base_dir string +---@param base_dir neotest-java.Path ---@param build_tool neotest-java.BuildTool ---@return neotest-java.Module function Module.new(base_dir, build_tool) local self = setmetatable({}, Module) self.base_dir = base_dir - self.name = base_dir:match(LAST_PATH_SEGMENT_REGEX) or base_dir + self.name = base_dir.name() self._build_tool = build_tool return self end function Module:get_output_dir() - return self._build_tool.get_output_dir(self.base_dir) + return self._build_tool.get_output_dir(self.base_dir.to_string()) end ---@return string[] diff --git a/lua/neotest-java/types/project.lua b/lua/neotest-java/types/project.lua index 98b8f9e2..b2f3e3b6 100644 --- a/lua/neotest-java/types/project.lua +++ b/lua/neotest-java/types/project.lua @@ -4,7 +4,7 @@ local Module = require("neotest-java.types.module") local scan = require("plenary.scandir") local logger = require("neotest-java.logger") local should_ignore_path = require("neotest-java.util.should_ignore_path") -local compatible_path = require("neotest-java.util.compatible_path") +local Path = require("neotest-java.util.path") ---@class neotest-java.Project ---@field root_dir string @@ -42,17 +42,14 @@ function Project:get_modules() ---@type table local modules = {} for _, dir in ipairs(dirs) do - local base_filepath = compatible_path(dir) - -- get the base directory of the module, by removing the last part of the path - -- taking care of both Windows and Unix-like systems - local base_dir = base_filepath:match("^(.*)[/\\][^/\\]+$") + local base_dir = Path(dir).parent() local mod = Module.new(base_dir, self.build_tool) modules[#modules + 1] = mod end local base_dirs = {} for _, mod in ipairs(modules) do - base_dirs[#base_dirs + 1] = mod.base_dir + base_dirs[#base_dirs + 1] = mod.base_dir.to_string() end logger.debug("modules: ", base_dirs) diff --git a/lua/neotest-java/util/path.lua b/lua/neotest-java/util/path.lua index c26bee0a..7ed7fb86 100644 --- a/lua/neotest-java/util/path.lua +++ b/lua/neotest-java/util/path.lua @@ -2,6 +2,7 @@ --- @field to_string fun(): string --- @field parent fun(): neotest-java.Path --- @field append fun(other: string): neotest-java.Path +--- @field name fun(): string local UNIX_SEPARATOR = "/" local WINDOWS_SEPARATOR = "\\" @@ -60,7 +61,11 @@ local function Path(raw_path, opts) table.insert(slugs, 1, ".") end + --- @type neotest-java.Path return { + name = function() + return slugs[#slugs] + end, append = function(other) return Path(raw_path .. SEP .. other, opts) end, diff --git a/tests/types/project_spec.lua b/tests/types/project_spec.lua index 9e0472e3..82371857 100644 --- a/tests/types/project_spec.lua +++ b/tests/types/project_spec.lua @@ -22,7 +22,7 @@ describe("project", function() local project = Project.from_root_dir(testcase.input) local results = {} for _, mod in ipairs(project:get_modules()) do - results[#results + 1] = { name = mod.name, base_dir = mod.base_dir } + results[#results + 1] = { name = mod.name, base_dir = mod.base_dir.to_string() } end assert.same(testcase.expected, results) end) diff --git a/tests/util/path_spec.lua b/tests/util/path_spec.lua index 1de58a19..e6619150 100644 --- a/tests/util/path_spec.lua +++ b/tests/util/path_spec.lua @@ -111,4 +111,27 @@ describe("Path", function() eq(case.expected, path.append(case.append_path).to_string()) end) end + + local cases_name = { + { + input_path = "/some/test", + expected = "test", + separator = "/", + }, + { + input_path = "\\some\\test", + expected = "test", + separator = "\\", + }, + } + for _, case in ipairs(cases_name) do + it("gets name: " .. case.input_path, function() + local path = Path(case.input_path, { + separator = function() + return case.separator + end, + }) + eq(case.expected, path.name()) + end) + end end) From 031d135e65d79e5e3422c8a02df4f677d81b1e3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Cas=C3=ADa?= <31012661+rcasia@users.noreply.github.com> Date: Fri, 5 Dec 2025 00:38:46 +0100 Subject: [PATCH 2/2] refactor: expand use of path structure --- lua/neotest-java/build_tool/gradle.lua | 8 ++++---- lua/neotest-java/build_tool/init.lua | 2 +- lua/neotest-java/build_tool/maven.lua | 8 ++++---- lua/neotest-java/core/spec_builder/init.lua | 7 +++---- lua/neotest-java/types/module.lua | 3 ++- tests/util/path_spec.lua | 6 ++++++ 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lua/neotest-java/build_tool/gradle.lua b/lua/neotest-java/build_tool/gradle.lua index 0972a23f..93bb229a 100644 --- a/lua/neotest-java/build_tool/gradle.lua +++ b/lua/neotest-java/build_tool/gradle.lua @@ -1,5 +1,5 @@ -local compatible_path = require("neotest-java.util.compatible_path") local generate_spring_property_filepaths = require("neotest-java.util.spring_property_filepaths") +local Path = require("neotest-java.util.path") local PROJECT_FILENAME = "build.gradle" @@ -8,7 +8,7 @@ local gradle = {} gradle.get_output_dir = function(root) root = root and root or "." - return compatible_path(root .. "/bin") + return Path(root).append("bin") end function gradle.get_project_filename() @@ -20,8 +20,8 @@ function gradle.get_spring_property_filepaths(roots) local base_dirs = vim.iter(roots) :map(function(root) return { - gradle.get_output_dir(root) .. "/main", - gradle.get_output_dir(root) .. "/test", + gradle.get_output_dir(root).append("main").to_string(), + gradle.get_output_dir(root).append("/test").to_string(), } end) :flatten() diff --git a/lua/neotest-java/build_tool/init.lua b/lua/neotest-java/build_tool/init.lua index 2f56c968..d24337d2 100644 --- a/lua/neotest-java/build_tool/init.lua +++ b/lua/neotest-java/build_tool/init.lua @@ -6,7 +6,7 @@ local Job = require("plenary.job") local lib = require("neotest.lib") ---@class neotest-java.BuildTool ----@field get_output_dir fun(root?: string): string +---@field get_output_dir fun(root?: neotest-java.Path): neotest-java.Path ---@field get_project_filename fun(): string ---@field get_module_dependencies fun(root: string): table ---@field get_spring_property_filepaths fun(root?: string): string[] diff --git a/lua/neotest-java/build_tool/maven.lua b/lua/neotest-java/build_tool/maven.lua index d413c521..6cceed58 100644 --- a/lua/neotest-java/build_tool/maven.lua +++ b/lua/neotest-java/build_tool/maven.lua @@ -1,5 +1,5 @@ -local compatible_path = require("neotest-java.util.compatible_path") local generate_spring_property_filepaths = require("neotest-java.util.spring_property_filepaths") +local Path = require("neotest-java.util.path") local PROJECT_FILE = "pom.xml" @@ -9,7 +9,7 @@ local maven = {} maven.get_output_dir = function(root) root = root and root or "." -- TODO: read from pom.xml - return compatible_path(root .. "/target/classes") + return Path(root).append("target/classes") end function maven.get_project_filename() @@ -21,8 +21,8 @@ function maven.get_spring_property_filepaths(roots) local base_dirs = vim.iter(roots) :map(function(root) return { - maven.get_output_dir(root) .. "/classes", - maven.get_output_dir(root) .. "/test-classes", + maven.get_output_dir(root).append("classes").to_string(), + maven.get_output_dir(root).append("/test-classes").to_string(), } end) :flatten() diff --git a/lua/neotest-java/core/spec_builder/init.lua b/lua/neotest-java/core/spec_builder/init.lua index f4acad03..babfdb68 100644 --- a/lua/neotest-java/core/spec_builder/init.lua +++ b/lua/neotest-java/core/spec_builder/init.lua @@ -28,9 +28,9 @@ function SpecBuilder.build_spec(args, project_type, config) local tree = args.tree local position = tree:data() local root = assert(ch:get_context().root) - local absolute_path = position.path local project = assert(Project.from_root_dir(root), "project not detected correctly") local modules = project:get_modules() + --- @type neotest-java.BuildTool local build_tool = build_tools.get(project_type) -- make sure we are in root_dir @@ -38,10 +38,9 @@ function SpecBuilder.build_spec(args, project_type, config) -- make sure outputDir is created to operate in it local output_dir = build_tool.get_output_dir() - local output_dir_parent = compatible_path(path:new(output_dir):parent().filename) - vim.uv.fs_mkdir(output_dir_parent, 493) - vim.uv.fs_mkdir(output_dir, 493) + vim.uv.fs_mkdir(output_dir.parent().to_string(), 493) + vim.uv.fs_mkdir(output_dir.to_string(), 493) -- JUNIT REPORT DIRECTORY local reports_dir = diff --git a/lua/neotest-java/types/module.lua b/lua/neotest-java/types/module.lua index 28894be7..e5597fab 100644 --- a/lua/neotest-java/types/module.lua +++ b/lua/neotest-java/types/module.lua @@ -17,8 +17,9 @@ function Module.new(base_dir, build_tool) return self end +--- @return neotest-java.Path function Module:get_output_dir() - return self._build_tool.get_output_dir(self.base_dir.to_string()) + return self._build_tool.get_output_dir(self.base_dir) end ---@return string[] diff --git a/tests/util/path_spec.lua b/tests/util/path_spec.lua index e6619150..c34bfe0d 100644 --- a/tests/util/path_spec.lua +++ b/tests/util/path_spec.lua @@ -94,6 +94,12 @@ describe("Path", function() expected = "/some/test", separator = "/", }, + { + input_path = "/some", + append_path = "test/more", + expected = "/some/test/more", + separator = "/", + }, { input_path = "\\some", append_path = "test",