diff --git a/lua/neotest-python/adapter.lua b/lua/neotest-python/adapter.lua index 1d9e143..f0a939d 100644 --- a/lua/neotest-python/adapter.lua +++ b/lua/neotest-python/adapter.lua @@ -50,16 +50,22 @@ return function(config) return script_args end + local function get_root(path) + return base.get_root(path) or vim.loop.cwd() or "" + end + + local function filter_dir(name) + return name ~= "venv" + end + ---@type neotest.Adapter return { name = "neotest-python", - root = base.get_root, - filter_dir = function(name) - return name ~= "venv" - end, + root = get_root, + filter_dir = filter_dir, is_test_file = config.is_test_file, discover_positions = function(path) - local root = base.get_root(path) or vim.loop.cwd() or "" + local root = get_root(path) local python_command = config.get_python_command(root) local runner = config.get_runner(python_command) @@ -79,8 +85,7 @@ return function(config) build_spec = function(args) local position = args.tree:data() - local root = base.get_root(position.path) or vim.loop.cwd() or "" - + local root = get_root(position.path) local python_command = config.get_python_command(root) local runner = config.get_runner(python_command) diff --git a/lua/neotest-python/base.lua b/lua/neotest-python/base.lua index 90cd6da..ec14d90 100644 --- a/lua/neotest-python/base.lua +++ b/lua/neotest-python/base.lua @@ -13,7 +13,7 @@ function M.is_test_file(file_path) return vim.startswith(file_name, "test_") or vim.endswith(file_name, "_test.py") end -M.module_exists = function(module, python_command) +function M.module_exists(module, python_command) return lib.process.run(vim .iter({ python_command, @@ -29,9 +29,11 @@ local python_command_mem = {} ---@return string[] function M.get_python_command(root) root = root or vim.loop.cwd() + if python_command_mem[root] then return python_command_mem[root] end + -- Use activated virtualenv. if vim.env.VIRTUAL_ENV then python_command_mem[root] = { Path:new(vim.env.VIRTUAL_ENV, "bin", "python").filename }