Skip to content

Commit

Permalink
Use go generate directives for mock generation
Browse files Browse the repository at this point in the history
Instead of adding commands to the Makefile, use go generate directives
on top of the interfaces we need mocks for. This approach has the following
advantages:

1) Less context switching: you can declare the need for a mock in the
   code and not have to edit the makefiles.
2) The same directive can be reused everywhere - you no longer need to
   specify the filepath, interface name and the path to the generated
   mock.
3) If the interface is renamed or moved, the code generation will still
   work without modification.

I have left some commands in the makefile for generating mocks from code
in `pkg`, and from autogenerated code. We could in theory get rid of
these usages as well, but I did not feel there was much point at this
time since they are edge cases.
  • Loading branch information
dmjb committed Apr 24, 2024
1 parent 888c39a commit ecb05a7
Show file tree
Hide file tree
Showing 34 changed files with 781 additions and 386 deletions.
26 changes: 8 additions & 18 deletions .mk/gen.mk
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,17 @@ buf: ## generate protobuf files
sqlc: ## generate sqlc files
sqlc generate

## Note: Do not add `mockgen` commands here unless you are mocking an interface
## from a third party library, the `pkg` directory, or autogenerated code
## For all other uses, use the following go generate in the .go file:
## //go:generate go run go.uber.org/mock/mockgen -package mock_$GOPACKAGE -destination=./mock/$GOFILE -source=./$GOFILE
.PHONY: mock
mock: ## generate mocks
go generate ./...
mockgen -package mockdb -destination database/mock/store.go github.com/stacklok/minder/internal/db Store
mockgen -package mockgh -destination internal/providers/github/mock/github.go -source pkg/providers/v1/providers.go GitHub
mockgen -package auth -destination internal/auth/mock/jwtauth.go github.com/stacklok/minder/internal/auth JwtValidator,KeySetFetcher
mockgen -package mockverify -destination internal/verifier/mock/verify.go github.com/stacklok/minder/internal/verifier/verifyif ArtifactVerifier
mockgen -package mockghhook -destination internal/repositories/github/webhooks/mock/manager.go github.com/stacklok/minder/internal/repositories/github/webhooks WebhookManager
mockgen -package mockcrypto -destination internal/crypto/mock/crypto.go github.com/stacklok/minder/internal/crypto Engine
mockgen -package mockevents -destination internal/events/mock/eventer.go github.com/stacklok/minder/internal/events Interface
mockgen -package mockghclients -destination internal/repositories/github/clients/mock/clients.go github.com/stacklok/minder/internal/repositories/github/clients GitHubRepoClient
mockgen -package mockghrepo -destination internal/repositories/github/mock/service.go github.com/stacklok/minder/internal/repositories/github RepositoryService
mockgen -package mockbundle -destination internal/marketplaces/bundles/mock/reader.go github.com/stacklok/minder/pkg/mindpak/reader BundleReader
mockgen -package mockprofsvc -destination internal/profiles/mock/service.go github.com/stacklok/minder/internal/profiles ProfileService
mockgen -package mockrulesvc -destination internal/ruletypes/mock/service.go github.com/stacklok/minder/internal/ruletypes RuleTypeService
mockgen -package mockbundle -destination internal/marketplaces/bundles/mock/source.go github.com/stacklok/minder/pkg/mindpak/sources BundleSource
mockgen -package mocksubscription -destination internal/marketplaces/subscriptions/mock/subscription.go github.com/stacklok/minder/internal/marketplaces/subscriptions SubscriptionService
mockgen -package mockghprovsvc -destination internal/providers/github/service/mock/service.go github.com/stacklok/minder/internal/providers/github/service GitHubProviderService
mockgen -package mockratecache -destination internal/providers/ratecache/mock/restcache.go github.com/stacklok/minder/internal/providers/ratecache RestClientCache
mockgen -package mockgh -destination internal/providers/github/mock/common.go github.com/stacklok/minder/internal/providers/github ClientService
mockgen -package mockprov -destination internal/providers/mock/store.go github.com/stacklok/minder/internal/providers ProviderStore

mockgen -package mock_github -destination internal/providers/github/mock/github.go -source pkg/providers/v1/providers.go GitHub
mockgen -package mockbundle -destination internal/marketplaces/bundles/mock/reader.go -source pkg/mindpak/reader/reader.go
mockgen -package mockbundle -destination internal/marketplaces/bundles/mock/source.go -source pkg/mindpak/sources/source.go

# Ugly hack: cobra uses tabs for code blocks in markdown in some places
# This leads to some issues with MDX in the docs renderer
Expand Down
2 changes: 2 additions & 0 deletions internal/auth/jwtauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/lestrrat-go/jwx/v2/jwt/openid"
)

//go:generate go run go.uber.org/mock/mockgen -package mock_$GOPACKAGE -destination=./mock/$GOFILE -source=./$GOFILE

// JwtValidator provides the functions to validate a JWT
type JwtValidator interface {
ParseAndValidate(tokenString string) (openid.Token, error)
Expand Down
16 changes: 8 additions & 8 deletions internal/auth/mock/jwtauth.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
serverconfig "github.com/stacklok/minder/internal/config/server"
)

//go:generate go run go.uber.org/mock/mockgen -package mock_$GOPACKAGE -destination=./mock/$GOFILE -source=./$GOFILE

// Engine provides all functions to encrypt and decrypt data
type Engine interface {
EncryptOAuthToken(data []byte) ([]byte, error)
Expand Down
40 changes: 20 additions & 20 deletions internal/crypto/mock/crypto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/engine/ingester/artifact/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
"github.com/stacklok/minder/internal/providers"
"github.com/stacklok/minder/internal/providers/credentials"
mock_ghclient "github.com/stacklok/minder/internal/providers/github/mock"
mockverify "github.com/stacklok/minder/internal/verifier/mock"
"github.com/stacklok/minder/internal/verifier/verifyif"
mockverify "github.com/stacklok/minder/internal/verifier/verifyif/mock"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
)
Expand Down
2 changes: 2 additions & 0 deletions internal/events/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/ThreeDotsLabs/watermill/message"
)

//go:generate go run go.uber.org/mock/mockgen -package mock_$GOPACKAGE -destination=./mock/$GOFILE -source=./$GOFILE

// Handler is an alias for the watermill handler type, which is both wordy and may be
// detail we don't want to expose.
type Handler = message.NoPublishHandlerFunc
Expand Down
136 changes: 0 additions & 136 deletions internal/events/mock/eventer.go

This file was deleted.

Loading

0 comments on commit ecb05a7

Please sign in to comment.