From 7ad7ebdcd96bafc8bd2ccb642c2ad2a68f42ddb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Cas=C3=ADa?= <31012661+rcasia@users.noreply.github.com> Date: Fri, 6 Mar 2026 21:13:24 +0100 Subject: [PATCH 1/3] fix: add build.gradle as root marker to prevent crash on single-module projects --- lua/neotest-java/core/file_checker.lua | 4 ++++ lua/neotest-java/core/root_finder.lua | 2 ++ 2 files changed, 6 insertions(+) diff --git a/lua/neotest-java/core/file_checker.lua b/lua/neotest-java/core/file_checker.lua index 26ba8597..8eb6a562 100644 --- a/lua/neotest-java/core/file_checker.lua +++ b/lua/neotest-java/core/file_checker.lua @@ -16,6 +16,10 @@ local FileChecker = function(dependencies) local my_path = Path(file_path) local base_dir = dependencies.root_getter() + if not base_dir then + return false + end + local relative_path = my_path:make_relative(base_dir) if relative_path:contains("main") then return false diff --git a/lua/neotest-java/core/root_finder.lua b/lua/neotest-java/core/root_finder.lua index b1b59acc..eaaa6d21 100644 --- a/lua/neotest-java/core/root_finder.lua +++ b/lua/neotest-java/core/root_finder.lua @@ -16,6 +16,8 @@ function RootFinder.find_root(dir, matcher) "pom.xml", "settings.gradle", "settings.gradle.kts", + "build.gradle", + "build.gradle.kts", ".git", } From d19ee08c1ab044060b87e659c29a1445acdba7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Cas=C3=ADa?= <31012661+rcasia@users.noreply.github.com> Date: Fri, 6 Mar 2026 21:15:07 +0100 Subject: [PATCH 2/3] test: add coverage for build.gradle root detection and nil root handling --- tests/unit/file_checker_spec.lua | 11 +++++++++++ tests/unit/root_finder_spec.lua | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/tests/unit/file_checker_spec.lua b/tests/unit/file_checker_spec.lua index 9dd4da92..325595dd 100644 --- a/tests/unit/file_checker_spec.lua +++ b/tests/unit/file_checker_spec.lua @@ -80,4 +80,15 @@ describe("file_checker", function() }).is_test_file(file_path)) end end) + + it("should return false when root_getter returns nil", function() + local file_checker = FileChecker({ + patterns = patterns, + root_getter = function() + return nil + end, + }) + + assert.is_false(file_checker.is_test_file("/any/path/Test.java")) + end) end) diff --git a/tests/unit/root_finder_spec.lua b/tests/unit/root_finder_spec.lua index ab9f41d6..89a10dc5 100644 --- a/tests/unit/root_finder_spec.lua +++ b/tests/unit/root_finder_spec.lua @@ -32,4 +32,26 @@ describe("RootFinder", function() -- then assert.is_nil(actualRoot) end) + + it("should find build.gradle before .git", function() + local call_order = {} + local function create_matcher(pattern) + return function(_) + table.insert(call_order, pattern) + if pattern == "build.gradle" then + return "found_build_gradle" + end + return nil + end + end + + local dir = "example/dir" + local actualRoot = root_finder.find_root(dir, create_matcher) + + assert.are.same("found_build_gradle", actualRoot) + assert.is_true( + call_order[1] == "pom.xml" or call_order[1] == "settings.gradle" or call_order[1] == "settings.gradle.kts" + ) + assert.is_true(call_order[#call_order] == "build.gradle") + end) end) From 3f49ca8ace18b0064a7c2dd0f02326f5c06d1298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Cas=C3=ADa?= <31012661+rcasia@users.noreply.github.com> Date: Fri, 6 Mar 2026 21:24:56 +0100 Subject: [PATCH 3/3] test: improve readability of build.gradle root finder test --- tests/unit/root_finder_spec.lua | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tests/unit/root_finder_spec.lua b/tests/unit/root_finder_spec.lua index 89a10dc5..da6e87ae 100644 --- a/tests/unit/root_finder_spec.lua +++ b/tests/unit/root_finder_spec.lua @@ -33,25 +33,22 @@ describe("RootFinder", function() assert.is_nil(actualRoot) end) - it("should find build.gradle before .git", function() - local call_order = {} - local function create_matcher(pattern) + it("should find build.gradle before .git for single-module Gradle projects", function() + local patterns_checked = {} + local matcher = function(pattern) + table.insert(patterns_checked, pattern) return function(_) - table.insert(call_order, pattern) if pattern == "build.gradle" then - return "found_build_gradle" + return "/path/to/project" end return nil end end - local dir = "example/dir" - local actualRoot = root_finder.find_root(dir, create_matcher) + local root = root_finder.find_root("/some/dir", matcher) - assert.are.same("found_build_gradle", actualRoot) - assert.is_true( - call_order[1] == "pom.xml" or call_order[1] == "settings.gradle" or call_order[1] == "settings.gradle.kts" - ) - assert.is_true(call_order[#call_order] == "build.gradle") + assert.are.same("/path/to/project", root) + assert.is_true(patterns_checked[#patterns_checked] == "build.gradle") + assert.is_true(patterns_checked[#patterns_checked - 1] ~= ".git") end) end)