Skip to content

Commit afc884d

Browse files
authored
PYTHON-5467 Add codecov integration (#2690)
1 parent e077ebd commit afc884d

8 files changed

Lines changed: 119 additions & 3 deletions

File tree

.evergreen/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ post:
3838
# Disabled, causing timeouts
3939
# - func: "upload working dir"
4040
- func: "teardown system"
41+
- func: "upload codecov"
4142
- func: "upload coverage"
4243
- func: "upload mo artifacts"
4344
- func: "upload test results"

.evergreen/generated_configs/functions.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,24 @@ functions:
252252
- TOOLCHAIN_VERSION
253253
type: test
254254

255+
# Upload coverage codecov
256+
upload codecov:
257+
- command: subprocess.exec
258+
params:
259+
binary: bash
260+
args:
261+
- .evergreen/scripts/upload-codecov.sh
262+
working_dir: src
263+
include_expansions_in_env:
264+
- CODECOV_TOKEN
265+
- build_variant
266+
- task_name
267+
- github_commit
268+
- github_pr_number
269+
- github_pr_head_branch
270+
- github_author
271+
type: test
272+
255273
# Upload coverage
256274
upload coverage:
257275
- command: ec2.assume_role

.evergreen/generated_configs/variants.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ buildvariants:
367367
display_name: No C Ext RHEL8
368368
run_on:
369369
- rhel87-small
370+
expansions:
371+
COVERAGE: "1"
372+
NO_EXT: "1"
370373

371374
# No server tests
372375
- name: no-server-rhel8

.evergreen/scripts/generate_config.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,10 @@ def create_green_framework_variants():
318318
def create_no_c_ext_variants():
319319
host = DEFAULT_HOST
320320
tasks = [".test-standard"]
321-
expansions = dict()
321+
expansions = dict(COVERAGE="1")
322322
handle_c_ext(C_EXTS[0], expansions)
323323
display_name = get_variant_name("No C Ext", host)
324-
return [create_variant(tasks, display_name, host=host)]
324+
return [create_variant(tasks, display_name, host=host, expansions=expansions)]
325325

326326

327327
def create_mod_wsgi_variants():
@@ -1077,6 +1077,24 @@ def create_upload_coverage_func():
10771077
return "upload coverage", [get_assume_role(), cmd]
10781078

10791079

1080+
def create_upload_coverage_codecov_func():
1081+
# Upload the coverage xml report to codecov.
1082+
include_expansions = [
1083+
"CODECOV_TOKEN",
1084+
"build_variant",
1085+
"task_name",
1086+
"github_commit",
1087+
"github_pr_number",
1088+
"github_pr_head_branch",
1089+
"github_author",
1090+
]
1091+
args = [
1092+
".evergreen/scripts/upload-codecov.sh",
1093+
]
1094+
upload_cmd = get_subprocess_exec(include_expansions_in_env=include_expansions, args=args)
1095+
return "upload codecov", [upload_cmd]
1096+
1097+
10801098
def create_download_and_merge_coverage_func():
10811099
include_expansions = ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN"]
10821100
args = [
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
# shellcheck disable=SC2154
3+
# Upload a coverate report to codecov.
4+
set -eu
5+
6+
HERE=$(dirname ${BASH_SOURCE:-$0})
7+
ROOT=$(dirname "$(dirname $HERE)")
8+
9+
pushd $ROOT > /dev/null
10+
export FNAME=coverage.xml
11+
12+
if [ -z "${github_pr_number:-}" ]; then
13+
echo "This is not a PR, not running codecov"
14+
exit 0
15+
fi
16+
17+
if [ ! -f ".coverage" ]; then
18+
echo "There are no XML test results, not running codecov"
19+
exit 0
20+
fi
21+
22+
echo "Uploading..."
23+
printf 'pr: %s\n' "$github_pr_number"
24+
printf 'sha: %s\n' "$github_commit"
25+
printf 'branch: %s:%s\n' "$github_author" "$github_pr_head_branch"
26+
printf 'flag: %s-%s\n' "$build_variant" "$task_name"
27+
printf 'file: %s\n' "$FNAME"
28+
uv tool run --with "coverage[toml]" coverage xml
29+
uv tool run --from codecov-cli codecovcli upload-process \
30+
--report-type coverage \
31+
--disable-search \
32+
--fail-on-error \
33+
--git-service github \
34+
--token ${CODECOV_TOKEN} \
35+
--pr ${github_pr_number} \
36+
--sha ${github_commit} \
37+
--branch "${github_author}:${github_pr_head_branch}" \
38+
--flag "${build_variant}-${task_name}" \
39+
--file $FNAME
40+
echo "Uploading...done."
41+
42+
popd > /dev/null

.github/workflows/test-python.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,35 @@ jobs:
7979
- name: Run tests
8080
run: uv run --extra test pytest -v
8181

82+
coverage:
83+
# This enables a coverage report for a given PR, which will be augmented by
84+
# the combined codecov report uploaded in Evergreen.
85+
runs-on: ubuntu-latest
86+
87+
name: Coverage
88+
steps:
89+
- uses: actions/checkout@v6
90+
with:
91+
persist-credentials: false
92+
- name: Install uv
93+
uses: astral-sh/setup-uv@61cb8a9741eeb8a550a1b8544337180c0fc8476b # v7
94+
with:
95+
enable-cache: true
96+
python-version: "3.10"
97+
- id: setup-mongodb
98+
uses: mongodb-labs/drivers-evergreen-tools@master
99+
with:
100+
version: "8.0"
101+
- name: Install just
102+
run: uv tool install rust-just
103+
- name: Setup tests
104+
run: COVERAGE=1 just setup-tests
105+
- name: Run tests
106+
run: just run-tests
107+
- name: Generate xml report
108+
run: uv tool run --with "coverage[toml]" coverage xml
109+
- name: Upload test results to Codecov
110+
uses: codecov/codecov-action@671740ac38dd9b0130fbe1cec585b89eea48d3de # v5
82111
doctest:
83112
runs-on: ubuntu-latest
84113
name: DocTest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ test/lambda/*.json
4141

4242
# test results and logs
4343
xunit-results/
44+
coverage.xml
4445
server.log

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,11 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?)|dummy.*)$"
239239

240240
[tool.coverage.run]
241241
branch = true
242-
source = ["pymongo", "bson", "gridfs" ]
242+
include = [
243+
"pymongo/*",
244+
"bson/*",
245+
"gridfs/*"
246+
]
243247
relative_files = true
244248

245249
[tool.coverage.report]

0 commit comments

Comments
 (0)