Skip to content

Commit 4fa04ac

Browse files
committed
tasks: calculate code coverage for all test groups
1 parent 2c3e47e commit 4fa04ac

File tree

13 files changed

+280
-63
lines changed

13 files changed

+280
-63
lines changed

.coveragerc.end2end

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[run]
2+
branch = True
3+
# concurrency = multiprocessing
4+
omit =
5+
build/
6+
**/output/cache/*
7+
**/Output/cache/*
8+
9+
[report]
10+
fail_under = 60.0
11+
precision = 2
12+
skip_covered = true
13+
show_missing = true

.coveragerc.integration

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[run]
2+
branch = True
3+
parallel = true
4+
omit =
5+
build/
6+
tests/
7+
**/output/cache/*
8+
**/Output/cache/*
9+
10+
[report]
11+
fail_under = 60.0
12+
precision = 2
13+
skip_covered = true
14+
show_missing = true

.coveragerc renamed to .coveragerc.unit

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
# .coveragerc to control coverage.py
21
[run]
32
branch = True
43
omit =
5-
*/.venv/*
4+
build/
65

76
[report]
87
fail_under = 60.0

.coveragerc.unit_server

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[run]
2+
branch = True
3+
omit =
4+
build/
5+
6+
[report]
7+
fail_under = 60.0
8+
precision = 2
9+
skip_covered = true
10+
show_missing = true

strictdoc/cli/main.py

+3
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
from strictdoc.core.actions.export_action import ExportAction
3434
from strictdoc.core.actions.import_action import ImportAction
3535
from strictdoc.core.project_config import ProjectConfig, ProjectConfigLoader
36+
from strictdoc.helpers.coverage import register_code_coverage_hook
3637
from strictdoc.helpers.exception import StrictDocException
3738
from strictdoc.helpers.parallelizer import Parallelizer
3839
from strictdoc.server.server import run_strictdoc_server
3940

4041

4142
def _main(parallelizer: Parallelizer) -> None:
43+
register_code_coverage_hook()
44+
4245
parser = create_sdoc_args_parser()
4346

4447
project_config: ProjectConfig

strictdoc/export/html2pdf/pdf_print_driver.py

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def get_pdf_from_html(
2020
# Using sys.executable instead of "python" is important because
2121
# venv subprocess call to python resolves to wrong interpreter,
2222
# https://github.com/python/cpython/issues/86207
23+
# Switching back to calling html2print directly because the
24+
# python -m doesn't work well with PyInstaller.
25+
# sys.executable, "-m"
2326
"html2print",
2427
"print",
2528
"--cache-dir",

strictdoc/helpers/coverage.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import atexit
2+
import os
3+
import signal
4+
import types
5+
from typing import Optional
6+
7+
8+
def register_code_coverage_hook() -> None:
9+
if not "COVERAGE_PROCESS_START" in os.environ:
10+
return
11+
12+
import coverage
13+
14+
current_coverage = coverage.Coverage.current()
15+
16+
if current_coverage:
17+
18+
def save_coverage() -> None:
19+
print( # noqa: T201
20+
"strictdoc/server: exit hook: saving code coverage...",
21+
flush=True,
22+
)
23+
current_coverage.stop()
24+
current_coverage.save()
25+
26+
atexit.register(save_coverage)
27+
28+
def handle_signal(
29+
signum: int,
30+
frame: Optional[types.FrameType], # noqa: ARG001
31+
) -> None:
32+
print( # noqa: T201
33+
f"strictdoc: caught signal {signum}.", flush=True
34+
)
35+
save_coverage()
36+
signal.signal(signum, signal.SIG_DFL)
37+
os.kill(os.getpid(), signum)
38+
39+
for sig in (signal.SIGINT, signal.SIGTERM):
40+
signal.signal(sig, handle_signal)

strictdoc/server/app.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
# mypy: disable-error-code="no-untyped-def"
21
import os
32
import sys
43
import time
4+
from typing import Awaitable, Callable
55

66
from fastapi import FastAPI
77
from fastapi.middleware.cors import CORSMiddleware
88
from starlette.requests import Request
9+
from starlette.responses import Response
910

1011
from strictdoc.core.project_config import ProjectConfig
12+
from strictdoc.helpers.coverage import register_code_coverage_hook
1113
from strictdoc.helpers.pickle import pickle_load
1214
from strictdoc.server.config import SDocServerEnvVariable
1315
from strictdoc.server.routers.main_router import create_main_router
@@ -20,7 +22,7 @@
2022
O_TEMPORARY = 0
2123

2224

23-
def create_app(*, project_config: ProjectConfig):
25+
def create_app(*, project_config: ProjectConfig) -> FastAPI:
2426
app = FastAPI()
2527

2628
origins = [
@@ -32,10 +34,10 @@ def create_app(*, project_config: ProjectConfig):
3234
# Uncomment this to enable performance measurements.
3335
@app.middleware("http")
3436
async def add_process_time_header( # pylint: disable=unused-variable
35-
request: Request, call_next
36-
):
37+
request: Request, call_next: Callable[[Request], Awaitable[Response]]
38+
) -> Response:
3739
start_time = time.time()
38-
response = await call_next(request)
40+
response: Response = await call_next(request)
3941
time_passed = round(time.time() - start_time, 3)
4042

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

6365

64-
def strictdoc_production_app():
66+
def strictdoc_production_app() -> FastAPI:
67+
register_code_coverage_hook()
68+
6569
# This is a work-around to allow opening a file created with
6670
# NamedTemporaryFile on Windows.
6771
# See https://stackoverflow.com/a/15235559
68-
def temp_opener(name, flag, mode=0o777):
72+
def temp_opener(name: str, flag: int, mode: int = 0o777) -> int:
6973
try:
7074
flag |= O_TEMPORARY
7175
except AttributeError:

0 commit comments

Comments
 (0)