diff --git a/.envrc b/.envrc index 481c4e38..b0d5a510 100644 --- a/.envrc +++ b/.envrc @@ -19,6 +19,7 @@ fi AKASH_DIRENV_SET=1 +export TOOLS export SEMVER export GOTOOLCHAIN export GOTOOLCHAIN_SEMVER diff --git a/Makefile b/Makefile index 580a1573..5897981a 100644 --- a/Makefile +++ b/Makefile @@ -23,20 +23,20 @@ ifeq (, $(GOTOOLCHAIN)) $(error "GOTOOLCHAIN is not set") endif -GO_PKG := go -TS_PKG := ts +GO_ROOT := go +TS_ROOT := $(AKASH_TS_ROOT) GO := GO111MODULE=$(GO111MODULE) go -GO_MOD_NAME := $(shell cd $(GO_PKG); go list -m 2>/dev/null) +GO_MOD_NAME := $(shell cd $(GO_ROOT); go list -m 2>/dev/null) BUF_VERSION ?= 1.28.1 PROTOC_VERSION ?= 26.1 -GOGOPROTO_VERSION ?= $(shell cd $(GO_PKG); $(GO) list -mod=readonly -m -f '{{ .Version }}' github.com/cosmos/gogoproto) +GOGOPROTO_VERSION ?= $(shell cd $(GO_ROOT); $(GO) list -mod=readonly -m -f '{{ .Version }}' github.com/cosmos/gogoproto) PROTOC_GEN_GOCOSMOS_VERSION ?= $(GOGOPROTO_VERSION) -PROTOC_GEN_GO_PULSAR_VERSION ?= $(shell cd $(GO_PKG); $(GO) list -mod=readonly -m -f '{{ .Version }}' github.com/cosmos/cosmos-proto) -PROTOC_GEN_GO_VERSION ?= $(shell cd $(GO_PKG); $(GO) list -mod=readonly -m -f '{{ .Version }}' google.golang.org/protobuf) -PROTOC_GEN_GRPC_GATEWAY_VERSION := $(shell cd $(GO_PKG); $(GO) list -mod=readonly -m -f '{{ .Version }}' github.com/grpc-ecosystem/grpc-gateway) -PROTOC_GEN_DOC_VERSION := $(shell cd $(GO_PKG); $(GO) list -mod=readonly -m -f '{{ .Version }}' github.com/pseudomuto/protoc-gen-doc) +PROTOC_GEN_GO_PULSAR_VERSION ?= $(shell cd $(GO_ROOT); $(GO) list -mod=readonly -m -f '{{ .Version }}' github.com/cosmos/cosmos-proto) +PROTOC_GEN_GO_VERSION ?= $(shell cd $(GO_ROOT); $(GO) list -mod=readonly -m -f '{{ .Version }}' google.golang.org/protobuf) +PROTOC_GEN_GRPC_GATEWAY_VERSION := $(shell cd $(GO_ROOT); $(GO) list -mod=readonly -m -f '{{ .Version }}' github.com/grpc-ecosystem/grpc-gateway) +PROTOC_GEN_DOC_VERSION := $(shell cd $(GO_ROOT); $(GO) list -mod=readonly -m -f '{{ .Version }}' github.com/pseudomuto/protoc-gen-doc) PROTOC_GEN_SWAGGER_VERSION := $(PROTOC_GEN_GRPC_GATEWAY_VERSION) MODVENDOR_VERSION ?= v0.5.0 diff --git a/make/codegen.mk b/make/codegen.mk index 4b1aade8..1048b93e 100644 --- a/make/codegen.mk +++ b/make/codegen.mk @@ -7,7 +7,7 @@ proto-gen: $(patsubst %, proto-gen-%,$(PROTO_GEN_MODS)) .PHONY: proto-gen-go proto-gen-go: $(BUF) $(GOGOPROTO) $(PROTOC_GEN_GOCOSMOS) $(PROTOC_GEN_GRPC_GATEWAY) $(PROTOC_GEN_GO) - ./script/protocgen.sh go $(GO_MOD_NAME) $(GO_PKG) + ./script/protocgen.sh go $(GO_MOD_NAME) $(GO_ROOT) .PHONY: proto-gen-ts proto-gen-ts: $(BUF) $(AKASH_TS_NODE_MODULES) @@ -18,7 +18,7 @@ proto-gen-doc: $(BUF) $(SWAGGER_COMBINE) $(PROTOC_GEN_DOC) $(PROTOC_GEN_SWAGGER) ./script/protocgen.sh doc $(GO_MOD_NAME) mocks: $(MOCKERY) - (cd $(GO_PKG); $(GO) generate ./...) + (cd $(GO_ROOT); $(GO) generate ./...) .PHONY: codegen codegen: proto-gen mocks diff --git a/make/lint.mk b/make/lint.mk index 6bcf8fa8..89940ea0 100644 --- a/make/lint.mk +++ b/make/lint.mk @@ -8,11 +8,11 @@ node .PHONY: lint-go lint-go: $(GOLANGCI_LINT) - (cd $(GO_PKG); $(GOLANGCI_LINT_RUN) ./... --issues-exit-code=0) + (cd $(GO_ROOT); $(GOLANGCI_LINT_RUN) ./... --issues-exit-code=0) .PHONY: lint-go-% lint-go-%: $(GOLANGCI_LINT) - (cd $(GO_PKG); $(GOLINT) $*) + (cd $(GO_ROOT); $(GOLINT) $*) .PHONY: lint-proto-% lint-proto-%: diff --git a/make/mod.mk b/make/mod.mk index 9c47c3b8..2c1bc3f4 100644 --- a/make/mod.mk +++ b/make/mod.mk @@ -18,11 +18,11 @@ endef .PHONY: deps-tidy deps-tidy: - (cd $(GO_PKG); $(GO) mod tidy) + (cd $(GO_ROOT); $(GO) mod tidy) .PHONY: deps-vendor deps-vendor: - (cd $(GO_PKG); go mod vendor) + (cd $(GO_ROOT); go mod vendor) .PHONY: modsensure modsensure: deps-tidy deps-vendor @@ -31,10 +31,10 @@ modsensure: deps-tidy deps-vendor modvendor: export VENDOR_BUF:=$(VENDOR_BUF) modvendor: $(MODVENDOR) modsensure @echo "vendoring non-go files..." - @(cd $(GO_PKG); \ + @(cd $(GO_ROOT); \ $(MODVENDOR) -copy="**/*.proto" -include=k8s.io/apimachinery \ $(MODVENDOR) -copy="**/swagger.yaml" -include=github.com/cosmos/cosmos-sdk/client/docs/swagger-ui \ ) @mkdir -p .cache/include/k8s - ln -snf ../../../$(GO_PKG)/vendor/k8s.io .cache/include/k8s/io - echo "$${VENDOR_BUF}" > $(GO_PKG)/vendor/k8s.io/buf.yaml + ln -snf ../../../$(GO_ROOT)/vendor/k8s.io .cache/include/k8s/io + echo "$${VENDOR_BUF}" > $(GO_ROOT)/vendor/k8s.io/buf.yaml diff --git a/make/setup-cache.mk b/make/setup-cache.mk index d518b553..8f299216 100644 --- a/make/setup-cache.mk +++ b/make/setup-cache.mk @@ -26,7 +26,7 @@ cache: $(AKASH_DEVCACHE) $(BUF_VERSION_FILE): $(AKASH_DEVCACHE) @echo "installing buf v$(BUF_VERSION) ..." rm -f $(BUF) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/bufbuild/buf/cmd/buf@v$(BUF_VERSION)) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/bufbuild/buf/cmd/buf@v$(BUF_VERSION)) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ @@ -47,7 +47,7 @@ $(PROTOC): $(PROTOC_VERSION_FILE) $(PROTOC_GEN_GOCOSMOS_VERSION_FILE): $(AKASH_DEVCACHE) @echo "installing protoc-gen-gocosmos $(PROTOC_GEN_GOCOSMOS_VERSION) ..." rm -f $(PROTOC_GEN_GOCOSMOS) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/cosmos/gogoproto/protoc-gen-gocosmos) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/cosmos/gogoproto/protoc-gen-gocosmos) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ @@ -56,7 +56,7 @@ $(PROTOC_GEN_GOCOSMOS): $(PROTOC_GEN_GOCOSMOS_VERSION_FILE) #modvendor $(GOGOPROTO_VERSION_FILE): $(AKASH_DEVCACHE) @echo "installing gogoproto $(GOGOPROTO_VERSION) ..." rm -f $(BUF) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/cosmos/gogoproto/gogoproto) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/cosmos/gogoproto/gogoproto) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ @@ -65,7 +65,7 @@ $(GOGOPROTO): $(GOGOPROTO_VERSION_FILE) $(PROTOC_GEN_GO_PULSAR_VERSION_FILE): $(AKASH_DEVCACHE) @echo "installing protoc-gen-go-pulsar $(PROTOC_GEN_GO_PULSAR_VERSION) ..." rm -f $(PROTOC_GEN_GO_PULSAR) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/cosmos/cosmos-proto/cmd/protoc-gen-go-pulsar@$(PROTOC_GEN_GO_PULSAR_VERSION)) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/cosmos/cosmos-proto/cmd/protoc-gen-go-pulsar@$(PROTOC_GEN_GO_PULSAR_VERSION)) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ @@ -74,7 +74,7 @@ $(PROTOC_GEN_GO_PULSAR): $(PROTOC_GEN_GO_PULSAR_VERSION_FILE) $(PROTOC_GEN_GO_VERSION_FILE): $(AKASH_DEVCACHE) @echo "installing protoc-gen-go $(PROTOC_GEN_GO_VERSION) ..." rm -f $(PROTOC_GEN_GO) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install google.golang.org/protobuf/cmd/protoc-gen-go@$(PROTOC_GEN_GO_VERSION)) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install google.golang.org/protobuf/cmd/protoc-gen-go@$(PROTOC_GEN_GO_VERSION)) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ @@ -83,7 +83,7 @@ $(PROTOC_GEN_GO): $(PROTOC_GEN_GO_VERSION_FILE) $(PROTOC_GEN_DOC_VERSION_FILE): $(AKASH_DEVCACHE) @echo "installing protoc-gen-doc $(PROTOC_GEN_DOC_VERSION) ..." rm -f $(PROTOC_GEN_DOC) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@$(PROTOC_GEN_DOC_VERSION)) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@$(PROTOC_GEN_DOC_VERSION)) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ @@ -92,7 +92,7 @@ $(PROTOC_GEN_DOC): $(PROTOC_GEN_DOC_VERSION_FILE) $(PROTOC_GEN_GRPC_GATEWAY_VERSION_FILE): $(AKASH_DEVCACHE) @echo "Installing protoc-gen-grpc-gateway $(PROTOC_GEN_GRPC_GATEWAY_VERSION) ..." rm -f $(PROTOC_GEN_GRPC_GATEWAY) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@$(PROTOC_GEN_GRPC_GATEWAY_VERSION)) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@$(PROTOC_GEN_GRPC_GATEWAY_VERSION)) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ @@ -101,7 +101,7 @@ $(PROTOC_GEN_GRPC_GATEWAY): $(PROTOC_GEN_GRPC_GATEWAY_VERSION_FILE) $(PROTOC_GEN_SWAGGER_VERSION_FILE): $(AKASH_DEVCACHE) @echo "Installing protoc-gen-grpc-gateway $(PROTOC_GEN_SWAGGER_VERSION) ..." rm -f $(PROTOC_GEN_GRPC_GATEWAY) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@$(PROTOC_GEN_SWAGGER_VERSION)) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@$(PROTOC_GEN_SWAGGER_VERSION)) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ @@ -110,7 +110,7 @@ $(PROTOC_GEN_SWAGGER): $(PROTOC_GEN_SWAGGER_VERSION_FILE) $(MODVENDOR_VERSION_FILE): $(AKASH_DEVCACHE) @echo "installing modvendor $(MODVENDOR_VERSION) ..." rm -f $(MODVENDOR) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/goware/modvendor@$(MODVENDOR_VERSION)) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install github.com/goware/modvendor@$(MODVENDOR_VERSION)) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ @@ -119,7 +119,7 @@ $(MODVENDOR): $(MODVENDOR_VERSION_FILE) $(GIT_CHGLOG_VERSION_FILE): $(AKASH_DEVCACHE) @echo "installing git-chglog $(GIT_CHGLOG_VERSION) ..." rm -f $(GIT_CHGLOG) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) go install github.com/git-chglog/git-chglog/cmd/git-chglog@$(GIT_CHGLOG_VERSION)) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) go install github.com/git-chglog/git-chglog/cmd/git-chglog@$(GIT_CHGLOG_VERSION)) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ @@ -128,7 +128,7 @@ $(GIT_CHGLOG): $(GIT_CHGLOG_VERSION_FILE) $(MOCKERY_VERSION_FILE): $(AKASH_DEVCACHE) @echo "installing mockery $(MOCKERY_VERSION) ..." rm -f $(MOCKERY) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) go install -ldflags '-s -w -X github.com/vektra/mockery/v2/pkg/config.SemVer=$(MOCKERY_VERSION)' github.com/vektra/mockery/v2@v$(MOCKERY_VERSION)) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) go install -ldflags '-s -w -X github.com/vektra/mockery/v2/pkg/config.SemVer=$(MOCKERY_VERSION)' github.com/vektra/mockery/v2@v$(MOCKERY_VERSION)) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ @@ -137,7 +137,7 @@ $(MOCKERY): $(MOCKERY_VERSION_FILE) $(GOLANGCI_LINT_VERSION_FILE): $(AP_DEVCACHE) @echo "installing golangci-lint $(GOLANGCI_LINT_VERSION) ..." rm -f $(MOCKERY) - (cd $(GO_PKG); GOBIN=$(AKASH_DEVCACHE_BIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)) + (cd $(GO_ROOT); GOBIN=$(AKASH_DEVCACHE_BIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)) rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ diff --git a/make/test.mk b/make/test.mk index e0dc1d11..a3af1089 100644 --- a/make/test.mk +++ b/make/test.mk @@ -1,24 +1,26 @@ -TEST_GO_MODULES ?= ./... +GO_MODULES ?= $(shell find * -name go.mod -exec dirname {} \;) +GO_TEST_DIRS ?= ./... +GO_COVER_PACKAGES = $(shell cd $(GO_ROOT); go list $(GO_TEST_DIRS) | grep -v mock | paste -sd, -) -SUB_TESTS ?= go \ -ts +GO_CURR_MODULE = $(shell go list -m 2>/dev/null) -COVER_GO_PACKAGES = $(shell cd $(GO_PKG); go list ./... | grep -v mock | paste -sd, -) +SUB_TESTS ?= go \ +ts -TEST_GO_OPTS ?= -TEST_GO_TIMEOUT ?= 300 +GO_TEST_OPTS ?= +GO_TEST_TIMEOUT ?= 300 -test_go_flags := -timeout $(TEST_GO_TIMEOUT)s +test_go_flags := -timeout $(GO_TEST_TIMEOUT)s -ifneq (,$(findstring nocache,$(TEST_GO_OPTS))) +ifneq (,$(findstring nocache,$(GO_TEST_OPTS))) test_go_flags += -count=1 endif -ifneq (,$(findstring race,$(TEST_GO_OPTS))) +ifneq (,$(findstring race,$(GO_TEST_OPTS))) test_go_flags += -race endif -ifneq (,$(findstring verbose,$(TEST_GO_OPTS))) +ifneq (,$(findstring verbose,$(GO_TEST_OPTS))) test_go_flags += -v endif @@ -30,23 +32,18 @@ test-coverage: $(patsubst %, test-coverage-%,$(SUB_TESTS)) .PHONY: test-ts test-ts: $(AKASH_TS_NODE_MODULES) - cd ts && npm run test + cd $(TS_ROOT) && npm run test .PHONY: test-coverage-ts test-coverage-ts: $(AKASH_TS_NODE_MODULES) - cd ts && npm run test:cov + cd $(TS_ROOT) && npm run test:cov .PHONY: test-go +test-go: export GO111MODULE := $(GO111MODULE) test-go: - cd $(GO_PKG) && $(GO) test $(test_go_flags) $(TEST_GO_MODULES) + @$(TOOLS) gotest "$(GO_MODULES)" "$(test_go_flags)" "$(GO_TEST_DIRS)" .PHONY: test-coverage-go +test-coverage-go: export GO111MODULE := $(GO111MODULE) test-coverage-go: - cd $(GO_PKG) && $(GO) test -coverprofile=coverage.txt \ - -covermode=count \ - -coverpkg="$(COVER_GO_PACKAGES)" \ - $(TEST_GO_MODULES) - -.PHONY: test-go-vet -test-go-vet: - cd $(GO_PKG) && $(GO) vet $(TEST_GO_MODULES) + @$(TOOLS) gocoverage "$(GO_MODULES)" "$(test_go_flags)" "$(GO_TEST_DIRS)" diff --git a/script/protocgen.sh b/script/protocgen.sh index b63607b1..191a2c44 100755 --- a/script/protocgen.sh +++ b/script/protocgen.sh @@ -34,6 +34,7 @@ function gen_go() { gen buf.gen.go.yaml set -x + # shellcheck disable=SC2086 cp -r ./$1/* ./$2/ } diff --git a/script/shellcheck.sh b/script/shellcheck.sh index 8b7fa613..f050c156 100644 --- a/script/shellcheck.sh +++ b/script/shellcheck.sh @@ -2,7 +2,7 @@ unset FAILED -FILES=$(find /shellcheck/ -type f -name "*.sh" ! -path "/shellcheck/vendor/*" ! -path "/shellcheck/.git/*" ! -path "/shellcheck/ts/.husky/*" ! -path "/shellcheck/ts/node_modules/*") +FILES=$(find /shellcheck/ -type f -name "*.sh" ! -path "/shellcheck/go/vendor/*" ! -path "/shellcheck/.git/*" ! -path "/shellcheck/ts/.husky/*" ! -path "/shellcheck/ts/node_modules/*") for file in $FILES; do name="$(basename "$file")"; diff --git a/script/tools.sh b/script/tools.sh index e4343068..8fc0e6e4 100755 --- a/script/tools.sh +++ b/script/tools.sh @@ -5,9 +5,9 @@ set -eo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" SEMVER=$SCRIPT_DIR/semver.sh -GO_PKG=$(realpath "$SCRIPT_DIR/../go") +GO_ROOT=$(realpath "$SCRIPT_DIR/../go") -gomod="$GO_PKG/go.mod" +gomod="$GO_ROOT/go.mod" function get_gotoolchain() { local gotoolchain @@ -69,7 +69,7 @@ function replace_import_path() { curr_module_name=$(cd go; go list -m) curr_version=$(echo "$curr_module_name" | sed -n 's/.*v\([0-9]*\).*/\1/p') - new_module_name=${curr_module_name%/v$curr_version}/$next_major_version + new_module_name=${curr_module_name%/"v$curr_version"}/$next_major_version echo "current import paths are $curr_module_name, replacing with $new_module_name" @@ -121,6 +121,69 @@ function replace_import_path() { replace_paths "./go/go.mod" "$curr_module_name" "$new_module_name" } +function run_gotest() { + declare -a modules + + modules=( "$1" ) + + if [ -z "$AKASH_ROOT" ]; then + echo "AKASH_ROOT environment variable is not set" + exit 1 + fi + + if [ -z "$GO111MODULE" ]; then + echo "GO111MODULE environment variable is not set" + exit 1 + fi + + # shellcheck disable=SC2068 + for module in ${modules[@]}; do + pushd "$(pwd)" + cd "$module" + # shellcheck disable=SC2086 + go test ${2} ${3} + popd + done +} + +function run_gocoverage() { + declare -a modules + + + modules=( "$1" ) + + if [ -z "$AKASH_ROOT" ]; then + echo "AKASH_ROOT environment variable is not set" + exit 1 + fi + + if [ -z "$GO111MODULE" ]; then + echo "GO111MODULE environment variable is not set" + exit 1 + fi + + # shellcheck disable=SC2068 + for module in ${modules[@]}; do + pushd "$(pwd)" + cd "$module" + local coverpkgs + + # shellcheck disable=SC2086 + coverpkgs=$(go list ${3} | grep -v mock | paste -sd, -) + + local coverprofile + coverprofile="$AKASH_ROOT/coverage-$(echo "$module" | tr '/' '-').txt" + + # shellcheck disable=SC2086 + go test ${2} -coverprofile="$coverprofile" \ + -covermode=count \ + -coverpkg="$coverpkgs" \ + ${3} + + popd + done +} + case "$1" in gotoolchain) get_gotoolchain @@ -129,4 +192,12 @@ replace-import-path) shift replace_import_path "$@" ;; +gotest) + shift + run_gotest "$@" + ;; +gocoverage) + shift + run_gocoverage "$@" + ;; esac