go.nvim icon indicating copy to clipboard operation
go.nvim copied to clipboard

Running file tests for a module in a subdirectory requires explicit cd

Open grddev opened this issue 2 years ago • 8 comments

Creating a sample repository with a nested module and opening the main project folder as below

mkdir -p project/mod1
cd project/mod1
go mod init example.com/mod1/v2
echo -e 'package mod1\nimport "testing"\nfunc TestX(t *testing.T) { }' > main_test.go
cd ..
nvim

Opening mod1/main_test.go in nvim (without switching directory) and running :GoTestFile runs the expected command go test . -run TestX, but it fails as it runs it from the main project directory instead of the mod1 directory. Adding a :cd mod1 before running the tests makes everything work, but is somewhat inconvenient, and given that the plugin has already figured out that the module path is . it seems like it already knows about the location of the go.mod file and thus could also run the tests from that directory.

grddev avatar Nov 24 '23 09:11 grddev

image

Not reproduce. I think it is more a nvim setup.

ray-x avatar Nov 26 '23 06:11 ray-x

Not reproduce. I think it is more a nvim setup.

Interesting. Is your nvim setup to automatically change directory based on file? If you run :pwd does that include the mod1 folder or not?

EDIT: For me, the :GoTestFile command works only when nvim:s :pwd is in the mod1 directory, and otherwise not.

grddev avatar Nov 26 '23 09:11 grddev

The test file folder is auto-detected. One thing you can try is using the minium setup in playground folder

cd go.nvim/playground/sampleApp
nvim -u ../init_lazy.lua pkg/findAllSubStr_test.go 
GoTestFile -F

ray-x avatar Nov 26 '23 12:11 ray-x

So in that case the go.mod file is in the :pwd directory. If move the module to a subdirectory as per my instructions above, if I updated the above instructions to be the following, it fails

cd go.nvim/playground/sampleApp
mkdir x
mv go.* *.go pkg x
HOME=$(mktemp -d) nvim -u ../init_lazy.lua x/pkg/findAllSubStr_test.go 
GoTestFile -F

Note that I added HOME=$(mktemp -d) to ensure that none of my default nvim configuration was loaded, so this should be with exactly the configuration from init_lazy.lua.

Technically, it first failed with a different error as I no longer had gopls installed and it then it instead tried to run go test x/pkg instead of go test ./pkg, but with gopls installed, this fails the same way as described in previous comments.

grddev avatar Nov 26 '23 22:11 grddev

Ok, I understand what happened. Pushed an fix for it.

ray-x avatar Nov 28 '23 11:11 ray-x

I'm not too familiar with lua scripting in nvim, but it looks like this just changes the directory, without changing back afterwards. In my environment that would cause other things to break. Ideally, you'd want to pass the workspace folder as the cwd argument of uv.spawn() in order to not actually change the cwd of nvim itself at all.

grddev avatar Dec 04 '23 23:12 grddev

I think it's a bug with GitHub. It refers to PR 402 instead of Issue 402 in the previous comments

weirdgiraffe avatar Feb 19 '24 18:02 weirdgiraffe