From 7762f3230578a7ec103bdb9a3f20b2a1bfa1bd40 Mon Sep 17 00:00:00 2001 From: Tronje Krop Date: Thu, 5 Feb 2026 12:52:41 +0100 Subject: [PATCH 1/2] feat: support for github.com/golang/mock/mockgen (#180) Signed-off-by: Tronje Krop --- Makefile | 2 +- VERSION | 2 +- config/Makefile | 2 +- config/Makefile.base | 38 +++++++++++++++++++++++++------------- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 5a533ff..e905fff 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ TMPDIR ?= /tmp # Setup default go-make installation flags. INSTALL_FLAGS ?= -mod=readonly -buildvcs=auto # Setup go-make version to use desired build and config scripts. -GOMAKE_DEP ?= github.com/tkrop/go-make@v0.0.168 +GOMAKE_DEP ?= github.com/tkrop/go-make@v0.0.169 # Request targets from go-make show-targets target. TARGETS := $(shell command -v $(GOBIN)/go-make >/dev/null || \ $(GO) install $(INSTALL_FLAGS) $(GOMAKE_DEP) >&2 && \ diff --git a/VERSION b/VERSION index c81f9cd..2f86a4f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.168 +0.0.169 diff --git a/config/Makefile b/config/Makefile index 5a533ff..e905fff 100644 --- a/config/Makefile +++ b/config/Makefile @@ -18,7 +18,7 @@ TMPDIR ?= /tmp # Setup default go-make installation flags. INSTALL_FLAGS ?= -mod=readonly -buildvcs=auto # Setup go-make version to use desired build and config scripts. -GOMAKE_DEP ?= github.com/tkrop/go-make@v0.0.168 +GOMAKE_DEP ?= github.com/tkrop/go-make@v0.0.169 # Request targets from go-make show-targets target. TARGETS := $(shell command -v $(GOBIN)/go-make >/dev/null || \ $(GO) install $(INSTALL_FLAGS) $(GOMAKE_DEP) >&2 && \ diff --git a/config/Makefile.base b/config/Makefile.base index 08e588f..f04331c 100644 --- a/config/Makefile.base +++ b/config/Makefile.base @@ -177,7 +177,7 @@ DIR_CACHE := $(abspath $(TMPDIR))/go-make-$(USER)$(DIR_REAL) $(call cdebug,using DIR_CACHE [$(DIR_CACHE)]) # Setup go-make to use desired build and config scripts. -GOMAKE_DEP := github.com/tkrop/go-make@v0.0.168 +GOMAKE_DEP := github.com/tkrop/go-make@v0.0.169 GOMAKE_PATH := $(GOPATH)/pkg/mod/$(GOMAKE_DEP)/config GOMAKE_MAKEFILE := $(realpath $(firstword $(MAKEFILE_LIST))) GOMAKE_CONFIG := $(patsubst %/,%,$(dir $(GOMAKE_MAKEFILE))) @@ -394,7 +394,9 @@ GOLANGCI_VERSION ?= v2.5.0 REVIVE_VERSION ?= v1.12.0 # Setup default tool command versions to be installed. -TOOLS_GO += \ +TOOL_MOCK ?= $(or $(shell awk '($$1 ~ "/mock$$") && ($$3 != "//") \ + { print $$1 }' go.mod 2>/dev/null),go.uber.org/mock)/mockgen +TOOLS_GO += $(TOOL_MOCK) \ github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_VERSION) \ github.com/zalando/zally/cli/zally \ golang.org/x/vuln/cmd/govulncheck \ @@ -405,7 +407,6 @@ TOOLS_GO += \ honnef.co/go/tools/cmd/staticcheck \ github.com/zricethezav/gitleaks/v8 \ github.com/icholy/gomajor \ - go.uber.org/mock/mockgen \ github.com/tkrop/go-testing/cmd/mock \ github.com/itchyny/gojq/cmd/gojq \ github.com/mikefarah/yq/v4 \ @@ -2056,19 +2057,30 @@ $(UPDATE_KUBE_APIS):: update-kube/%: $(UPDATE_KUBE_TARGETS) # Helper to optimized generation of mocks without relying on go:generate. update-mocks = \ awk 'BEGIN { \ - sarg="-source"; targ = "-destination"; dlen = length("$(CURDIR)") + 2; \ + parg = "-package"; sarg = "-source"; targ = "-destination"; \ + dlen = length("$(CURDIR)") + 2; \ } $(awk-gensub) \ $(awk-beginfile) { path = gensub("/[^/]*$$", "", "1", FILENAME); } \ - function file(path, arg, param, len, link) { \ - if (arg != param) { arg = substr(arg, len) } else { i++; arg = $$i; } \ - cmd = "readlink -f \"" path "/" arg "\""; cmd | getline link; close(cmd); \ + function norm(path, cmd, link) { \ + cmd = "readlink -f \"" path "\""; cmd | getline link; close(cmd); \ + if (link == "") { \ + cmd = "readlink -m \"" path "\""; cmd | getline link; close(cmd); \ + } \ return substr(link, dlen) \ } \ + function file(path, arg, param, len) { \ + if (arg != param) { return norm(path "/" substr(arg, len)) } \ + else { i++; return norm(path "/" $$i); } \ + } \ + function value(arg, param, len) { \ + if (arg != param) { return substr(arg, len) } else { i++; return $$i; } \ + } \ ($$1 == "//go:generate" && $$2 ~ "^mock(gen)?") { \ - source = ""; target = ""; package = ""; \ - command = $$2; \ + package = ""; source = ""; target = ""; command = $$2; \ for (i = 3; i <= NF; i++) { arg = $$i; \ - if (arg ~ "^" sarg) { \ + if (arg ~ "^" parg) { \ + package = value(arg, parg, 10); arg = parg "=" package; \ + } else if (arg ~ "^" sarg) { \ source = file(path, arg, sarg, 9); arg = sarg "=" source; \ } else if (arg ~ "^" targ) { \ target = file(path, arg, targ, 14); arg = targ "=" target; \ @@ -2078,7 +2090,7 @@ update-mocks = \ if (!target) { printf($(call amsg,error,target missing \ [%s]\n), command) > "/dev/stderr"; } \ if (!source) { package = $$(NF-1); } \ - print target "|" source "|" package "|" command \ + print target "|" source "|" package "|" command; \ }' $(SOURCES) | sort -u # | sed ':a; N; s/\n/;/; ta' | tr ';' '\n' #@ updates mock source files using mockgen to latest version. @@ -2092,7 +2104,7 @@ update-mocks:: $(DIR_CACHE) install-mockgen install-mock update-kube if [ -n "$${SOURCE}" ]; then touch "$(FILE_MOCKS).src"; \ if [ ! -f "$${TARGET}" ] || [ "$${TARGET}" -ot "$${SOURCE}" ] || \ [ -z "$$(grep "$${TARGET} $${CMD}" "$(FILE_MOCKS).src")" ]; then \ - echo "$${CMD}" >&2; $${CMD} && touch "$${TARGET}" && sed --in-place \ + echo "$${CMD}" >&2; $${CMD} 2>/dev/null && touch "$${TARGET}" && sed --in-place \ --expression="s|^$${TARGET} .*||; /^$$/d;" "$(FILE_MOCKS).src" && \ echo "$${TARGET} $${CMD}" >>"$(FILE_MOCKS).src"; \ fi; \ @@ -2101,7 +2113,7 @@ update-mocks:: $(DIR_CACHE) install-mockgen install-mock update-kube '(pkg ~ "^"$$1) { print $$2; nextfile }')"; \ if [ ! -f "$${TARGET}" ] || \ [ -z "$$(grep "$${PACKAGE} $${CMD}" "$(FILE_MOCKS).pkg")" ]; then \ - echo "$${CMD}" >&2; $${CMD} && touch "$${TARGET}" && sed --in-place \ + echo "$${CMD}" >&2; $${CMD} 2>/dev/null && touch "$${TARGET}" && sed --in-place \ --expression="s|^$${PACKAGE%@*}@.* $${CMD}||; /^$$/d;" "$(FILE_MOCKS).pkg" && \ echo "$${PACKAGE} $${CMD}" >>"$(FILE_MOCKS).pkg"; \ fi; \ From 2a137583f89359a9cf1065e262428374cd02fe66 Mon Sep 17 00:00:00 2001 From: Tronje Krop Date: Fri, 6 Feb 2026 19:03:27 +0100 Subject: [PATCH 2/2] feat: improve mockgen selection (#181) Signed-off-by: Tronje Krop --- Makefile | 2 +- VERSION | 2 +- config/Makefile | 2 +- config/Makefile.base | 38 +++++++++++-------- .../make/fixtures/targets/go-make-std.out | 1 + .../make/fixtures/targets/go-make-trace.out | 1 + internal/make/fixtures/targets/std.out | 1 + internal/make/fixtures/targets/trace.out | 1 + 8 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index e905fff..32a6e43 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ TMPDIR ?= /tmp # Setup default go-make installation flags. INSTALL_FLAGS ?= -mod=readonly -buildvcs=auto # Setup go-make version to use desired build and config scripts. -GOMAKE_DEP ?= github.com/tkrop/go-make@v0.0.169 +GOMAKE_DEP ?= github.com/tkrop/go-make@v0.0.170 # Request targets from go-make show-targets target. TARGETS := $(shell command -v $(GOBIN)/go-make >/dev/null || \ $(GO) install $(INSTALL_FLAGS) $(GOMAKE_DEP) >&2 && \ diff --git a/VERSION b/VERSION index 2f86a4f..e649778 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.169 +0.0.170 diff --git a/config/Makefile b/config/Makefile index e905fff..32a6e43 100644 --- a/config/Makefile +++ b/config/Makefile @@ -18,7 +18,7 @@ TMPDIR ?= /tmp # Setup default go-make installation flags. INSTALL_FLAGS ?= -mod=readonly -buildvcs=auto # Setup go-make version to use desired build and config scripts. -GOMAKE_DEP ?= github.com/tkrop/go-make@v0.0.169 +GOMAKE_DEP ?= github.com/tkrop/go-make@v0.0.170 # Request targets from go-make show-targets target. TARGETS := $(shell command -v $(GOBIN)/go-make >/dev/null || \ $(GO) install $(INSTALL_FLAGS) $(GOMAKE_DEP) >&2 && \ diff --git a/config/Makefile.base b/config/Makefile.base index f04331c..7ef2c33 100644 --- a/config/Makefile.base +++ b/config/Makefile.base @@ -177,7 +177,7 @@ DIR_CACHE := $(abspath $(TMPDIR))/go-make-$(USER)$(DIR_REAL) $(call cdebug,using DIR_CACHE [$(DIR_CACHE)]) # Setup go-make to use desired build and config scripts. -GOMAKE_DEP := github.com/tkrop/go-make@v0.0.169 +GOMAKE_DEP := github.com/tkrop/go-make@v0.0.170 GOMAKE_PATH := $(GOPATH)/pkg/mod/$(GOMAKE_DEP)/config GOMAKE_MAKEFILE := $(realpath $(firstword $(MAKEFILE_LIST))) GOMAKE_CONFIG := $(patsubst %/,%,$(dir $(GOMAKE_MAKEFILE))) @@ -393,10 +393,23 @@ GOLANGCI_VERSION ?= v2.5.0 # https://github.com/codacy/codacy-gorevive/blob/master/.tool_version REVIVE_VERSION ?= v1.12.0 +# Setup default variables for kubernetes code generation. +KUBE_APIS ?= $(shell FILES=($$($(call find-go,.,-path "*/apis/*/*/*.go"))); \ + if [ -n "$${FILES}" ]; then grep -E "\+(kubebuilder|k8s)" "$${FILES[@]}" | \ + sed "s|apis/[^/]*/[^/]*/[^/]*.go:.*|apis|" | sort --unique; \ + fi) +KUBE_DIRS += $(strip $(KUBE_APIS:%/apis=%/clients) $(KUBE_APIS:%/apis=%/manifests)) +$(call cdebug,using KUBE_DIRS [$(KUBE_DIRS)]) +KUBE_SOURCES += $(strip $(shell $(call find-go,.,-name "mock_*_test.go" -o \ + -name "mock_*.go" -o -name "$(FILE_DEEPCOPY)")) \ + $(shell if [ -n "$(KUBE_DIRS)" ]; then $(call find-go,$(KUBE_DIRS),); fi)) +SOURCES := $(filter-out $(KUBE_SOURCES),$(shell $(call find-go,.,))) +MODULES := $(shell echo $(SOURCES) | xargs -r dirname | sort --unique) + # Setup default tool command versions to be installed. -TOOL_MOCK ?= $(or $(shell awk '($$1 ~ "/mock$$") && ($$3 != "//") \ - { print $$1 }' go.mod 2>/dev/null),go.uber.org/mock)/mockgen -TOOLS_GO += $(TOOL_MOCK) \ +TOOL_MOCKGEN ?= $(or $(shell awk '($$1 ~ "/mock$$") { print $$1 }' \ + go.mod 2>/dev/null | head --line=1),go.uber.org/mock)/mockgen +TOOLS_GO += $(TOOL_MOCKGEN) \ github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_VERSION) \ github.com/zalando/zally/cli/zally \ golang.org/x/vuln/cmd/govulncheck \ @@ -569,10 +582,6 @@ $(call cdebug,using CODACY_PROJECT_TOKEN [$(CODACY_PROJECT_TOKEN)]) $(call cdebug,using CODACY_API_BASE_URL [$(CODACY_API_BASE_URL)]) $(call cdebug,using CODACY_REPORTER_VERSION [$(CODACY_REPORTER_VERSION)]) -KUBE_APIS ?= $(shell FILES=($$($(call find-go,.,-path "*/apis/*/*/*.go"))); \ - if [ -n "$${FILES}" ]; then grep -E "\+(kubebuilder|k8s)" "$${FILES[@]}" | \ - sed "s|apis/[^/]*/[^/]*/[^/]*.go:.*|apis|" | sort --unique; \ - fi) UPDATE_KUBE_APIS ?= $(KUBE_APIS:%=update-kube/%) $(call cdebug,using UPDATE_KUBE_APIS [$(UPDATE_KUBE_APIS)]) UPDATE_KUBE_TARGETS := install-deepcopy-gen install-client-gen \ @@ -623,13 +632,6 @@ run-token-link = \ # Setup default environment variables. -KUBE_DIRS += $(strip $(KUBE_APIS:%/apis=%/clients) $(KUBE_APIS:%/apis=%/manifests)) -$(call cdebug,using KUBE_DIRS [$(KUBE_DIRS)]) -KUBE_SOURCES += $(strip $(shell $(call find-go,.,-name "mock_*_test.go" -o \ - -name "mock_*.go" -o -name "$(FILE_DEEPCOPY)")) \ - $(shell if [ -n "$(KUBE_DIRS)" ]; then $(call find-go,$(KUBE_DIRS),); fi)) -SOURCES := $(filter-out $(KUBE_SOURCES),$(shell $(call find-go,.,))) -MODULES := $(shell echo $(SOURCES) | xargs -r dirname | sort --unique) COMMANDS := $(shell $(call find-go,.,-name "main.go") | xargs -r readlink -f | \ awk -F '/' '{ for ( field = NF-1; field > 0; field--) { \ if ($$field != "cmd") { print $$field; break; } \ @@ -2345,6 +2347,12 @@ debug-kube:: echo 'KUBE_SOURCES $(KUBE_SOURCES)'; \ echo 'KUBE_TARGETS $(KUBE_TARGETS)'; +#@ debug the command environment variables. +debug-tools:: + @echo 'TOOLS_GO $(TOOLS_GO)'; \ + echo 'TOOLS_SH $(TOOLS_SH)'; \ + echo 'TOOLS_NPM $(TOOLS_NPM)'; + #@ debug the command environment variables. debug-commands:: @echo 'COMMANDS $(COMMANDS)'; \ diff --git a/internal/make/fixtures/targets/go-make-std.out b/internal/make/fixtures/targets/go-make-std.out index ff669f3..417f1e3 100644 --- a/internal/make/fixtures/targets/go-make-std.out +++ b/internal/make/fixtures/targets/go-make-std.out @@ -81,6 +81,7 @@ debug-targets-run debug-targets-test debug-targets-uninstall debug-targets-update +debug-tools format format-go git-clean diff --git a/internal/make/fixtures/targets/go-make-trace.out b/internal/make/fixtures/targets/go-make-trace.out index 282b3bd..0d7c806 100644 --- a/internal/make/fixtures/targets/go-make-trace.out +++ b/internal/make/fixtures/targets/go-make-trace.out @@ -92,6 +92,7 @@ debug-targets-run debug-targets-test debug-targets-uninstall debug-targets-update +debug-tools format format-go git-clean diff --git a/internal/make/fixtures/targets/std.out b/internal/make/fixtures/targets/std.out index ff669f3..417f1e3 100644 --- a/internal/make/fixtures/targets/std.out +++ b/internal/make/fixtures/targets/std.out @@ -81,6 +81,7 @@ debug-targets-run debug-targets-test debug-targets-uninstall debug-targets-update +debug-tools format format-go git-clean diff --git a/internal/make/fixtures/targets/trace.out b/internal/make/fixtures/targets/trace.out index 9e2f21a..d9b889b 100644 --- a/internal/make/fixtures/targets/trace.out +++ b/internal/make/fixtures/targets/trace.out @@ -92,6 +92,7 @@ debug-targets-run debug-targets-test debug-targets-uninstall debug-targets-update +debug-tools format format-go git-clean