Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autogenerate mockery mocks #2215

Merged
merged 19 commits into from Jun 5, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -258,9 +258,9 @@ jobs:
- restore_cache:
keys:
- pre-commit-dot-cache-{{ checksum ".pre-commit-config.yaml" }}
- run: echo 'export PATH=${PATH}:~/go/bin' >> $BASH_ENV
- run: echo 'export PATH=${PATH}:~/go/bin:~/transcom/mymove/bin' >> $BASH_ENV
- run: make bin/callgraph
- run: make server_generate
- run: make server_generate mocks_generate
# this is so we can avoid go mod downloading and resulting in an error on a false positive
- run: scripts/pre-commit-go-mod || exit 0
- run:
@@ -281,9 +281,10 @@ jobs:
- restore_cache:
keys:
- go-mod-sources-v2-{{ checksum "go.sum" }}
- run: echo 'export PATH=${PATH}:~/go/bin:~/transcom/mymove/bin' >> $BASH_ENV
- run:
name: Run make server_generate
command: make server_generate
name: Run make server_generate mocks_generate
command: make server_generate mocks_generate
- run:
name: Run acceptance tests
command: |
@@ -437,7 +438,7 @@ jobs:
sudo apt-get -qq update
# Debian stretch only supports 9.6 client
sudo apt-get -qq -y install postgresql-client-9.6
- run: echo 'export PATH=${PATH}:~/go/bin' >> $BASH_ENV
- run: echo 'export PATH=${PATH}:~/go/bin:~/transcom/mymove/bin' >> $BASH_ENV
- run:
name: make server_test
command: |
@@ -476,7 +477,7 @@ jobs:
sudo apt-get -qq update
# Debian stretch only supports 9.6 client
sudo apt-get -qq -y install postgresql-client-9.6
- run: echo 'export PATH=${PATH}:~/go/bin' >> $BASH_ENV
- run: echo 'export PATH=${PATH}:~/go/bin:~/transcom/mymove/bin' >> $BASH_ENV
- run:
name: Setup Code Climate test-reporter
command: |
@@ -541,6 +542,7 @@ jobs:
- health-checker # for confirming deploy success.
- ecs-service-logs # for showing logs on failed deploy
- ecs-deploy-task-container # for deploying task container
- mockery # for generating mocks
- announce_failure

# `build_app` builds the application container and pushes to the container repository
@@ -611,9 +613,10 @@ jobs:
- restore_cache:
keys:
- go-mod-sources-v2-{{ checksum "go.sum" }}
- run: echo 'export PATH=${PATH}:~/go/bin:~/transcom/mymove/bin' >> $BASH_ENV
- run:
name: Run make server_generate
command: make server_generate
name: Run make server_generate mocks_generate
command: make server_generate mocks_generate
- run:
name: Build Chamber
command: make bin/chamber
@@ -682,9 +685,10 @@ jobs:
- restore_cache:
keys:
- go-mod-sources-v2-{{ checksum "go.sum" }}
- run: echo 'export PATH=${PATH}:~/go/bin:~/transcom/mymove/bin' >> $BASH_ENV
- run:
name: Run make server_generate
command: make server_generate
name: Run make server_generate mocks_generate
command: make server_generate mocks_generate
- run:
name: Build Chamber
command: make bin/chamber
@@ -7,6 +7,7 @@

# testing
/coverage
**/mocks
coverage.out
__snapshots__

@@ -11,6 +11,7 @@

/pkg/gen
/pkg/assets
**/mocks

# testing
/coverage
@@ -187,6 +187,10 @@ bin/soda: .check_go_version.stamp .check_gopath.stamp
bin/swagger: .check_go_version.stamp .check_gopath.stamp
go build -ldflags "$(LDFLAGS)" -o bin/swagger github.com/go-swagger/go-swagger/cmd/swagger

# No static linking / $(LDFLAGS) because mockery is only used for testing
bin/mockery: .check_go_version.stamp .check_gopath.stamp
go build -o bin/mockery github.com/vektra/mockery/cmd/mockery
This conversation was marked as resolved by mkrump

This comment has been minimized.

Copy link
@chrisgilmerproj

chrisgilmerproj Jun 3, 2019

Contributor

Would be good to add a note here about why we don't have -ldflags here. Maybe something like "No need for static linking because this tool is only used for testing".

This comment has been minimized.

Copy link
@mkrump

mkrump Jun 3, 2019

Author Contributor

👍


### Cert Targets

bin/rds-combined-ca-bundle.pem:
@@ -290,6 +294,7 @@ server_deps: .check_gopath.stamp \
bin/gin \
bin/soda \
bin/swagger \
bin/mockery \
bin/rds-combined-ca-bundle.pem ## Install or Build server dependencies

