forgejo/contrib/coverage-helper.sh
Earl Warren 7d3fcde71c chore: collect coverage using GOCOVERDIR (#9004)
- run all unit tests and integration tests in sequence in the same job
- upload the coverage data as an artifact to the run
- only run via dispatch because it is very long
- arguments are used to select which unit or integration tests to focus on

Refs https://go.dev/blog/integration-test-coverage

### Testing

- Run this workflow via workflow_dispatch (see https://codeberg.org/forgejo-integration/forgejo/actions/runs/11889)
- Download the coverage information (see https://codeberg.org/forgejo-integration/forgejo/actions/runs/11889/artifacts/coverage)
- Download the coverage information from an end-to-end cascade test (see https://code.forgejo.org/forgejo/end-to-end/actions/runs/3961)
- Unzip them in a the `coverage/data` sub-directory at the root of the Forgejo source tree
    ```sh
    $ ls coverage/data
    actions  coverage-actions.zip  coverage-federation.zip  coverage-tests.zip  coverage-upgrade.zip  federation  tests  upgrade
    ```
- Display the coverage percentage
```
$ make coverage-show-percentage
...
forgejo.org/services/wiki/wiki_path.go:96:					WebPathToGitPath						81.8%
forgejo.org/services/wiki/wiki_path.go:113:					GitPathToWebPath						90.0%
forgejo.org/services/wiki/wiki_path.go:129:					WebPathToUserTitle						71.4%
forgejo.org/services/wiki/wiki_path.go:140:					WebPathToURLPath						100.0%
forgejo.org/services/wiki/wiki_path.go:144:					WebPathFromRequest						100.0%
forgejo.org/services/wiki/wiki_path.go:151:					UserTitleToWebPath						80.0%
forgejo.org/services/wiki/wiki_path.go:163:					ToWikiPageMetaData						100.0%
forgejo.org/tests/integration/api_repo_file_helpers.go:18:			createFileInBranch						100.0%
...
total:										(statements)							63.9%
```

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9004
Reviewed-by: jerger <jerger@noreply.codeberg.org>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
2025-08-26 10:10:46 +02:00

50 lines
1.8 KiB
Bash
Executable file

#!/bin/bash
set -e
#set -x
PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: '
#
# Those must be explicitly required and are excluded from the full list of packages because they
# would interfere with the testing fixtures.
#
excluded+='forgejo.org/models/migrations|' # must be run before database specific tests
excluded+='forgejo.org/models/forgejo_migrations|' # must be run before database specific tests
excluded+='forgejo.org/tests/integration/migration-test|' # must be run before database specific tests
excluded+='forgejo.org/tests|' # only tests, no coverage to get there
excluded+='forgejo.org/tests/e2e|' # JavaScript is not in scope here and if it adds coverage it should not be counted
excluded+='FAKETERMINATOR' # do not modify
: ${COVERAGEDIR:=$(pwd)/coverage/data}
: ${GO:=$(go env GOROOT)/bin/go}
DEFAULT_TEST_PACKAGES=$($GO list ./... | grep -E -v "$excluded")
COVERED_PACKAGES=$($GO list ./...)
COVERED_PACKAGES=$(echo $COVERED_PACKAGES | sed -e 's/ /,/g')
function run_test() {
local package="$1"
if echo "$package" | grep --quiet --fixed-string ".."; then
echo "$package contains a suspicious .."
return 1
fi
local coverage="$COVERAGEDIR/$COVERAGE_TEST_DATABASE/$package"
rm -fr $coverage
mkdir -p $coverage
#
# -race cannot be used because it requires -covermode atomic which is
# different from the end-to-end tests and would cause issues wen merging
#
$GO test -timeout=20m -tags='sqlite sqlite_unlock_notify' -cover $package -coverpkg $COVERED_PACKAGES $COVERAGE_TEST_ARGS -args -test.gocoverdir=$coverage |& grep -v 'warning: no packages being tested depend on matches for pattern'
}
function test_packages() {
for package in ${@:-$DEFAULT_TEST_PACKAGES}; do
run_test $package
done
}
"$@"