Skip to content

Commit

Permalink
Try markers in all roots to detect test framework (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
a3ng7n committed Sep 8, 2024
1 parent 7c427e2 commit 0844bf3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ can install it manually using either:
- Use `:lua require('dap-python').test_method()` to debug the closest method above the cursor.

Supported test frameworks are `unittest`, `pytest` and `django`. By default it
tries to detect the runner by probing for `pytest.ini` and `manage.py`, if
neither are present it defaults to `unittest`.
tries to detect the runner by probing for presence of `pytest.ini` or
`manage.py`, or for a `tool.pytest` directive inside `pyproject.toml`, if
none are present it defaults to `unittest`.

To configure a different runner, change the `test_runner` variable. For
example, to configure `pytest` set the test runner like this in your
Expand Down
4 changes: 2 additions & 2 deletions doc/dap-python.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Python extension for nvim-dap *dap-python*

M.test_runner *dap-python.test_runner*
Test runner to use by default.
The default value is dynamic and depends on `pytest.ini` or `manage.py` markers.
If neither is found "unittest" is used. See |dap-python.test_runners|
The default value is dynamic and depends on `pytest.ini`, `manage.py` or `pyproject.toml` markers.
If none are found "unittest" is used. See |dap-python.test_runners|
Override this to set a different runner:
```
require('dap-python').test_runner = "pytest"
Expand Down
46 changes: 24 additions & 22 deletions lua/dap-python.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,6 @@ M.test_runner = nil
M.resolve_python = nil


local function default_runner()
if uv.fs_stat('pytest.ini') then
return 'pytest'
elseif uv.fs_stat('manage.py') then
return 'django'
elseif uv.fs_stat("pyproject.toml") then
local f = io.open("pyproject.toml")
if f then
for line in f:lines() do
if line:find("%[tool.pytest") then
f:close()
return "pytest"
end
end
f:close()
end
return 'unittest'
else
return 'unittest'
end
end

--- Table to register test runners.
--- Built-in are test runners for unittest, pytest and django.
--- The key is the test runner name, the value a function to generate the
Expand Down Expand Up @@ -88,6 +66,30 @@ local function roots()
end


local function default_runner()
for root in roots() do
if uv.fs_stat(root .. "/pytest.ini") then
return "pytest"
elseif uv.fs_stat(root .. "/manage.py") then
return "django"
elseif uv.fs_stat(root .. "/pyproject.toml") then
local f = io.open(root .. "/pyproject.toml")
if f then
for line in f:lines() do
if line:find("%[tool.pytest") then
f:close()
return "pytest"
end
end
f:close()
end
end
end

return "unittest"
end


local get_python_path = function()
local venv_path = os.getenv('VIRTUAL_ENV')
if venv_path then
Expand Down

0 comments on commit 0844bf3

Please sign in to comment.