Skip to content

tasks: calculate code coverage for all test groups #2178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .coveragerc.end2end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[run]
branch = True
# concurrency = multiprocessing
omit =
build/
**/output/cache/*
**/Output/cache/*

[report]
fail_under = 60.0
precision = 2
skip_covered = true
show_missing = true
14 changes: 14 additions & 0 deletions .coveragerc.integration
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[run]
branch = True
parallel = true
omit =
build/
tests/
**/output/cache/*
**/Output/cache/*

[report]
fail_under = 60.0
precision = 2
skip_covered = true
show_missing = true
3 changes: 1 addition & 2 deletions .coveragerc → .coveragerc.unit
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# .coveragerc to control coverage.py
[run]
branch = True
omit =
*/.venv/*
build/

[report]
fail_under = 60.0
Expand Down
10 changes: 10 additions & 0 deletions .coveragerc.unit_server
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[run]
branch = True
omit =
build/

[report]
fail_under = 60.0
precision = 2
skip_covered = true
show_missing = true
3 changes: 3 additions & 0 deletions strictdoc/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
from strictdoc.core.actions.export_action import ExportAction
from strictdoc.core.actions.import_action import ImportAction
from strictdoc.core.project_config import ProjectConfig, ProjectConfigLoader
from strictdoc.helpers.coverage import register_code_coverage_hook
from strictdoc.helpers.exception import StrictDocException
from strictdoc.helpers.parallelizer import Parallelizer
from strictdoc.server.server import run_strictdoc_server


def _main(parallelizer: Parallelizer) -> None:
register_code_coverage_hook()

parser = create_sdoc_args_parser()

project_config: ProjectConfig
Expand Down
3 changes: 3 additions & 0 deletions strictdoc/export/html2pdf/pdf_print_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def get_pdf_from_html(
# Using sys.executable instead of "python" is important because
# venv subprocess call to python resolves to wrong interpreter,
# https://github.com/python/cpython/issues/86207
# Switching back to calling html2print directly because the
# python -m doesn't work well with PyInstaller.
# sys.executable, "-m"
"html2print",
"print",
"--cache-dir",
Expand Down
40 changes: 40 additions & 0 deletions strictdoc/helpers/coverage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import atexit
import os
import signal
import types
from typing import Optional


def register_code_coverage_hook() -> None:
if not "COVERAGE_PROCESS_START" in os.environ:
return

import coverage

current_coverage = coverage.Coverage.current()

if current_coverage:

def save_coverage() -> None:
print( # noqa: T201
"strictdoc/server: exit hook: saving code coverage...",
flush=True,
)
current_coverage.stop()
current_coverage.save()

atexit.register(save_coverage)

def handle_signal(
signum: int,
frame: Optional[types.FrameType], # noqa: ARG001
) -> None:
print( # noqa: T201
f"strictdoc: caught signal {signum}.", flush=True
)
save_coverage()
signal.signal(signum, signal.SIG_DFL)
os.kill(os.getpid(), signum)

for sig in (signal.SIGINT, signal.SIGTERM):
signal.signal(sig, handle_signal)
18 changes: 11 additions & 7 deletions strictdoc/server/app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# mypy: disable-error-code="no-untyped-def"
import os
import sys
import time
from typing import Awaitable, Callable

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.requests import Request
from starlette.responses import Response

from strictdoc.core.project_config import ProjectConfig
from strictdoc.helpers.coverage import register_code_coverage_hook
from strictdoc.helpers.pickle import pickle_load
from strictdoc.server.config import SDocServerEnvVariable
from strictdoc.server.routers.main_router import create_main_router
Expand All @@ -20,7 +22,7 @@
O_TEMPORARY = 0


def create_app(*, project_config: ProjectConfig):
def create_app(*, project_config: ProjectConfig) -> FastAPI:
app = FastAPI()

origins = [
Expand All @@ -32,10 +34,10 @@ def create_app(*, project_config: ProjectConfig):
# Uncomment this to enable performance measurements.
@app.middleware("http")
async def add_process_time_header( # pylint: disable=unused-variable
request: Request, call_next
):
request: Request, call_next: Callable[[Request], Awaitable[Response]]
) -> Response:
start_time = time.time()
response = await call_next(request)
response: Response = await call_next(request)
time_passed = round(time.time() - start_time, 3)

request_path = request.url.path
Expand All @@ -61,11 +63,13 @@ async def add_process_time_header( # pylint: disable=unused-variable
return app


def strictdoc_production_app():
def strictdoc_production_app() -> FastAPI:
register_code_coverage_hook()

# This is a work-around to allow opening a file created with
# NamedTemporaryFile on Windows.
# See https://stackoverflow.com/a/15235559
def temp_opener(name, flag, mode=0o777):
def temp_opener(name: str, flag: int, mode: int = 0o777) -> int:
try:
flag |= O_TEMPORARY
except AttributeError:
Expand Down
Loading
Loading