diff --git a/lua/neotest-java/core/file_checker.lua b/lua/neotest-java/core/file_checker.lua index 26ba859..8eb6a56 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 b1b59ac..eaaa6d2 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", } diff --git a/tests/unit/file_checker_spec.lua b/tests/unit/file_checker_spec.lua index 9dd4da9..325595d 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 ab9f41d..da6e87a 100644 --- a/tests/unit/root_finder_spec.lua +++ b/tests/unit/root_finder_spec.lua @@ -32,4 +32,23 @@ describe("RootFinder", function() -- then assert.is_nil(actualRoot) end) + + 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(_) + if pattern == "build.gradle" then + return "/path/to/project" + end + return nil + end + end + + local root = root_finder.find_root("/some/dir", matcher) + + 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)