diff --git a/README.md b/README.md index 01d2784..f2db21c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/doc/dap-python.txt b/doc/dap-python.txt index b9ce360..c7afd76 100644 --- a/doc/dap-python.txt +++ b/doc/dap-python.txt @@ -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" diff --git a/lua/dap-python.lua b/lua/dap-python.lua index 4873b15..9560792 100644 --- a/lua/dap-python.lua +++ b/lua/dap-python.lua @@ -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 @@ -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