feat: remove file searching

This commit is contained in:
Rónán Carrigan
2022-01-07 23:06:55 +00:00
parent c0a5d07143
commit 5bca040611

View File

@@ -4,45 +4,44 @@ local lib = require("neotest.lib")
local base = require("neotest-python.base") local base = require("neotest-python.base")
local function script_path() local function script_path()
local str = debug.getinfo(2, "S").source:sub(2) local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*/)") return str:match("(.*/)")
end end
local python_script = (Path.new(script_path()):parent():parent() / "neotest.py").filename local python_script = (Path.new(script_path()):parent():parent() / "neotest.py").filename
local get_args = function(runner, position) local get_args = function(runner, position)
if runner == "unittest" then if runner == "unittest" then
runner = "pyunit" runner = "pyunit"
end end
return lib.vim_test.collect_args("python", runner, position) return lib.vim_test.collect_args("python", runner, position)
end end
local get_runner = function() local get_runner = function()
local vim_test_runner = vim.g["test#python#runner"] local vim_test_runner = vim.g["test#python#runner"]
if vim_test_runner == "pyunit" then if vim_test_runner == "pyunit" then
return "unittest" return "unittest"
end end
if vim_test_runner and lib.func_util.index({ "unittest", "pytest" }, vim_test_runner) then if vim_test_runner and lib.func_util.index({ "unittest", "pytest" }, vim_test_runner) then
return vim_test_runner return vim_test_runner
end end
if vim.fn.executable("pytest") == 1 then if vim.fn.executable("pytest") == 1 then
return "pytest" return "pytest"
end end
return "unittest" return "unittest"
end end
---@type NeotestAdapter ---@type NeotestAdapter
local PythonNeotestAdapter = {name = "neotest-python"} local PythonNeotestAdapter = { name = "neotest-python" }
function PythonNeotestAdapter.is_test_file(file_path) function PythonNeotestAdapter.is_test_file(file_path)
return base.is_test_file(file_path) return base.is_test_file(file_path)
end end
---@async ---@async
---@return Tree | nil ---@return Tree | nil
function PythonNeotestAdapter.discover_positions(path) function PythonNeotestAdapter.discover_positions(path)
if path and not lib.files.is_dir(path) then local query = [[
local query = [[
((function_definition ((function_definition
name: (identifier) @test.name) name: (identifier) @test.name)
(#match? @test.name "^test_")) (#match? @test.name "^test_"))
@@ -52,44 +51,41 @@ function PythonNeotestAdapter.discover_positions(path)
name: (identifier) @namespace.name) name: (identifier) @namespace.name)
@namespace.definition @namespace.definition
]] ]]
return lib.treesitter.parse_positions(path, query, { return lib.treesitter.parse_positions(path, query, {
require_namespaces = get_runner() == "unittest", require_namespaces = get_runner() == "unittest",
}) })
end
local files = lib.func_util.filter_list(base.is_test_file, lib.files.find({ path }))
return lib.files.parse_dir_from_files(path, files)
end end
---@param args NeotestRunArgs ---@param args NeotestRunArgs
---@return NeotestRunSpec ---@return NeotestRunSpec
function PythonNeotestAdapter.build_spec(args) function PythonNeotestAdapter.build_spec(args)
local position = args.tree:data() local position = args.tree:data()
local results_path = vim.fn.tempname() local results_path = vim.fn.tempname()
local runner = get_runner() local runner = get_runner()
local python = base.get_python_command(vim.fn.getcwd()) local python = base.get_python_command(vim.fn.getcwd())
local script_args = vim.tbl_flatten({ local script_args = vim.tbl_flatten({
"--results-file", "--results-file",
results_path, results_path,
"--runner", "--runner",
runner, runner,
"--", "--",
get_args(runner, position), get_args(runner, position),
}) })
if position then if position then
table.insert(script_args, position.id) table.insert(script_args, position.id)
end end
local command = vim.tbl_flatten({ local command = vim.tbl_flatten({
python, python,
python_script, python_script,
script_args, script_args,
}) })
return { return {
command = command, command = command,
context = { context = {
results_path = results_path, results_path = results_path,
}, },
strategy = base.get_strategy_config(args.strategy, python, python_script, script_args), strategy = base.get_strategy_config(args.strategy, python, python_script, script_args),
} }
end end
---@async ---@async
@@ -97,35 +93,35 @@ end
---@param result NeotestStrategyResult ---@param result NeotestStrategyResult
---@return NeotestResult[] ---@return NeotestResult[]
function PythonNeotestAdapter.results(spec, result) function PythonNeotestAdapter.results(spec, result)
-- TODO: Find out if this JSON option is supported in future -- TODO: Find out if this JSON option is supported in future
local success, data = pcall(lib.files.read, spec.context.results_path) local success, data = pcall(lib.files.read, spec.context.results_path)
if not success then if not success then
data = "{}" data = "{}"
end end
local results = vim.json.decode(data, { luanil = { object = true } }) local results = vim.json.decode(data, { luanil = { object = true } })
for _, pos_result in pairs(results) do for _, pos_result in pairs(results) do
result.output_path = pos_result.output_path result.output_path = pos_result.output_path
end end
return results return results
end end
setmetatable(PythonNeotestAdapter, { setmetatable(PythonNeotestAdapter, {
__call = function(_, opts) __call = function(_, opts)
if type(opts.args) == "function" or (type(opts.args) == "table" and opts.args.__call) then if type(opts.args) == "function" or (type(opts.args) == "table" and opts.args.__call) then
get_args = opts.args get_args = opts.args
elseif opts.args then elseif opts.args then
get_args = function() get_args = function()
return opts.args return opts.args
end end
end end
if type(opts.runner) == "function" or (type(opts.runner) == "table" and opts.runner.__call) then if type(opts.runner) == "function" or (type(opts.runner) == "table" and opts.runner.__call) then
get_runner = opts.runner get_runner = opts.runner
elseif opts.runner then elseif opts.runner then
get_runner = function() get_runner = function()
return opts.runner return opts.runner
end end
end end
end, end,
}) })
return PythonNeotestAdapter return PythonNeotestAdapter