Using go test
- is there a way to run tests for a package inside the current project without providing the relative path?
I can run go test fmt
for example regardless of my current working directory and go
will look around for the fmt
package and will start running its tests. This is go test
in package list mode.
However, I can’t figure out how to run go test
for a package in my current project.
For example - given this folder structure:
mySpecialProject
|--foo
|--bar
|--bizz
|--buzz
I already know that I can run go test ./bar/bizz
to run the tests in the bizz
package; I know about using go test -run
to run specifically named tests; I also know about go test ./...
to run all tests for all packages inside the current directory. That's not what I'm asking for here. I'm looking to run tests from the root of a project for a single package without providing the relative path.
Reading the docs about go test
regarding package list mode, it "feels" like it should work to run go test bizz
or something like it to run the tests in bizz without providing the relative path.
When I run that command however, it just says package bizz is not in GOROOT (/usr/local/Cellar/go/1.17.1/libexec/src/bizz)
Any ideas? I've figured out a workaround (find . -type d -name "bizz" | xargs go test
) but I'd much rather use something built into go test.
CodePudding user response:
I'm fairly certain this is not possible. go test
calls PackagesAndErrors function to process the args.
Stepping through the code i don't see any way for what you want to work natively to go test
. The shell trickery you are using seems to be the best way to do what you are wanting.
As there might be another way around your issue, can you describe why you are wanting this specific behavior?
CodePudding user response:
You should be able to execute go test ./**/bizz
. This is a shell feature called globstar, so depending on your shell this might work for you. You may need to enable first with set -o globstar
or shopt -o globstar
.
Edit:
go test ./.../bizz
also works, in this case it is a go feature. Only difference is that this command doesn't work of bizz is a direct sub-directory of the current working dir.