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

unittest ValueError: Empty module name #51

Closed
rhinoxi opened this issue Jun 16, 2022 · 6 comments
Closed

unittest ValueError: Empty module name #51

rhinoxi opened this issue Jun 16, 2022 · 6 comments

Comments

@rhinoxi
Copy link
Contributor

rhinoxi commented Jun 16, 2022

Hi,
I'm using unittest and have a project with following structure:

.
├── proj
│   ├── __init__.py
│   ├── some_func.py
└── tests
    ├── __init__.py
    ├── test_some_func.py

When I open test_some_func.py and run :lua require('dap-python').test_method(), it works fine.
But If I open some other file first, then switch to test_some_func.py file, and run :lua require('dap-python').test_method(), it said ValueError: Empty module name.

So I looked into the code.

local path = vim.fn.expand('%:r:gs?/?.?')

the code above returns tests.test_some_func if I open test_some_func.py directly.
but returns .Path.To.Project.tests.test_some_func if I switch to test_some_func.py from other file.

So I googled a bit, and found changing the code to the following works fine for me

local path = vim.fn.expand('%:p:~:.:r:gs?/?.?')

Since I'm not so familiar with the vim/nvim plugin thing, can you guys confirm this or if I'm doing anything wrong?

Thanks.

@mfussenegger
Copy link
Owner

Are you opening neovim outside of the project directory?

'%:p:~:.:r:gs?/?.?' looks a bit redundandent. :p turns the path into an absolute path, :~ makes it relative to the home directory, :. makes it relative to the current directory. Those are kinda incompatible with each other.

See :help filename-modifiers

@rhinoxi
Copy link
Contributor Author

rhinoxi commented Jun 27, 2022

Thanks @mfussenegger .

'%:p:~:.:r:gs?/?.?' looks a bit redundandent.

Yes, %:p:~:.:r:gs?/?.? and %:.:r:gs?/?.? are returning the same results.

Are you opening neovim outside of the project directory?

No, I don't think so.

Let me use https://github.com/navdeep-G/samplemod to show the detail.

cd /Users/lds
git clone git@github.com:navdeep-G/samplemod.git
cd samplemod/sample
nvim core.py

Then use :e tests/test_basic.py to switch to the test file, and running the following command get different results:

  • :echo expand('%:r:gs?/?.?') get .Users.lds.samplemod.tests.test_basic
  • :echo expand('%:.:r:gs?/?.?') get tests.test_basic

If I want to get :lua require('dap-python').test_method() works fine, I think the second one is necessary.

@mfussenegger
Copy link
Owner

In your example I get tests.test_basic in both cases which is kinda the expected behavior of neovim. (Assuming you opened neovim in samplemod root, not sampelmod/sample, :e tests/test_basic.py would not work from inside the sample folder)

Are you using some kind of rooter plugin as well ?

In any case, I'd accept a PR that adds the :. to force a relative path on the working dir.

@rhinoxi
Copy link
Contributor Author

rhinoxi commented Jul 1, 2022

ok, I'll try to figure out which plugin or config is causing this behavior.

@rhinoxi
Copy link
Contributor Author

rhinoxi commented Jul 1, 2022

It seems like this project.nvim plugin is changing the working directory automatically, so that I can use :e tests/test_basic.py to switch when opening nvim in samplemod/sample.

But with nvim + project.nvim, both :echo expand('%:r:gs?/?.?') and :echo expand('%:.:r:gs?/?.?') will get the same result, tests.test_basic.

I got the problem with LunarVim and some custom configuration, still looking into it.

@rhinoxi
Copy link
Contributor Author

rhinoxi commented Jul 8, 2022

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

No branches or pull requests

2 participants