Skip to content

Commit

Permalink
Add guards against resolving empty qualified_name node
Browse files Browse the repository at this point in the history
  • Loading branch information
asmodeus812 committed Oct 30, 2024
1 parent 58c6440 commit f487e28
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
5 changes: 4 additions & 1 deletion lua/neotest-java/command/junit_command_builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ local CommandBuilder = {
return setmetatable({}, self)
end,

---@param qualified_name string example: com.example.ExampleTest
---@param qualified_name string | nil example: com.example.ExampleTest
---@param node_name? string example: shouldNotFail
---@return CommandBuilder
test_reference = function(self, qualified_name, node_name, type)
if not qualified_name or #qualified_name == 0 then
return self
end
self._test_references = self._test_references or {}

if type == "dir" then
Expand Down
6 changes: 5 additions & 1 deletion lua/neotest-java/core/result_builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ function ResultBuilder.build_results(spec, result, tree) -- luacheck: ignore 212
goto continue
end

local unique_key = build_unique_key(resolve_qualified_name(node.path), node.name)
local qualified_name = resolve_qualified_name(node.path)
if not qualified_name or #qualified_name == 0 then
goto continue
end
local unique_key = build_unique_key(qualified_name, node.name)

if is_parameterized then
local jtestcases = extract_parameterized_tests(testcases, unique_key)
Expand Down
10 changes: 5 additions & 5 deletions lua/neotest-java/core/spec_builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local compile = require("neotest-java.command.compile")
local find_module_by_filepath = require("neotest-java.util.find_module_by_filepath")
local logger = require("neotest-java.logger")
local random_port = require("neotest-java.util.random_port")
local resolve_qualfied_name = require("neotest-java.util.resolve_qualified_name")
local resolve_qualified_name = require("neotest-java.util.resolve_qualified_name")

local path = require("plenary.path")
local lib = require("neotest.lib")
Expand Down Expand Up @@ -60,20 +60,20 @@ function SpecBuilder.build_spec(args, project_type, config)
if position.type == "dir" then
for _, child in tree:iter() do
if child.type == "file" then
command:test_reference(resolve_qualfied_name(child.path), child.name, "file")
command:test_reference(resolve_qualified_name(child.path), child.name, "file")
end
end
elseif position.type == "namespace" then
for _, child in tree:iter() do
if child.type == "test" then
command:test_reference(resolve_qualfied_name(child.path), child.name, "test")
command:test_reference(resolve_qualified_name(child.path), child.name, "test")
end
end
elseif position.type == "file" then
command:test_reference(resolve_qualfied_name(absolute_path), position.name, "file")
command:test_reference(resolve_qualified_name(absolute_path), position.name, "file")
elseif position.type == "test" then
-- note: parameterized tests are not being discovered by the junit standalone, so we run tests per file
command:test_reference(resolve_qualfied_name(absolute_path), position.name, "file")
command:test_reference(resolve_qualified_name(absolute_path), position.name, "file")
end

-- COMPILATION STEP
Expand Down
5 changes: 3 additions & 2 deletions lua/neotest-java/util/resolve_qualified_name.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local read_file = require("neotest-java.util.read_file")
local TEST_CLASS_PATTERNS = require("neotest-java.types.test_class_patterns")

---@return string | nil
local function resolve_qualified_name(filename)
---@param raw_query string
---@param content string
Expand Down Expand Up @@ -44,7 +45,7 @@ local function resolve_qualified_name(filename)

-- as there can be different class names
-- searches for the one the mathces the test class patterns
local name = ""
local name = nil
for _, _name in ipairs(names) do
for _, pattern in ipairs(TEST_CLASS_PATTERNS) do
if _name:find(pattern) then
Expand All @@ -54,7 +55,7 @@ local function resolve_qualified_name(filename)
end
end

return package_line .. "." .. name
return name and (package_line .. "." .. name) or nil
end

return resolve_qualified_name

0 comments on commit f487e28

Please sign in to comment.