nvim-neotest / neotest-python

MIT License
137 stars 40 forks source link

No tests found if running from a non-test file #40

Closed filipgodlewski closed 1 year ago

filipgodlewski commented 2 years ago

Hey, I know this might be self explanatory, but hear me out ;)

So let's say I have the following folder structure:

test_py
├── main.py
├── test_bar.py
└── test_foo.py

If I open either test_foo.py or test_bar.py and run :lua require'neotest'.summary.toggle(), it will successfully parse all tests (including the other not-opened-yet test files). I can then run the tests from the summary, all is totally fine.

The problem: If I try to do the same from main.py (let's say that I don't have other buffers open yet), it will parse the tests forever ('Parsing tests'). If I close and reopen the summary, it will finally tell me that it could not find any tests. All is fixed again, if I open any test file, but as you can imagine, it is far from optimal :(

The question is, is it a bug, is it an enhancement request, or can it be solved with a better .is_test_file()?

rcarriga commented 2 years ago

It sounds like a project root is not being found and thus parsing isn't started until a test buffer is opened, which would be expected behaviour. The adapter looks for certain files to determine the project root (can be found here https://github.com/rcarriga/neotest-python/blob/52624d1306cb2b401aa72c1d0bee90438916ba7d/lua/neotest-python/init.lua#L72).

The summary not updating to say No tests found is odd though, could you enable info logs (pass log_level = vim.log.levels.INFO, to the setup config) and paste the log here after reproducing?

filipgodlewski commented 2 years ago

I will test it in about 3h, but an idea came to me that maybe the reason was because there was no .git/ root folder. This was actually a one-off test dir (not a repo), and so I'll give it a try and will let you know

filipgodlewski commented 2 years ago

So I double-checked with initializing git repo, and then ran nvim in verbose mode, and this is what I got regarding neotest-summary:

not found in runtime path: "ftplugin/neotest-summary.vim ftplugin/neotest-summary_*.vim ftplugin/neotest-summary/*.vim"
not found in runtime path: "ftplugin/neotest-summary.lua ftplugin/neotest-summary_*.lua ftplugin/neotest-summary/*.lua"
Executing FileType Autocommands for "*"
autocommand call s:LoadIndent()

not found in runtime path: "indent/neotest-summary.vim"
not found in runtime path: "indent/neotest-summary.lua"
Executing FileType Autocommands for "*"
autocommand if !exists('b:ts_highlight') | 0verbose exe "set syntax=" . expand("<amatch>") | endif

Executing FileType Autocommands for "*"
autocommand lua require('luasnip.loaders.from_lua')._load_lazy_loaded(tonumber(vim.fn.expand("<abuf>")))

not found in runtime path: "parser/neotest-summary.*"
Executing FileType Autocommands for "*"
autocommand <Lua 153: ~/.local/share/nvim/site/pack/packer/start/nvim-treesitter/lua/nvim-treesitter/configs.lua:130>

Executing BufEnter Autocommands for "*"
autocommand <Lua 321: ~/.local/share/nvim/site/pack/packer/start/neotest/lua/neotest/client/init.lua:414>

autocommand <Lua 327: ~/.local/share/nvim/site/pack/packer/start/neotest/lua/neotest/client/init.lua:421>

and that's about it.

filipgodlewski commented 2 years ago

Or maybe that's not the log that I was supposed to paste :P Sorry, but I could not find info on where is the vim.log stored, if it is different from the runtime log

filipgodlewski commented 2 years ago

I think, you gave the answer to the question:

lib.files.match_root_pattern("pyproject.toml", "setup.cfg", "mypy.ini", "pytest.ini", "setup.py")

As soon as I added any of those files, it recognized the tests. So to reproduce it, just create a subfolder without any of those files.

Question 1: Could we add .git/ and/or a fallback try/except call assuming that the root is cwd? That way this plugin could also be useful for scratch tests and/or small projects.

Here's the log that you were asking for. For future reference: it is stored in ~/.local/state/nvim/neotest.log:

INFO | 2022-11-28T17:32:10Z+0100 | ...te/pack/packer/start/neotest/lua/neotest/config/init.lua:283 | Configuration complete
INFO | 2022-11-28T17:32:10Z+0100 | ...pack/packer/start/neotest/lua/neotest/lib/subprocess.lua:25 | Starting child process
INFO | 2022-11-28T17:32:10Z+0100 | ...pack/packer/start/neotest/lua/neotest/lib/subprocess.lua:29 | Starting child process with command: /opt/homebrew/Cellar/neovim/0.8.1/bin/nvim --embed --headless
INFO | 2022-11-28T17:32:11Z+0100 | ...te/pack/packer/start/neotest/lua/neotest/config/init.lua:283 | Configuration complete
INFO | 2022-11-28T17:32:11Z+0100 | ...pack/packer/start/neotest/lua/neotest/lib/subprocess.lua:66 | CHILD | Connected to parent instance
INFO | 2022-11-28T17:32:11Z+0100 | ...te/pack/packer/start/neotest/lua/neotest/client/init.lua:337 | Initialising client
INFO | 2022-11-28T17:32:11Z+0100 | .../packer/start/neotest/lua/neotest/client/events/init.lua:48 | Emitting starting event
INFO | 2022-11-28T17:32:11Z+0100 | .../pack/packer/start/neotest/lua/neotest/adapters/init.lua:18 | Found 0 adapters for directory /Users/filipgodlewski/personal/t
INFO | 2022-11-28T17:32:11Z+0100 | ...te/pack/packer/start/neotest/lua/neotest/client/init.lua:435 | Initialisation finished in 0.004 seconds
INFO | 2022-11-28T17:32:11Z+0100 | .../packer/start/neotest/lua/neotest/client/events/init.lua:48 | Emitting started event
INFO | 2022-11-28T17:32:14Z+0100 | ...pack/packer/start/neotest/lua/neotest/lib/subprocess.lua:13 | Closing child channel
INFO | 2022-11-28T17:32:14Z+0100 | ...pack/packer/start/neotest/lua/neotest/lib/subprocess.lua:33 | Child process exited
INFO | 2022-11-28T17:32:16Z+0100 | ...te/pack/packer/start/neotest/lua/neotest/config/init.lua:283 | Configuration complete
rcarriga commented 1 year ago

As soon as I added any of those files, it recognized the tests. So to reproduce it, just create a subfolder without any of those files.

Yep that's exactly the expected behaviour :+1:

Question 1: Could we add .git/ and/or a fallback try/except call assuming that the root is cwd? That way this plugin could also be useful for scratch tests and/or small projects.

Not by default no, otherwise this adapter would be started every time neotest was started. The adapters are specifically designed to only use root patterns that are specific to them. Even if this was configurable I wouldn't suggest doing that, you'll run into neotest trying to parse random directories which could significantly impact the editor depending on size.

My suggestion would be to just create one of those config files if you're working in a directory that you normally have test files inside (they don't have to contain anything) or to setup some autocmd to open one of the test files in a buffer so that when neotest starts, it will know that the python adapter should be use.

filipgodlewski commented 1 year ago

Great, thanks, will bear that in mind