Skip to content

Commit 54e726a

Browse files
committed
use three dot notation to calculate diff
1 parent 1d7560e commit 54e726a

File tree

5 files changed

+144
-13
lines changed

5 files changed

+144
-13
lines changed

coverage_comment/coverage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def get_added_lines(
293293
# don't merge chunks. This means the headers that describe line number
294294
# are always enough to derive what line numbers were added.
295295
git.fetch("origin", base_ref, "--depth=1000")
296-
diff = git.diff("--unified=0", "FETCH_HEAD", "--", ".")
296+
diff = git.diff("--unified=0", "FETCH_HEAD...HEAD")
297297
return parse_diff_output(diff)
298298

299299

tests/integration/conftest.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
from __future__ import annotations
2+
3+
import os
4+
import pathlib
5+
import subprocess
6+
import uuid
7+
8+
import pytest
9+
10+
11+
@pytest.fixture
12+
def in_integration_env(integration_env, integration_dir):
13+
curdir = os.getcwd()
14+
os.chdir(integration_dir)
15+
yield integration_dir
16+
os.chdir(curdir)
17+
18+
19+
@pytest.fixture
20+
def integration_dir(tmp_path: pathlib.Path):
21+
test_dir = tmp_path / "integration_test"
22+
test_dir.mkdir()
23+
return test_dir
24+
25+
26+
@pytest.fixture
27+
def file_path(integration_dir):
28+
return integration_dir / "foo.py"
29+
30+
31+
@pytest.fixture
32+
def write_file(file_path):
33+
def _(*variables):
34+
content = "import os"
35+
for i, var in enumerate(variables):
36+
content += f"""\nif os.environ.get("{var}"):\n {i}\n"""
37+
file_path.write_text(content, encoding="utf8")
38+
39+
return _
40+
41+
42+
@pytest.fixture
43+
def run_coverage(file_path, integration_dir):
44+
def _(*variables):
45+
subprocess.check_call(
46+
["coverage", "run", "--parallel", file_path.name],
47+
cwd=integration_dir,
48+
env=os.environ | dict.fromkeys(variables, "1"),
49+
)
50+
51+
return _
52+
53+
54+
@pytest.fixture
55+
def commit(integration_dir):
56+
def _():
57+
subprocess.check_call(
58+
["git", "add", "."],
59+
cwd=integration_dir,
60+
)
61+
subprocess.check_call(
62+
["git", "commit", "-m", str(uuid.uuid4())],
63+
cwd=integration_dir,
64+
env={
65+
"GIT_AUTHOR_NAME": "foo",
66+
"GIT_AUTHOR_EMAIL": "foo",
67+
"GIT_COMMITTER_NAME": "foo",
68+
"GIT_COMMITTER_EMAIL": "foo",
69+
"GIT_CONFIG_GLOBAL": "/dev/null",
70+
"GIT_CONFIG_SYSTEM": "/dev/null",
71+
},
72+
)
73+
74+
return _
75+
76+
77+
@pytest.fixture
78+
def integration_env(integration_dir, write_file, run_coverage, commit, request):
79+
subprocess.check_call(["git", "init", "-b", "main"], cwd=integration_dir)
80+
# diff coverage reads the "origin/{...}" branch so we simulate an origin remote
81+
subprocess.check_call(["git", "remote", "add", "origin", "."], cwd=integration_dir)
82+
write_file("A", "B")
83+
commit()
84+
85+
add_branch_mark = request.node.get_closest_marker("add_branches")
86+
for additional_branch in add_branch_mark.args if add_branch_mark else []:
87+
subprocess.check_call(
88+
["git", "switch", "-c", additional_branch],
89+
cwd=integration_dir,
90+
)
91+
92+
subprocess.check_call(
93+
["git", "switch", "-c", "branch"],
94+
cwd=integration_dir,
95+
)
96+
97+
write_file("A", "B", "C", "D")
98+
commit()
99+
100+
run_coverage("A", "C")
101+
subprocess.check_call(["git", "fetch", "origin"], cwd=integration_dir)

tests/integration/test_coverage.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from __future__ import annotations
2+
3+
import subprocess
4+
from coverage_comment import coverage, subprocess as subprocess_module
5+
6+
7+
def test_get_added_lines(
8+
commit, file_path, integration_dir, in_integration_env, write_file
9+
):
10+
"""
11+
Lines added in the base_ref should not appear as added in HEAD
12+
"""
13+
git = subprocess_module.Git()
14+
relative_file_path = file_path.relative_to(integration_dir)
15+
16+
def _check_added_lines():
17+
added_lines = coverage.get_added_lines(git, "main")
18+
19+
assert added_lines == {
20+
relative_file_path: list(range(7, 13)) # Line numbers start at 1
21+
}
22+
23+
_check_added_lines()
24+
subprocess.check_call(["git", "switch", "main"], cwd=integration_dir)
25+
write_file("E", "F")
26+
commit()
27+
subprocess.check_call(["git", "push", "origin", "main"], cwd=integration_dir)
28+
subprocess.check_call(["git", "switch", "branch"], cwd=integration_dir)
29+
30+
_check_added_lines()

tests/integration/test_main.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def checker(payload):
172172
)(status_code=403)
173173

174174
git.register("git fetch origin main --depth=1000")()
175-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)
175+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
176176

177177
result = main.action(
178178
config=pull_request_config(
@@ -253,7 +253,7 @@ def checker(payload):
253253
)(status_code=403)
254254

255255
git.register("git fetch origin foo --depth=1000")(stdout=DIFF_STDOUT)
256-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)
256+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
257257

258258
result = main.action(
259259
config=pull_request_config(
@@ -300,7 +300,7 @@ def test_action__pull_request__post_comment(
300300
session.register("GET", "/repos/py-cov-action/foobar/issues/2/comments")(json=[])
301301

302302
git.register("git fetch origin main --depth=1000")()
303-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)
303+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
304304

305305
comment = None
306306

@@ -347,7 +347,7 @@ def test_action__push__non_default_branch(
347347
json={"default_branch": "main", "visibility": "public"}
348348
)
349349
git.register("git fetch origin main --depth=1000")(stdout=DIFF_STDOUT)
350-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)
350+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
351351

352352
payload = json.dumps({"coverage": 30.00})
353353
# There is an existing badge in this test, allowing to test the coverage evolution
@@ -436,7 +436,7 @@ def test_action__push__non_default_branch__no_pr(
436436
json={"default_branch": "main", "visibility": "public"}
437437
)
438438
git.register("git fetch origin main --depth=1000")(stdout=DIFF_STDOUT)
439-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)
439+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
440440

441441
payload = json.dumps({"coverage": 30.00})
442442
# There is an existing badge in this test, allowing to test the coverage evolution
@@ -500,7 +500,7 @@ def test_action__pull_request__force_store_comment(
500500
)(text=payload, headers={"content-type": "application/vnd.github.raw+json"})
501501

502502
git.register("git fetch origin main --depth=1000")()
503-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)
503+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
504504

505505
result = main.action(
506506
config=pull_request_config(FORCE_WORKFLOW_RUN=True, GITHUB_OUTPUT=output_file),
@@ -531,7 +531,7 @@ def test_action__pull_request__post_comment__no_marker(
531531
)(status_code=404)
532532

533533
git.register("git fetch origin main --depth=1000")()
534-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)
534+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
535535

536536
result = main.action(
537537
config=pull_request_config(COMMENT_TEMPLATE="""foo"""),
@@ -556,7 +556,7 @@ def test_action__pull_request__annotations(
556556
)(status_code=404)
557557

558558
git.register("git fetch origin main --depth=1000")()
559-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)
559+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
560560

561561
# Who am I
562562
session.register("GET", "/user")(json={"login": "foo"})
@@ -598,7 +598,7 @@ def test_action__pull_request__post_comment__template_error(
598598
)(status_code=404)
599599

600600
git.register("git fetch origin main --depth=1000")()
601-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=DIFF_STDOUT)
601+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=DIFF_STDOUT)
602602

603603
result = main.action(
604604
config=pull_request_config(COMMENT_TEMPLATE="""{%"""),

tests/unit/test_coverage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def test_get_added_lines(git):
309309
"""+++ b/README.md\n@@ -1,2 +1,3 @@\n-# coverage-comment\n+coverage-comment\n"""
310310
)
311311
git.register("git fetch origin main --depth=1000")()
312-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=diff)
312+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=diff)
313313
assert coverage.get_added_lines(git=git, base_ref="main") == {
314314
pathlib.Path("README.md"): [1, 2, 3]
315315
}
@@ -364,7 +364,7 @@ def test_parse_diff_output(git):
364364
rename to coverage_comment/annotations2.py
365365
"""
366366
git.register("git fetch origin main --depth=1000")()
367-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=diff)
367+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=diff)
368368
assert coverage.parse_diff_output(diff=diff) == {
369369
pathlib.Path("README.md"): [1, 3, 4, 5, 6],
370370
pathlib.Path("foo.txt"): [1],
@@ -379,6 +379,6 @@ def test_parse_diff_output__error(git):
379379
index 1f1d9a4..e69de29 100644
380380
"""
381381
git.register("git fetch origin main --depth=1000")()
382-
git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=diff)
382+
git.register("git diff --unified=0 FETCH_HEAD...HEAD")(stdout=diff)
383383
with pytest.raises(ValueError):
384384
coverage.parse_diff_output(diff=diff)

0 commit comments

Comments
 (0)