0

I am trying to write some tests for a ftplugin but I am encountering some issues. The following is the plugin structure:

.
├── ftplugin
│   └── python.vim
├── lib
│   └── myfunctions.vim
├── plugin
│   └── myplugin.vim
├── test_script.vim
└── testfile.py

The content of the files is as follows:

./ftplugin/python.vim

vim9script

b:foo = 'Success!'

./lib/myfunctions.vim

vim9script

export def MyFunction()
    if !empty(getbufvar('%', "foo"))
        echom b:foo
    else
        echom 'This does not seem a python file.'
    endif
enddef

./plugin/myplugin.vim

vim9script

import autoload "../lib/myfunctions.vim"

command! MyCommand myfunctions.MyFunction()

testfile.py

a = 3
b = 2
c = a + b

and finally test_script.vim

vim9script

filetype plugin indent on

import "./plugin/myplugin.vim"

var filename = 'testfile.py'
exe $"edit {filename}"
exe $'buffer {filename}'
    
MyCommand

The above example is reproducible.

If I open Vim and source test_script.vim from the current directory, I get the message 'This does not seem a python file.' whereas I would like to have 'Success' due to the fact I wish the local ftplugin to be considered as a "standard" ftplugin folder.

How shall I change test_script.vim to get the wanted result?

Note: something similar was poorly asked in What options shall be set for loading ftplugins? but for some reason SO won't allow to delete such a question.

1 Answer 1

1

As far as I'm concerned, filetype plugin indent on should appear in a .vimrc and nowhere else.

Also I see an explicit loading of the plugin script. This is fishy. If your plugin isn't in the runtimepath, how could your ftplugin also be in the runtimepath. Without it, it can't be loaded automatically.

For automating the unit testing my plugins, I rely on a plugin manager that takes care of making sure the plugin I'm testing (and its dependencies) are loaded. It could be done by hand from a dedicated .vimrc (actually my tests related .vimrc takes care of activating the plugin manager, and so on), but still it takes care of setting runtimepath as it should.

PS: I know I know nothing of Vim 9 scripting language, yet explicit importing of plugin/ or autoload/ scripts feels wrong.

4
  • Thanks. One way could be to copy the whole plugin in $VIMRUNTIME/pack/dist/myplugin followed by packadd myplugin and then remove the myplugin folder at the end.
    – Barzi2001
    Commented Jun 14 at 11:47
  • That's what I do with vim-flavor -- the plugin manager I use to automate the tests in the "github" CI. Commented Jun 14 at 11:51
  • 2
    A dedicated test vimrc that adds the plugin directory to runtimepath will also do.
    – D. Ben Knoble
    Commented Jun 14 at 12:00
  • Yes, the dedicated .vimrc in-fact works. The only drawback is that you must carry an additional file around. I was also trying to create a new folder ¨~/myname/.vim/pack/opt/myplugin and add it to 'packpath' and then run packadd myplugin but it didn't work.
    – Barzi2001
    Commented Jun 14 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.