-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_git_operations.py
More file actions
190 lines (155 loc) · 6.35 KB
/
test_git_operations.py
File metadata and controls
190 lines (155 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import unittest
from unittest.mock import patch, MagicMock
import subprocess
from git_py_stats.git_operations import run_git_command, check_git_repository
class TestGitOperations(unittest.TestCase):
"""
Unit test class for testing git_operations.
"""
@patch("subprocess.run")
def test_run_git_command_success(self, mock_subprocess_run):
"""
Test run_git_command with a successful git command.
"""
# Mock the subprocess.run to return a successful result
mock_result = MagicMock()
mock_result.stdout = "git version 2.30.0\n"
mock_result.stderr = ""
mock_result.returncode = 0
mock_subprocess_run.return_value = mock_result
output = run_git_command(["git", "--version"])
self.assertEqual(output, "git version 2.30.0")
mock_subprocess_run.assert_called_once_with(
["git", "--version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
encoding="utf-8",
errors="surrogateescape",
)
@patch("subprocess.run")
def test_run_git_command_failure(self, mock_subprocess_run):
"""
Test run_git_command with a failing git command.
"""
# Mock the subprocess.run to raise a CalledProcessError
mock_subprocess_run.side_effect = subprocess.CalledProcessError(
returncode=1,
cmd=["git", "invalidcommand"],
stderr="git: 'invalidcommand' is not a git command.",
)
output = run_git_command(["git", "invalidcommand"])
self.assertIsNone(output)
mock_subprocess_run.assert_called_once_with(
["git", "invalidcommand"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
encoding="utf-8",
errors="surrogateescape",
)
@patch("subprocess.run")
def test_run_git_command_no_output(self, mock_subprocess_run):
"""
Test run_git_command with a command that produces no output.
"""
# Mock the subprocess.run to return empty stdout
mock_result = MagicMock()
mock_result.stdout = ""
mock_result.stderr = ""
mock_result.returncode = 0
mock_subprocess_run.return_value = mock_result
output = run_git_command(["git", "status"])
self.assertEqual(output, "")
mock_subprocess_run.assert_called_once_with(
["git", "status"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
encoding="utf-8",
errors="surrogateescape",
)
@patch("subprocess.run")
def test_run_git_command_exception(self, mock_subprocess_run):
"""
Test run_git_command when subprocess.run raises an exception.
"""
# Mock the subprocess.run to raise an OSError
mock_subprocess_run.side_effect = OSError("No such file or directory")
output = run_git_command(["git", "status"])
self.assertIsNone(output)
mock_subprocess_run.assert_called_once_with(
["git", "status"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
encoding="utf-8",
errors="surrogateescape",
)
def test_run_git_command_empty_command(self):
"""
Test run_git_command with an empty command list.
"""
output = run_git_command([])
self.assertIsNone(output)
@patch("git_py_stats.git_operations.run_git_command")
def test_check_git_repository_true(self, mock_run_git_command):
"""
Test check_git_repository when inside a git repository.
"""
mock_run_git_command.return_value = "true"
result = check_git_repository()
self.assertTrue(result)
mock_run_git_command.assert_called_once_with(["git", "rev-parse", "--is-inside-work-tree"])
@patch("git_py_stats.git_operations.run_git_command")
def test_check_git_repository_false(self, mock_run_git_command):
"""
Test check_git_repository when not inside a git repository.
"""
mock_run_git_command.return_value = "false"
result = check_git_repository()
self.assertFalse(result)
mock_run_git_command.assert_called_once_with(["git", "rev-parse", "--is-inside-work-tree"])
@patch("git_py_stats.git_operations.run_git_command")
def test_check_git_repository_none(self, mock_run_git_command):
"""
Test check_git_repository when run_git_command returns None.
"""
mock_run_git_command.return_value = None
result = check_git_repository()
self.assertFalse(result)
mock_run_git_command.assert_called_once_with(["git", "rev-parse", "--is-inside-work-tree"])
@patch("git_py_stats.git_operations.run_git_command")
def test_check_git_repository_unexpected_output(self, mock_run_git_command):
"""
Test check_git_repository with unexpected output from run_git_command.
"""
mock_run_git_command.return_value = "unexpected"
result = check_git_repository()
self.assertFalse(result)
mock_run_git_command.assert_called_once_with(["git", "rev-parse", "--is-inside-work-tree"])
@patch("git_py_stats.git_operations.run_git_command")
def test_check_git_repository_error_message(self, mock_run_git_command):
"""
Test that check_git_repository prints error message when not in a git repo.
"""
mock_run_git_command.return_value = "false"
with patch("builtins.print") as mock_print:
result = check_git_repository()
self.assertFalse(result)
mock_print.assert_called_once_with("This script must be run inside a git repository.")
@patch("git_py_stats.git_operations.run_git_command")
def test_check_git_repository_no_output(self, mock_run_git_command):
"""
Test check_git_repository when run_git_command returns empty string.
"""
mock_run_git_command.return_value = ""
result = check_git_repository()
self.assertFalse(result)
mock_run_git_command.assert_called_once_with(["git", "rev-parse", "--is-inside-work-tree"])
if __name__ == "__main__":
unittest.main()