From bc444f4eb265ff8a6c94bbbea218299d57079f5b Mon Sep 17 00:00:00 2001 From: Jiun Lee Date: Fri, 19 Jul 2024 22:41:33 +0800 Subject: [PATCH] ci: support submodule unit test (#2) * ci: support submodule unit test * Update pr-check.yml --- .github/workflows/pr-check.yml | 54 +++++++++++++--------------------- .github/workflows/tests.yml | 26 +++++++++------- .golangci.yaml | 11 ++++--- Makefile | 13 ++++++++ hack/resolve-modules.sh | 22 ++++++++++++++ hack/tools.sh | 51 ++++++++++++++++++++++++++++++++ hack/util.sh | 14 +++++++++ testdata/go.mod | 3 ++ testdata/main.go | 22 ++++++++++++++ 9 files changed, 166 insertions(+), 50 deletions(-) create mode 100644 Makefile create mode 100644 hack/resolve-modules.sh create mode 100644 hack/tools.sh create mode 100644 hack/util.sh create mode 100644 testdata/go.mod create mode 100644 testdata/main.go diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 03828a8..d1175ce 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -13,48 +13,36 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Check Spell + - name: typos-action uses: crate-ci/typos@master - staticcheck: - runs-on: [ self-hosted, X64 ] + resolve-modules: + name: resolve module + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version: "1.20" + - name: Checkout Repo + uses: actions/checkout@v3 - - uses: actions/cache@v3 - with: - path: ~/go/pkg/mod - key: reviewdog-${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - reviewdog-${{ runner.os }}-go- + - name: Set execute permission for resolve-modules.sh + run: chmod +x ./hack/resolve-modules.sh - - uses: reviewdog/action-staticcheck@v1 - with: - github_token: ${{ secrets.github_token }} - # Change reviewdog reporter if you need [github-pr-check,github-check,github-pr-review]. - reporter: github-pr-review - # Report all results. - filter_mode: nofilter - # Exit with 1 when it find at least one finding. - fail_on_error: true - # Set staticcheck flags - staticcheck_flags: -checks=inherit,-SA1029 + - 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.20" - - - 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 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3c53e3f..ff1b4b1 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -3,22 +3,26 @@ name: Tests on: [ push, pull_request ] jobs: - unit-benchmark-test: - strategy: - matrix: - go: [ "1.18", "1.19", "1.20", "1.21" ] - os: [ X64 ] - runs-on: ${{ matrix.os }} + ut: + runs-on: ubuntu-latest + steps: - uses: actions/checkout@v3 - name: Set up Go uses: actions/setup-go@v3 with: - go-version: ${{ matrix.go }} + go-version: 1.18 - - name: Unit Test - run: go test -race -covermode=atomic -coverprofile=coverage.out ./... + - uses: actions/cache@v3 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- - - name: Benchmark - run: go test -bench=. -benchmem -run=none ./... + - name: Set execute permission for tools.sh + run: chmod +x ./hack/tools.sh + + - name: Unit Test + run: make test diff --git a/.golangci.yaml b/.golangci.yaml index 71405b0..e634450 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -2,14 +2,10 @@ run: # include `vendor` `third_party` `testdata` `examples` `Godeps` `builtin` skip-dirs-use-default: true - skip-dirs: - - kitex_gen - skip-files: - - ".*\\.mock\\.go$" # output configuration options output: # Format: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions - format: colored-line-number + formats: colored-line-number # All available settings of specific linters. # Refer to https://golangci-lint.run/usage/linters linters-settings: @@ -30,8 +26,11 @@ linters: disable: - errcheck - typecheck - - deadcode - varcheck - staticcheck issues: exclude-use-default: true + exclude-files: + - ".*\\.mock\\.go$" + exclude-dirs: + - kitex_gen 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 100644 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 100644 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..91b10a2 --- /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 {} +} diff --git a/testdata/go.mod b/testdata/go.mod new file mode 100644 index 0000000..1f77041 --- /dev/null +++ b/testdata/go.mod @@ -0,0 +1,3 @@ +go 1.18 + +module github.com/hertz-contrib/swagger-generate/testdata diff --git a/testdata/main.go b/testdata/main.go new file mode 100644 index 0000000..4b3d389 --- /dev/null +++ b/testdata/main.go @@ -0,0 +1,22 @@ +// Copyright 2024 CloudWeGo Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// REMOVE THIS testdata DIR +package main + +import "fmt" + +func main() { + fmt.Println("Hello, World!") +}