Skip to content

Commit

Permalink
Fix tests, add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim committed Sep 17, 2023
1 parent 4b58f87 commit 2a98d71
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 16 deletions.
19 changes: 12 additions & 7 deletions coverage_comment/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,24 @@ def get_diff_coverage_info(
num_changed_lines = 0

for path, added_lines_for_file in added_lines.items():
num_changed_lines += len(added_lines_for_file)

try:
file = coverage.files[path]
except KeyError:
continue

count_executed = len(set(file.executed_lines) & set(added_lines_for_file))
executed = set(file.executed_lines) & set(added_lines_for_file)
count_executed = len(executed)

missing = sorted(set(file.missing_lines) & set(added_lines.get(path, [])))
missing = set(file.missing_lines) & set(added_lines_for_file)
count_missing = len(missing)
count_total = count_executed + count_missing
# We don't want to count twice the lines that are both executed and
# missing (e.g. the branch lines partially executed)
count_total = len(executed | missing)

total_num_lines += count_executed + count_missing
total_num_lines += count_total
total_num_violations += count_missing
num_changed_lines += len(added_lines_for_file)

percent_covered = decimal.Decimal("1")
if count_total != 0:
Expand All @@ -251,7 +255,7 @@ def get_diff_coverage_info(
files[path] = FileDiffCoverage(
path=path,
percent_covered=percent_covered,
violation_lines=missing,
violation_lines=sorted(missing),
)
final_percentage = decimal.Decimal("1")

Expand All @@ -274,7 +278,8 @@ def get_added_lines(
# --unified=0 means we don't get any context lines for chunk, and we
# don't merge chunks. This means the headers that describe line number
# are always enough to derive what line numbers were added.
diff = git.diff("--unified=0", f"origin/{base_ref}", "--", ".")
git.fetch("origin", base_ref, "--depth=1000")
diff = git.diff("--unified=0", "FETCH_HEAD", "--", ".")
return parse_diff_output(diff)


Expand Down
27 changes: 18 additions & 9 deletions tests/integration/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ def checker(payload):
"POST", "/repos/py-cov-action/foobar/issues/2/comments", json=checker
)(status_code=403)

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)
git.register("git fetch origin main --depth=1000")()
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)

result = main.action(
config=pull_request_config(
Expand Down Expand Up @@ -245,7 +246,8 @@ def checker(payload):
"POST", "/repos/py-cov-action/foobar/issues/2/comments", json=checker
)(status_code=403)

git.register("git diff --unified=0 origin/foo -- .")(stdout=DIFF_STDOUT)
git.register("git fetch origin foo --depth=1000")(stdout=DIFF_STDOUT)
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)

result = main.action(
config=pull_request_config(
Expand Down Expand Up @@ -288,7 +290,8 @@ def test_action__pull_request__post_comment(
# Are there already comments
session.register("GET", "/repos/py-cov-action/foobar/issues/2/comments")(json=[])

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)
git.register("git fetch origin main --depth=1000")()
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)

comment = None

Expand Down Expand Up @@ -333,7 +336,8 @@ def test_action__push__non_default_branch(
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
)
git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)
git.register("git fetch origin main --depth=1000")(stdout=DIFF_STDOUT)
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)

payload = json.dumps({"coverage": 30.00})
# There is an existing badge in this test, allowing to test the coverage evolution
Expand Down Expand Up @@ -402,7 +406,8 @@ def test_action__push__non_default_branch__no_pr(
session.register("GET", "/repos/py-cov-action/foobar")(
json={"default_branch": "main", "visibility": "public"}
)
git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)
git.register("git fetch origin main --depth=1000")(stdout=DIFF_STDOUT)
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)

payload = json.dumps({"coverage": 30.00})
# There is an existing badge in this test, allowing to test the coverage evolution
Expand Down Expand Up @@ -465,7 +470,8 @@ def test_action__pull_request__force_store_comment(
"/repos/py-cov-action/foobar/contents/data.json",
)(json={"content": base64.b64encode(payload.encode()).decode()})

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)
git.register("git fetch origin main --depth=1000")()
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)

result = main.action(
config=pull_request_config(FORCE_WORKFLOW_RUN=True, GITHUB_OUTPUT=output_file),
Expand Down Expand Up @@ -495,7 +501,8 @@ def test_action__pull_request__post_comment__no_marker(
"/repos/py-cov-action/foobar/contents/data.json",
)(status_code=404)

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)
git.register("git fetch origin main --depth=1000")()
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)

