Skip to content

Commit 973c593

Browse files
fix: make the file_checker sensible for the main before the root (#220)
For some reason i had my java project inside a folder main, and that was causing the tests to be ignored. So, basically i created a PR to make the file_checker use only the relative path.
1 parent 0cb36a4 commit 973c593

File tree

2 files changed

+100
-42
lines changed

2 files changed

+100
-42
lines changed
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
local JAVA_TEST_FILE_PATTERNS = require("neotest-java.types.patterns").JAVA_TEST_FILE_PATTERNS
2+
local root_finder = require("neotest-java.core.root_finder")
3+
local compatible_path = require("neotest-java.util.compatible_path")
4+
local ch = require("neotest-java.context_holder")
5+
local path = require("plenary.path")
26

37
local FileChecker = {}
48

9+
local matcher = function(pattern)
10+
return function(dir)
11+
return string.find(dir, pattern)
12+
end
13+
end
14+
515
---@async
616
---@param file_path string
717
---@return boolean
818
function FileChecker.is_test_file(file_path)
9-
if string.find(file_path, "/main/") then
10-
return false
11-
end
12-
for _, pattern in ipairs(JAVA_TEST_FILE_PATTERNS) do
13-
if string.find(file_path, pattern) then
14-
return true
15-
end
16-
end
17-
return false
19+
file_path = compatible_path(file_path)
20+
local root = compatible_path(ch.get_context().root or root_finder.find_root(vim.fn.getcwd(), matcher) or "")
21+
22+
local relative_path = path:new(file_path):make_relative(root)
23+
if string.find(relative_path, compatible_path("/main/")) then
24+
return false
25+
end
26+
for _, pattern in ipairs(JAVA_TEST_FILE_PATTERNS) do
27+
if string.find(relative_path, pattern) then
28+
return true
29+
end
30+
end
31+
return false
1832
end
1933

2034
return FileChecker

tests/core/file_checker_spec.lua

Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,81 @@ local plugin = require("neotest-java")
22
local it = require("nio").tests.it -- async
33

44
describe("file_checker", function()
5-
it("should return true for test files", function()
6-
local test_files = {
7-
"src/test/java/neotest/NeotestTest.java",
8-
"src/test/java/neotest/RepositoryTests.java",
9-
"src/test/java/neotest/NeotestIT.java",
10-
"src/test/java/neotest/ProductAceptanceTests.java",
11-
"src/test/java/neotest/domain/ProductAceptanceTests.java",
12-
}
13-
14-
for _, file_path in ipairs(test_files) do
15-
assert.is_true(plugin.is_test_file(file_path))
16-
end
17-
end)
18-
19-
it("should return false for a java non-test file", function()
20-
local non_test_files = {
21-
"src/test/java/neotest/Configuration.java",
22-
"src/test/java/neotest/TestRepository.java",
23-
"src/test/java/neotest/Neotest.java",
24-
}
25-
for _, file_path in ipairs(non_test_files) do
26-
assert.is_false(plugin.is_test_file(file_path))
27-
end
28-
end)
29-
30-
it("should return false for every class inside main folder", function()
31-
local non_test_files = {
32-
"src/main/java/neotest/NeotestTest.java",
33-
}
34-
for _, file_path in ipairs(non_test_files) do
35-
assert.is_false(plugin.is_test_file(file_path))
36-
end
37-
end)
5+
it("should return true for test files", function()
6+
local test_files = {
7+
"src/test/java/neotest/NeotestTest.java",
8+
"src/test/java/neotest/RepositoryTests.java",
9+
"src/test/java/neotest/NeotestIT.java",
10+
"src/test/java/neotest/ProductAceptanceTests.java",
11+
"src/test/java/neotest/domain/ProductAceptanceTests.java",
12+
}
13+
14+
for _, file_path in ipairs(test_files) do
15+
assert.is_true(plugin.is_test_file(file_path))
16+
end
17+
end)
18+
19+
it("should return false for a java non-test file", function()
20+
local non_test_files = {
21+
"src/test/java/neotest/Configuration.java",
22+
"src/test/java/neotest/TestRepository.java",
23+
"src/test/java/neotest/Neotest.java",
24+
}
25+
for _, file_path in ipairs(non_test_files) do
26+
assert.is_false(plugin.is_test_file(file_path))
27+
end
28+
end)
29+
30+
it("should return false for every class inside main folder", function()
31+
local non_test_files = {
32+
"src/main/java/neotest/NeotestTest.java",
33+
}
34+
for _, file_path in ipairs(non_test_files) do
35+
assert.is_false(plugin.is_test_file(file_path))
36+
end
37+
end)
38+
39+
it("should return true if theres a /main/ outside the root path", function()
40+
local ch = require "neotest-java.context_holder"
41+
ch.set_root '/absolute_path/main/src'
42+
local non_test_files = {
43+
"/absolute_path/main/src/java/neotest/NeotestTest.java",
44+
}
45+
for _, file_path in ipairs(non_test_files) do
46+
assert.is_true(plugin.is_test_file(file_path))
47+
end
48+
ch.set_root ''
49+
end)
50+
51+
it("should return false if theres a /main/ inside the root path in a windows env", function()
52+
vim.fn.win64 = {}
53+
local ch = require "neotest-java.context_holder"
54+
ch.set_root 'C:\\absolute_path\\main\\src'
55+
56+
local non_test_files = {
57+
"C:\\absolute_path\\src\\main\\java\\neotest\\NeotestTest.java",
58+
}
59+
for _, file_path in ipairs(non_test_files) do
60+
assert.is_false(plugin.is_test_file(file_path))
61+
end
62+
63+
ch.set_root ''
64+
vim.fn.win64 = nil
65+
end)
66+
67+
it("should return true if theres a /main/ outside the root path in a windows env", function()
68+
vim.fn.win64 = {}
69+
local ch = require "neotest-java.context_holder"
70+
ch.set_root 'C:\\absolute_path\\main\\src'
71+
72+
local non_test_files = {
73+
"C:\\absolute_path\\main\\src\\java\\neotest\\NeotestTest.java",
74+
}
75+
for _, file_path in ipairs(non_test_files) do
76+
assert.is_true(plugin.is_test_file(file_path))
77+
end
78+
79+
ch.set_root ''
80+
vim.fn.win64 = nil
81+
end)
3882
end)

0 commit comments

Comments
 (0)