Skip to content

test: improved test coverage #128

test: improved test coverage

test: improved test coverage #128

Workflow file for this run

name: go-test
on:
push:
branches:
- master
pull_request:
permissions:
pull-requests: read
contents: read
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
-
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
-
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
with:
go-version: stable
check-latest: true
cache: true
-
name: golangci-lint
uses: golangci/golangci-lint-action@0a35821d5c230e903fcfe077583637dea1b27b47 # v9.0.0
with:
version: latest
only-new-issues: true
skip-cache: true
test:
name: Unit tests
runs-on: ${{ matrix.os }}
needs: [lint]
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
go: ['oldstable', 'stable' ]
steps:
-
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
-
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
with:
go-version: '${{ matrix.go }}'
check-latest: true
cache: true
-
name: Install Tools
# TODO: pin version -> fork + update dedicated github action
run: |
go install gotest.tools/gotestsum@latest
-
name: Run unit tests
shell: bash
run: >
gotestsum
--jsonfile 'unit.report.${{ matrix.os }}-${{ matrix.go }}.json'
--
-race
-p 2
-count 1
-timeout=20m
-coverprofile='unit.coverage.${{ matrix.os }}-${{ matrix.go }}.out'
-covermode=atomic
-coverpkg=$(go list)/...
./...
-
name: Upload coverage artifacts
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
with:
# *.coverage.* pattern is automatically detected by codecov
path: '**/*.coverage.*.out'
name: 'unit.coverage.${{ matrix.os }}-${{ matrix.go }}'
retention-days: 1
-
name: Upload test report artifacts
# upload report even if test fail. BTW, this is when they are valuable.
if: ${{ !cancelled() }}
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
with:
path: '**/unit.report.*.json'
name: 'unit.report.${{ matrix.os }}-${{ matrix.go }}'
retention-days: 1
test-complete:
# description: |
# Be explicit about all tests being passed. This allows for setting up only a few status checks on PRs.
name: tests completed
needs: [test]
runs-on: ubuntu-latest
steps:
-
name: Tests completed
run: |
echo "::notice title=Success:All tests passed"
collect-coverage:
# description: |
# Gather, merge then uploads test coverage files from all test jobs (this includes integration tests,
# like codegen-test). This reduces the number of failures due to codecov hitting github API rate limit.
name: collect test coverage
needs: [test-complete]
if: ${{ !cancelled() && needs.test-complete.result == 'success' }}
runs-on: ubuntu-latest
steps:
-
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
-
name: Download coverage artifacts
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
with:
run-id: "${{ github.run_id }}"
pattern: "*.coverage.*"
# artifacts resolve as folders
path: coverage/
-
name: Upload coverage to codecov
uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1
with:
name: Aggregated coverage
# All *.coverage.*.out files uploaded should be detected by the codecov action.
# NOTE: we lose the flags on individual test reports (e.g. by os, by go version, unit vs integration tests)
fail_ci_if_error: false
verbose: false
collect-reports:
# description: |
# Gather, merge then uploads test report files from unit test jobs.
#
# At this moment test reports are published on both codecov
# (see <https://app.codecov.io/gh/go-swagger/go-swagger/tests>) and the github actions UI
# (see <https://github.com/go-swagger/go-swagger/actions>).
name: collect test reports
needs: [test]
if: ${{ !cancelled() }}
runs-on: ubuntu-latest
steps:
-
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
with:
go-version: stable
check-latest: true
cache: true
-
name: Download test report artifacts
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
with:
run-id: "${{ github.run_id }}"
pattern: "*.report.*"
# artifacts resolve as folders
path: reports/
-
name: Convert test reports to a merged JUnit XML
# NOTE: codecov test reports only support JUnit format at this moment. See https://docs.codecov.com/docs/test-analytics.
# Ideally, codecov improve a bit their platform, so we may only need a single pass to CTRF format.
#
# As a contemplated alternative, we could use gotestsum above to produce the JUnit XML directly.
# At this moment, we keep a json format to dispatch test reports to codecov as well as to CTRF reports.
#
# TODO(fredbi): sec compliance - pin go-junit-report
# TODO(fredbi): investigate - use mikepenz/action-junit-report@v5, that packages most of the following scripts
# in a single action. Alternative: for that action.
run: |
go install github.com/jstemmer/go-junit-report/v2@latest
go-junit-report -version
find reports/ -name \*.json | xargs cat | go-junit-report -parser gojson -out=reports/junit_report.xml
-
name: Upload test results to Codecov
# This allows for using the test results UI on codecov
uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1
with:
files: '**/junit_report.xml'
report_type: 'test_results'
fail_ci_if_error: false
handle_no_reports_found: true
verbose: true
-
name: Convert test reports to CTRF JSON
# description: |
# This step publishes CTRF test reports on github UI (actions)
# TODO: pin this dependency
run: |
go install github.com/ctrf-io/go-ctrf-json-reporter/cmd/[email protected]
appName="${{ github.repository }}"
buildNumber="${{ github.run_id }}"
appVersion="${{ github.event.pull_request.head.sha }}"
if [[ -z "${appVersion}" ]] ; then
# for push events
appVersion="${{ github.sha }}"
fi
# reconstruct platform information from the file name
# set -x
while read report ; do
# 'unit.report.${{ matrix.os }}-${{ matrix.go }}.json'
reformated=$(echo "${report##*/}"|sed -E 's/(go)([[:digit:]]+)\.([[:digit:]]+)/\1\2\3/') # e.g. go1.24 becomes go124
mapfile -d'.' -t -s 2 -n 2 split < <(echo $reformated) # skip the first 2 parts, stop on 2 more parts
envstring="${split[0]}"
osPlatform="${envstring%-*}"
osRelease="${envstring##*-}"
# this is a best effort only: tests may be cancelled upstream and produce incorrect reports
go-ctrf-json-reporter \
-quiet \
-appName "${appName}" \
-appVersion "${appVersion}" \
-buildNumber "${buildNumber}" \
-osPlatform "${osPlatform}" \
-osRelease "${osRelease}" \
-output "./reports/ctrf_report_${osPlatform}_${osRelease}.json" < "${report}" || true
done < <(find reports -name \*.json)
# NOTE: at this moment, we don't upload CTRF reports as artifacts.
# Some of the CTRF reports are therefore not available (flaky tests, history, ...).
#
# See https://github.com/ctrf-io/github-test-reporter?tab=readme-ov-file#report-showcase
# for more reporting possibilities. At the moment, we keep it simple, as most advanced features
# require a github token (thus adding the complexity of a separate workflow starting on pull_request_target).
#
# For the moment, we are contented with these simple reports. This is an opportunity to compare the insight they
# provide as compared to what is uploaded to codecov.
#
# Codecov analytics are pretty poor at this moment. On the other hand, they manage the bot that pushes back
# PR comments.
#
# They also handle the storage of past test reports, so as to assess flaky tests.
-
name: Publish Test Summary Results
uses: ctrf-io/github-test-reporter@024bc4b64d997ca9da86833c6b9548c55c620e40 # v1.0.26
with:
report-path: 'reports/ctrf_report_*.json'
use-suite-name: true
summary-report: true # post a report to the github actions summary
github-report: true
failed-folded-report: true