From 02425efbbe9574948195e6d89d8605b96995f616 Mon Sep 17 00:00:00 2001 From: kinggo Date: Sun, 3 Mar 2024 21:36:31 +0800 Subject: [PATCH] feat: add submoudle unittest --- .github/workflows/pr-check.yml | 29 ++++++++--- .github/workflows/tests.yml | 4 +- Makefile | 13 +++++ hack/resolve-modules.sh | 22 ++++++++ hack/tools.sh | 51 +++++++++++++++++++ hack/util.sh | 14 +++++ ...ServiceName.circuit_break@@DEFAULT_GROUP@@ | 0 ...entName.ServiceName.retry@@DEFAULT_GROUP@@ | 0 ...e.ServiceName.rpc_timeout@@DEFAULT_GROUP@@ | 0 .../config/ServiceName.limit@@DEFAULT_GROUP@@ | 0 v2/example/go.mod | 4 +- v2/example/go.sum | 2 + 12 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 Makefile create mode 100755 hack/resolve-modules.sh create mode 100755 hack/tools.sh create mode 100644 hack/util.sh delete mode 100644 v2/example/cache/config/ClientName.ServiceName.circuit_break@@DEFAULT_GROUP@@ delete mode 100644 v2/example/cache/config/ClientName.ServiceName.retry@@DEFAULT_GROUP@@ delete mode 100644 v2/example/cache/config/ClientName.ServiceName.rpc_timeout@@DEFAULT_GROUP@@ delete mode 100644 v2/example/cache/config/ServiceName.limit@@DEFAULT_GROUP@@ diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 8cb7338..5a1762e 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -44,17 +44,30 @@ jobs: # Set staticcheck flags staticcheck_flags: -checks=inherit,-SA1029 + resolve-modules: + name: resolve module + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout Repo + uses: actions/checkout@v3 + + - id: set-matrix + run: ./hack/resolve-modules.sh + lint: - runs-on: [ self-hosted, X64 ] + name: lint module + runs-on: ubuntu-latest + needs: resolve-modules + strategy: + matrix: ${{ fromJson(needs.resolve-modules.outputs.matrix) }} steps: - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version: 1.19 - - - name: Golangci Lint - # https://golangci-lint.run/ + - name: Lint uses: golangci/golangci-lint-action@v3 with: version: latest + working-directory: ${{ matrix.workdir }} + args: -E gofumpt --timeout 10m + skip-pkg-cache: true \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0c74d2d..5eac826 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -6,7 +6,7 @@ jobs: unit-benchmark-test: strategy: matrix: - go: [ 1.17, 1.18, 1.19 ] + go: [ 1.20 ] os: [ X64, ARM64 ] runs-on: ${{ matrix.os }} steps: @@ -26,7 +26,7 @@ jobs: # ${{ runner.os }}-go- - name: Unit Test - run: go test -race -covermode=atomic -coverprofile=coverage.out ./... + run: make test - name: Benchmark run: go test -bench=. -benchmem -run=none ./... diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..92603b5 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +TOOLS_SHELL="./hack/tools.sh" + +.PHONY: test +test: + @${TOOLS_SHELL} test + @echo "go test finished" + + + +.PHONY: vet +vet: + @${TOOLS_SHELL} vet + @echo "vet check finished" \ No newline at end of file diff --git a/hack/resolve-modules.sh b/hack/resolve-modules.sh new file mode 100755 index 0000000..acef142 --- /dev/null +++ b/hack/resolve-modules.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +# This is used by the linter action. +# Recursively finds all directories with a go.mod file and creates +# a GitHub Actions JSON output option. + +set -o errexit + +HOME=$( + cd "$(dirname "${BASH_SOURCE[0]}")" && + cd .. && + pwd +) + +source "${HOME}/hack/util.sh" +all_modules=$(util::find_modules) +PATHS="" +for mod in $all_modules; do + PATHS+=$(printf '{"workdir":"%s"},' ${mod}) +done + +echo "::set-output name=matrix::{\"include\":[${PATHS%?}]}" \ No newline at end of file diff --git a/hack/tools.sh b/hack/tools.sh new file mode 100755 index 0000000..d11c8b6 --- /dev/null +++ b/hack/tools.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +HOME=$( + cd "$(dirname "${BASH_SOURCE[0]}")" && + cd .. && + pwd +) + +source "${HOME}/hack/util.sh" + +all_modules=$(util::find_modules) + +# test all mod +function test() { + for mod in $all_modules; do + pushd "$mod" >/dev/null && + echo "go test $(sed -n 1p go.mod | cut -d ' ' -f2)" && + go test -race -covermode=atomic -coverprofile=coverage.out ./... + popd >/dev/null || exit + done +} + +# vet all mod +function vet() { + for mod in $all_modules; do + pushd "$mod" >/dev/null && + echo "go vet $(sed -n 1p go.mod | cut -d ' ' -f2)" && + go vet -stdmethods=false ./... + popd >/dev/null || exit + done +} + +function help() { + echo "use: test,vet" +} + +case $1 in +vet) + vet + ;; +test) + test + ;; +*) + help + ;; +esac diff --git a/hack/util.sh b/hack/util.sh new file mode 100644 index 0000000..f6d4d76 --- /dev/null +++ b/hack/util.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +# find all go mod path +# returns an array contains mod path +function util::find_modules() { + find . -not \( \ + \( \ + -path './output' \ + -o -path './.git' \ + -o -path '*/third_party/*' \ + -o -path '*/vendor/*' \ + \) -prune \ + \) -name 'go.mod' -print0 | xargs -0 -I {} dirname {} +} \ No newline at end of file diff --git a/v2/example/cache/config/ClientName.ServiceName.circuit_break@@DEFAULT_GROUP@@ b/v2/example/cache/config/ClientName.ServiceName.circuit_break@@DEFAULT_GROUP@@ deleted file mode 100644 index e69de29..0000000 diff --git a/v2/example/cache/config/ClientName.ServiceName.retry@@DEFAULT_GROUP@@ b/v2/example/cache/config/ClientName.ServiceName.retry@@DEFAULT_GROUP@@ deleted file mode 100644 index e69de29..0000000 diff --git a/v2/example/cache/config/ClientName.ServiceName.rpc_timeout@@DEFAULT_GROUP@@ b/v2/example/cache/config/ClientName.ServiceName.rpc_timeout@@DEFAULT_GROUP@@ deleted file mode 100644 index e69de29..0000000 diff --git a/v2/example/cache/config/ServiceName.limit@@DEFAULT_GROUP@@ b/v2/example/cache/config/ServiceName.limit@@DEFAULT_GROUP@@ deleted file mode 100644 index e69de29..0000000 diff --git a/v2/example/go.mod b/v2/example/go.mod index 746bec6..98a2eea 100644 --- a/v2/example/go.mod +++ b/v2/example/go.mod @@ -1,6 +1,8 @@ module example -go 1.19 +go 1.21 + +toolchain go1.21.1 require ( github.com/cloudwego/kitex v0.8.0 diff --git a/v2/example/go.sum b/v2/example/go.sum index 70e6635..00b308f 100644 --- a/v2/example/go.sum +++ b/v2/example/go.sum @@ -287,6 +287,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= @@ -396,6 +397,7 @@ go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=