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

fix:default_runner: Look for marker files within all possible root_dirs #156

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading