Skip to content

Commit 0bbc72f

Browse files
authored
Merge pull request py-cov-action#255 from py-cov-action/save-more-data
Also store raw coverage information on data branch
2 parents dda94fd + 1d20d38 commit 0bbc72f

File tree

6 files changed

+33
-12
lines changed

6 files changed

+33
-12
lines changed

coverage_comment/coverage.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ def compute_coverage(num_covered: int, num_total: int) -> decimal.Decimal:
6767
return decimal.Decimal(num_covered) / decimal.Decimal(num_total)
6868

6969

70-
def get_coverage_info(merge: bool, coverage_path: pathlib.Path) -> Coverage:
70+
def get_coverage_info(
71+
merge: bool, coverage_path: pathlib.Path
72+
) -> tuple[dict, Coverage]:
7173
try:
7274
if merge:
7375
subprocess.run("coverage", "combine", path=coverage_path)
7476

75-
json_coverage = subprocess.run(
76-
"coverage", "json", "-o", "-", path=coverage_path
77+
json_coverage = json.loads(
78+
subprocess.run("coverage", "json", "-o", "-", path=coverage_path)
7779
)
7880
except subprocess.SubProcessError as exc:
7981
if "No source for code:" in str(exc):
@@ -89,7 +91,7 @@ def get_coverage_info(merge: bool, coverage_path: pathlib.Path) -> Coverage:
8991
)
9092
raise
9193

92-
return extract_info(data=json.loads(json_coverage), coverage_path=coverage_path)
94+
return json_coverage, extract_info(data=json_coverage, coverage_path=coverage_path)
9395

9496

9597
def generate_coverage_html_files(

coverage_comment/files.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def apply(self):
5858

5959
def compute_files(
6060
line_rate: decimal.Decimal,
61+
raw_coverage_data: dict,
6162
minimum_green: decimal.Decimal,
6263
minimum_orange: decimal.Decimal,
6364
http_session: httpx.Client,
@@ -77,7 +78,10 @@ def compute_files(
7778
),
7879
WriteFile(
7980
path=DATA_PATH,
80-
contents=compute_datafile(line_rate=line_rate),
81+
contents=compute_datafile(
82+
raw_coverage_data=raw_coverage_data,
83+
line_rate=line_rate,
84+
),
8185
),
8286
WriteFile(
8387
path=BADGE_PATH,
@@ -88,8 +92,8 @@ def compute_files(
8892
]
8993

9094

91-
def compute_datafile(line_rate: decimal.Decimal) -> str:
92-
return json.dumps({"coverage": float(line_rate)})
95+
def compute_datafile(raw_coverage_data: dict, line_rate: decimal.Decimal) -> str:
96+
return json.dumps({"coverage": float(line_rate), "raw_data": raw_coverage_data})
9397

9498

9599
def parse_datafile(contents) -> decimal.Decimal:

coverage_comment/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def action(
7272
return 1
7373

7474
if event_name in {"pull_request", "push"}:
75-
coverage = coverage_module.get_coverage_info(
75+
raw_coverage, coverage = coverage_module.get_coverage_info(
7676
merge=config.MERGE_COVERAGE_FILES, coverage_path=config.COVERAGE_PATH
7777
)
7878
if event_name == "pull_request":
@@ -94,6 +94,7 @@ def action(
9494
return save_coverage_data_files(
9595
config=config,
9696
coverage=coverage,
97+
raw_coverage_data=raw_coverage,
9798
github_session=github_session,
9899
git=git,
99100
http_session=http_session,
@@ -250,6 +251,7 @@ def post_comment(config: settings.Config, github_session: httpx.Client) -> int:
250251
def save_coverage_data_files(
251252
config: settings.Config,
252253
coverage: coverage_module.Coverage,
254+
raw_coverage_data: dict,
253255
github_session: httpx.Client,
254256
git: subprocess.Git,
255257
http_session: httpx.Client,
@@ -269,6 +271,7 @@ def save_coverage_data_files(
269271
log.info("Computing coverage files & badge")
270272
operations: list[files.Operation] = files.compute_files(
271273
line_rate=coverage.info.percent_covered,
274+
raw_coverage_data=raw_coverage_data,
272275
minimum_green=config.MINIMUM_GREEN,
273276
minimum_orange=config.MINIMUM_ORANGE,
274277
http_session=http_session,

tests/end_to_end/test_all.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def test_public_repo(
9494
# unit tests are for.
9595
data = client.get(f"{raw_url_prefix}/data.json", follow_redirects=True).json()
9696
assert "coverage" in data
97+
assert "raw_data" in data
98+
assert "meta" in data["raw_data"]
9799

98100
endpoint = client.get(
99101
f"{raw_url_prefix}/endpoint.json", follow_redirects=True

tests/unit/test_coverage.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ def test_get_coverage_info(mocker, coverage_json, coverage_obj):
2828
"coverage_comment.subprocess.run", return_value=json.dumps(coverage_json)
2929
)
3030

31-
result = coverage.get_coverage_info(merge=True, coverage_path=pathlib.Path("."))
31+
raw_coverage_information, result = coverage.get_coverage_info(
32+
merge=True, coverage_path=pathlib.Path(".")
33+
)
3234

3335
assert run.call_args_list == [
3436
mocker.call("coverage", "combine", path=pathlib.Path(".")),
3537
mocker.call("coverage", "json", "-o", "-", path=pathlib.Path(".")),
3638
]
3739

3840
assert result == coverage_obj
41+
assert raw_coverage_information == coverage_json
3942

4043

4144
def test_get_coverage_info__no_merge(mocker, coverage_json):

tests/unit/test_files.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def test_compute_files(session):
3131

3232
result = files.compute_files(
3333
line_rate=decimal.Decimal("0.1234"),
34+
raw_coverage_data={"foo": ["bar", "bar2"]},
3435
minimum_green=decimal.Decimal("25"),
3536
minimum_orange=decimal.Decimal("70"),
3637
http_session=session,
@@ -40,16 +41,22 @@ def test_compute_files(session):
4041
path=pathlib.Path("endpoint.json"),
4142
contents='{"schemaVersion": 1, "label": "Coverage", "message": "12%", "color": "red"}',
4243
),
43-
files.WriteFile(path=pathlib.Path("data.json"), contents='{"coverage": 12.34}'),
44+
files.WriteFile(
45+
path=pathlib.Path("data.json"),
46+
contents='{"coverage": 12.34, "raw_data": {"foo": ["bar", "bar2"]}}',
47+
),
4448
files.WriteFile(path=pathlib.Path("badge.svg"), contents="foo"),
4549
]
4650
assert result == expected
4751

4852

4953
def test_compute_datafile():
5054
assert (
51-
files.compute_datafile(line_rate=decimal.Decimal("12.34"))
52-
== """{"coverage": 12.34}"""
55+
files.compute_datafile(
56+
line_rate=decimal.Decimal("12.34"),
57+
raw_coverage_data={"meta": {"version": "5.5"}},
58+
)
59+
== """{"coverage": 12.34, "raw_data": {"meta": {"version": "5.5"}}}"""
5360
)
5461

5562

0 commit comments

Comments
 (0)