Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local M = {}
---@param specs string the package name of the file you are interpreting
---@param outfile string where the test output will be written to.
---@return string command the gradle command to execute
M.parse = function(tests, specs, outfile)
function M.build(tests, specs, outfile)
local INIT_SCRIPT_NAME = "test-logging.init.gradle.kts"

local init_script_path = vim.api.nvim_get_runtime_file(INIT_SCRIPT_NAME, false)[1]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
local M = {}

local ignored_directories =
{ "docs", "build", "out", "generated", ".gradle", "main", ".idea", "buildSrc", "kapt", "taret" }
local ignored_directories = {
"docs",
"build",
"out",
"generated",
".gradle",
"main",
".idea",
"buildSrc",
"kapt",
"target",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"target" was typo'd as "taret" super minor change, doesn't really make sense in a refactor, but couldn't help myself

}

-- This filters out non-test directories that would bog down scnanning.
---This filters out non-test directories that would bog down scnanning.
---@param path string Name of directory
M.is_test_directory = function(path)
function M.is_test_directory(path)
for _, v in ipairs(ignored_directories) do
if v == path then
return false
end
end

return true
end

M.is_test_file = function(file_path)
---Whether the file_path provided is a test file.
---@param file_path string?
---@return boolean
function M.is_test_file(file_path)
if file_path == nil then
return false
end
Expand Down
33 changes: 11 additions & 22 deletions lua/neotest-kotlin/init.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
local lib = require("neotest.lib")
local position_parser = require("neotest-kotlin.src.position-parser")
local package_query = require("neotest-kotlin.src.treesitter.package-query")
local class_query = require("neotest-kotlin.src.treesitter.class-query")
local async = require("neotest.async")
local command = require("neotest-kotlin.src.command")
local filter = require("neotest-kotlin.src.filter")
local treesitter_query = require("neotest-kotlin.kotest-treesitter-query")
local output_parser = require("neotest-kotlin.src.output-parser")
local treesitter = require("neotest-kotlin.treesitter")
local command = require("neotest-kotlin.command")
local filter = require("neotest-kotlin.filter")
local output_parser = require("neotest-kotlin.output_parser")

local M = {}

Expand Down Expand Up @@ -40,17 +37,12 @@ function M.Adapter.is_test_file(file_path)
return filter.is_test_file(file_path)
end

---Given a file path, parse all the tests within it.
---Given a file path, parse all the tests within it
---@async
---@param file_path string Absolute file path
---@return neotest.Tree | nil
function M.Adapter.discover_positions(file_path)
local positions = lib.treesitter.parse_positions(file_path, treesitter_query, {
nested_namespaces = true,
nested_tests = false,
})

return positions
return treesitter.parse_positions(file_path)
end

---Determines the package of a directory
Expand All @@ -74,7 +66,7 @@ local function dir_determine_package(dir)
return nil
end

return position_parser.get_first_match_string(test_file, package_query)
return treesitter.java_package(test_file)
end

---@class Context
Expand Down Expand Up @@ -110,15 +102,12 @@ function M.Adapter.build_spec(args)

if pos.type == "dir" then
local package = dir_determine_package(pos.path) .. ".*"
run_spec.command = command.parse(tests, package, results_path)
run_spec.command = command.build(tests, package, results_path)
elseif pos.type == "file" or pos.type == "namespace" or pos.type == "test" then
local package = string.format(
"%s.%s",
position_parser.get_first_match_string(pos.path, package_query),
position_parser.get_first_match_string(pos.path, class_query)
)
local package =
string.format("%s.%s", treesitter.java_package(pos.path), treesitter.list_all_classes(pos.path)[1])

run_spec.command = command.parse(tests, package, results_path)
run_spec.command = command.build(tests, package, results_path)
end

print(run_spec.command)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
local neotest = require("neotest.lib")
local parser = require("neotest-kotlin.src.position-parser")
local package_query = require("neotest-kotlin.src.treesitter.package-query")
local class_query = require("neotest-kotlin.src.treesitter.class-query")
local treesitter = require("neotest-kotlin.treesitter")

local M = {}

---Gets the result of a Gradle test output line
---@param line string
---@return string status passed, skipped, failed, none
M.parse_status = function(line)
function M.parse_status(line)
local result = "none"

if vim.endswith(line, "PASSED") then
Expand Down Expand Up @@ -114,8 +112,8 @@ local function determine_all_classes_file(file)

---@type table<string, string>
local results = {}
local package = parser.get_first_match_string(file, package_query)
local classes = parser.get_all_matches_as_string(file, class_query)
local package = treesitter.java_package(file)
local classes = treesitter.list_all_classes(file)

for _, class in ipairs(classes) do
results[package .. "." .. class] = file
Expand Down Expand Up @@ -147,7 +145,7 @@ end
---@param lines string[]
---@param path string
---@return table<string, neotest.Result>
M.parse_lines = function(lines, path)
function M.parse_lines(lines, path)
local results = {}
local classes = M.determine_all_classes(path)

Expand Down
3 changes: 0 additions & 3 deletions lua/neotest-kotlin/src/stream_handler.lua

This file was deleted.

137 changes: 0 additions & 137 deletions lua/neotest-kotlin/src/treesitter/spec-query.lua

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
local lib = require("neotest.lib")
local neotest = require("neotest.lib")

local M = {}
local class_query = require("neotest-kotlin.treesitter.class-query")
local package_query = require("neotest-kotlin.treesitter.package-query")
local kotest_query = require("neotest-kotlin.treesitter.kotest-query")

M.parse = function(path, query)
local positions = lib.treesitter.parse_positions(path, query, {
nested_namespaces = true,
nested_tests = false,
fast = false,
})

return positions
end
local M = {}

---Get all matches for the query perform on the path
---@param path string
---@param query string
---@return string[]
M.get_all_matches_as_string = function(path, query)
local function get_all_matches_as_string(path, query)
local language = "kotlin"

local bufnr = vim.api.nvim_create_buf(false, true)
Expand Down Expand Up @@ -54,17 +48,28 @@ M.get_all_matches_as_string = function(path, query)
return results
end

--- This will take in a path to a file, run a treesitter query on it, and return the first match as a string.
---@param path string path to file
---@param query string treesitter query
---@return string? first_match
M.get_first_match_string = function(path, query)
local results = M.get_all_matches_as_string(path, query)
if #results > 0 then
return results[1]
end
---List all classes in a provided file using treesitter
---@param file string
---@return string[] classes
function M.list_all_classes(file)
return get_all_matches_as_string(file, class_query)
end

---Get the first java package in a provided file using treesitter
---@param file string
---@return string? package
function M.java_package(file)
return get_all_matches_as_string(file, package_query)[1]
end

return nil
---Uses neotest.treeistter.parse_positions to discover all namespaces/tests in a file.
---@param file string
---@return neotest.Tree?
function M.parse_positions(file)
return neotest.treesitter.parse_positions(file, kotest_query, {
nested_namespaces = true,
nested_tests = false,
})
Comment on lines +51 to +72
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of exposing the raw Treesitter queries using the init.lua here to only expose functions that use the treesitter queries. Moved the kotest query and parse_positions in here too. Trying to encapsulate all treesitter logic in one spot

end

return M
29 changes: 0 additions & 29 deletions test.kt

This file was deleted.

Loading