From 01cb704d9a57d2c198dbaedfa9b5e5a7790fc3d2 Mon Sep 17 00:00:00 2001 From: Liam Fallon <35595825+liamfallon@users.noreply.github.com> Date: Wed, 21 Feb 2024 12:29:15 +0000 Subject: [PATCH] Refactor support for Mockery in the makefiles (#526) 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. --- .mockery.yaml | 7 +------ Makefile | 19 +------------------ controllers/pkg/Makefile | 13 ------------- default-go-test.mk | 17 ----------------- default-go.mk | 1 + default-mockery.mk | 24 +++++++++++++++++++++++- testing/mockeryutils/Makefile | 2 +- 7 files changed, 27 insertions(+), 56 deletions(-) diff --git a/.mockery.yaml b/.mockery.yaml index 7baec8e4..931a76fa 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -1,6 +1 @@ -packages: - github.com/nephio-project/nephio/controllers/pkg/giteaclient: - interfaces: - GiteaClient: - config: - dir: "{{.InterfaceDir}}" \ No newline at end of file +with-expecter: true diff --git a/Makefile b/Makefile index fa16babc..d07232fb 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/controllers/pkg/Makefile b/controllers/pkg/Makefile index 22b8c143..cb9045ab 100644 --- a/controllers/pkg/Makefile +++ b/controllers/pkg/Makefile @@ -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 diff --git a/default-go-test.mk b/default-go-test.mk index e4157197..3c49577a 100644 --- a/default-go-test.mk +++ b/default-go-test.mk @@ -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 @@ -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) diff --git a/default-go.mk b/default-go.mk index 7e601789..f28cf289 100644 --- a/default-go.mk +++ b/default-go.mk @@ -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 diff --git a/default-mockery.mk b/default-mockery.mk index aae27cd6..5a3795d6 100644 --- a/default-mockery.mk +++ b/default-mockery.mk @@ -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 diff --git a/testing/mockeryutils/Makefile b/testing/mockeryutils/Makefile index a3afad3b..92610f41 100644 --- a/testing/mockeryutils/Makefile +++ b/testing/mockeryutils/Makefile @@ -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 \ No newline at end of file +include ../../default-help.mk