Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 11, 2025

Generated mock files (**/mockgen.go) were being included in coverage reports, artificially inflating coverage metrics with 542 lines of machine-generated code across 3 files.

Changes

Shared test script (scripts/run-tests.sh):

  • Made configurable via environment variables (COVERAGE_FILE, GENERATE_HTML)
  • Filter mockgen.go entries after test execution
  • Fallback to preserve file if no matches found
  • Conditional HTML generation based on environment

CI/CD pipeline (.github/workflows/publish-coverage.yaml):

  • Uses the shared scripts/run-tests.sh script with environment variables
  • Maintains single source of truth for coverage generation logic

Implementation

The script now accepts environment variables for configuration:

# CI usage
COVERAGE_FILE=coverage.txt GENERATE_HTML=false ./scripts/run-tests.sh cover

# Local usage (defaults)
./scripts/run-tests.sh cover  # Generates coverage.out + coverage.html

Filter pattern applied to coverage files:

grep -v "mockgen.go" $COVERAGE_FILE > ${COVERAGE_FILE}.filtered || cp $COVERAGE_FILE ${COVERAGE_FILE}.filtered
mv ${COVERAGE_FILE}.filtered $COVERAGE_FILE

The coverage file format remains unchanged and compatible with go tool cover. All 3 mockgen.go files (package/service/mocks/, package/filestorage/, package/repository/mocks/) are now excluded from tracking.

Benefits

  • Single source of truth: Coverage logic maintained in one place
  • Easier maintenance: Changes only need to be made in the script
  • Consistent behavior: Local and CI environments use identical logic
  • Better testability: CI behavior can be tested locally with environment variables
Original prompt

This section details on the original issue you should resolve

<issue_title>Exclude mocks in */mockgen.go from test coverage</issue_title>
<issue_description>After generation coverage and before publishing it, all mockgen.go files should be stripped from tracked coverage</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits December 11, 2025 02:09
Co-authored-by: HermanPlay <78978614+HermanPlay@users.noreply.github.com>
Co-authored-by: HermanPlay <78978614+HermanPlay@users.noreply.github.com>
Co-authored-by: HermanPlay <78978614+HermanPlay@users.noreply.github.com>
Copilot AI changed the title [WIP] Exclude mockgen.go files from test coverage Exclude mockgen.go files from test coverage tracking Dec 11, 2025
Copilot AI requested a review from HermanPlay December 11, 2025 02:18
@HermanPlay HermanPlay marked this pull request as ready for review December 11, 2025 16:27
@HermanPlay HermanPlay requested a review from a team as a code owner December 11, 2025 16:27
Copilot AI review requested due to automatic review settings December 11, 2025 16:27
@HermanPlay
Copy link
Member

@copilot can we make workflow use the same script for local generation to make it easier to maintain

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds filtering logic to exclude MockGen-generated files from test coverage tracking, both in local development (scripts/run-tests.sh) and CI/CD pipeline (.github/workflows/publish-coverage.yaml). The changes aim to prevent machine-generated mock code from artificially inflating coverage metrics.

Key Changes:

  • Added post-test filtering step in local coverage script to exclude mockgen.go files
  • Added similar filtering step in GitHub Actions coverage workflow before Codecov upload
  • Both implementations use grep -v with fallback to preserve original file if filtering fails

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
scripts/run-tests.sh Adds filtering after test execution to exclude mockgen.go from coverage.out before HTML report generation
.github/workflows/publish-coverage.yaml Adds filtering step before Codecov upload to exclude mockgen.go from coverage.txt

Co-authored-by: HermanPlay <78978614+HermanPlay@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 11, 2025

@copilot can we make workflow use the same script for local generation to make it easier to maintain

Done! The workflow now uses the same scripts/run-tests.sh script with environment variables to control behavior:

  • COVERAGE_FILE=coverage.txt sets the output file
  • GENERATE_HTML=false skips HTML generation in CI

This maintains a single source of truth for the coverage logic. Commit: 6c7c46f

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • invalid-host
    • Triggering command: /tmp/go-build772853445/b844/queue.test /tmp/go-build772853445/b844/queue.test -test.paniconexit0 -test.gocoverdir=/tmp/go-build772853445/b844/gocoverdir -test.timeout=10m0s -test.v=true -test.coverprofile=/tmp/go-build772853445/b844/_cover_.out -c=4 -nolocalimports -importcfg /tmp/go-build772853445/b821/importcfg -pack /tmp/go-build772853445/b821/covervars.go -uns�� j-FCDufCz /tmp/go-build772-ifaceassert x_amd64/vet om/mattn/go-sqli/opt/hostedtoolcache/go/1.24.2/x64/pkg/tool/linux_amd64/cover g/genproto/googl-pkgcfg (dns block)
    • Triggering command: /tmp/go-build164121792/b844/queue.test /tmp/go-build164121792/b844/queue.test -test.paniconexit0 -test.gocoverdir=/tmp/go-build164121792/b844/gocoverdir -test.timeout=10m0s -test.v=true -test.coverprofile=/tmp/go-build164121792/b844/_cover_.out -trimpath x_amd64/compile om/mattn/go-sqlias te3@v1.14.28 -lang=go1.23 x_amd64/compile -uns�� olang.org/grpc@v-I o_.o x_amd64/vet om/mattn/go-sqli/opt/hostedtoolcache/go/1.24.2/x64/pkg/tool/linux_amd64/covdata sterresolver -importcfg x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build1812217515/b844/queue.test /tmp/go-build1812217515/b844/queue.test -test.paniconexit0 -test.gocoverdir=/tmp/go-build1812217515/b844/gocoverdir -test.timeout=10m0s -test.v=true -test.coverprofile=/tmp/go-build1812217515/b844/_cover_.out cgo_.o x_amd64/vet uster.pb.go uster.pb.validat-atomic -nilfunc x_amd64/vet -ato�� om/mattn/go-sqli-errorsas -buildtags x_amd64/vet -errorsas om/mattn/go-sqli-a -nilfunc x_amd64/vet (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@HermanPlay HermanPlay merged commit 10d4efc into master Dec 11, 2025
10 checks passed
@HermanPlay HermanPlay deleted the copilot/exclude-mocks-from-coverage branch December 11, 2025 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exclude mocks in */mockgen.go from test coverage

2 participants