Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom test function names are not handled #65

Open
nicos68 opened this issue Feb 3, 2024 · 3 comments · May be fixed by #66
Open

Custom test function names are not handled #65

nicos68 opened this issue Feb 3, 2024 · 3 comments · May be fixed by #66

Comments

@nicos68
Copy link

nicos68 commented Feb 3, 2024

I am working on a project for which we have cutomized the python_functions option value (using the pyproject.toml file). In this case neotest-python does not detect the tests.

I tried fiddling with the treesitter query and it does solve the issue.

@nicos68 nicos68 linked a pull request Feb 11, 2024 that will close this issue
@alecgerona
Copy link

Do you have an existing workaround while this is still being looked at? I don't know how to request a bump for your pr.

@nicos68
Copy link
Author

nicos68 commented May 27, 2024

There are two things you can do

  • Since this repo hasn't had any update since my pr, you can just point your plugin manager to my PR branch on my fork, as long as the repo is not updated you won't miss any new feature
  • Modify the treesitter query on your local copy of the plugin's lua code. That's what I have done at work. Personally I still use packer, so the file is located at ~/.local/share/nvim/site/pack/packer/start/neotest-python/lua/neotest-python/base.lua

@e7nd7r
Copy link

e7nd7r commented Jan 8, 2025

Hi @nicos68 I just had the same issue but my use case is having tests in the same implementation file. I implemented the is_test_fille function to get the file pattern from pyproject.toml... I think it'll be nice to get these configuration for test names as well.

I'm leaving my implementation below... I had to install toml using luarocks which was a challenge, but nvm I'm just starting with neovim. I also learn from here that we need to avoid using vim.fn.glob2regpat since it's not coroutine safe, so I had to use :match.

Summarizing:

  • Had to avoid using non corutine safe code.
  • Had to cache the config load since this function is called intensively.
  • Had to use nio to load config file.
  • Had to use nio.schedule() for some reason my function didn't work without this.

Also to be able to use toml library I had to install it for lua 5.1 (since neovim runs in that version).

eval "$(luarocks --lua-version=5.1 path --bin)"

It'll be good to have this functionality by default since it's not straighforward to make it work.

local toml = require("toml")
local nio = require("nio")

local cached_config = nil

local function is_match(base_name, pattern)
    local lua_pattern = pattern:gsub("%%", "%%%%"):gsub("%*", ".*"):gsub("%?", ".") -- Convert glob to Lua pattern

    return base_name:match(lua_pattern) ~= nil
end

local function get_config()
    if cached_config then
        return cached_config
    end

    local file, err = nio.file.open(vim.fn.getcwd() .. '/' .. 'pyproject.toml')

    if not file or err then
        return nil
    end

    local content = file.read(nil, 0)

    cached_config = toml.decode(content)

    return cached_config
end

local function get_pytest_config()
    local config = get_config()

    if config and config.tool and config.tool.pytest and config.tool.pytest.ini_options then
        return config.tool.pytest.ini_options
    end

    return nil
end

-- @async
local function is_test_file(file_path)
    if not vim.endswith(file_path, ".py") then
        return false
    end

    local pytest_config = get_pytest_config()
    local test_patterns = { "test_*.py", "*_test.py" } -- Default patterns

    -- If pytest configuration is found, override test_patterns
    if pytest_config and pytest_config.python_files then
        -- Handle both single string and list of patterns
        if type(pytest_config.python_files) == "string" then
            test_patterns = { pytest_config.python_files }
        elseif type(pytest_config.python_files) == "table" then
            test_patterns = pytest_config.python_files
        end
    end

    -- Check if the file matches any test pattern
    local base_name = file_path:match("^.+/(.+)$")

    for _, pattern in ipairs(test_patterns) do
        if is_match(base_name, pattern) then
            return true
        end
    end

    return false
end

return {
    "nvim-neotest/neotest",
    version = "~5.6.0",
    dependencies = {
        "nvim-neotest/nvim-nio",
        "nvim-lua/plenary.nvim",
        "antoinemadec/FixCursorHold.nvim",
        "nvim-treesitter/nvim-treesitter",
        "nvim-neotest/neotest-python",
        "nvim-neotest/neotest-plenary",
        "nvim-neotest/neotest-vim-test",
        "nvim-treesitter/nvim-treesitter",
        "LebJe/toml.lua",
    },
    config = function ()
        require("neotest").setup {
            adapters = {
                require("neotest-python") {
                    runner = "pytest",
                    dap = { justMyCode = false },
                    args = { "--log-level", "DEBUG" },
                    is_test_file = function (test_file)
                        nio.scheduler()
                        return is_test_file(test_file)
                    end
                },
               -- ...
            },
        }
        -- ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants