Skip to content

Commit 1565c1e

Browse files
committed
extend coverage-xm` command: item key now coverage-xml-line-rate
+ small refactorings
1 parent 5dbd93f commit 1565c1e

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
55

66
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](https://semver.org/)
77

8-
## [0.11.0] 2025-03-xx
8+
## [0.11.0] 2025-03-21
99

1010
### Added
1111

@@ -15,6 +15,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1515
- `extend gitlab-link` command:
1616
- option `--field` to optionally change the target of the expression pattern
1717

18+
### Changed
19+
20+
- `extend coverage-xml` command:
21+
- item key now `coverage-xml-line-rate`
22+
1823

1924
## [0.10.1] 2025-03-21
2025

cimd/commands/extract/coverage_xml.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
"""coverage-xml command"""
22

3+
from enum import Enum
4+
35
import click
46
from defusedxml.ElementTree import ParseError, parse
57

68
from cimd.classes.context import ApplicationContext
79
from cimd.classes.metadata import Item
810
from cimd.classes.shields_link import ShieldsLink
911

10-
BEST_THRESHOLD = 90
11-
GOOD_THRESHOLD = 75
12-
MEDIUM_THRESHOLD = 50
12+
COMMAND_AND_PREFIX = "coverage-xml"
13+
14+
15+
class Threshold(float, Enum):
16+
"""Color thresholds"""
17+
18+
MEDIUM = 50
19+
GOOD = 75
20+
BEST = 90
1321

1422

1523
def image_for_line_rate_item(line_rate: float) -> ShieldsLink:
@@ -18,18 +26,18 @@ def image_for_line_rate_item(line_rate: float) -> ShieldsLink:
1826
same colors as in genbadge project:
1927
https://github.com/smarie/python-genbadge/blob/main/genbadge/utils_coverage.py#L105C1-L119C17
2028
"""
21-
if line_rate < MEDIUM_THRESHOLD:
29+
if line_rate < Threshold.MEDIUM:
2230
color = "red"
23-
elif line_rate < GOOD_THRESHOLD:
31+
elif line_rate < Threshold.GOOD:
2432
color = "orange"
25-
elif line_rate < BEST_THRESHOLD:
33+
elif line_rate < Threshold.BEST:
2634
color = "green"
2735
else:
2836
color = "brightgreen"
2937
return ShieldsLink(label="Coverage", message=f"{line_rate!s}%", color=color)
3038

3139

32-
@click.command(name="coverage-xml")
40+
@click.command(name=COMMAND_AND_PREFIX)
3341
@click.argument(
3442
"XML_FILE",
3543
type=click.Path(exists=True, dir_okay=False, file_okay=True, readable=True, resolve_path=True),
@@ -52,7 +60,7 @@ def coverage_xml_command(app: ApplicationContext, xml_file: str, replace: bool)
5260
raise click.UsageError(f"{xml_file} - {error!s}") from error
5361
root = xml.getroot()
5462
line_rate: float = round(float(root.attrib.get("line-rate")) * 100, ndigits=2)
55-
key = "coverage-py-line-rate"
63+
key = f"{COMMAND_AND_PREFIX}-line-rate"
5664
new_item = Item(
5765
value=str(line_rate),
5866
label="Coverage",

cimd/commands/extract/junit_xml.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from cimd.classes.metadata import Item
1010
from cimd.classes.shields_link import ShieldsLink
1111

12+
COMMAND_AND_PREFIX = "junit-xml"
13+
1214
COLORS = {
1315
"FAILURES": "red",
1416
"ERRORS": "orange",
@@ -58,7 +60,7 @@ def junit_xml_command(app: ApplicationContext, xml_file: str, replace: bool) ->
5860
}
5961
for _ in counter:
6062
count = counter.get(_, 0)
61-
key = f"junit-xml-{_.lower()}"
63+
key = f"{COMMAND_AND_PREFIX}-{_.lower()}"
6264
new_item = Item(
6365
value=str(count),
6466
label=f"Test {_.title()}",

cimd/commands/extract/trivy_scans.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from cimd.classes.metadata import Item
1010
from cimd.classes.shields_link import ShieldsLink
1111

12+
COMMAND_AND_PREFIX = "trivy-scan"
13+
1214
COLORS = {
1315
"CRITICAL": "red",
1416
"HIGH": "orange",
@@ -56,7 +58,7 @@ def image_for_severity_count(severity: str, count: int) -> str:
5658
return ShieldsLink(label=severity, message=str(count), color=color, logo="trivy").to_string()
5759

5860

59-
@click.command(name="trivy-scan")
61+
@click.command(name=COMMAND_AND_PREFIX)
6062
@click.argument(
6163
"JSON_FILE",
6264
type=click.Path(exists=True, dir_okay=False, file_okay=True, readable=True, resolve_path=True),
@@ -96,7 +98,7 @@ def trivy_scan_command(
9698
severities = list(set(severities))
9799
for _ in severities:
98100
count = counter.get(_, 0)
99-
key = f"trivy-scan-{_.lower()}"
101+
key = f"{COMMAND_AND_PREFIX}-{_.lower()}"
100102
new_item = Item(
101103
value=str(count),
102104
label=_,

tests/test_extract_coverage_xml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_success(fixture_data: FixtureData) -> None:
4444
assert run(command=("list", "--keys-only")).line_count == 0
4545
run(command=("extract", "coverage-xml", fixture_data.filepath))
4646
assert run(command=("list", "--keys-only")).line_count == 1
47-
assert run(command=("get", "coverage-py-line-rate")).lines[0] == str(fixture_data.line_rate)
47+
assert run(command=("get", "coverage-xml-line-rate")).lines[0] == str(fixture_data.line_rate)
4848

4949

5050
def test_fail() -> None:

0 commit comments

Comments
 (0)