Skip to content

Commit

Permalink
Account for file deletions in git diff parsing (#277)
Browse files Browse the repository at this point in the history
When a file gets deleted, we get /dev/null as added filename and therefore shouldn't assume that we always have a valid filename when there are no added lines
  • Loading branch information
kieferro authored Sep 27, 2023
1 parent 27f188a commit be8e107
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
8 changes: 4 additions & 4 deletions coverage_comment/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,11 @@ def parse_diff_output(diff: str) -> dict[pathlib.Path, list[int]]:
current_file = pathlib.Path(line.removeprefix(added_filename_prefix))
continue
if line.startswith("@@"):
if current_file is None:
raise ValueError(f"Unexpected diff output format: \n{diff}")
lines = parse_line_number_diff_line(line)
result.setdefault(current_file, []).extend(lines)
continue
if len(lines) > 0:
if current_file is None:
raise ValueError(f"Unexpected diff output format: \n{diff}")
result.setdefault(current_file, []).extend(lines)

return result

Expand Down
15 changes: 13 additions & 2 deletions tests/unit/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,15 @@ def test_parse_line_number_diff_line(git, line_number_diff_line, expected):


def test_parse_diff_output(git):
diff = """diff --git a/README.md b/README.md
diff = """diff --git a/action.yml b/action.yml
deleted file mode 100644
index 42249d1..0000000
--- a/action.yml
+++ /dev/null
@@ -1,2 +0,0 @@
-name: Python Coverage Comment
-branding:
diff --git a/README.md b/README.md
index 1f1d9a4..e69de29 100644
--- a/README.md
+++ b/README.md
Expand All @@ -268,11 +276,14 @@ def test_parse_diff_output(git):
+++ b/bar.txt
@@ -8 +7,0 @@
-foo
diff --git a/coverage_comment/annotations.py b/coverage_comment/annotations2.py
similarity index 100%
rename from coverage_comment/annotations.py
rename to coverage_comment/annotations2.py
"""
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],
pathlib.Path("bar.txt"): [],
}

0 comments on commit be8e107

Please sign in to comment.