Skip to content

Commit

Permalink
Fix label recalculation (#200)
Browse files Browse the repository at this point in the history
When Codecov doesn't return the absent labels we were re-calculating only the absent labels.
The issue is that, becaue Codecov didn't have them in the first place they couldn't filter out labels that might have been changed in the current commit. And we (CLI) weren't doing that either.

That's an issue because pytest fails if you tell it to run a label that doesn't exist.
So if we get the payload back with no absent labels we have to recalculate everything.
  • Loading branch information
giovanni-guidini authored Jul 17, 2023
1 parent 5f3d7f0 commit 157996f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
24 changes: 21 additions & 3 deletions codecov_cli/commands/labelanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,29 @@ def _potentially_calculate_absent_labels(
request_result, requested_labels
) -> LabelAnalysisRequestResult:
if request_result["absent_labels"]:
# This means that Codecov already calculated everything for us
return LabelAnalysisRequestResult(request_result)
# Here we have to calculate the absent labels
# And also remove labels that maybe don't exist anymore from the set of labels to test
# Because codecov didn't have this info previously
requested_labels_set = set(requested_labels)
present_labels_set = set(request_result["present_report_labels"])
request_result["absent_labels"] = list(requested_labels_set - present_labels_set)
return LabelAnalysisRequestResult(request_result)
present_diff_labels_set = set(request_result.get("present_diff_labels", []))
present_report_labels_set = set(request_result.get("present_report_labels", []))
global_level_labels_set = set(request_result.get("global_level_labels", []))
return LabelAnalysisRequestResult(
{
"present_report_labels": sorted(
present_report_labels_set & requested_labels_set
),
"present_diff_labels": sorted(
present_diff_labels_set & requested_labels_set
),
"absent_labels": sorted(requested_labels_set - present_report_labels_set),
"global_level_labels": sorted(
global_level_labels_set & requested_labels_set
),
}
)


def _patch_labels(payload, url, token_header):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="codecov-cli",
version="0.1.15",
version="0.1.16",
packages=find_packages(exclude=["contrib", "docs", "tests*"]),
description="Codecov Command Line Interface",
long_description=long_description,
Expand Down
25 changes: 15 additions & 10 deletions tests/commands/test_invoke_labelanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,26 @@ def get_labelanalysis_deps(mocker):


class TestLabelAnalysisNotInvoke(object):
def test_potentially_calculate_labels(self):
def test_potentially_calculate_labels_recalculate(self):
request_result = {
"present_report_labels": ["label_1", "label_2", "label_3"],
"present_report_labels": [
"label_1",
"label_2",
"label_3",
"label_old",
"label_older",
],
"absent_labels": [],
"present_diff_labels": ["label_2", "label_3", "label_old"],
"global_level_labels": ["label_1", "label_older"],
}
collected_labels = ["label_1", "label_2", "label_3", "label_4"]
expected = {
"present_diff_labels": ["label_2", "label_3"],
"global_level_labels": ["label_1"],
"absent_labels": ["label_4"],
"present_report_labels": ["label_1", "label_2", "label_3"],
}
collected_labels = ["label_1", "label_2", "label_3", "label_4"]
expected = {**request_result, "absent_labels": ["label_4"]}
assert (
_potentially_calculate_absent_labels(request_result, collected_labels)
== expected
)
request_result["absent_labels"] = ["label_4", "label_5"]
expected["absent_labels"].append("label_5")
assert (
_potentially_calculate_absent_labels(request_result, collected_labels)
== expected
Expand Down

0 comments on commit 157996f

Please sign in to comment.