-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_git_client.py
More file actions
109 lines (90 loc) · 3.73 KB
/
Copy pathtest_git_client.py
File metadata and controls
109 lines (90 loc) · 3.73 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
"""
Git 客户端测试
"""
import subprocess
from unittest.mock import patch, MagicMock
from gh_similarity_detector.infrastructure.git_client.client import GitClient
class TestGitClient:
def test_init_default_timeout(self):
client = GitClient()
assert client.timeout == 300
def test_init_custom_timeout(self):
client = GitClient(timeout=60)
assert client.timeout == 60
@patch("subprocess.run")
def test_clone_success_shallow(self, mock_run):
mock_run.return_value = MagicMock(returncode=0, stderr="")
client = GitClient()
result = client.clone("https://github.com/user/repo", "/tmp/repo")
assert result is True
cmd = mock_run.call_args[0][0]
assert "--depth" in cmd
assert "1" in cmd
@patch("subprocess.run")
def test_clone_success_not_shallow(self, mock_run):
mock_run.return_value = MagicMock(returncode=0, stderr="")
client = GitClient()
result = client.clone("https://github.com/user/repo", "/tmp/repo", shallow=False)
assert result is True
cmd = mock_run.call_args[0][0]
assert "--depth" not in cmd
@patch("subprocess.run")
def test_clone_with_branch(self, mock_run):
mock_run.return_value = MagicMock(returncode=0, stderr="")
client = GitClient()
result = client.clone("https://github.com/user/repo", "/tmp/repo", branch="dev")
assert result is True
cmd = mock_run.call_args[0][0]
assert "--branch" in cmd
assert "dev" in cmd
@patch("subprocess.run")
def test_clone_failure(self, mock_run):
mock_run.return_value = MagicMock(returncode=1, stderr="error")
client = GitClient()
result = client.clone("https://github.com/user/repo", "/tmp/repo")
assert result is False
@patch("subprocess.run")
def test_clone_timeout(self, mock_run):
mock_run.side_effect = subprocess.TimeoutExpired(cmd="git", timeout=300)
client = GitClient()
result = client.clone("https://github.com/user/repo", "/tmp/repo")
assert result is False
@patch("subprocess.run")
def test_clone_exception(self, mock_run):
mock_run.side_effect = OSError("no git")
client = GitClient()
result = client.clone("https://github.com/user/repo", "/tmp/repo")
assert result is False
@patch("subprocess.run")
def test_get_first_commit_date_success(self, mock_run):
mock_run.return_value = MagicMock(returncode=0, stdout="2023-01-15 10:30:00 +0800\n")
client = GitClient()
result = client.get_first_commit_date("/tmp/repo")
assert result is not None
assert result.year == 2023
assert result.month == 1
@patch("subprocess.run")
def test_get_first_commit_date_failure(self, mock_run):
mock_run.return_value = MagicMock(returncode=1, stdout="")
client = GitClient()
result = client.get_first_commit_date("/tmp/repo")
assert result is None
@patch("subprocess.run")
def test_get_first_commit_date_exception(self, mock_run):
mock_run.side_effect = Exception("error")
client = GitClient()
result = client.get_first_commit_date("/tmp/repo")
assert result is None
def test_create_temp_repo_dir(self):
import os
path = GitClient.create_temp_repo_dir(prefix="test_")
assert os.path.isdir(path)
os.rmdir(path)
def test_cleanup_repo_dir(self):
import os
import tempfile
path = tempfile.mkdtemp(prefix="test_cleanup_")
GitClient.cleanup_repo_dir(path)
assert not os.path.exists(path)
def test_cleanup_repo_dir_nonexistent(self):
GitClient.cleanup_repo_dir("/nonexistent/path/12345")