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

Commit 3f90e14

Browse files
committed
Fix handling files with unicode names or contents
This adds the `git ls-files` which instructs git to output files separated by `\0`, and otherwise output filenames verbatim without any special encoding. The second fix is related to enforcing `utf-8` encoding when reading in file contents in order to produce "file fixes".
1 parent 8364881 commit 3f90e14

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

codecov_cli/helpers/versioning_systems.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,11 @@ def list_relevant_files(
132132
if dir_to_use is None:
133133
raise ValueError("Can't determine root folder")
134134

135-
cmd = ["git", "-C", str(dir_to_use), "ls-files"]
135+
cmd = ["git", "-C", str(dir_to_use), "ls-files", "-z"]
136136
if recurse_submodules:
137137
cmd.append("--recurse-submodules")
138138
res = subprocess.run(cmd, capture_output=True)
139-
140-
return [
141-
(
142-
filename[1:-1]
143-
if filename.startswith('"') and filename.endswith('"')
144-
else filename
145-
)
146-
for filename in res.stdout.decode("unicode_escape").strip().split("\n")
147-
]
139+
return res.stdout.decode().split("\0")
148140

149141

150142
class NoVersioningSystem(VersioningSystemInterface):

codecov_cli/services/upload/upload_collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def _get_file_fixes(
115115
eof = None
116116

117117
try:
118-
with open(filename, "r") as f:
118+
with open(filename, "r", encoding="utf-8") as f:
119119
# If lineno is unset that means that the
120120
# file is empty thus the eof should be 0
121121
# so lineno will be set to -1 here
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// See <https://github.com/codecov/codecov-action/issues/1550>
2+
// Plus, add a bunch of unicode chars in order to trigger
3+
// <https://github.com/codecov/codecov-action/issues/1539>
4+
5+
// µ, ¹², ‘’“”, őá…–🤮🚀¿ 한글 테스트

tests/helpers/test_versioning_systems.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ def test_list_relevant_files_returns_correct_network_files(self, mocker, tmp_pat
105105
"codecov_cli.helpers.versioning_systems.subprocess.run",
106106
return_value=mocked_subprocess,
107107
)
108-
# git ls-files diplays a single \n as \\\\n
109-
mocked_subprocess.stdout = b'a.txt\nb.txt\n"a\\\\nb.txt"\nc.txt\nd.txt\n.circleci/config.yml\nLICENSE\napp/advanced calculations/advanced_calculator.js\n'
108+
mocked_subprocess.stdout = b"a.txt\0b.txt\0a\\nb.txt\0c.txt\0d.txt\0.circleci/config.yml\0LICENSE\0app/advanced calculations/advanced_calculator.js"
110109

111110
vs = GitVersioningSystem()
112111

@@ -138,6 +137,16 @@ def test_list_relevant_files_recurse_submodules(self, mocker, tmp_path):
138137
vs = GitVersioningSystem()
139138
_ = vs.list_relevant_files(tmp_path, recurse_submodules=True)
140139
subproc_run.assert_called_with(
141-
["git", "-C", str(tmp_path), "ls-files", "--recurse-submodules"],
140+
["git", "-C", str(tmp_path), "ls-files", "-z", "--recurse-submodules"],
142141
capture_output=True,
143142
)
143+
144+
145+
def test_exotic_git_filenames():
146+
vs = GitVersioningSystem()
147+
found_repo_files = vs.list_relevant_files()
148+
149+
# See <https://github.com/codecov/codecov-action/issues/1550>
150+
assert (
151+
"tests/data/Контроллеры/Пользователь/ГлавныйКонтроллер.php" in found_repo_files
152+
)

tests/services/upload/test_upload_collector.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ def test_fix_php_files():
8585
assert fixes_for_php_file.fixed_lines_with_reason == set([])
8686

8787

88+
def test_can_read_unicode_file():
89+
col = UploadCollector(None, None, None, None, True)
90+
91+
php_file = Path("tests/data/Контроллеры/Пользователь/ГлавныйКонтроллер.php")
92+
_fixes = col._produce_file_fixes([php_file])
93+
# we just want to assert that this is not throwing an error related to file encoding,
94+
# see <https://github.com/codecov/codecov-action/issues/1539>
95+
96+
8897
def test_fix_for_cpp_swift_vala(tmp_path):
8998
cpp_file = Path("tests/data/files_to_fix_examples/sample.cpp")
9099

0 commit comments

Comments
 (0)