Skip to content

Commit 9838111

Browse files
committed
test: fix tests to work with new encoding-safe subprocess calls
- Update all mock return values to use bytes instead of strings - Remove text=True from subprocess.run assertions - Tests now match the new behavior that handles non-UTF-8 encodings - All 133 tests passing with 72.67% coverage
1 parent abe22a6 commit 9838111

File tree

3 files changed

+40
-41
lines changed

3 files changed

+40
-41
lines changed

tests/test_git/test_commits.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_create_commit_success(mock_run, git_operations):
2121
# git diff --cached --quiet
2222
MagicMock(returncode=1), # Non-zero means there are staged changes
2323
# git commit
24-
MagicMock(returncode=0, stdout="", stderr=""),
24+
MagicMock(returncode=0, stdout=b"", stderr=b""),
2525
]
2626

2727
result = git_operations.create_commit(title="test: add new feature", message="Detailed commit message")
@@ -37,7 +37,6 @@ def test_create_commit_success(mock_run, git_operations):
3737
"Detailed commit message",
3838
],
3939
capture_output=True,
40-
text=True,
4140
check=True,
4241
)
4342

@@ -66,7 +65,7 @@ def test_create_commit_with_warning(mock_logger, mock_run, git_operations):
6665
# git diff --cached --quiet
6766
MagicMock(returncode=1), # Non-zero means there are staged changes
6867
# git commit
69-
MagicMock(returncode=0, stderr="warning: CRLF will be replaced by LF", stdout=""),
68+
MagicMock(returncode=0, stderr=b"warning: CRLF will be replaced by LF", stdout=b""),
7069
]
7170

7271
result = git_operations.create_commit(title="test", message="message")

tests/test_git/test_files.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def test_get_diff_text_files(mock_run, git_operations, mock_git_file):
1919
"""Test getting diff for text files."""
2020
mock_diff = "diff --git a/file1.py b/file1.py\n+new line"
2121
mock_run.return_value = MagicMock(
22-
stdout=mock_diff,
23-
stderr="",
22+
stdout=mock_diff.encode('utf-8'),
23+
stderr=b"",
2424
returncode=0,
2525
)
2626

@@ -33,8 +33,8 @@ def test_get_diff_text_files(mock_run, git_operations, mock_git_file):
3333
def test_get_diff_binary_files(mock_run, git_operations, mock_git_file):
3434
"""Test getting diff for binary files."""
3535
mock_run.return_value = MagicMock(
36-
stdout="Binary files a/image.png and b/image.png differ",
37-
stderr="",
36+
stdout=b"Binary files a/image.png and b/image.png differ",
37+
stderr=b"",
3838
returncode=0,
3939
)
4040

@@ -50,7 +50,7 @@ def test_reset_staged_changes_success(mock_run, git_operations):
5050

5151
git_operations.reset_staged_changes()
5252

53-
mock_run.assert_called_with(["git", "reset"], capture_output=True, text=True, check=True)
53+
mock_run.assert_called_with(["git", "reset"], capture_output=True, check=True)
5454

5555

5656
@patch("subprocess.run")
@@ -70,8 +70,8 @@ def test_stage_files_with_warning(mock_logger, mock_run, git_operations):
7070
"""Test handling of git warnings during staging."""
7171
mock_run.return_value = MagicMock(
7272
returncode=0,
73-
stderr="warning: LF will be replaced by CRLF in file1.py",
74-
stdout="",
73+
stderr=b"warning: LF will be replaced by CRLF in file1.py",
74+
stdout=b"",
7575
)
7676

7777
git_operations.stage_files(["file1.py"])
@@ -90,8 +90,8 @@ def test_stage_files_with_info(mock_logger, mock_run, git_operations):
9090
"""Test handling of git info messages during staging."""
9191
mock_run.return_value = MagicMock(
9292
returncode=0,
93-
stderr="Updating index",
94-
stdout="",
93+
stderr=b"Updating index",
94+
stdout=b"",
9595
)
9696

9797
git_operations.stage_files(["file1.py"])

tests/test_git/test_operations.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def test_should_ignore_file(git_operations):
2424
def test_get_staged_files_success(mock_run, git_operations, mock_git_file):
2525
"""Test successful retrieval of staged files."""
2626
mock_run.return_value = MagicMock(
27-
stdout=" M file1.py\nM file2.py\n",
28-
stderr="",
27+
stdout=b" M file1.py\nM file2.py\n",
28+
stderr=b"",
2929
returncode=0,
3030
)
3131

