Skip to content
/ ote Public

ote updates a packages' go.mod file with a comment next to all dependencies that are test dependencies; identifying them as such.

License

Notifications You must be signed in to change notification settings

komuw/ote

Repository files navigation

ote

ote ci codecov PkgGoDev Go Report Card

ote updates a packages' go.mod file with a comment next to all dependencies that are test dependencies; identifying them as such.

It's name is derived from Kenyan hip hop artiste, Oteraw(One third of the hiphop group Kalamashaka).

By default, go and its related tools(go mod etc) do not differentiate regular dependencies from test ones when updating/writing the go.mod file.
There are various reasons why this is so, see go/issues/26955 & go/issues/26913
Thus ote fills that missing gap.
It is not perfect, but it seems to work. See How it works

For an example of a project using ote, see: ong/go.mod

Installation

go install github.com/komuw/ote@latest

Usage

ote --help
examples:

  ote .               # update go.mod in the current directory
        
  ote -f /someDir     # update go.mod in the /someDir directory

  ote -r              # write to stdout instead of updating go.mod in the current directory

  ote -f /someDir -r  # write to stdout instead of updating go.mod in the /someDir directory

If your application has a go.mod file like the following;

module github.com/pkg/myPkg

go 1.17

require (
	github.com/Shopify/sarama v1.26.4
	github.com/google/go-cmp v0.5.0
	github.com/nats-io/nats.go v1.10.0
	github.com/stretchr/testify v1.6.1 // priorComment
	golang.org/x/mod v0.3.0
)

require (
	github.com/golang/protobuf v1.4.2 // indirect
	github.com/nats-io/nats-server/v2 v2.1.7 // indirect
)

running ote will update the go.mod to the following;

module github.com/pkg/myPkg

go 1.17

require (
	github.com/Shopify/sarama v1.26.4
	github.com/nats-io/nats.go v1.10.0
	golang.org/x/mod v0.3.0
)

require (
	github.com/golang/protobuf v1.4.2 // indirect
	github.com/nats-io/nats-server/v2 v2.1.7 // indirect
)

require (
	github.com/google/go-cmp v0.5.0 // test
	github.com/stretchr/testify v1.6.1 // test; priorComment
)

ie; assuming that github.com/google/go-cmp and github.com/stretchr/testify are test-only dependencies in your application.

Features.

  • Update go.mod file with a comment // test next to any dependencies that are only used in tests.
  • Makes only the minimal of changes to go.mod files.
  • Preserves any prior comments that were in existence.
  • If a dependency was a test-only dependency and then it starts been used in other non-test contexts, ote will also recognise that and remove the // test comment.

How it works

  1. read go.mod file.
  2. get all the imports of all the files used by the package
    here we consider all the known build tags(darwin, openbsd, riscv64 etc)
  3. get all the modules of which all the imports belong to.
  4. find which of those are test-only modules.
  5. update go.mod with a comment(// test) next to all the test dependencies.

Inspiration(Hat tip)

  1. x/mod.
  2. go/packages.

About

ote updates a packages' go.mod file with a comment next to all dependencies that are test dependencies; identifying them as such.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages