11from __future__ import annotations
22
33import json
4+ import os
45import pathlib
6+ import subprocess
7+ import uuid
58
69import pytest
710
811from coverage_comment import main
912
13+
14+ @pytest .fixture
15+ def in_integration_env (integration_env , integration_dir ):
16+ curdir = os .getcwd ()
17+ os .chdir (integration_dir )
18+ yield integration_dir
19+ os .chdir (curdir )
20+
21+
22+ @pytest .fixture
23+ def integration_dir (tmp_path : pathlib .Path ):
24+ test_dir = tmp_path / "integration_test"
25+ test_dir .mkdir ()
26+ return test_dir
27+
28+
29+ @pytest .fixture
30+ def file_path (integration_dir ):
31+ return integration_dir / "foo.py"
32+
33+
34+ @pytest .fixture
35+ def write_file (file_path ):
36+ def _ (* variables ):
37+ content = "import os"
38+ for i , var in enumerate (variables ):
39+ content += f"""\n if os.environ.get("{ var } "):\n { i } \n """
40+ file_path .write_text (content , encoding = "utf8" )
41+
42+ return _
43+
44+
45+ @pytest .fixture
46+ def run_coverage (file_path , integration_dir ):
47+ def _ (* variables ):
48+ subprocess .check_call (
49+ ["coverage" , "run" , "--parallel" , file_path .name ],
50+ cwd = integration_dir ,
51+ env = os .environ | dict .fromkeys (variables , "1" ),
52+ )
53+
54+ return _
55+
56+
1057DIFF_STDOUT = """diff --git a/foo.py b/foo.py
1158index 6c08c94..b65c612 100644
1259--- a/foo.py
2168"""
2269
2370
71+ @pytest .fixture
72+ def commit (integration_dir ):
73+ def _ ():
74+ subprocess .check_call (
75+ ["git" , "add" , "." ],
76+ cwd = integration_dir ,
77+ )
78+ subprocess .check_call (
79+ ["git" , "commit" , "-m" , str (uuid .uuid4 ())],
80+ cwd = integration_dir ,
81+ env = {
82+ "GIT_AUTHOR_NAME" : "foo" ,
83+ "GIT_AUTHOR_EMAIL" : "foo" ,
84+ "GIT_COMMITTER_NAME" : "foo" ,
85+ "GIT_COMMITTER_EMAIL" : "foo" ,
86+ "GIT_CONFIG_GLOBAL" : "/dev/null" ,
87+ "GIT_CONFIG_SYSTEM" : "/dev/null" ,
88+ },
89+ )
90+
91+ return _
92+
93+
94+ @pytest .fixture
95+ def integration_env (integration_dir , write_file , run_coverage , commit , request ):
96+ subprocess .check_call (["git" , "init" , "-b" , "main" ], cwd = integration_dir )
97+ # diff coverage reads the "origin/{...}" branch so we simulate an origin remote
98+ subprocess .check_call (["git" , "remote" , "add" , "origin" , "." ], cwd = integration_dir )
99+ write_file ("A" , "B" )
100+ commit ()
101+
102+ add_branch_mark = request .node .get_closest_marker ("add_branches" )
103+ for additional_branch in add_branch_mark .args if add_branch_mark else []:
104+ subprocess .check_call (
105+ ["git" , "switch" , "-c" , additional_branch ],
106+ cwd = integration_dir ,
107+ )
108+
109+ subprocess .check_call (
110+ ["git" , "switch" , "-c" , "branch" ],
111+ cwd = integration_dir ,
112+ )
113+
114+ write_file ("A" , "B" , "C" , "D" )
115+ commit ()
116+
117+ run_coverage ("A" , "C" )
118+ subprocess .check_call (["git" , "fetch" , "origin" ], cwd = integration_dir )
119+
120+
24121def test_action__invalid_event_name (session , push_config , in_integration_env , get_logs ):
25122 session .register ("GET" , "/repos/py-cov-action/foobar" )(
26123 json = {"default_branch" : "main" , "visibility" : "public" }
@@ -75,7 +172,7 @@ def checker(payload):
75172 )(status_code = 403 )
76173
77174 git .register ("git fetch origin main --depth=1000" )()
78- git .register ("git diff --unified=0 FETCH_HEAD...HEAD " )(stdout = DIFF_STDOUT )
175+ git .register ("git diff --unified=0 FETCH_HEAD -- . " )(stdout = DIFF_STDOUT )
79176
80177 result = main .action (
81178 config = pull_request_config (
@@ -156,7 +253,7 @@ def checker(payload):
156253 )(status_code = 403 )
157254
158255 git .register ("git fetch origin foo --depth=1000" )(stdout = DIFF_STDOUT )
159- git .register ("git diff --unified=0 FETCH_HEAD...HEAD " )(stdout = DIFF_STDOUT )
256+ git .register ("git diff --unified=0 FETCH_HEAD -- . " )(stdout = DIFF_STDOUT )
160257
161258 result = main .action (
162259 config = pull_request_config (
@@ -203,7 +300,7 @@ def test_action__pull_request__post_comment(
203300 session .register ("GET" , "/repos/py-cov-action/foobar/issues/2/comments" )(json = [])
204301
205302 git .register ("git fetch origin main --depth=1000" )()
206- git .register ("git diff --unified=0 FETCH_HEAD...HEAD " )(stdout = DIFF_STDOUT )
303+ git .register ("git diff --unified=0 FETCH_HEAD -- . " )(stdout = DIFF_STDOUT )
207304
208305 comment = None
209306
@@ -250,7 +347,7 @@ def test_action__push__non_default_branch(
250347 json = {"default_branch" : "main" , "visibility" : "public" }
251348 )
252349 git .register ("git fetch origin main --depth=1000" )(stdout = DIFF_STDOUT )
253- git .register ("git diff --unified=0 FETCH_HEAD...HEAD " )(stdout = DIFF_STDOUT )
350+ git .register ("git diff --unified=0 FETCH_HEAD -- . " )(stdout = DIFF_STDOUT )
254351
255352 payload = json .dumps ({"coverage" : 30.00 })
256353 # There is an existing badge in this test, allowing to test the coverage evolution
@@ -339,7 +436,7 @@ def test_action__push__non_default_branch__no_pr(
339436 json = {"default_branch" : "main" , "visibility" : "public" }
340437 )
341438 git .register ("git fetch origin main --depth=1000" )(stdout = DIFF_STDOUT )
342- git .register ("git diff --unified=0 FETCH_HEAD...HEAD " )(stdout = DIFF_STDOUT )
439+ git .register ("git diff --unified=0 FETCH_HEAD -- . " )(stdout = DIFF_STDOUT )
343440
344441 payload = json .dumps ({"coverage" : 30.00 })
345442 # There is an existing badge in this test, allowing to test the coverage evolution
@@ -403,7 +500,7 @@ def test_action__pull_request__force_store_comment(
403500 )(text = payload , headers = {"content-type" : "application/vnd.github.raw+json" })
404501
405502 git .register ("git fetch origin main --depth=1000" )()
406- git .register ("git diff --unified=0 FETCH_HEAD...HEAD " )(stdout = DIFF_STDOUT )
503+ git .register ("git diff --unified=0 FETCH_HEAD -- . " )(stdout = DIFF_STDOUT )
407504
408505 result = main .action (
409506 config = pull_request_config (FORCE_WORKFLOW_RUN = True , GITHUB_OUTPUT = output_file ),
@@ -434,7 +531,7 @@ def test_action__pull_request__post_comment__no_marker(
434531 )(status_code = 404 )
435532
436533 git .register ("git fetch origin main --depth=1000" )()
437- git .register ("git diff --unified=0 FETCH_HEAD...HEAD " )(stdout = DIFF_STDOUT )
534+ git .register ("git diff --unified=0 FETCH_HEAD -- . " )(stdout = DIFF_STDOUT )
438535
439536 result = main .action (
440537 config = pull_request_config (COMMENT_TEMPLATE = """foo""" ),
@@ -459,7 +556,7 @@ def test_action__pull_request__annotations(
459556 )(status_code = 404 )
460557
461558 git .register ("git fetch origin main --depth=1000" )()
462- git .register ("git diff --unified=0 FETCH_HEAD...HEAD " )(stdout = DIFF_STDOUT )
559+ git .register ("git diff --unified=0 FETCH_HEAD -- . " )(stdout = DIFF_STDOUT )
463560
464561 # Who am I
465562 session .register ("GET" , "/user" )(json = {"login" : "foo" })
@@ -501,7 +598,7 @@ def test_action__pull_request__post_comment__template_error(
501598 )(status_code = 404 )
502599
503600 git .register ("git fetch origin main --depth=1000" )()
504- git .register ("git diff --unified=0 FETCH_HEAD...HEAD " )(stdout = DIFF_STDOUT )
601+ git .register ("git diff --unified=0 FETCH_HEAD -- . " )(stdout = DIFF_STDOUT )
505602
506603 result = main .action (
507604 config = pull_request_config (COMMENT_TEMPLATE = """{%""" ),
0 commit comments