查找Go程序的测试依赖项

I am currently using vendor/ directory to locally vendor my dependencies.

go list -f '{{join .Deps "
"}}' ./...  |grep -Eo 'vendor/.*'

This shows me all "build dependencies" used in my build, (that are used from vendor/). This feature is documented here: https://golang.org/cmd/go/#hdr-List_packages

But it doesn't include your "test dependencies"!

So to find my "test dependencies", I run this command:

$ go list -f '{{join .TestImports "
"}}' ./...  |grep -Eo 'vendor/.*'
vendor/github.com/stretchr/testify/assert
...

It correctly list the "test imports", but it doesn't list the "dependencies" of those test imports.

For example, the vendor/github.com/stretchr/testify/assert directory listed above actually depends on vendor/github.com/pmezard/go-difflib/difflib (and a few other packages). This is not shown in the output above!

But if I run this command, it shows the dependencies of testify/assert:

$ go list -f '{{join .Deps "
"}}' ./vendor/github.com/stretchr/testify/assert | grep -Eo 'vendor/.*'
vendor/github.com/davecgh/go-spew/spew
vendor/github.com/pmezard/go-difflib/difflib
...

So it appears like I need to call go list -f '{{join .Deps " "}}' on all "test imports" to find "all test dependencies"?

Is there a more efficient way of doing this, ideally with a single go list call and some templating?

I am afraid there is no way to show all the recursive test dependencies using just one "go list".

You will need to use at least two:

go list -f '{{join .Deps "
"}}' `go list -f '{{join .TestImports " "}}' ./...`