From 5422abf47e75d36dbb8c4a001fd518d21cd1b2c2 Mon Sep 17 00:00:00 2001 From: jojo Date: Tue, 25 Nov 2025 23:49:35 -0300 Subject: [PATCH 1/4] chore: makes the file_checker sensible for the main before the root --- lua/neotest-java/core/file_checker.lua | 15 +++++++++++++-- tests/core/file_checker_spec.lua | 9 +++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lua/neotest-java/core/file_checker.lua b/lua/neotest-java/core/file_checker.lua index 4014cf6e..d1eadb8c 100644 --- a/lua/neotest-java/core/file_checker.lua +++ b/lua/neotest-java/core/file_checker.lua @@ -1,16 +1,27 @@ local JAVA_TEST_FILE_PATTERNS = require("neotest-java.types.patterns").JAVA_TEST_FILE_PATTERNS +local root_finder = require("neotest-java.core.root_finder") +local ch = require("neotest-java.context_holder") +local path = require("plenary.path") local FileChecker = {} +local matcher = function(pattern) + return function(dir) + return string.find(dir, pattern) + end +end + ---@async ---@param file_path string ---@return boolean function FileChecker.is_test_file(file_path) - if string.find(file_path, "/main/") then + local root = ch.get_context().root or root_finder.find_root(vim.fn.getcwd(), matcher) or "" + local relative_path = path:new(file_path):make_relative(root) + if string.find(relative_path, "/main/") then return false end for _, pattern in ipairs(JAVA_TEST_FILE_PATTERNS) do - if string.find(file_path, pattern) then + if string.find(relative_path, pattern) then return true end end diff --git a/tests/core/file_checker_spec.lua b/tests/core/file_checker_spec.lua index 5005c09f..f75d2297 100644 --- a/tests/core/file_checker_spec.lua +++ b/tests/core/file_checker_spec.lua @@ -35,4 +35,13 @@ describe("file_checker", function() assert.is_false(plugin.is_test_file(file_path)) end end) + + it("should return true if theres a /main/ outside the root path", function() + local non_test_files = { + "main/src/java/neotest/NeotestTest.java", + } + for _, file_path in ipairs(non_test_files) do + assert.is_true(plugin.is_test_file(file_path)) + end + end) end) From 443158540dafc6c14687665774615e4dc6524d96 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 26 Nov 2025 00:23:27 -0300 Subject: [PATCH 2/4] chore: setups the root for the tests --- tests/core/file_checker_spec.lua | 77 +++++++++++++++++--------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/tests/core/file_checker_spec.lua b/tests/core/file_checker_spec.lua index f75d2297..3b2985ec 100644 --- a/tests/core/file_checker_spec.lua +++ b/tests/core/file_checker_spec.lua @@ -2,46 +2,49 @@ local plugin = require("neotest-java") local it = require("nio").tests.it -- async describe("file_checker", function() - it("should return true for test files", function() - local test_files = { - "src/test/java/neotest/NeotestTest.java", - "src/test/java/neotest/RepositoryTests.java", - "src/test/java/neotest/NeotestIT.java", - "src/test/java/neotest/ProductAceptanceTests.java", - "src/test/java/neotest/domain/ProductAceptanceTests.java", - } + it("should return true for test files", function() + local test_files = { + "src/test/java/neotest/NeotestTest.java", + "src/test/java/neotest/RepositoryTests.java", + "src/test/java/neotest/NeotestIT.java", + "src/test/java/neotest/ProductAceptanceTests.java", + "src/test/java/neotest/domain/ProductAceptanceTests.java", + } - for _, file_path in ipairs(test_files) do - assert.is_true(plugin.is_test_file(file_path)) - end - end) + for _, file_path in ipairs(test_files) do + assert.is_true(plugin.is_test_file(file_path)) + end + end) - it("should return false for a java non-test file", function() - local non_test_files = { - "src/test/java/neotest/Configuration.java", - "src/test/java/neotest/TestRepository.java", - "src/test/java/neotest/Neotest.java", - } - for _, file_path in ipairs(non_test_files) do - assert.is_false(plugin.is_test_file(file_path)) - end - end) + it("should return false for a java non-test file", function() + local non_test_files = { + "src/test/java/neotest/Configuration.java", + "src/test/java/neotest/TestRepository.java", + "src/test/java/neotest/Neotest.java", + } + for _, file_path in ipairs(non_test_files) do + assert.is_false(plugin.is_test_file(file_path)) + end + end) - it("should return false for every class inside main folder", function() - local non_test_files = { - "src/main/java/neotest/NeotestTest.java", - } - for _, file_path in ipairs(non_test_files) do - assert.is_false(plugin.is_test_file(file_path)) - end - end) + it("should return false for every class inside main folder", function() + local non_test_files = { + "src/main/java/neotest/NeotestTest.java", + } + for _, file_path in ipairs(non_test_files) do + assert.is_false(plugin.is_test_file(file_path)) + end + end) it("should return true if theres a /main/ outside the root path", function() - local non_test_files = { - "main/src/java/neotest/NeotestTest.java", - } - for _, file_path in ipairs(non_test_files) do - assert.is_true(plugin.is_test_file(file_path)) - end - end) + local ch = require "neotest-java.context_holder" + ch.set_root '/absolute_path/main/src' + local non_test_files = { + "/absolute_path/main/src/java/neotest/NeotestTest.java", + } + for _, file_path in ipairs(non_test_files) do + assert.is_true(plugin.is_test_file(file_path)) + end + ch.set_root '' + end) end) From 979f3096be3b8682725c4bab3f69ae7525eb0782 Mon Sep 17 00:00:00 2001 From: jojo Date: Mon, 1 Dec 2025 13:13:33 -0300 Subject: [PATCH 3/4] chore: adds windows compatibility --- lua/neotest-java/core/file_checker.lua | 32 +++++++++++++++----------- tests/core/file_checker_spec.lua | 32 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/lua/neotest-java/core/file_checker.lua b/lua/neotest-java/core/file_checker.lua index d1eadb8c..730363db 100644 --- a/lua/neotest-java/core/file_checker.lua +++ b/lua/neotest-java/core/file_checker.lua @@ -1,31 +1,35 @@ local JAVA_TEST_FILE_PATTERNS = require("neotest-java.types.patterns").JAVA_TEST_FILE_PATTERNS local root_finder = require("neotest-java.core.root_finder") +local compatible_path = require("neotest-java.util.compatible_path") local ch = require("neotest-java.context_holder") local path = require("plenary.path") local FileChecker = {} local matcher = function(pattern) - return function(dir) - return string.find(dir, pattern) - end + return function(dir) + return string.find(dir, pattern) + end end ---@async ---@param file_path string ---@return boolean function FileChecker.is_test_file(file_path) - local root = ch.get_context().root or root_finder.find_root(vim.fn.getcwd(), matcher) or "" - local relative_path = path:new(file_path):make_relative(root) - if string.find(relative_path, "/main/") then - return false - end - for _, pattern in ipairs(JAVA_TEST_FILE_PATTERNS) do - if string.find(relative_path, pattern) then - return true - end - end - return false + file_path = compatible_path(file_path) + + local root = compatible_path(ch.get_context().root or root_finder.find_root(vim.fn.getcwd(), matcher) or "") + + local relative_path = path:new(file_path):make_relative(root) + if string.find(relative_path, "/main/") then + return false + end + for _, pattern in ipairs(JAVA_TEST_FILE_PATTERNS) do + if string.find(relative_path, pattern) then + return true + end + end + return false end return FileChecker diff --git a/tests/core/file_checker_spec.lua b/tests/core/file_checker_spec.lua index 3b2985ec..144fcfda 100644 --- a/tests/core/file_checker_spec.lua +++ b/tests/core/file_checker_spec.lua @@ -47,4 +47,36 @@ describe("file_checker", function() end ch.set_root '' end) + + it("should return false if theres a /main/ inside the root path in a windows env", function() + vim.fn.win64 = {} + local ch = require "neotest-java.context_holder" + ch.set_root 'C:\\absolute_path\\main\\src' + + local non_test_files = { + "C:\\absolute_path\\src\\main\\java\\neotest\\NeotestTest.java", + } + for _, file_path in ipairs(non_test_files) do + assert.is_false(plugin.is_test_file(file_path)) + end + + ch.set_root '' + vim.fn.win64 = nil + end) + + it("should return true if theres a /main/ outside the root path in a windows env", function() + vim.fn.win64 = {} + local ch = require "neotest-java.context_holder" + ch.set_root 'C:\\absolute_path\\main\\src' + + local non_test_files = { + "C:\\absolute_path\\main\\src\\java\\neotest\\NeotestTest.java", + } + for _, file_path in ipairs(non_test_files) do + assert.is_true(plugin.is_test_file(file_path)) + end + + ch.set_root '' + vim.fn.win64 = nil + end) end) From 0b4d22befe13c7b42483528d0b1b9ce9abbf04f7 Mon Sep 17 00:00:00 2001 From: joseildo filho Date: Mon, 1 Dec 2025 19:23:52 -0300 Subject: [PATCH 4/4] chore: adds prints --- lua/neotest-java/core/file_checker.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/neotest-java/core/file_checker.lua b/lua/neotest-java/core/file_checker.lua index 730363db..9adfcf66 100644 --- a/lua/neotest-java/core/file_checker.lua +++ b/lua/neotest-java/core/file_checker.lua @@ -17,8 +17,10 @@ end ---@return boolean function FileChecker.is_test_file(file_path) file_path = compatible_path(file_path) + print(file_path) local root = compatible_path(ch.get_context().root or root_finder.find_root(vim.fn.getcwd(), matcher) or "") + print(root) local relative_path = path:new(file_path):make_relative(root) if string.find(relative_path, "/main/") then