Skip to content

Commit

Permalink
fix: no tests found bug in python files
Browse files Browse the repository at this point in the history
  • Loading branch information
olisikh committed Jan 3, 2025
1 parent a2861ab commit cde9b13
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
19 changes: 12 additions & 7 deletions lua/neotest-python/adapter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
8 changes: 5 additions & 3 deletions lua/neotest-python/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
local module_exists = function(module, python_command)
return lib.process.run(vim
.iter({
python_command,
Expand All @@ -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 }
Expand Down Expand Up @@ -152,8 +154,8 @@ function M.get_runner(python_path)
then
return vim_test_runner
end
local runner = M.module_exists("pytest", python_path) and "pytest"
or M.module_exists("django", python_path) and "django"
local runner = module_exists("pytest", python_path) and "pytest"
or module_exists("django", python_path) and "django"
or "unittest"
stored_runners[command_str] = runner
return runner
Expand Down
1 change: 1 addition & 0 deletions lua/neotest-python/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ end
---@param config neotest-python.AdapterConfig
local augment_config = function(config)
local get_python_command = base.get_python_command

if config.python then
get_python_command = function(root)
local python = config.python
Expand Down

0 comments on commit cde9b13

Please sign in to comment.