From 89f3653b1e1ef64b46c4bdd902f3618f3809a4aa Mon Sep 17 00:00:00 2001 From: Blaine Gardner Date: Fri, 6 Sep 2024 15:27:44 -0600 Subject: [PATCH] add kubebuilder suggested e2e and lint targets Add the test-e2e, lint, and lint-fix targets that are suggested by kubebuilder, with adjustments to work for COSI monorepo. Signed-off-by: Blaine Gardner --- .gitignore | 2 +- .golangci.yaml | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 .golangci.yaml diff --git a/.gitignore b/.gitignore index 590a65c1..98aeed90 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ release-tools .idea travis.yml - +.cache diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 00000000..6fe2afba --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,47 @@ +run: + timeout: 5m + allow-parallel-runners: true + +issues: + # don't skip warning about doc comments + # don't exclude the default set of lint + exclude-use-default: false + # restore some of the defaults + # (fill in the rest as needed) + exclude-rules: + - path: "apis/*" + linters: + - lll + - path: "internal/*" + linters: + - dupl + - lll +linters: + disable-all: true + enable: + - copyloopvar + - dupl + - errcheck + - ginkgolinter + - goconst + - gocyclo + - gofmt + - goimports + - gosimple + - govet + - ineffassign + - lll + - misspell + - nakedret + - prealloc + - revive + - staticcheck + - typecheck + - unconvert + - unparam + - unused + +linters-settings: + revive: + rules: + - name: comment-spacings diff --git a/Makefile b/Makefile index 8e78ef42..9fd0cffe 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,16 @@ vet: vet.client vet.controller vet.sidecar ## Vet code .PHONY: test test: .test.proto test.client test.controller test.sidecar ## Run tests including unit tests +.PHONY: test-e2e +test-e2e: # Run e2e tests + @echo "unimplemented placeholder" + +.PHONY: lint +lint: golangci-lint.client golangci-lint.controller golangci-lint.sidecar ## Run all linters (suggest `make -k`) + +.PHONY: lint-fix +lint-fix: golangci-lint-fix.client golangci-lint-fix.controller golangci-lint-fix.sidecar ## Run all linters and perform fixes where possible (suggest `make -k`) + ##@ Build @@ -81,7 +91,38 @@ clean: ## Clean build environment and cached tools clobber: $(MAKE) -C proto clobber + rm -rf $(CURDIR)/.cache +## +## === TOOLS === # + +GOLANGCI_LINT_VERSION ?= v1.61.0 + +TOOLBIN ?= $(CURDIR)/.cache/tools +$(TOOLBIN): + mkdir -p $(TOOLBIN) + +GOLANGCI_LINT ?= $(TOOLBIN)/golangci-lint +# .PHONY: golangci-lint +# golangci-lint: $(GOLANGCI_LINT) +$(GOLANGCI_LINT): $(TOOLBIN) + $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist +# $1 - target path with name of binary +# $2 - package url which can be installed +# $3 - specific version of package +define go-install-tool +@[ -f "$(1)-$(3)" ] || { \ +set -e; \ +package=$(2)@$(3) ;\ +echo "Downloading $${package}" ;\ +rm -f $(1) || true ;\ +GOBIN=$(TOOLBIN) go install $${package} ;\ +mv $(1) $(1)-$(3) ;\ +} ;\ +ln -sf $(1)-$(3) $(1) +endef ## ## === INTERMEDIATES === # @@ -101,6 +142,13 @@ vet.%: FORCE test.%: fmt.% vet.% FORCE cd $* && go test ./... +# golangci-lint --new flag only complains about new code +golangci-lint.%: $(GOLANGCI_LINT) + cd $* && $(GOLANGCI_LINT) run --config $(CURDIR)/.golangci.yaml --new + +golangci-lint-fix.%: $(GOLANGCI_LINT) + cd $* && $(GOLANGCI_LINT) run --config $(CURDIR)/.golangci.yaml --new --fix + .PHONY: .test.proto .test.proto: # gRPC proto has a special unit test $(MAKE) -C proto check