Skip to content

Commit

Permalink
Improve our mock setup (G-Research#389)
Browse files Browse the repository at this point in the history
* Use a config file for mockery

* Add `install-tools` Makefile target

* Dev container: install Go tools in /usr/local/bin

* Upgrade Go tools

- mockery from 2.32.4 to 2.34.0
- golangci-lint from 1.54.1 to 1.54.2
- goimports from 0.11.0 to 0.13.0

* Add generated mocks

* Check mocks have been committed in CI

* Do not run goimports on mocks

* Simply formatting check logic in CI

* Remove mocks generation from Go unit tests job in CI
  • Loading branch information
jgiannuzzi authored Sep 26, 2023
1 parent dd9d0af commit c6695c8
Show file tree
Hide file tree
Showing 13 changed files with 898 additions and 36 deletions.
24 changes: 16 additions & 8 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# [Choice] Go version (use -bookworm, or -bullseye variants on local arm64/Apple Silicon): 1, 1.21, 1.20, 1-bookworm, 1.21-bookworm, 1.20-bookworm, 1-bullseye, 1.21-bullseye, 1.20-bullseye
ARG VARIANT=1-bookworm
FROM mcr.microsoft.com/devcontainers/go:1-${VARIANT}
FROM mcr.microsoft.com/devcontainers/go:1-${VARIANT} AS base

# ==================================================================

FROM base AS tools
RUN export GOBIN=/tmp/tools \
&& go install github.com/vektra/mockery/v2@v2.34.0 \
&& go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2 \
&& go install golang.org/x/tools/cmd/goimports@v0.13.0 \
&& go install mvdan.cc/gofumpt@v0.5.0

# ==================================================================

FROM base AS dev

# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update \
Expand All @@ -17,13 +30,8 @@ RUN apt-get update \
ripgrep \
sqlite3

# [Optional] Uncomment the next lines to use go get to install anything else you need
USER vscode
RUN go install github.com/vektra/mockery/v2@v2.32.4 \
&& go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.1 \
&& go install golang.org/x/tools/cmd/goimports@v0.11.0 \
&& go install mvdan.cc/gofumpt@v0.5.0
USER root
# Install tools
COPY --from=tools /tmp/tools /usr/local/bin

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
Expand Down
37 changes: 16 additions & 21 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,30 @@ jobs:
id: tags
run: echo tags=$(cat .go-build-tags) >> $GITHUB_OUTPUT

- name: Download formatters
run: |
go install golang.org/x/tools/cmd/goimports@v0.11.0
go install mvdan.cc/gofumpt@v0.5.0
- name: Install tools
run: make install-tools

- name: Check formatting
run: |
unformatted=$(
gofumpt -l .
goimports -l --local github.com/G-Research/fasttrackml .
)
if [ -n "$unformatted" ]; then
for file in $unformatted; do
make go-format
modified=$(git ls-files --modified -- '*.go')
if [ -n "$modified" ]; then
for file in $modified; do
echo "::error file=$file::$file is not formatted properly (hint: run \"make go-format\" to fix this)"
done
exit 1
fi
- name: Download mockery
run: go install github.com/vektra/mockery/v2@v2.32.4

- name: Generate mocks
run: make mocks-generate
- name: Check mocks
run: |
make mocks-generate
modified=$(git ls-files --modified -- '*/mock_*.go')
if [ -n "$modified" ]; then
for file in $modified; do
echo "::error file=$file::$file is not up to date (hint: run \"make mocks-generate\" to fix this)"
done
exit 1
fi
- name: Check with go vet
run: go vet --tags "${{ steps.tags.outputs.tags }}" ./...
Expand Down Expand Up @@ -94,12 +95,6 @@ jobs:
with:
go-version: "1.21"

- name: Download mockery
run: go install github.com/vektra/mockery/v2@v2.32.4

- name: Generate mocks
run: make mocks-generate

- name: Run Go Unit Tests
run: make test-go-unit

Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ tests/integration/python/*/*.src
outputs
poetry.lock

# Mocks
mock_*

# Misc
.DS_Store

Expand Down
17 changes: 17 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
dir: "{{ .InterfaceDir }}"
filename: "mock_{{ .InterfaceNameSnake }}.go"
with-expecter: false
inpackage: true
packages:
github.com/G-Research/fasttrackml/pkg/api/mlflow/dao/repositories:
interfaces:
BaseRepositoryProvider:
ExperimentRepositoryProvider:
MetricRepositoryProvider:
ParamRepositoryProvider:
RunRepositoryProvider:
TagRepositoryProvider:
github.com/G-Research/fasttrackml/pkg/api/mlflow/service/artifact/storage:
interfaces:
ArtifactStorageFactoryProvider:
ArtifactStorageProvider:
21 changes: 17 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ help: ## display this help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-24s\033[0m - %s\n", $$1, $$2}'
@echo

#
# Tools targets.
#
.PHONY: install-tools ## install tools.
install-tools:
@echo '>>> Installing tools.'
@go install github.com/vektra/mockery/v2@v2.34.0
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2
@go install golang.org/x/tools/cmd/goimports@v0.13.0
@go install mvdan.cc/gofumpt@v0.5.0

#
# Linter targets.
#
Expand All @@ -76,7 +87,7 @@ go-build: ## build app binary.
go-format: ## format go code.
@echo '>>> Formatting go code.'
@gofumpt -w .
@goimports -w -local github.com/G-Research/fasttrackml .
@goimports -w -local github.com/G-Research/fasttrackml $(shell find . -type f -name '*.go' -not -name 'mock_*.go')

.PHONY: go-dist
go-dist: go-build ## archive app binary.
Expand Down Expand Up @@ -170,12 +181,14 @@ service-clean: ## clean containers.
# Mockery targets.
#
.PHONY: mocks-clean
mocks-clean: ## cleans old mocks.
find . -name "mock_*.go" -type f -print0 | xargs -0 /bin/rm -f
mocks-clean: ## cleans mocks.
@echo ">>> Cleaning mocks."
@find ./pkg -name 'mock_*.go' -type f -delete

.PHONY: mocks-generate
mocks-generate: mocks-clean ## generate mock based on all project interfaces.
mockery --all --dir "./pkg/api/mlflow" --inpackage --case underscore
@echo ">>> Generating mocks."
@mockery

#
# Build targets
Expand Down
43 changes: 43 additions & 0 deletions pkg/api/mlflow/dao/repositories/mock_base_repository_provider.go

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

137 changes: 137 additions & 0 deletions pkg/api/mlflow/dao/repositories/mock_experiment_repository_provider.go

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

Loading

0 comments on commit c6695c8

Please sign in to comment.