Skip to content

Commit

Permalink
Refactor support for Mockery in the makefiles (#526)
Browse files Browse the repository at this point in the history
The support for mockery is moved from all other makefiles and .mk files
into the `default-mockery.mk` file. Mockery related code in other
makefiles and .mk files is removed.

The included `default-go.mk` file now includes `default-mockery.mk`.

There are two targets in `default-mockery.mk`:

1. `install-mockery`: Installs mockery in docker or locally if docker is
not available
2. `generate-mocks`: Runs generation of the mocks for go interfaces

The targets above must be run explicitly.

The `generate-mocks` target looks for `.mockery.yaml` files in the repo
and it runs the mockery mock generator on each `.mockery.yaml` file it
finds. This has the nice effect of allowing `.mockery.yaml` files to be
in either the root of the repo or in subdirectories, so the choice of
placement of `.mockery.yaml` files is left to the developer.
  • Loading branch information
liamfallon committed Feb 21, 2024
1 parent a32530a commit 01cb704
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 56 deletions.
7 changes: 1 addition & 6 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
packages:
github.com/nephio-project/nephio/controllers/pkg/giteaclient:
interfaces:
GiteaClient:
config:
dir: "{{.InterfaceDir}}"
with-expecter: true
19 changes: 1 addition & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,4 @@ unit-clean: ## These targets are delegated to the Makefiles of individual Go mod
docker-build docker-push: ## These targets are delegated to the Makefiles next to Dockerfiles
for dir in $(DOCKERFILE_DIRS); do \
$(MAKE) -C "$$dir" $@ ; \
done


##@ Mockery code

.PHONY: install-mockery
install-mockery: ## install mockery
ifeq ($(CONTAINER_RUNNABLE), 0)
$(CONTAINER_RUNTIME) pull docker.io/vektra/mockery:v${MOCKERY_VERSION}
else
wget -qO- https://github.com/vektra/mockery/releases/download/v${MOCKERY_VERSION}/mockery_${MOCKERY_VERSION}_${OS}_${OS_ARCH}.tar.gz | sudo tar -xvzf - -C /usr/local/bin
endif

.PHONY: generate-mocks
generate-mocks:
for dir in $(GO_MOD_DIRS); do \
$(MAKE) -C "$$dir" $@ || true ; \
done
done
13 changes: 0 additions & 13 deletions controllers/pkg/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,3 @@ include ../../default-go.mk

# This includes the 'help' target that prints out all targets with their descriptions organized by categories
include ../../default-help.mk

# This includes the below targets for mockery
# install-mockery
# generate-mocks
include ../../default-mockery.mk
.PHONY: generate-mocks
generate-mocks:
ifeq ($(CONTAINER_RUNNABLE), 0)
echo ${PWD}
$(CONTAINER_RUNTIME) run --security-opt label=disable -v ${PWD}:/src -w /src/controllers/pkg docker.io/vektra/mockery:v${MOCKERY_VERSION}
else
mockery
endif
17 changes: 0 additions & 17 deletions default-go-test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@


GO_VERSION ?= 1.20.2
MOCKERY_VERSION=2.37.1
TEST_COVERAGE_FILE=lcov.info
TEST_COVERAGE_HTML_FILE=coverage_unit.html
TEST_COVERAGE_FUNC_FILE=func_coverage.out
Expand All @@ -39,22 +38,6 @@ else
go tool cover -func=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_FUNC_FILE}
endif

.PHONY: install-mockery
install-mockery: ## install mockery
ifeq ($(CONTAINER_RUNNABLE), 0)
$(CONTAINER_RUNTIME) pull docker.io/vektra/mockery:v${MOCKERY_VERSION}
else
wget -qO- https://github.com/vektra/mockery/releases/download/v${MOCKERY_VERSION}/mockery_${MOCKERY_VERSION}_${OS}_${OS_ARCH}.tar.gz | sudo tar -xvzf - -C /usr/local/bin
endif

.PHONY: generate-mocks
generate-mocks:
ifeq ($(CONTAINER_RUNNABLE), 0)
$(CONTAINER_RUNTIME) run --security-opt label=disable -v ${PWD}:/src -w /src docker.io/vektra/mockery:v${MOCKERY_VERSION}
else
mockery
endif

.PHONY: unit-clean
unit-clean: ## Clean up the artifacts created by the unit tests
ifeq ($(CONTAINER_RUNNABLE), 0)
Expand Down
1 change: 1 addition & 0 deletions default-go.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ include $(GIT_ROOT_DIR)/default-go-misc.mk
include $(GIT_ROOT_DIR)/default-go-test.mk
include $(GIT_ROOT_DIR)/default-go-lint.mk
include $(GIT_ROOT_DIR)/default-gosec.mk
include $(GIT_ROOT_DIR)/default-mockery.mk
24 changes: 23 additions & 1 deletion default-mockery.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,30 @@
# limitations under the License.


MOCKERY_VERSION=2.37.1
MOCKERY_VERSION=2.41.0
GIT_ROOT_DIR ?= $(dir $(lastword $(MAKEFILE_LIST)))
OS_ARCH ?= $(shell uname -m)
OS ?= $(shell uname)
include $(GIT_ROOT_DIR)/detect-container-runtime.mk

.PHONY: install-mockery
install-mockery: ## install mockery
ifeq ($(CONTAINER_RUNNABLE), 0)
$(CONTAINER_RUNTIME) pull docker.io/vektra/mockery:v${MOCKERY_VERSION}
else
wget -qO- https://github.com/vektra/mockery/releases/download/v${MOCKERY_VERSION}/mockery_${MOCKERY_VERSION}_${OS}_${OS_ARCH}.tar.gz | sudo tar -xvzf - -C /usr/local/bin
endif

.PHONY: generate-mocks
generate-mocks:
ifeq ($(CONTAINER_RUNNABLE), 0)
find . -name .mockery.yaml \
-exec echo generating mocks specified in {} . . . \; \
-execdir $(CONTAINER_RUNTIME) run --security-opt label=disable -v .:/src -w /src docker.io/vektra/mockery:v${MOCKERY_VERSION} \; \
-exec echo generated mocks specified in {} \;
else
find . -name .mockery.yaml \
-exec echo generating mocks specified in {} . . . \; \
-execdir mockery \; \
-exec echo generated mocks specified in {} \;
endif
2 changes: 1 addition & 1 deletion testing/mockeryutils/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ all: fmt lint gosec
include ../../default-go.mk

# This includes the 'help' target that prints out all targets with their descriptions organized by categories
include ../../default-help.mk
include ../../default-help.mk

0 comments on commit 01cb704

Please sign in to comment.