Skip to content

Commit a3bc6df

Browse files
committed
Implement code coverage in GitHub actions
This overwrites the `codecov.yml` file in the root of the repository with `codecov-upstream.yml` file (which contains the code-cov token)´, so PRs and branches on the repository can upload coverage. Suggestion from here: pytest-dev#6421 (comment) Security concerns: the token might be misused, but only to upload bogus coverage to `codecov.io`, so the team believe this is harmless. If we decide to fallback from this decision , we just need to revoke the token. Related to pytest-dev#6369
1 parent 622995a commit a3bc6df

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

.github/codecov-upstream.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# this file replaces <root>/codecov.yml when building on the main repository and PRs
2+
coverage:
3+
status:
4+
project: true
5+
patch: true
6+
changes: true
7+
8+
comment: off
9+
10+
codecov:
11+
# token from: https://codecov.io/gh/pytest-dev/pytest/settings
12+
# use same URL to regenerate it if needed
13+
token: "1eca3b1f-31a2-4fb8-a8c3-138b441b50a7"

.github/workflows/main.yml

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
# evaluating GitHub actions for CI, disconsider failures when evaluating PRs
1+
# evaluating GitHub actions for CI, disregard failures when evaluating PRs
22
#
33
# this is still missing:
44
# - deploy
5-
# - coverage
65
# - upload github notes
76
#
87
name: main
@@ -17,7 +16,6 @@ on:
1716

1817
jobs:
1918
build:
20-
2119
runs-on: ${{ matrix.os }}
2220

2321
strategy:
@@ -86,6 +84,8 @@ jobs:
8684
python: "3.7"
8785
os: ubuntu-latest
8886
tox_env: "py37-freeze"
87+
# coverage does not apply for freeze test, skip it
88+
skip_coverage: true
8989
- name: "ubuntu-py38"
9090
python: "3.8"
9191
os: ubuntu-latest
@@ -94,6 +94,8 @@ jobs:
9494
python: "pypy3"
9595
os: ubuntu-latest
9696
tox_env: "pypy3-xdist"
97+
# coverage too slow with pypy3, skip it
98+
skip_coverage: true
9799

98100
- name: "macos-py37"
99101
python: "3.7"
@@ -118,6 +120,37 @@ jobs:
118120
- name: Install dependencies
119121
run: |
120122
python -m pip install --upgrade pip
121-
pip install tox
122-
- name: Test
123-
run: tox -e ${{ matrix.tox_env }}
123+
pip install tox coverage
124+
125+
- name: Test without coverage
126+
if: "matrix.skip_coverage"
127+
run: "tox -e ${{ matrix.tox_env }}"
128+
129+
- name: Test with coverage
130+
if: "! matrix.skip_coverage"
131+
env:
132+
_PYTEST_TOX_COVERAGE_RUN: "coverage run -m"
133+
COVERAGE_PROCESS_START: ".coveragerc"
134+
_PYTEST_TOX_EXTRA_DEP: "coverage-enable-subprocess"
135+
run: "tox -e ${{ matrix.tox_env }}"
136+
137+
- name: Prepare coverage token
138+
if: success() && !matrix.skip_coverage && ( github.repository == 'pytest-dev/pytest' || github.event_name == 'pull_request' )
139+
run: |
140+
cp .github/codecov-upstream.yml codecov.yml
141+
142+
- name: Combine coverage
143+
if: success() && !matrix.skip_coverage
144+
run: |
145+
python -m coverage combine
146+
python -m coverage xml
147+
148+
- name: Codecov upload
149+
if: success() && !matrix.skip_coverage
150+
uses: codecov/codecov-action@v1
151+
with:
152+
token: ${{ secrets.codecov }}
153+
file: ./coverage.xml
154+
flags: ${{ runner.os }}
155+
fail_ci_if_error: false
156+
name: ${{ matrix.name }}

codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# note: `.github/codecov-upstream.yml` is basically a copy of this file, please propagate
2+
# changes as needed
13
coverage:
24
status:
35
project: true

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ passenv = USER USERNAME COVERAGE_* TRAVIS PYTEST_ADDOPTS TERM
2626
setenv =
2727
_PYTEST_TOX_DEFAULT_POSARGS={env:_PYTEST_TOX_POSARGS_LSOF:} {env:_PYTEST_TOX_POSARGS_XDIST:}
2828

29-
# Configuration to run with coverage similar to Travis/Appveyor, e.g.
29+
# Configuration to run with coverage similar to CI, e.g.
3030
# "tox -e py37-coverage".
3131
coverage: _PYTEST_TOX_COVERAGE_RUN=coverage run -m
3232
coverage: _PYTEST_TOX_EXTRA_DEP=coverage-enable-subprocess

0 commit comments

Comments
 (0)