.PHONY: server_generate
@@ -364,7 +369,7 @@ build: server_build build_tools client_build ## Build the server, tools, and cli
# webserver_test runs a few acceptance tests against a local or remote environment.
# This can help identify potential errors before deploying a container.
.PHONY: webserver_test
webserver_test: bin/rds-combined-ca-bundle.pem server_generate bin/chamber ## Run acceptance tests
webserver_test: bin/rds-combined-ca-bundle.pem server_generate mocks_generate bin/chamber ## Run acceptance tests
ifndef TEST_ACC_ENV
@echo "Running acceptance tests for webserver using local environment."
@echo "* Use environment XYZ by setting environment variable to TEST_ACC_ENV=XYZ."
@@ -389,28 +394,34 @@ else
endif
endif

.PHONY: mocks_generate
mocks_generate: .mocks_generate.stamp ## Generate mockery mocks for tests
.mocks_generate.stamp : bin/mockery
go generate $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/)

This comment has been minimized.

Copy link
@mikena-truss

mikena-truss Jun 3, 2019

Contributor

If this is slow, at some point, we can get fancy and only update changed files since the last run. I think this is ok for now though.

touch .mocks_generate.stamp

.PHONY: server_test
server_test: server_deps server_generate db_test_reset db_test_migrate ## Run server unit tests
server_test: server_deps server_generate mocks_generate db_test_reset db_test_migrate ## Run server unit tests
# Don't run tests in /cmd or /pkg/gen & pass `-short` to exclude long running tests
# Use -test.parallel 1 to test packages serially and avoid database collisions
# Disable test caching with `-count 1` - caching was masking local test failures
DB_PORT=$(DB_PORT_TEST) go test -p 1 -count 1 -short $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/)

.PHONY: server_test_all
server_test_all: server_deps server_generate db_dev_reset db_dev_migrate ## Run all server unit tests
server_test_all: server_deps server_generate mocks_generate db_dev_reset db_dev_migrate ## Run all server unit tests
# Like server_test but runs extended tests that may hit external services.
DB_PORT=$(DB_PORT_TEST) go test -p 1 -count 1 $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/)

.PHONY: server_test_coverage_generate
server_test_coverage_generate: server_deps server_generate db_test_reset db_test_migrate ## Run server unit test coverage
server_test_coverage_generate: server_deps server_generate mocks_generate db_test_reset db_test_migrate ## Run server unit test coverage
# Don't run tests in /cmd or /pkg/gen
# Use -test.parallel 1 to test packages serially and avoid database collisions
# Disable test caching with `-count 1` - caching was masking local test failures
# Add coverage tracker via go cover
DB_PORT=$(DB_PORT_TEST) go test -coverprofile=coverage.out -covermode=count -p 1 -count 1 -short $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/)

.PHONY: server_test_coverage
server_test_coverage: server_deps server_generate db_test_reset db_test_migrate server_test_coverage_generate ## Run server unit test coverage with html output
server_test_coverage: server_deps server_generate mocks_generate db_test_reset db_test_migrate server_test_coverage_generate ## Run server unit test coverage with html output
DB_PORT=$(DB_PORT_TEST) go tool cover -html=coverage.out

#
@@ -857,6 +868,7 @@ clean: # Clean all generated files
rm -rf ./public/swagger-ui/*.{css,js,png}
rm -rf ./tmp/secure_migrations
rm -rf ./tmp/storage
find ./pkg -type d -name "mocks" -exec rm -rf {} +

.PHONY: spellcheck
spellcheck: .client_deps.stamp # Run interactive spellchecker
@@ -2,10 +2,17 @@

[Mockery](https://github.com/vektra/mockery) provides the ability to easily generate mocks for golang interfaces. It removes the boilerplate coding required to use mocks.

To generate a mock for testing purposes, you must use the mockery command line interface tool to do so.
*In Golang, mocks can only be created on interfaces - not structs. So, it is important that for whichever mock you are trying to generate, it should correspond to the appropriate interface.*

`$GOPATH/bin/mockery -name <nameOfInterface> -dir $GOPATH/src/github.com/transcom/mymove/<directoryInterfaceIsLocatedIn>`
## Autogenerating mocks with `go generate`

After you run this command a mock, corresponding to the interface name, will be created in the `/mocks` directory.
The `make mocks_generate` command will regenerate mocks for all interfaces tagged with the appropriate `go generate` command. To add an interface to the list of autogenerated mocks, just add a
`go:generate` comment like below and update the name with your interface name.

*In Golang, mocks can only be created on interfaces - not structs. So, it is important that for whichever mock you are trying to generate, it should correspond to the appropriate interface.*
```.go
// AccessCodeClaimer is the service object interface for ValidateAccessCode
//go:generate mockery -name AccessCodeClaimer
type AccessCodeClaimer interface {
ClaimAccessCode(code string, serviceMemberID uuid.UUID) (*models.AccessCode, *validate.Errors, error)
}
```
1 go.mod
@@ -67,6 +67,7 @@ require (
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709
github.com/tealeg/xlsx v1.0.3
github.com/trussworks/pdfcpu v0.1.15-0.20180823221217-a53781a43f9d
github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5
go.mozilla.org/pkcs7 v0.0.0-20181213175627-3cffc6fbfe83
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.