Skip to content

Commit 01cb704

Browse files
authored
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.
1 parent a32530a commit 01cb704

File tree

7 files changed

+27
-56
lines changed

7 files changed

+27
-56
lines changed

.mockery.yaml

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
packages:
2-
github.com/nephio-project/nephio/controllers/pkg/giteaclient:
3-
interfaces:
4-
GiteaClient:
5-
config:
6-
dir: "{{.InterfaceDir}}"
1+
with-expecter: true

Makefile

+1-18
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,4 @@ unit-clean: ## These targets are delegated to the Makefiles of individual Go mod
5151
docker-build docker-push: ## These targets are delegated to the Makefiles next to Dockerfiles
5252
for dir in $(DOCKERFILE_DIRS); do \
5353
$(MAKE) -C "$$dir" $@ ; \
54-
done
55-
56-
57-
##@ Mockery code
58-
59-
.PHONY: install-mockery
60-
install-mockery: ## install mockery
61-
ifeq ($(CONTAINER_RUNNABLE), 0)
62-
$(CONTAINER_RUNTIME) pull docker.io/vektra/mockery:v${MOCKERY_VERSION}
63-
else
64-
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
65-
endif
66-
67-
.PHONY: generate-mocks
68-
generate-mocks:
69-
for dir in $(GO_MOD_DIRS); do \
70-
$(MAKE) -C "$$dir" $@ || true ; \
71-
done
54+
done

controllers/pkg/Makefile

-13
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,3 @@ include ../../default-go.mk
2323

2424
# This includes the 'help' target that prints out all targets with their descriptions organized by categories
2525
include ../../default-help.mk
26-
27-
# This includes the below targets for mockery
28-
# install-mockery
29-
# generate-mocks
30-
include ../../default-mockery.mk
31-
.PHONY: generate-mocks
32-
generate-mocks:
33-
ifeq ($(CONTAINER_RUNNABLE), 0)
34-
echo ${PWD}
35-
$(CONTAINER_RUNTIME) run --security-opt label=disable -v ${PWD}:/src -w /src/controllers/pkg docker.io/vektra/mockery:v${MOCKERY_VERSION}
36-
else
37-
mockery
38-
endif

default-go-test.mk

-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515

1616
GO_VERSION ?= 1.20.2
17-
MOCKERY_VERSION=2.37.1
1817
TEST_COVERAGE_FILE=lcov.info
1918
TEST_COVERAGE_HTML_FILE=coverage_unit.html
2019
TEST_COVERAGE_FUNC_FILE=func_coverage.out
@@ -39,22 +38,6 @@ else
3938
go tool cover -func=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_FUNC_FILE}
4039
endif
4140

42-
.PHONY: install-mockery
43-
install-mockery: ## install mockery
44-
ifeq ($(CONTAINER_RUNNABLE), 0)
45-
$(CONTAINER_RUNTIME) pull docker.io/vektra/mockery:v${MOCKERY_VERSION}
46-
else
47-
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
48-
endif
49-
50-
.PHONY: generate-mocks
51-
generate-mocks:
52-
ifeq ($(CONTAINER_RUNNABLE), 0)
53-
$(CONTAINER_RUNTIME) run --security-opt label=disable -v ${PWD}:/src -w /src docker.io/vektra/mockery:v${MOCKERY_VERSION}
54-
else
55-
mockery
56-
endif
57-
5841
.PHONY: unit-clean
5942
unit-clean: ## Clean up the artifacts created by the unit tests
6043
ifeq ($(CONTAINER_RUNNABLE), 0)

default-go.mk

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ include $(GIT_ROOT_DIR)/default-go-misc.mk
2020
include $(GIT_ROOT_DIR)/default-go-test.mk
2121
include $(GIT_ROOT_DIR)/default-go-lint.mk
2222
include $(GIT_ROOT_DIR)/default-gosec.mk
23+
include $(GIT_ROOT_DIR)/default-mockery.mk

default-mockery.mk

+23-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,30 @@
1313
# limitations under the License.
1414

1515

16-
MOCKERY_VERSION=2.37.1
16+
MOCKERY_VERSION=2.41.0
1717
GIT_ROOT_DIR ?= $(dir $(lastword $(MAKEFILE_LIST)))
1818
OS_ARCH ?= $(shell uname -m)
1919
OS ?= $(shell uname)
2020
include $(GIT_ROOT_DIR)/detect-container-runtime.mk
21+
22+
.PHONY: install-mockery
23+
install-mockery: ## install mockery
24+
ifeq ($(CONTAINER_RUNNABLE), 0)
25+
$(CONTAINER_RUNTIME) pull docker.io/vektra/mockery:v${MOCKERY_VERSION}
26+
else
27+
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
28+
endif
29+
30+
.PHONY: generate-mocks
31+
generate-mocks:
32+
ifeq ($(CONTAINER_RUNNABLE), 0)
33+
find . -name .mockery.yaml \
34+
-exec echo generating mocks specified in {} . . . \; \
35+
-execdir $(CONTAINER_RUNTIME) run --security-opt label=disable -v .:/src -w /src docker.io/vektra/mockery:v${MOCKERY_VERSION} \; \
36+
-exec echo generated mocks specified in {} \;
37+
else
38+
find . -name .mockery.yaml \
39+
-exec echo generating mocks specified in {} . . . \; \
40+
-execdir mockery \; \
41+
-exec echo generated mocks specified in {} \;
42+
endif

testing/mockeryutils/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ all: fmt lint gosec
88
include ../../default-go.mk
99

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

0 commit comments

Comments
 (0)