@@ -41,7 +41,7 @@ def test_get_staged_files_success(mock_run, git_operations, mock_git_file):
4141
@patch("subprocess.run")
4242
def test_get_staged_files_empty(mock_run, git_operations):
4343
"""Test when no files are staged."""
44-
mock_run.return_value = MagicMock(stdout="", stderr="", returncode=0)
44+
mock_run.return_value = MagicMock(stdout=b"", stderr=b"", returncode=0)
4545

4646
files = git_operations.get_staged_files()
4747

@@ -63,8 +63,8 @@ def test_get_staged_files_error(mock_run, git_operations):
6363
def test_get_staged_files_with_renames(mock_run, git_operations):
6464
"""Test handling of renamed files."""
6565
mock_run.return_value = MagicMock(
66-
stdout='R "old file.py" -> "new file.py"\n',
67-
stderr="",
66+
stdout=b'R "old file.py" -> "new file.py"\n',
67+
stderr=b"",
6868
returncode=0,
6969
)
7070

@@ -80,8 +80,8 @@ def test_get_staged_files_with_renames(mock_run, git_operations):
8080
def test_get_staged_files_ignores_untracked(mock_run, git_operations):
8181
"""Test that untracked files are ignored."""
8282
mock_run.return_value = MagicMock(
83-
stdout="?? new.py\n M tracked.py\n",
84-
stderr="",
83+
stdout=b"?? new.py\n M tracked.py\n",
84+
stderr=b"",
8585
returncode=0,
8686
)
8787

@@ -94,9 +94,9 @@ def test_get_staged_files_ignores_untracked(mock_run, git_operations):
9494

9595
def test_get_staged_files_with_spaces(git_operations):
9696
"""Test getting staged files with spaces in paths."""
97-
mock_output = "M path with spaces/file.py\nA another path/with spaces.py\n"
97+
mock_output = b"M path with spaces/file.py\nA another path/with spaces.py\n"
9898
with patch("subprocess.run") as mock_run:
99-
mock_run.return_value = MagicMock(stdout=mock_output, stderr="", returncode=0)
99+
mock_run.return_value = MagicMock(stdout=mock_output, stderr=b"", returncode=0)
100100
files = git_operations.get_staged_files()
101101

102102
assert len(files) == 2
@@ -106,9 +106,9 @@ def test_get_staged_files_with_spaces(git_operations):
106106

107107
def test_get_staged_files_with_special_chars(git_operations):
108108
"""Test getting staged files with special characters."""
109-
mock_output = "M path/with-dashes.py\nA path/with_underscores.py\nM path/with.dots.py\n"
109+
mock_output = b"M path/with-dashes.py\nA path/with_underscores.py\nM path/with.dots.py\n"
110110
with patch("subprocess.run") as mock_run:
111-
mock_run.return_value = MagicMock(stdout=mock_output, stderr="", returncode=0)
111+
mock_run.return_value = MagicMock(stdout=mock_output, stderr=b"", returncode=0)
112112
files = git_operations.get_staged_files()
113113

114114
assert len(files) == 3
@@ -119,9 +119,9 @@ def test_get_staged_files_with_special_chars(git_operations):
119119

120120
def test_get_staged_files_with_unicode(git_operations):
121121
"""Test getting staged files with unicode characters."""
122-
mock_output = "M path/with/émoji/🚀.py\nA path/with/áccents/file.py\n"
122+
mock_output = "M path/with/émoji/🚀.py\nA path/with/áccents/file.py\n".encode('utf-8')
123123
with patch("subprocess.run") as mock_run:
124-
mock_run.return_value = MagicMock(stdout=mock_output, stderr="", returncode=0)
124+
mock_run.return_value = MagicMock(stdout=mock_output, stderr=b"", returncode=0)
125125
files = git_operations.get_staged_files()
126126

127127
assert len(files) == 2
@@ -131,8 +131,8 @@ def test_get_staged_files_with_unicode(git_operations):
131131

