Skip to content
Draft
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
8 changes: 4 additions & 4 deletions lua/neotest-java/build_tool/gradle.lua
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-java/build_tool/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
8 changes: 4 additions & 4 deletions lua/neotest-java/build_tool/maven.lua
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -9,7 +9,7 @@ local maven = {}
maven.get_output_dir = function(root)
root = root and root or "."
-- TODO: read from pom.xml <build><directory>
return compatible_path(root .. "/target/classes")
return Path(root).append("target/classes")
end

function maven.get_project_filename()
Expand All @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions lua/neotest-java/core/spec_builder/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@ 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
nio.fn.chdir(root)

-- 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 =
Expand Down
9 changes: 4 additions & 5 deletions lua/neotest-java/types/module.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
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

--- @return neotest-java.Path
function Module:get_output_dir()
return self._build_tool.get_output_dir(self.base_dir)
end
Expand Down
9 changes: 3 additions & 6 deletions lua/neotest-java/types/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,17 +42,14 @@ function Project:get_modules()
---@type table<neotest-java.Module>
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)

Expand Down
5 changes: 5 additions & 0 deletions lua/neotest-java/util/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "\\"
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/types/project_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions tests/util/path_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -111,4 +117,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)
Loading