Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lua/neotest-java/build_tool/gradle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function gradle.get_project_filename()
return PROJECT_FILENAME
end

function gradle.get_artifact_id(base_dir)
return base_dir:name()
end

--- @param roots neotest-java.Path[]
function gradle.get_spring_property_filepaths(roots)
local base_dirs = vim
Expand Down
1 change: 1 addition & 0 deletions lua/neotest-java/build_tool/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local _, repl = pcall(require, "dap.repl")
---@field get_project_filename fun(): string
---@field get_module_dependencies fun(root: string): table
---@field get_spring_property_filepaths fun(roots: neotest-java.Path[]): neotest-java.Path[]
---@field get_artifact_id fun(base_dir: neotest-java.Path): string

---@type table<string, neotest-java.BuildTool>
local build_tools = { gradle = gradle, maven = maven }
Expand Down
6 changes: 6 additions & 0 deletions lua/neotest-java/build_tool/maven.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local generate_spring_property_filepaths = require("neotest-java.util.spring_property_filepaths")
local Path = require("neotest-java.model.path")
local read_xml_tag = require("neotest-java.util.read_xml_tag")

local PROJECT_FILE = "pom.xml"

Expand All @@ -15,6 +16,11 @@ function maven.get_project_filename()
return PROJECT_FILE
end

function maven.get_artifact_id(base_dir)
local pom_path = base_dir:append("pom.xml"):to_string()
return read_xml_tag(pom_path, "project.artifactId") or base_dir:name()
end

--- @param roots neotest-java.Path[]
function maven.get_spring_property_filepaths(roots)
local base_dirs = vim
Expand Down
4 changes: 2 additions & 2 deletions lua/neotest-java/core/spec_builder/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ local SpecBuilder = function(deps)
local build_tool = deps.build_tool_getter(project_type)
local command = CommandBuilder.new():junit_jar(config.junit_jar):jvm_args(config.jvm_args)
local project = assert(
Project.from_dirs_and_project_file(deps.scan(root), build_tool.get_project_filename()),
Project.from_dirs_and_project_file(deps.scan(root), build_tool.get_project_filename(), build_tool),
"project not detected correctly"
)

Expand Down Expand Up @@ -104,7 +104,7 @@ local SpecBuilder = function(deps)
logger.debug("junit debug command: ", junit.command, " ", table.concat(junit.args, " "))
local terminated_command_event = deps.launch_debug_test(junit.command, junit.args, module.base_dir)

local project_name = root:name()
local project_name = module.name
return {
strategy = {
type = "java",
Expand Down
5 changes: 3 additions & 2 deletions lua/neotest-java/model/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ local Module = {}
Module.__index = Module

---@param base_dir neotest-java.Path
---@param build_tool neotest-java.BuildTool
---@return neotest-java.Module
function Module.new(base_dir)
function Module.new(base_dir, build_tool)
local self = setmetatable({}, Module)
self.base_dir = base_dir
self.name = base_dir:name()
self.name = build_tool and build_tool.get_artifact_id(base_dir) or base_dir:name()
return self
end

Expand Down
11 changes: 7 additions & 4 deletions lua/neotest-java/model/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@ local Module = require("neotest-java.model.module")
---@class neotest-java.Project
---@field project_filename string
---@field private _modules neotest-java.Module[]
---@field private _build_tool neotest-java.BuildTool
local Project = {}
Project.__index = Project

local modules_from_dirs_and_project_file = function(dirs, project_filename)
local modules_from_dirs_and_project_file = function(dirs, project_filename, build_tool)
---@type table<neotest-java.Module>
local modules = {}
for _, path in ipairs(dirs) do
if path:to_string():find(project_filename) then
modules[#modules + 1] = Module.new(path:parent())
modules[#modules + 1] = Module.new(path:parent(), build_tool)
end
end
return modules
end

---@param dirs neotest-java.Path[]
---@param project_filename string
---@param build_tool neotest-java.BuildTool
---@return neotest-java.Project
function Project.from_dirs_and_project_file(dirs, project_filename)
function Project.from_dirs_and_project_file(dirs, project_filename, build_tool)
local self = setmetatable({}, Project)
self.project_filename = project_filename
self._modules = modules_from_dirs_and_project_file(dirs, project_filename)
self._build_tool = build_tool
self._modules = modules_from_dirs_and_project_file(dirs, project_filename, build_tool)
return self
end

Expand Down
4 changes: 4 additions & 0 deletions tests/fake_build_tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ return {
"/user/home/target/test-classes",
}
end,

get_artifact_id = function(base_dir)
return base_dir:name()
end,
}
30 changes: 28 additions & 2 deletions tests/unit/project_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ local Path = require("neotest-java.model.path")
local eq = assert.are.same

describe("project", function()
local fake_build_tool_with_artifact_id = {
get_artifact_id = function(base_dir)
return "artifact-" .. base_dir:name()
end,
}

local fake_build_tool_dir_name = {
get_artifact_id = function(base_dir)
return base_dir:name()
end,
}

local testscases = {
{
input = {
Expand Down Expand Up @@ -64,7 +76,11 @@ describe("project", function()
}
for _, testcase in ipairs(testscases) do
it("should get modules: " .. testcase.input.root_dir:to_string(), function()
local project = Project.from_dirs_and_project_file(testcase.input.dirs, testcase.input.project_filename)
local project = Project.from_dirs_and_project_file(
testcase.input.dirs,
testcase.input.project_filename,
fake_build_tool_dir_name
)

local results = {}
for _, mod in ipairs(project:get_modules()) do
Expand All @@ -74,13 +90,23 @@ describe("project", function()
end)
end

it("should use artifactId from build tool instead of directory name", function()
local project = Project.from_dirs_and_project_file({
Path("./my_project/pom.xml"),
}, "pom.xml", fake_build_tool_with_artifact_id)

local modules = project:get_modules()
assert(#modules == 1)
eq("artifact-my_project", modules[1].name)
end)

it("find module by filepath", function()
local project = Project.from_dirs_and_project_file({

Path("./tests/fixtures/multi-module-demo/pom.xml"),
Path("./tests/fixtures/multi-module-demo/module-a/pom.xml"),
Path("./tests/fixtures/multi-module-demo/module-b/pom.xml"),
}, "pom.xml")
}, "pom.xml", fake_build_tool_dir_name)

local not_found_module = project:find_module_by_filepath(Path("./tests/fixtures/some-other-project"))
eq(nil, not_found_module)
Expand Down