Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to use test dependencies in Go but prevent export them

Given a project in Golang (1.14+) which is using test dependencies (like github.com/stretchr/testify) and now assume this project is a public library which can be used by others.

Usually when I now use go mod graph I'll always see this dependency like:

github.com/its-me/[email protected]
github.com/stretchr/[email protected] github.com/davecgh/[email protected]
github.com/stretchr/[email protected] github.com/pmezard/[email protected]
github.com/stretchr/[email protected] github.com/stretchr/[email protected]
github.com/stretchr/[email protected] gopkg.in/[email protected]
gopkg.in/[email protected] gopkg.in/[email protected]

go mod tidy or go mod download also seems to download all the test dependencies from the used lib. But instead of telling everybody to use exclude in their go.mod files is there a way to even prevent this been exported?

like image 274
GreNodge Avatar asked Sep 25 '20 21:09

GreNodge


People also ask

How do you manage dependency in Go?

In Go, you manage dependencies as modules that contain the packages you import. This process is supported by: A decentralized system for publishing modules and retrieving their code. Developers make their modules available for other developers to use from their own repository and publish with a version number.

Does Go build ignore test files?

When go builds a package normally ( go build or go install ) it will ignore any files with the name pattern *_test.go . This means that object code for any packages that are only imported from those test files will not be linked into your executable.

Is Gopath required with Go modules?

As mentioned above, Go runtime still does use $GOPATH as a download directory of Go packages. To make the Google's saying correct, Go module does not entirely replace GOPATH , but replaces GOPATH for version control and package distribution.

What Go mod tidy do?

go mod tidy ensures that the go. mod file matches the source code in the module. It adds any missing module requirements necessary to build the current module's packages and dependencies, if there are some not used dependencies go mod tidy will remove those from go.


1 Answers

go mod tidy is intended to provide all of the dependencies needed to run go test all. Note that in Go 1.16, go test all will be somewhat less aggressive about transitive dependencies of tests (https://tip.golang.org/doc/go1.16#all-pattern).

However, if your own test itself is using testify, then users of your package will need to download testify in order to run go test all in their own module.

(As colm.anseo notes, if you want you can split out the heavier tests to a separate package, so that when your users run go test all they will not run those tests and will not need to download the source code for those dependencies.)

Note that Go 1.17 adds support for module graph pruning: if your module specifies go 1.17 or higher and a consumer of your module does not use your test dependency in their own module, they will not need to download the source code or go.mod file for that dependency. (And once https://golang.org/issue/44435 is implemented, when they run go mod download it will not download the irrelevant dependency either.)

like image 168
bcmills Avatar answered Oct 16 '22 15:10

bcmills