I am forking a Swift project to GitLab and I’m trying to take advantage of GitLab’s test coverage visualization in merge requests and in the diff view by creating a pipeline job that tests the project and creates a report for it.
This is the job I have defined, that’s running on a custom docker image based on swift-latest
:
export-test-coverage-job:
stage: test
needs: [build-job]
script:
- swift test -c debug -Xswiftc -enable-testing --explicit-target-dependency-import-check error --parallel --enable-code-coverage
- shopt -s nullglob
- dot_os=(.build/x86_64-unknown-linux-gnu/debug/*.build/*.o .build/x86_64-unknown-linux-gnu/debug/*.build/**/*.o)
- bin_params=("${dot_os[0]}")
- for o in "${dot_os[@]:1}"; do bin_params+=("-object" "${o}"); done
- llvm-cov-15 export -format="lcov" -instr-profile "$(swift test -c debug --show-codecov-path | xargs dirname)"/default.profdata "${bin_params[@]}" > ./.build/info.lcov
- lcov_cobertura ./.build/info.lcov --output ./.build/coverage.xml
- pycobertura show --format csv ./.build/coverage.xml --delimiter ";"
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: ./.build/coverage.xml
I have two problems:
First of all, this script, which is based on the project I am forking, I extended with lcov-cobertura
to generate the cobertura report which GitLab requires. The thing is that it generates a huge report which includes (what I believe are) parts of the standard Swift library and other dependencies of the project I am forking such as .build/checkouts/swift-numerics/Sources/RealModule/Float80+Real.swift;95;95;0.00%;18-166
. This leads to this barebones project that I currently have to have only 0.1% coverage when it only includes two sample methods that are tested. How can I generate this report to only cover .swift
files in the Sources/
folder. It even lists the coverage of the Tests
folder. The documentation of lcov_cobertura
mention the existence of the --excludes
flag which takes in a list of regexes and excludes those from the report, however it does not seem to be working. I thought about changing the list of .o
files, but that seems not be an optimal solution as it would require constant changing of this job every time a new file is added in Sources/
or a very long and involved process of finding out every file that is compiled as an external library.
Secondly, I checked the output of this specific job in GitLab and it seems identical to the output on my machine when I run these commands myself (in a devcontainer using the same image), but the coverage doesn’t show up in the GitLab merge requests UI. What am I doing wrong, it seems to me that I followed the instructions in the documentation exactly.
Thanks!