result = main.action(
config=pull_request_config(COMMENT_TEMPLATE="""foo"""),
Expand All @@ -519,7 +526,8 @@ def test_action__pull_request__annotations(
"/repos/py-cov-action/foobar/contents/data.json",
)(status_code=404)

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)
git.register("git fetch origin main --depth=1000")()
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)

# Who am I
session.register("GET", "/user")(json={"login": "foo"})
Expand Down Expand Up @@ -560,7 +568,8 @@ def test_action__pull_request__post_comment__template_error(
"/repos/py-cov-action/foobar/contents/data.json",
)(status_code=404)

git.register("git diff --unified=0 origin/main -- .")(stdout=DIFF_STDOUT)
git.register("git fetch origin main --depth=1000")()
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)

result = main.action(
config=pull_request_config(COMMENT_TEMPLATE="""{%"""),
Expand Down
116 changes: 116 additions & 0 deletions tests/unit/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,119 @@ def test_generate_coverage_markdown(mocker):
]

assert result == "foo"


@pytest.mark.parametrize(
"added_lines, executed_lines, missing_lines, expected",
[
(
{pathlib.Path("codebase/code.py"): [1, 3]},
[1, 2],
[3],
coverage.DiffCoverage(
total_num_lines=2,
total_num_violations=1,
total_percent_covered=decimal.Decimal("0.5"),
num_changed_lines=2,
files={
pathlib.Path("codebase/code.py"): coverage.FileDiffCoverage(
path=pathlib.Path("codebase/code.py"),
percent_covered=decimal.Decimal("0.5"),
violation_lines=[3],
)
},
),
),
(
{pathlib.Path("codebase/code2.py"): [1, 3]},
[1, 2],
[3],
coverage.DiffCoverage(
total_num_lines=0,
total_num_violations=0,
total_percent_covered=decimal.Decimal("1"),
num_changed_lines=2,
files={},
),
),
(
{pathlib.Path("codebase/code.py"): [4, 5, 6]},
[1, 2, 3],
[7],
coverage.DiffCoverage(
total_num_lines=0,
total_num_violations=0,
total_percent_covered=decimal.Decimal("1"),
num_changed_lines=3,
files={
pathlib.Path("codebase/code.py"): coverage.FileDiffCoverage(
path=pathlib.Path("codebase/code.py"),
percent_covered=decimal.Decimal("1"),
violation_lines=[],
)
},
),
),
],
)
def test_get_diff_coverage_info(
coverage_obj_no_branch, added_lines, executed_lines, missing_lines, expected
):
cov_file = coverage_obj_no_branch.files[pathlib.Path("codebase/code.py")]
cov_file.executed_lines = executed_lines
cov_file.missing_lines = missing_lines
result = coverage.get_diff_coverage_info(
added_lines=added_lines, coverage=coverage_obj_no_branch
)
assert result == expected


def test_get_added_lines(git):
diff = (
"""+++ b/README.md\n@@ -1,2 +1,3 @@\n-# coverage-comment\n+coverage-comment\n"""
)
git.register("git fetch origin main --depth=1000")()
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=diff)
assert coverage.get_added_lines(git=git, base_ref="main") == {
pathlib.Path("README.md"): [1, 2, 3]
}


@pytest.mark.parametrize(
"line_number_diff_line, expected",
[
("@@ -1,2 +7,4 @@ foo()", [7, 8, 9, 10]),
("@@ -1,2 +8 @@ foo()", [8]),
],
)
def test_parse_line_number_diff_line(git, line_number_diff_line, expected):
result = list(coverage.parse_line_number_diff_line(line_number_diff_line))
assert result == expected


def test_parse_diff_output(git):
diff = """diff --git a/README.md b/README.md
index 1f1d9a4..e69de29 100644
--- a/README.md
+++ b/README.md
@@ -1 +1 @@
-# coverage-comment
-coverage-comment
@@ -3,2 +3,4 @@
-foo
-bar
+foo1
+bar1
+foo2
+bar2
--- a/foo.txt
+++ b/foo.txt
@@ -0,0 +1 @@
+bar
"""
git.register("git fetch origin main --depth=1000")()
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=diff)
assert coverage.parse_diff_output(diff=diff) == {
pathlib.Path("README.md"): [1, 3, 4, 5, 6],
pathlib.Path("foo.txt"): [1],
}

0 comments on commit 2a98d71

Please sign in to comment.