diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 0000000..60ab883 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,52 @@ +name: Coverage + +on: + push: + branches: + - main + tags: + - 'v*' + +permissions: + contents: read + +concurrency: + group: coverage-${{ github.ref }} + cancel-in-progress: true + +jobs: + coverage: + name: Coverage + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - name: Checkout + uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - name: Install mise + uses: jdx/mise-action@e6a8b3978addb5a52f2b4cd9d91eafa7f0ab959d # v4.2.0 + + - name: Cache Go modules and build + uses: actions/cache@v4 + with: + path: | + ~/go/pkg/mod + ~/.cache/go-build + key: go-${{ runner.os }}-${{ hashFiles('**/go.sum') }} + restore-keys: go-${{ runner.os }}- + + - name: Download Go dependencies + run: go mod download + + - name: Run tests with coverage + run: make cover + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v5 + with: + files: coverage.out + fail_ci_if_error: true + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.gitignore b/.gitignore index 3f90277..9b62ff9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,10 @@ go.work go.work.sum +# coverage output +coverage.out +coverage.html + # Local configuration files *.local.* *.local/ diff --git a/Makefile b/Makefile index d7d3ae0..779e053 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,15 @@ lint: test: go test -race -failfast ./... +.PHONY: cover +cover: + go test -race -covermode=atomic -coverprofile=coverage.out ./... + go tool cover -func=coverage.out + +.PHONY: cover-html +cover-html: cover + go tool cover -html=coverage.out -o coverage.html + .PHONY: check-tidy check-tidy: go mod tidy -diff diff --git a/README.md b/README.md index d742551..36affe6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Generic Sync Map [![Go Reference](https://pkg.go.dev/badge/github.com/min0625/gsyncmap.svg)](https://pkg.go.dev/github.com/min0625/gsyncmap) +[![codecov](https://codecov.io/gh/min0625/gsyncmap/branch/main/graph/badge.svg)](https://codecov.io/gh/min0625/gsyncmap) **English** | [繁體中文](./README.zh-TW.md) diff --git a/README.zh-TW.md b/README.zh-TW.md index bdd2e86..d65c8d7 100644 --- a/README.zh-TW.md +++ b/README.zh-TW.md @@ -1,6 +1,7 @@ # Generic Sync Map [![Go Reference](https://pkg.go.dev/badge/github.com/min0625/gsyncmap.svg)](https://pkg.go.dev/github.com/min0625/gsyncmap) +[![codecov](https://codecov.io/gh/min0625/gsyncmap/branch/main/graph/badge.svg)](https://codecov.io/gh/min0625/gsyncmap) [English](./README.md) | **繁體中文**