Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions lua/neotest-java/core/file_checker.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
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
end

---@async
---@param file_path string
---@return boolean
function FileChecker.is_test_file(file_path)
if string.find(file_path, "/main/") then
return false
end
for _, pattern in ipairs(JAVA_TEST_FILE_PATTERNS) do
if string.find(file_path, pattern) then
return true
end
end
return false
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
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
110 changes: 77 additions & 33 deletions tests/core/file_checker_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,81 @@ 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",
}

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 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 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)

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 true if theres a /main/ outside the root path", function()
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)

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)