Skip to content

Commit a043676

Browse files
committed
Test fixes
1 parent dbc28d9 commit a043676

File tree

5 files changed

+114
-25
lines changed

5 files changed

+114
-25
lines changed

coverage_comment/github.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def get_repository_info(
4444
github: github_client.GitHub, repository: str
4545
) -> RepositoryInfo:
4646
response = github.repos(repository).get()
47+
assert response is not None
4748

4849
return RepositoryInfo(
4950
default_branch=response.default_branch, visibility=response.visibility
@@ -132,6 +133,7 @@ def get_branch_from_workflow_run(
132133
) -> tuple[str, str]:
133134
repo_path = github.repos(repository)
134135
run = repo_path.actions.runs(run_id).get()
136+
assert run is not None
135137
branch = run.head_branch
136138
owner = run.head_repository.owner.login
137139
return owner, branch
@@ -147,9 +149,11 @@ def find_pr_for_branch(
147149
full_branch = f"{owner}:{branch}"
148150

149151
for state in ["open", "all"]:
150-
for pr in github.repos(repository).pulls.get(
152+
prs = github.repos(repository).pulls.get(
151153
state=state, head=full_branch, sort="updated", direction="desc"
152-
):
154+
)
155+
assert prs is not None
156+
for pr in prs:
153157
return pr.number # pyright: ignore
154158

155159
raise CannotDeterminePR(f"No open PR found for branch {branch}")
@@ -165,6 +169,7 @@ def get_my_login(github: github_client.GitHub) -> str:
165169
return GITHUB_ACTIONS_LOGIN
166170

167171
else:
172+
assert response is not None
168173
return response.login
169174

170175

@@ -179,7 +184,9 @@ def post_comment(
179184
issue_comments_path = github.repos(repository).issues(pr_number).comments
180185
comments_path = github.repos(repository).issues.comments
181186

182-
for comment in issue_comments_path.get():
187+
comments = issue_comments_path.get()
188+
assert comments is not None
189+
for comment in comments:
183190
login: str = comment.user.login # pyright: ignore
184191
body: str = comment.body # pyright: ignore
185192
comment_id: int = comment.id # pyright: ignore

coverage_comment/github_client.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __call__(
3838
bytes: Literal[False] = False,
3939
headers: dict[str, str] | None = None,
4040
**kwargs: Any,
41-
) -> JsonObject: ...
41+
) -> JsonObject | None: ...
4242

4343
@overload
4444
def __call__(
@@ -66,10 +66,10 @@ def __call__(
6666
bytes: bool = False,
6767
headers: dict[str, str] | None = None,
6868
**kwargs: Any,
69-
) -> JsonObject | str | bytes:
69+
) -> JsonObject | str | bytes | None:
7070
return self.gh.http(
71-
self.method,
72-
self.path,
71+
method=self.method,
72+
path=self.path,
7373
text=text,
7474
bytes=bytes,
7575
headers=headers,
@@ -85,8 +85,8 @@ class Endpoint:
8585
def __call__(self, *args: Any):
8686
if len(args) == 0:
8787
return self
88-
name = "{}/{}".format(self._name, "/".join([str(arg) for arg in args]))
89-
return Endpoint(self.gh, name)
88+
name = "{}/{}".format(self.name, "/".join([str(arg) for arg in args]))
89+
return Endpoint(gh=self.gh, name=name)
9090

9191
@property
9292
def get(self) -> HttpCall:
@@ -127,21 +127,21 @@ def __getattr__(self, attr: str):
127127
@overload
128128
def http(
129129
self,
130+
*,
130131
method: Method,
131132
path: str,
132-
*,
133133
text: Literal[False] = False,
134134
bytes: Literal[False] = False,
135135
headers: dict[str, str] | None,
136136
**kwargs: Any,
137-
) -> JsonObject: ...
137+
) -> JsonObject | None: ...
138138

139139
@overload
140140
def http(
141141
self,
142+
*,
142143
method: Method,
143144
path: str,
144-
*,
145145
text: Literal[True],
146146
bytes: Literal[False] = False,
147147
headers: dict[str, str] | None,
@@ -151,9 +151,9 @@ def http(
151151
@overload
152152
def http(
153153
self,
154+
*,
154155
method: Method,
155156
path: str,
156-
*,
157157
text: Literal[False] = False,
158158
bytes: Literal[True],
159159
headers: dict[str, str] | None,
@@ -163,25 +163,25 @@ def http(
163163
@overload
164164
def http(
165165
self,
166+
*,
166167
method: Method,
167168
path: str,
168-
*,
169169
text: bool,
170170
bytes: bool,
171171
headers: dict[str, str] | None,
172172
**kwargs: Any,
173-
) -> JsonObject | str | bytes: ...
173+
) -> JsonObject | str | bytes | None: ...
174174

175175
def http(
176176
self,
177+
*,
177178
method: Method,
178179
path: str,
179-
*,
180180
text: bool = False,
181181
bytes: bool = False,
182182
headers: dict[str, str] | None = None,
183183
**kwargs: Any,
184-
) -> JsonObject | str | bytes:
184+
) -> JsonObject | str | bytes | None:
185185
_method = method.lower()
186186
params: dict[str, Any] | None = None
187187
json: dict[str, Any] | None = None
@@ -257,14 +257,22 @@ def response_contents(
257257
*,
258258
text: bool,
259259
bytes: bool,
260-
) -> JsonObject | str | bytes:
260+
) -> JsonObject | str | bytes | None:
261261
if bytes:
262262
return response.content
263263

264264
if text:
265265
return response.text
266266

267-
if not response.headers.get("content-type", "").startswith("application/json"):
267+
if not response.text:
268+
return None
269+
270+
# Assume that missing content type (which should only happen in tests) isn't
271+
# indicative that the content isn't json. If it's not json, then .json() will fail
272+
# later on.
273+
if not response.headers.get("content-type", "application/json").startswith(
274+
"application/json"
275+
):
268276
raise InvalidResponseType(
269277
f"Response is requested as JSON but doesn't have proper content type. "
270278
f"Response: {response.text}"

coverage_comment/template.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,5 @@ def get_file_url(
335335
link += f"#L{lines[0]}-L{lines[1]}"
336336

337337
return link
338-
339338
else:
340-
raise ValueError("Cannot use both pr_number and branch_name")
339+
raise ValueError("Need either pr_number or branch_name")

tests/unit/test_github_client.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,36 @@ def test_github_client__get(session, gh):
1313
assert gh.repos("a/b").issues().get(a=1) == {"foo": "bar"}
1414

1515

16+
def test_github_client__post(session, gh):
17+
session.register("POST", "/repos/a/b/issues", timeout=60, json={"a": 1})(
18+
json={"foo": "bar"}
19+
)
20+
21+
assert gh.repos("a/b").issues().post(a=1) == {"foo": "bar"}
22+
23+
24+
def test_github_client__put(session, gh):
25+
session.register("PUT", "/repos/a/b/issues", timeout=60, json={"a": 1})(
26+
json={"foo": "bar"}
27+
)
28+
29+
assert gh.repos("a/b").issues().put(a=1) == {"foo": "bar"}
30+
31+
32+
def test_github_client__patch(session, gh):
33+
session.register("PATCH", "/repos/a/b/issues", timeout=60, json={"a": 1})(
34+
json={"foo": "bar"}
35+
)
36+
37+
assert gh.repos("a/b").issues().patch(a=1) == {"foo": "bar"}
38+
39+
40+
def test_github_client__delete(session, gh):
41+
session.register("DELETE", "/repos/a/b/issues", timeout=60)(json={"foo": "bar"})
42+
43+
assert gh.repos("a/b").issues().delete() == {"foo": "bar"}
44+
45+
1646
def test_github_client__get_text(session, gh):
1747
session.register("GET", "/repos/a/b/issues", timeout=60, params={"a": 1})(
1848
text="foobar", headers={"content-type": "application/vnd.github.raw+json"}
@@ -88,4 +118,7 @@ def test_github_client__get_error_non_json(session, gh):
88118
with pytest.raises(github_client.ApiError) as exc_info:
89119
gh.repos.get()
90120

91-
assert str(exc_info.value) == "{foobar"
121+
assert str(exc_info.value) == (
122+
"Response is requested as JSON but doesn't have proper content type. "
123+
"Response: {foobar"
124+
)

tests/unit/test_template.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def test_get_comment_markdown(coverage_obj, diff_coverage_obj):
3030
github_host="https://github.com",
3131
repo_name="org/repo",
3232
pr_number=1,
33+
branch_name=None,
3334
base_template="""
3435
{{ previous_coverage_rate | pct }}
3536
{{ coverage.info.percent_covered | pct }}
@@ -70,6 +71,7 @@ def test_template(coverage_obj, diff_coverage_obj):
7071
github_host="https://github.com",
7172
repo_name="org/repo",
7273
pr_number=5,
74+
branch_name=None,
7375
base_template=template.read_template_file("comment.md.j2"),
7476
marker="<!-- foo -->",
7577
subproject_id="foo",
@@ -200,6 +202,7 @@ def test_template_full(make_coverage, make_coverage_and_diff):
200202
github_host="https://github.com",
201203
repo_name="org/repo",
202204
pr_number=12,
205+
branch_name=None,
203206
base_template=template.read_template_file("comment.md.j2"),
204207
)
205208
expected = """## Coverage report
@@ -263,6 +266,7 @@ def test_template__no_previous(coverage_obj_no_branch, diff_coverage_obj):
263266
github_host="https://github.com",
264267
repo_name="org/repo",
265268
pr_number=3,
269+
branch_name=None,
266270
base_template=template.read_template_file("comment.md.j2"),
267271
)
268272
expected = """## Coverage report
@@ -316,6 +320,7 @@ def test_template__max_files(coverage_obj_more_files, diff_coverage_obj_more_fil
316320
github_host="https://github.com",
317321
repo_name="org/repo",
318322
pr_number=5,
323+
branch_name=None,
319324
max_files=1,
320325
base_template=template.read_template_file("comment.md.j2"),
321326
marker="<!-- foo -->",
@@ -348,6 +353,7 @@ def test_template__no_max_files(coverage_obj_more_files, diff_coverage_obj_more_
348353
github_host="https://github.com",
349354
repo_name="org/repo",
350355
pr_number=5,
356+
branch_name=None,
351357
max_files=None,
352358
base_template=template.read_template_file("comment.md.j2"),
353359
marker="<!-- foo -->",
@@ -383,6 +389,7 @@ def test_template__no_files(coverage_obj, diff_coverage_obj_more_files):
383389
github_host="https://github.com",
384390
repo_name="org/repo",
385391
pr_number=5,
392+
branch_name=None,
386393
max_files=25,
387394
base_template=template.read_template_file("comment.md.j2"),
388395
marker="<!-- foo -->",
@@ -422,6 +429,7 @@ def test_template__no_marker(coverage_obj, diff_coverage_obj):
422429
github_host="https://github.com",
423430
repo_name="org/repo",
424431
pr_number=1,
432+
branch_name=None,
425433
base_template=template.read_template_file("comment.md.j2"),
426434
marker="<!-- foo -->",
427435
custom_template="""foo bar""",
@@ -443,6 +451,7 @@ def test_template__broken_template(coverage_obj, diff_coverage_obj):
443451
github_host="https://github.com",
444452
repo_name="org/repo",
445453
pr_number=1,
454+
branch_name=None,
446455
base_template=template.read_template_file("comment.md.j2"),
447456
marker="<!-- foo -->",
448457
custom_template="""{% extends "foo" %}""",
@@ -482,36 +491,69 @@ def test_pluralize(number, singular, plural, expected):
482491

483492

484493
@pytest.mark.parametrize(
485-
"filepath, lines, expected",
494+
"filepath, lines, pr_number, branch_name, expected",
486495
[
487496
(
488497
pathlib.Path("tests/conftest.py"),
489498
None,
499+
33,
500+
None,
490501
"https://github.com/py-cov-action/python-coverage-comment-action/pull/33/files#diff-e52e4ddd58b7ef887ab03c04116e676f6280b824ab7469d5d3080e5cba4f2128",
491502
),
492503
(
493504
pathlib.Path("main.py"),
494505
(12, 15),
506+
33,
507+
None,
495508
"https://github.com/py-cov-action/python-coverage-comment-action/pull/33/files#diff-b10564ab7d2c520cdd0243874879fb0a782862c3c902ab535faabe57d5a505e1R12-R15",
496509
),
497510
(
498511
pathlib.Path("codebase/other.py"),
499512
(22, 22),
513+
33,
514+
None,
500515
"https://github.com/py-cov-action/python-coverage-comment-action/pull/33/files#diff-30cad827f61772ec66bb9ef8887058e6d8443a2afedb331d800feaa60228a26eR22-R22",
501516
),
517+
(
518+
pathlib.Path("tests/conftest.py"),
519+
None,
520+
None,
521+
"mybranch",
522+
"https://github.com/py-cov-action/python-coverage-comment-action/blob/mybranch/tests/conftest.py",
523+
),
524+
(
525+
pathlib.Path("main.py"),
526+
(12, 15),
527+
None,
528+
"mybranch",
529+
"https://github.com/py-cov-action/python-coverage-comment-action/blob/mybranch/main.py#L12-L15",
530+
),
502531
],
503532
)
504-
def test_get_file_url(filepath, lines, expected):
533+
def test_get_file_url(filepath, lines, pr_number, branch_name, expected):
505534
result = template.get_file_url(
506535
filename=filepath,
507536
lines=lines,
508537
github_host="https://github.com",
509538
repo_name="py-cov-action/python-coverage-comment-action",
510-
pr_number=33,
539+
pr_number=pr_number,
540+
branch_name=branch_name,
511541
)
512542
assert result == expected
513543

514544

545+
def test_get_file_url__neither_pr_number_nor_branch_name():
546+
with pytest.raises(Exception):
547+
template.get_file_url(
548+
filename=pathlib.Path("main.py"),
549+
lines=None,
550+
github_host="https://github.com",
551+
repo_name="py-cov-action/python-coverage-comment-action",
552+
pr_number=None,
553+
branch_name=None,
554+
)
555+
556+
515557
def test_uptodate():
516558
assert template.uptodate() is True
517559

0 commit comments

Comments
 (0)