Skip to content

Commit

Permalink
refactor: Rework test coverage combining
Browse files Browse the repository at this point in the history
Fake coverage report directly in duty.
This way it works both locally and in CI.
  • Loading branch information
pawamoy committed Mar 12, 2021
1 parent 56e4973 commit 10a1410
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 34 deletions.
25 changes: 12 additions & 13 deletions project/.gitlab-ci.yml
Expand Up @@ -5,19 +5,19 @@ variables:
cache:
key: "${CI_JOB_NAME}"
paths:
- .cache/pip
- .venv
- .cache/pip
- .venv

stages:
- quality
- tests
- deploy
- quality
- tests
- deploy

.install-deps-template: &install-deps
before_script:
- pip install poetry
- poetry --version
- poetry install -vv
- pip install poetry
- poetry --version
- poetry install -vv

# Quality jobs

Expand All @@ -28,10 +28,7 @@ stages:

check-docs:
<<: *quality
script:
- mkdir -p build/coverage
- touch build/coverage/index.html
- poetry run duty check-docs
script: poetry run duty check-docs

check-code-quality:
<<: *quality
Expand All @@ -51,6 +48,9 @@ check-dependencies:
<<: *install-deps
stage: tests
script: poetry run duty test
artifacts:
paths:
- .coverage-*

python3.6:
<<: *test
Expand Down Expand Up @@ -79,7 +79,6 @@ pages:
- python3.8
- python3.9
script:
- poetry run duty test
- poetry run duty coverage
- poetry run duty docs
coverage: '/^TOTAL.+?(\d+\.\d+\%)$/'
Expand Down
4 changes: 3 additions & 1 deletion project/Makefile → project/Makefile.jinja
Expand Up @@ -7,14 +7,16 @@ args = $(foreach a,$($(subst -,_,$1)_args),$(if $(value $a),$a="$($a)"))
check_code_quality_args = files
docs_serve_args = host port
release_args = version
test_args = cleancov match
test_args = match

BASIC_DUTIES = \
changelog \
clean \
coverage \
docs \
[%- if repository_provider == "github.com" %]
docs-deploy \
[%- endif %]
docs-regen \
docs-serve \
format \
Expand Down
3 changes: 1 addition & 2 deletions project/config/pytest.ini
Expand Up @@ -11,7 +11,6 @@ python_files =
tests.py
addopts =
--cov
--cov-append
--cov-config config/coverage.ini
testpaths =
tests
tests
30 changes: 12 additions & 18 deletions project/duties.py → project/duties.py.jinja
Expand Up @@ -2,6 +2,7 @@

import os
import re
import sys
from pathlib import Path
from shutil import which
from typing import List, Optional, Pattern
Expand Down Expand Up @@ -196,6 +197,8 @@ def check_docs(ctx):
Arguments:
ctx: The context instance (passed automatically).
"""
Path("build/coverage").mkdir(parents=True, exist_ok=True)
Path("build/coverage/index.html").touch(exist_ok=True)
ctx.run("mkdocs build -s", title="Building documentation", pty=PTY)


Expand Down Expand Up @@ -254,6 +257,7 @@ def docs_serve(ctx, host="127.0.0.1", port=8000):
ctx.run(f"mkdocs serve -a {host}:{port}", title="Serving documentation", capture=False)


[% if repository_provider == "github.com" -%]
@duty
def docs_deploy(ctx):
"""
Expand All @@ -264,19 +268,8 @@ def docs_deploy(ctx):
"""
ctx.run("mkdocs gh-deploy", title="Deploying documentation")


@duty
def docs_deploy_gitlab(ctx):
"""
Deploy the documentation on Gitlab Pages.
Arguments:
ctx: The context instance (passed automatically).
"""
ctx.run("mkdocs build -s --site-dir public", title="Deploying documentation")



[% endif -%]
@duty
def format(ctx): # noqa: W0622 (we don't mind shadowing the format builtin)
"""
Expand Down Expand Up @@ -312,8 +305,9 @@ def release(ctx, version):
ctx.run("git push --tags", title="Pushing tags", pty=False)
ctx.run("poetry build", title="Building dist/wheel", pty=PTY)
ctx.run("poetry publish", title="Publishing version", pty=PTY)
# building docs with the site_dir specified in mkdocs.yml ensures the proper path 'site' or 'public' is used and this will work
ctx.run("mkdocs build", title="Deploying documentation")
[%- if repository_provider == "github.com" %]
docs_deploy.run()
[%- endif %]


@duty(silent=True)
Expand All @@ -324,22 +318,22 @@ def coverage(ctx):
Arguments:
ctx: The context instance (passed automatically).
"""
ctx.run("coverage combine .coverage-*", nofail=True)
ctx.run("coverage report --rcfile=config/coverage.ini", capture=False)
ctx.run("coverage html --rcfile=config/coverage.ini")


@duty
def test(ctx, cleancov: bool = True, match: str = ""):
def test(ctx, match: str = ""):
"""
Run the test suite.

Arguments:
ctx: The context instance (passed automatically).
cleancov: Whether to remove the `.coverage` file before running the tests.
match: A pytest expression to filter selected tests.
"""
if cleancov:
ctx.run("rm -f .coverage", silent=True)
py_version = f"{sys.version_info.major}{sys.version_info.minor}"
os.environ["COVERAGE_FILE"] = f".coverage-{py_version}"
ctx.run(
["pytest", "-c", "config/pytest.ini", "-n", "auto", "-k", match, "tests"],
title="Running tests",
Expand Down

0 comments on commit 10a1410

Please sign in to comment.