Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 14741f0

Browse files
authored
Slightly improve FileFinder typing (#509)
This fixes a bunch of typing errors, as well as auto-reformats a bunch of stuff via the precommit hook.
1 parent 6269e67 commit 14741f0

File tree

13 files changed

+205
-60
lines changed

13 files changed

+205
-60
lines changed

codecov_cli/helpers/folder_searcher.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import os
33
import pathlib
44
import re
5-
import typing
65
from fnmatch import translate
6+
from typing import Generator, List, Optional, Pattern
77

88

99
def _is_included(
10-
filename_include_regex: typing.Pattern,
11-
multipart_include_regex: typing.Optional[typing.Pattern],
10+
filename_include_regex: Pattern,
11+
multipart_include_regex: Optional[Pattern],
1212
path: pathlib.Path,
1313
):
1414
return filename_include_regex.match(path.name) and (
@@ -18,8 +18,8 @@ def _is_included(
1818

1919

2020
def _is_excluded(
21-
filename_exclude_regex: typing.Optional[typing.Pattern],
22-
multipart_exclude_regex: typing.Optional[typing.Pattern],
21+
filename_exclude_regex: Optional[Pattern],
22+
multipart_exclude_regex: Optional[Pattern],
2323
path: pathlib.Path,
2424
):
2525
return (
@@ -31,14 +31,14 @@ def _is_excluded(
3131

3232
def search_files(
3333
folder_to_search: pathlib.Path,
34-
folders_to_ignore: typing.List[str],
34+
folders_to_ignore: List[str],
3535
*,
36-
filename_include_regex: typing.Pattern,
37-
filename_exclude_regex: typing.Optional[typing.Pattern] = None,
38-
multipart_include_regex: typing.Optional[typing.Pattern] = None,
39-
multipart_exclude_regex: typing.Optional[typing.Pattern] = None,
36+
filename_include_regex: Pattern,
37+
filename_exclude_regex: Optional[Pattern] = None,
38+
multipart_include_regex: Optional[Pattern] = None,
39+
multipart_exclude_regex: Optional[Pattern] = None,
4040
search_for_directories: bool = False,
41-
) -> typing.Generator[pathlib.Path, None, None]:
41+
) -> Generator[pathlib.Path, None, None]:
4242
""" "
4343
Searches for files or directories in a given folder
4444
@@ -85,7 +85,7 @@ def search_files(
8585
yield file_path
8686

8787

88-
def globs_to_regex(patterns: typing.List[str]) -> typing.Optional[typing.Pattern]:
88+
def globs_to_regex(patterns: List[str]) -> Optional[Pattern]:
8989
"""
9090
Converts a list of glob patterns to a combined ORed regex
9191

codecov_cli/services/upload/file_finder.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import os
3-
import typing
43
from pathlib import Path
4+
from typing import Iterable, List, Optional, Pattern
55

66
from codecov_cli.helpers.folder_searcher import globs_to_regex, search_files
77
from codecov_cli.types import UploadCollectionResultFile
@@ -183,9 +183,9 @@
183183
class FileFinder(object):
184184
def __init__(
185185
self,
186-
search_root: Path = None,
187-
folders_to_ignore: typing.List[str] = None,
188-
explicitly_listed_files: typing.List[Path] = None,
186+
search_root: Optional[Path] = None,
187+
folders_to_ignore: Optional[List[str]] = None,
188+
explicitly_listed_files: Optional[List[Path]] = None,
189189
disable_search: bool = False,
190190
report_type: str = "coverage",
191191
):
@@ -195,29 +195,29 @@ def __init__(
195195
self.disable_search = disable_search
196196
self.report_type = report_type
197197

198-
def find_files(self) -> typing.List[UploadCollectionResultFile]:
198+
def find_files(self) -> List[UploadCollectionResultFile]:
199199
if self.report_type == "coverage":
200200
files_excluded_patterns = coverage_files_excluded_patterns
201201
files_patterns = coverage_files_patterns
202202
elif self.report_type == "test_results":
203203
files_excluded_patterns = test_results_files_excluded_patterns
204204
files_patterns = test_results_files_patterns
205205
regex_patterns_to_exclude = globs_to_regex(files_excluded_patterns)
206-
files_paths = []
206+
assert regex_patterns_to_exclude # this is never `None`
207+
files_paths: Iterable[Path] = []
207208
user_files_paths = []
208209
if self.explicitly_listed_files:
209210
user_files_paths = self.get_user_specified_files(regex_patterns_to_exclude)
210211
if not self.disable_search:
211212
regex_patterns_to_include = globs_to_regex(files_patterns)
213+
assert regex_patterns_to_include # this is never `None`
212214
files_paths = search_files(
213215
self.search_root,
214216
default_folders_to_ignore + self.folders_to_ignore,
215217
filename_include_regex=regex_patterns_to_include,
216218
filename_exclude_regex=regex_patterns_to_exclude,
217219
)
218-
result_files = [
219-
UploadCollectionResultFile(path) for path in files_paths if files_paths
220-
]
220+
result_files = [UploadCollectionResultFile(path) for path in files_paths]
221221
user_result_files = [
222222
UploadCollectionResultFile(path)
223223
for path in user_files_paths
@@ -226,7 +226,7 @@ def find_files(self) -> typing.List[UploadCollectionResultFile]:
226226

227227
return list(set(result_files + user_result_files))
228228

229-
def get_user_specified_files(self, regex_patterns_to_exclude):
229+
def get_user_specified_files(self, regex_patterns_to_exclude: Pattern):
230230
user_filenames_to_include = []
231231
files_excluded_but_user_includes = []
232232
for file in self.explicitly_listed_files:

tests/ci_adapters/test_azure_pipelines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_detect(self, env_dict, expected, mocker):
4747
(
4848
{
4949
AzurePipelinesEnvEnum.BUILD_SOURCEVERSION: "123456789000111",
50-
AzurePipelinesEnvEnum.SYSTEM_PULLREQUEST_SOURCECOMMITID: "111000987654321"
50+
AzurePipelinesEnvEnum.SYSTEM_PULLREQUEST_SOURCECOMMITID: "111000987654321",
5151
},
5252
"111000987654321",
5353
),

tests/commands/test_process_test_results.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,22 @@ def test_process_test_results(
4444

4545
assert result.exit_code == 0
4646

47-
4847
mocked_post.assert_called_with(
4948
url="https://api.github.com/repos/fake/repo/issues/pull/comments",
5049
data={
5150
"body": "### :x: Failed Test Results: \nCompleted 4 tests with **`1 failed`**, 3 passed and 0 skipped.\n<details><summary>View the full list of failed tests</summary>\n\n| **Test Description** | **Failure message** |\n| :-- | :-- |\n| <pre>Testsuite:<br>api.temp.calculator.test_calculator::test_divide<br><br>Test name:<br>pytest<br></pre> | <pre>def<br> test_divide():<br> &amp;gt; assert Calculator.divide(1, 2) == 0.5<br> E assert 1.0 == 0.5<br> E + where 1.0 = &amp;lt;function Calculator.divide at 0x104c9eb90&amp;gt;(1, 2)<br> E + where &amp;lt;function Calculator.divide at 0x104c9eb90&amp;gt; = Calculator.divide<br> .../temp/calculator/test_calculator.py:30: AssertionError</pre> |",
52-
"cli_args": {'auto_load_params_from': None, 'codecov_yml_path': None, 'enterprise_url': None, 'verbose': False, 'version': 'cli-0.7.4', 'command': 'process-test-results', 'provider_token': 'whatever', 'disable_search': True, 'dir': os.getcwd(), 'exclude_folders': ()},
51+
"cli_args": {
52+
"auto_load_params_from": None,
53+
"codecov_yml_path": None,
54+
"enterprise_url": None,
55+
"verbose": False,
56+
"version": "cli-0.7.4",
57+
"command": "process-test-results",
58+
"provider_token": "whatever",
59+
"disable_search": True,
60+
"dir": os.getcwd(),
61+
"exclude_folders": (),
62+
},
5363
},
5464
headers={
5565
"Accept": "application/vnd.github+json",
@@ -59,7 +69,6 @@ def test_process_test_results(
5969
)
6070

6171

62-
6372
def test_process_test_results_non_existent_file(mocker, tmpdir):
6473
tmp_file = tmpdir.mkdir("folder").join("summary.txt")
6574

@@ -94,7 +103,7 @@ def test_process_test_results_non_existent_file(mocker, tmpdir):
94103
assert result.exit_code == 1
95104
expected_logs = [
96105
"ci service found",
97-
'Some files were not found',
106+
"Some files were not found",
98107
]
99108
for log in expected_logs:
100109
assert log in result.output
@@ -183,7 +192,6 @@ def test_process_test_results_missing_ref(mocker, tmpdir):
183192
assert log in result.output
184193

185194

186-
187195
def test_process_test_results_missing_step_summary(mocker, tmpdir):
188196
tmp_file = tmpdir.mkdir("folder").join("summary.txt")
189197

tests/helpers/test_git.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,3 @@ def test_get_git_service_class():
133133
assert isinstance(git.get_git_service("github"), Github)
134134
assert git.get_git_service("gitlab") == None
135135
assert git.get_git_service("bitbucket") == None
136-
137-

tests/helpers/test_network_finder.py

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,115 @@ def test_find_files(mocker, tmp_path):
1212
mocked_vs = MagicMock()
1313
mocked_vs.list_relevant_files.return_value = filenames
1414

15-
assert NetworkFinder(versioning_system=mocked_vs, network_filter=None, network_prefix=None, network_root_folder=tmp_path).find_files() == filenames
16-
assert NetworkFinder(versioning_system=mocked_vs, network_filter="hello", network_prefix="bello", network_root_folder=tmp_path).find_files(False) == filtered_filenames
17-
assert NetworkFinder(versioning_system=mocked_vs, network_filter="hello", network_prefix="bello", network_root_folder=tmp_path).find_files(True) == filenames
15+
assert (
16+
NetworkFinder(
17+
versioning_system=mocked_vs,
18+
network_filter=None,
19+
network_prefix=None,
20+
network_root_folder=tmp_path,
21+
).find_files()
22+
== filenames
23+
)
24+
assert (
25+
NetworkFinder(
26+
versioning_system=mocked_vs,
27+
network_filter="hello",
28+
network_prefix="bello",
29+
network_root_folder=tmp_path,
30+
).find_files(False)
31+
== filtered_filenames
32+
)
33+
assert (
34+
NetworkFinder(
35+
versioning_system=mocked_vs,
36+
network_filter="hello",
37+
network_prefix="bello",
38+
network_root_folder=tmp_path,
39+
).find_files(True)
40+
== filenames
41+
)
1842
mocked_vs.list_relevant_files.assert_called_with(tmp_path)
1943

44+
2045
def test_find_files_with_filter(mocker, tmp_path):
2146
filenames = ["hello/a.txt", "hello/c.txt", "bello/b.txt"]
2247
filtered_filenames = ["hello/a.txt", "hello/c.txt"]
2348

2449
mocked_vs = MagicMock()
2550
mocked_vs.list_relevant_files.return_value = filenames
2651

27-
assert NetworkFinder(versioning_system=mocked_vs, network_filter="hello", network_prefix=None, network_root_folder=tmp_path).find_files() == filtered_filenames
28-
assert NetworkFinder(versioning_system=mocked_vs, network_filter="hello", network_prefix="bello", network_root_folder=tmp_path).find_files(True) == filenames
52+
assert (
53+
NetworkFinder(
54+
versioning_system=mocked_vs,
55+
network_filter="hello",
56+
network_prefix=None,
57+
network_root_folder=tmp_path,
58+
).find_files()
59+
== filtered_filenames
60+
)
61+
assert (
62+
NetworkFinder(
63+
versioning_system=mocked_vs,
64+
network_filter="hello",
65+
network_prefix="bello",
66+
network_root_folder=tmp_path,
67+
).find_files(True)
68+
== filenames
69+
)
2970
mocked_vs.list_relevant_files.assert_called_with(tmp_path)
3071

72+
3173
def test_find_files_with_prefix(mocker, tmp_path):
3274
filenames = ["hello/a.txt", "hello/c.txt", "bello/b.txt"]
3375
filtered_filenames = ["hellohello/a.txt", "hellohello/c.txt", "hellobello/b.txt"]
3476

3577
mocked_vs = MagicMock()
3678
mocked_vs.list_relevant_files.return_value = filenames
3779

38-
assert NetworkFinder(versioning_system=mocked_vs, network_filter=None, network_prefix="hello", network_root_folder=tmp_path).find_files() == filtered_filenames
39-
assert NetworkFinder(versioning_system=mocked_vs, network_filter="hello", network_prefix="bello", network_root_folder=tmp_path).find_files(True) == filenames
80+
assert (
81+
NetworkFinder(
82+
versioning_system=mocked_vs,
83+
network_filter=None,
84+
network_prefix="hello",
85+
network_root_folder=tmp_path,
86+
).find_files()
87+
== filtered_filenames
88+
)
89+
assert (
90+
NetworkFinder(
91+
versioning_system=mocked_vs,
92+
network_filter="hello",
93+
network_prefix="bello",
94+
network_root_folder=tmp_path,
95+
).find_files(True)
96+
== filenames
97+
)
4098
mocked_vs.list_relevant_files.assert_called_with(tmp_path)
4199

100+
42101
def test_find_files_with_filter_and_prefix(mocker, tmp_path):
43102
filenames = ["hello/a.txt", "hello/c.txt", "bello/b.txt"]
44103
filtered_filenames = ["bellohello/a.txt", "bellohello/c.txt"]
45104

46105
mocked_vs = MagicMock()
47106
mocked_vs.list_relevant_files.return_value = filenames
48107

49-
assert NetworkFinder(versioning_system=mocked_vs, network_filter="hello", network_prefix="bello", network_root_folder=tmp_path).find_files() == filtered_filenames
50-
assert NetworkFinder(versioning_system=mocked_vs, network_filter="hello", network_prefix="bello", network_root_folder=tmp_path).find_files(True) == filenames
108+
assert (
109+
NetworkFinder(
110+
versioning_system=mocked_vs,
111+
network_filter="hello",
112+
network_prefix="bello",
113+
network_root_folder=tmp_path,
114+
).find_files()
115+
== filtered_filenames
116+
)
117+
assert (
118+
NetworkFinder(
119+
versioning_system=mocked_vs,
120+
network_filter="hello",
121+
network_prefix="bello",
122+
network_root_folder=tmp_path,
123+
).find_files(True)
124+
== filenames
125+
)
51126
mocked_vs.list_relevant_files.assert_called_with(tmp_path)

tests/helpers/test_request.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ def test_log_result_without_token(mocker):
6060
error=None,
6161
warnings=[],
6262
status_code=201,
63-
text="{\"message\":\"commit\",\"timestamp\":\"2024-03-25T15:41:07Z\",\"ci_passed\":true,\"state\":\"complete\",\"repository\":{\"name\":\"repo\",\"is_private\":false,\"active\":true,\"language\":\"python\",\"yaml\":null},\"author\":{\"avatar_url\":\"https://example.com\",\"service\":\"github\",\"username\":null,\"name\":\"dependabot[bot]\",\"ownerid\":2780265},\"commitid\":\"commit\",\"parent_commit_id\":\"parent\",\"pullid\":1,\"branch\":\"main\"}"
63+
text='{"message":"commit","timestamp":"2024-03-25T15:41:07Z","ci_passed":true,"state":"complete","repository":{"name":"repo","is_private":false,"active":true,"language":"python","yaml":null},"author":{"avatar_url":"https://example.com","service":"github","username":null,"name":"dependabot[bot]","ownerid":2780265},"commitid":"commit","parent_commit_id":"parent","pullid":1,"branch":"main"}',
6464
)
6565
log_warnings_and_errors_if_any(result, "Commit creating", False)
66-
mock_log_debug.assert_called_with('Commit creating result', extra={'extra_log_attributes': {'result': result}})
66+
mock_log_debug.assert_called_with(
67+
"Commit creating result", extra={"extra_log_attributes": {"result": result}}
68+
)
6769

6870

6971
def test_log_result_with_token(mocker):
@@ -72,18 +74,20 @@ def test_log_result_with_token(mocker):
7274
error=None,
7375
warnings=[],
7476
status_code=201,
75-
text="{\"message\": \"commit\", \"timestamp\": \"2024-07-16T20:51:07Z\", \"ci_passed\": true, \"state\": \"complete\", \"repository\": {\"name\": \"repo\", \"is_private\": false, \"active\": true, \"language\": \"python\", \"yaml\": {\"codecov\": {\"token\": \"faketoken\"}}, \"author\": {\"avatar_url\": \"https://example.com\", \"service\": \"github\", \"username\": \"author\", \"name\": \"author\", \"ownerid\": 3461769}, \"commitid\": \"commit\", \"parent_commit_id\": \"parent_commit\", \"pullid\": null, \"branch\": \"main\"}}"
77+
text='{"message": "commit", "timestamp": "2024-07-16T20:51:07Z", "ci_passed": true, "state": "complete", "repository": {"name": "repo", "is_private": false, "active": true, "language": "python", "yaml": {"codecov": {"token": "faketoken"}}, "author": {"avatar_url": "https://example.com", "service": "github", "username": "author", "name": "author", "ownerid": 3461769}, "commitid": "commit", "parent_commit_id": "parent_commit", "pullid": null, "branch": "main"}}',
7678
)
7779

78-
expected_text = "{\"message\": \"commit\", \"timestamp\": \"2024-07-16T20:51:07Z\", \"ci_passed\": true, \"state\": \"complete\", \"repository\": {\"name\": \"repo\", \"is_private\": false, \"active\": true, \"language\": \"python\", \"yaml\": {\"codecov\": {\"token\": \"f******************\"}}, \"author\": {\"avatar_url\": \"https://example.com\", \"service\": \"github\", \"username\": \"author\", \"name\": \"author\", \"ownerid\": 3461769}, \"commitid\": \"commit\", \"parent_commit_id\": \"parent_commit\", \"pullid\": null, \"branch\": \"main\"}}"
80+
expected_text = '{"message": "commit", "timestamp": "2024-07-16T20:51:07Z", "ci_passed": true, "state": "complete", "repository": {"name": "repo", "is_private": false, "active": true, "language": "python", "yaml": {"codecov": {"token": "f******************"}}, "author": {"avatar_url": "https://example.com", "service": "github", "username": "author", "name": "author", "ownerid": 3461769}, "commitid": "commit", "parent_commit_id": "parent_commit", "pullid": null, "branch": "main"}}'
7981
expected = RequestResult(
8082
error=None,
8183
warnings=[],
8284
status_code=201,
8385
text=expected_text,
8486
)
8587
log_warnings_and_errors_if_any(result, "Commit creating", False)
86-
mock_log_debug.assert_called_with('Commit creating result', extra={'extra_log_attributes': {'result': expected}})
88+
mock_log_debug.assert_called_with(
89+
"Commit creating result", extra={"extra_log_attributes": {"result": expected}}
90+
)
8791

8892

8993
def test_get_token_header_or_fail():
@@ -102,6 +106,7 @@ def test_get_token_header_or_fail():
102106
== "Codecov token not found. Please provide Codecov token with -t flag."
103107
)
104108

109+
105110
def test_get_token_header():
106111
# Test with a valid UUID token
107112
token = uuid.uuid4()

tests/helpers/test_upload_sender.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,14 @@ def test_upload_sender_result_fail_post_400(
301301

302302
assert sender.warnings is not None
303303

304-
305304
@pytest.mark.parametrize("error_code", [500, 502])
306305
def test_upload_sender_result_fail_post_500s(
307-
self, mocker, mocked_responses, mocked_legacy_upload_endpoint, capsys, error_code
306+
self,
307+
mocker,
308+
mocked_responses,
309+
mocked_legacy_upload_endpoint,
310+
capsys,
311+
error_code,
308312
):
309313
mocker.patch("codecov_cli.helpers.request.sleep")
310314
mocked_legacy_upload_endpoint.status = error_code

0 commit comments

Comments
 (0)