132132
def test_get_staged_files_with_warnings(git_operations):
133133
"""Test getting staged files with git warnings."""
134-
mock_output = "M file.py\n"
135-
mock_warning = "warning: CRLF will be replaced by LF in file.py"
134+
mock_output = b"M file.py\n"
135+
mock_warning = b"warning: CRLF will be replaced by LF in file.py"
136136
with patch("subprocess.run") as mock_run:
137137
mock_run.return_value = MagicMock(stdout=mock_output, stderr=mock_warning, returncode=0)
138138
files = git_operations.get_staged_files()
@@ -143,19 +143,19 @@ def test_get_staged_files_with_warnings(git_operations):
143143

144144
def test_get_staged_files_with_binary_detection(git_operations):
145145
"""Test getting staged files with binary file detection."""
146-
mock_output = "M text.py\nM image.png\n"
146+
mock_output = b"M text.py\nM image.png\n"
147147

148148
def mock_run_side_effect(*args, **kwargs):
149149
if args[0][0] == "git" and args[0][1] == "status":
150-
return MagicMock(stdout=mock_output, stderr="", returncode=0)
150+
return MagicMock(stdout=mock_output, stderr=b"", returncode=0)
151151
elif args[0][0] == "git" and args[0][1] == "diff":
152152
# Return binary file indicator for image.png
153153
if "image.png" in args[0]:
154-
return MagicMock(stdout="-\t-\timage.png\n", stderr="", returncode=0)
155-
return MagicMock(stdout="1\t1\ttext.py\n", stderr="", returncode=0)
154+
return MagicMock(stdout=b"-\t-\timage.png\n", stderr=b"", returncode=0)
155+
return MagicMock(stdout=b"1\t1\ttext.py\n", stderr=b"", returncode=0)
156156
elif args[0][0] == "git" and args[0][1] == "hash-object":
157-
return MagicMock(stdout="abc123\n", stderr="", returncode=0)
158-
return MagicMock(stdout="", stderr="", returncode=0)
157+
return MagicMock(stdout=b"abc123\n", stderr=b"", returncode=0)
158+
return MagicMock(stdout=b"", stderr=b"", returncode=0)
159159

160160
with patch("subprocess.run", side_effect=mock_run_side_effect):
161161
with patch("os.path.exists", return_value=True):
@@ -172,12 +172,12 @@ def mock_run_side_effect(*args, **kwargs):
172172
def test_get_staged_files_with_complex_renames(git_operations):
173173
"""Test getting staged files with complex rename scenarios."""
174174
mock_output = (
175-
"R old_name.py -> new_name.py\n"
176-
"R old/path/file.py -> new/path/file.py\n"
177-
"R100 renamed_completely.py -> totally_different.py\n"
175+
b"R old_name.py -> new_name.py\n"
176+
b"R old/path/file.py -> new/path/file.py\n"
177+
b"R100 renamed_completely.py -> totally_different.py\n"
178178
)
179179
with patch("subprocess.run") as mock_run:
180-
mock_run.return_value = MagicMock(stdout=mock_output, stderr="", returncode=0)
180+
mock_run.return_value = MagicMock(stdout=mock_output, stderr=b"", returncode=0)
181181
files = git_operations.get_staged_files()
182182

183183
assert len(files) == 3
@@ -189,9 +189,9 @@ def test_get_staged_files_with_complex_renames(git_operations):
189189

190190
def test_get_staged_files_with_submodules(git_operations):
191191
"""Test getting staged files with submodule changes."""
192-
mock_output = "M regular_file.py\nM submodule\n" # Submodule change
192+
mock_output = b"M regular_file.py\nM submodule\n" # Submodule change
193193
with patch("subprocess.run") as mock_run:
194-
mock_run.return_value = MagicMock(stdout=mock_output, stderr="", returncode=0)
194+
mock_run.return_value = MagicMock(stdout=mock_output, stderr=b"", returncode=0)
195195
files = git_operations.get_staged_files()
196196

197197
assert len(files) == 2
@@ -201,9 +201,9 @@ def test_get_staged_files_with_submodules(git_operations):
201201

202202
def test_get_staged_files_with_permission_changes(git_operations):
203203
"""Test getting staged files with permission changes."""
204-
mock_output = "M file_with_chmod.sh\n"
204+
mock_output = b"M file_with_chmod.sh\n"
205205
with patch("subprocess.run") as mock_run:
206-
mock_run.return_value = MagicMock(stdout=mock_output, stderr="", returncode=0)
206+
mock_run.return_value = MagicMock(stdout=mock_output, stderr=b"", returncode=0)
207207
files = git_operations.get_staged_files()
208208

209209
assert len(files) == 1

0 commit comments

Comments
 (0)