Skip to content

Commit 8056391

Browse files
committed
refactor: switch to surrogateescape and update tests per review feedback
1 parent 8c013fa commit 8056391

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

git_py_stats/git_operations.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ def run_git_command(cmd: List[str]) -> Optional[str]:
2020
print("Error: Command list is empty!")
2121
return None
2222
try:
23+
# We enforce utf-8 encoding and use surrogateescape to handle non-ascii characters
24+
# that may be present in git history (e.g. in author names or commit messages)
25+
# as recommended by PEP 383 for system interfaces.
2326
result = subprocess.run(
2427
cmd,
2528
stdout=subprocess.PIPE,
2629
stderr=subprocess.PIPE,
2730
text=True,
2831
check=True,
2932
encoding="utf-8",
30-
errors="replace",
33+
errors="surrogateescape",
3134
)
3235
return result.stdout.strip()
3336
except subprocess.CalledProcessError as e:

git_py_stats/tests/test_git_operations.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def test_run_git_command_success(self, mock_subprocess_run):
3131
stderr=subprocess.PIPE,
3232
text=True,
3333
check=True,
34+
encoding="utf-8",
35+
errors="surrogateescape",
3436
)
3537

3638
@patch("subprocess.run")
@@ -54,6 +56,8 @@ def test_run_git_command_failure(self, mock_subprocess_run):
5456
stderr=subprocess.PIPE,
5557
text=True,
5658
check=True,
59+
encoding="utf-8",
60+
errors="surrogateescape",
5761
)
5862

5963
@patch("subprocess.run")
@@ -72,7 +76,13 @@ def test_run_git_command_no_output(self, mock_subprocess_run):
7276
self.assertEqual(output, "")
7377

7478
mock_subprocess_run.assert_called_once_with(
75-
["git", "status"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True
79+
["git", "status"],
80+
stdout=subprocess.PIPE,
81+
stderr=subprocess.PIPE,
82+
text=True,
83+
check=True,
84+
encoding="utf-8",
85+
errors="surrogateescape",
7686
)
7787

7888
@patch("subprocess.run")
@@ -87,7 +97,13 @@ def test_run_git_command_exception(self, mock_subprocess_run):
8797
self.assertIsNone(output)
8898

8999
mock_subprocess_run.assert_called_once_with(
90-
["git", "status"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True
100+
["git", "status"],
101+
stdout=subprocess.PIPE,
102+
stderr=subprocess.PIPE,
103+
text=True,
104+
check=True,
105+
encoding="utf-8",
106+
errors="surrogateescape",
91107
)
92108

93109
def test_run_git_command_empty_command(self):

0 commit comments

Comments
 (0)