Skip to content

Commit 00adb4e

Browse files
authored
Implement code coverage in GitHub actions (#6441)
Implement code coverage in GitHub actions
2 parents b2cb87f + d291905 commit 00adb4e

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

.github/workflows/main.yml

+39-6
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+
python scripts/append_codecov_token.py
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 }}

scripts/append_codecov_token.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Appends the codecov token to the 'codecov.yml' file at the root of the repository.
3+
4+
This is done by CI during PRs and builds on the pytest-dev repository so we can upload coverage, at least
5+
until codecov grows some native integration like it has with Travis and AppVeyor.
6+
7+
See discussion in https://github.com/pytest-dev/pytest/pull/6441 for more information.
8+
"""
9+
import os.path
10+
from textwrap import dedent
11+
12+
13+
def main():
14+
this_dir = os.path.dirname(__file__)
15+
cov_file = os.path.join(this_dir, "..", "codecov.yml")
16+
17+
assert os.path.isfile(cov_file), "{cov_file} does not exist".format(
18+
cov_file=cov_file
19+
)
20+
21+
with open(cov_file, "a") as f:
22+
# token from: https://codecov.io/gh/pytest-dev/pytest/settings
23+
# use same URL to regenerate it if needed
24+
text = dedent(
25+
"""
26+
codecov:
27+
token: "1eca3b1f-31a2-4fb8-a8c3-138b441b50a7"
28+
"""
29+
)
30+
f.write(text)
31+
32+
print("Token updated:", cov_file)
33+
34+
35+
if __name__ == "__main__":
36+
main()

tox.ini

+1-1
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)