Skip to content

Commit

Permalink
add coverage report generating in pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
zhPavel committed Mar 4, 2024
1 parent b45f1d5 commit 8f76c64
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
44 changes: 41 additions & 3 deletions .github/workflows/lint_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ concurrency:

jobs:
linting:
name: Run linters
name: Linting
runs-on: ubuntu-latest
timeout-minutes: 3

Expand All @@ -44,7 +44,7 @@ jobs:
just lint

testing:
name: Run tests
name: Testing
runs-on: ${{ matrix.os }}
needs: linting

Expand Down Expand Up @@ -82,4 +82,42 @@ jobs:

- name: Run tests
run:
just test-on ${{ matrix.python_version.tox }}
just test-with-coverage ${{ matrix.python_version.tox }}

- name: Store coverage file
uses: actions/upload-artifact@v4
with:
name: coverage-${{ matrix.python_version.setup }}
path: coverage.db
if-no-files-found: error

coverage:
name: Coverage
runs-on: ubuntu-latest
needs: testing
permissions:
pull-requests: write
contents: write

steps:
- uses: actions/checkout@v4

- uses: actions/download-artifact@v4
id: download
with:
pattern: coverage-*
merge-multiple: true

- name: Coverage comment
id: coverage_comment
uses: py-cov-action/python-coverage-comment-action@v3
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MERGE_COVERAGE_FILES: true

- name: Store Pull Request comment to be posted
uses: actions/upload-artifact@v4
if: steps.coverage_comment.outputs.COMMENT_FILE_WRITTEN == 'true'
with:
name: python-coverage-comment-action
path: python-coverage-comment-action.txt
11 changes: 8 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ set windows-powershell := true
tox -e $(tox list --no-desc | grep '^py' | sort -r | tr '\n' ',') -p auto

# run all tests on specific python version
@test-on target:
tox -e $(tox list --no-desc | grep '^{{ target }}' | sort -r | tr '\n' ',')
@test-with-coverage target:
{{ inv }} cov \
--env-list $(tox list --no-desc | grep '^{{ target }}' | sort -r | tr '\n' ',') \
--output coverage.db

inv := "inv -r scripts -c invoke_tasks"

@cov:
{{ inv }} cov
{{ inv }} cov \
--env-list $(tox list --no-desc | grep -e '^py' | sort -r | tr '\n' ',') \
--output coverage.db \
--parallel

@deps-compile:
{{ inv }} deps-compile
Expand Down
21 changes: 13 additions & 8 deletions scripts/invoke_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ def q(value: Union[Path, str]) -> str:
return shlex.quote(str(value))


def if_str(flag: bool, value: str) -> str:
return value if flag else ''


@task
def cov(c: Context):
def cov(c: Context, env_list, output='coverage.xml', parallel=False):
inner_bash_command = q(
"coverage run"
" --branch"
Expand All @@ -21,25 +25,26 @@ def cov(c: Context):
)
tox_commands = f"bash -c '{q(inner_bash_command)}'"
c.run(
r"tox -e $(tox -l | grep -e '^py' | grep -v 'bench' | sort -r | tr '\n' ',')"
" -p auto"
f"tox -e {q(env_list)}"
+ if_str(parallel, " -p auto") +
" --override 'testenv.allowlist_externals=bash'"
f" --override 'testenv.commands={tox_commands}'",
pty=True,
)
c.run("coverage combine --data-file .tox/cov-storage/.coverage .tox/cov-storage")
c.run("coverage xml --data-file .tox/cov-storage/.coverage")
if output.endswith('.xml'):
c.run(f"coverage xml --data-file .tox/cov-storage/.coverage -o {output}")
else:
c.run(f"cp .tox/cov-storage/.coverage {output}")


@task
def deps_compile(c: Context, upgrade=False):
extra = ""
if upgrade:
extra += " --upgrade"
promises = [
c.run(
f'pip-compile {req} -o {Path("requirements") / req.name}'
' -q --allow-unsafe --strip-extras' + extra,
' -q --allow-unsafe --strip-extras'
+ if_str(upgrade, " --upgrade"),
asynchronous=True,
)
for req in Path(".").glob("requirements/raw/*.txt")
Expand Down

0 comments on commit 8f76c64

Please sign in to comment.