refactor: extract focused clients from GiteaClient (Slices 1-6)
- Create gitea/issues_client.py with IssuesClient class (9 methods) - Create gitea/prs_client.py with PullRequestsClient class (17 methods) - Create gitea/files_client.py with FilesClient class (4 methods) - Create gitea/notifications_client.py with NotificationsClient class (2 methods) - Create gitea/repos_client.py with ReposClient class (2 methods) - Create gitea/__init__.py to export all client classes - Remove delegation methods from GiteaClient (now ~70 lines) - Update all callers to use sub-clients (client.issues, client.prs, etc.) - Update test files to mock sub-client attributes GiteaClient is now a facade that provides access to focused sub-clients: - repos: Repository operations (ReposClient) - issues: Issue operations (IssuesClient) - prs: Pull request operations (PullRequestsClient) - files: File and git ref operations (FilesClient) - notifications: Notification operations (NotificationsClient) Refs: #godclass-refactor
This commit is contained in:
+30
-25
@@ -9,25 +9,25 @@ from gitea.workspace import WorkspaceManager
|
||||
@patch("gitea.workspace.subprocess.run")
|
||||
@patch("gitea.workspace.GiteaClient")
|
||||
def test_workspace_manager_configure_repo_user(
|
||||
mock_client_class: MagicMock,
|
||||
mock_run: MagicMock
|
||||
mock_client_class: MagicMock, mock_run: MagicMock
|
||||
) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.repos = MagicMock()
|
||||
mock_user = MagicMock()
|
||||
mock_user.full_name = "Agent Tester"
|
||||
mock_user.login = "agent-test"
|
||||
mock_user.email = "agent-test@example.com"
|
||||
mock_client.get_authenticated_user.return_value = mock_user
|
||||
mock_client.repos.get_authenticated_user.return_value = mock_user
|
||||
|
||||
workspace = WorkspaceManager()
|
||||
repo_path = Path("/tmp/mock-repo")
|
||||
|
||||
|
||||
workspace._configure_repo_user(repo_path)
|
||||
|
||||
|
||||
assert mock_run.call_count >= 3
|
||||
calls = [c[0][0] for c in mock_run.call_args_list]
|
||||
|
||||
|
||||
assert any("http.extraHeader" in call for call in calls)
|
||||
assert any("user.name" in call for call in calls)
|
||||
assert any("user.email" in call for call in calls)
|
||||
@@ -36,27 +36,27 @@ def test_workspace_manager_configure_repo_user(
|
||||
@patch("gitea.workspace.subprocess.run")
|
||||
@patch("gitea.workspace.GiteaClient")
|
||||
def test_workspace_manager_clone_repo(
|
||||
mock_client_class: MagicMock,
|
||||
mock_run: MagicMock
|
||||
mock_client_class: MagicMock, mock_run: MagicMock
|
||||
) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.repos = MagicMock()
|
||||
mock_user = MagicMock()
|
||||
mock_user.full_name = "Agent Tester"
|
||||
mock_user.login = "agent-test"
|
||||
mock_user.email = "agent-test@example.com"
|
||||
mock_client.get_authenticated_user.return_value = mock_user
|
||||
mock_client.repos.get_authenticated_user.return_value = mock_user
|
||||
|
||||
workspace = WorkspaceManager()
|
||||
|
||||
|
||||
with patch.object(workspace, "_configure_repo_user") as mock_configure:
|
||||
with patch.object(workspace, "get_repo_path") as mock_get_path:
|
||||
mock_repo_path = MagicMock(spec=Path)
|
||||
mock_repo_path.exists.return_value = False
|
||||
mock_get_path.return_value = mock_repo_path
|
||||
|
||||
|
||||
workspace.clone_repo("meeks/repo1")
|
||||
|
||||
|
||||
mock_run.assert_called_once()
|
||||
args = mock_run.call_args[0][0]
|
||||
assert "clone" in args
|
||||
@@ -64,13 +64,15 @@ def test_workspace_manager_clone_repo(
|
||||
mock_configure.assert_called_once_with(mock_repo_path)
|
||||
|
||||
|
||||
@patch("gitea.workspace.subprocess.run")
|
||||
@patch("gitea.workspace.GiteaClient")
|
||||
def test_workspace_manager_fails_if_no_authenticated_user(
|
||||
mock_client_class: MagicMock
|
||||
mock_client_class: MagicMock, mock_run: MagicMock
|
||||
) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.get_authenticated_user.return_value = None
|
||||
mock_client.repos = MagicMock()
|
||||
mock_client.repos.get_authenticated_user.return_value = None
|
||||
|
||||
workspace = WorkspaceManager()
|
||||
with pytest.raises(RuntimeError, match="No authenticated user found."):
|
||||
@@ -80,15 +82,17 @@ def test_workspace_manager_fails_if_no_authenticated_user(
|
||||
workspace._configure_repo_user(Path("/tmp/mock-repo"))
|
||||
|
||||
|
||||
@patch("gitea.workspace.subprocess.run")
|
||||
@patch("gitea.workspace.GiteaClient")
|
||||
def test_workspace_manager_fails_if_authenticated_user_has_no_login(
|
||||
mock_client_class: MagicMock
|
||||
mock_client_class: MagicMock, mock_run: MagicMock
|
||||
) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.repos = MagicMock()
|
||||
mock_user = MagicMock()
|
||||
mock_user.login = ""
|
||||
mock_client.get_authenticated_user.return_value = mock_user
|
||||
mock_client.repos.get_authenticated_user.return_value = mock_user
|
||||
|
||||
workspace = WorkspaceManager()
|
||||
with pytest.raises(RuntimeError, match="No authenticated user found."):
|
||||
@@ -101,17 +105,17 @@ def test_workspace_manager_fails_if_authenticated_user_has_no_login(
|
||||
@patch("gitea.workspace.subprocess.run")
|
||||
@patch("gitea.workspace.GiteaClient")
|
||||
def test_workspace_manager_sanitize_repo_no_changes(
|
||||
mock_client_class: MagicMock,
|
||||
mock_run: MagicMock
|
||||
mock_client_class: MagicMock, mock_run: MagicMock
|
||||
) -> None:
|
||||
# Setup Gitea client mock
|
||||
mock_client = MagicMock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.repos = MagicMock()
|
||||
mock_user = MagicMock()
|
||||
mock_user.full_name = "Agent Tester"
|
||||
mock_user.login = "agent-test"
|
||||
mock_user.email = "agent-test@example.com"
|
||||
mock_client.get_authenticated_user.return_value = mock_user
|
||||
mock_client.repos.get_authenticated_user.return_value = mock_user
|
||||
|
||||
# Mock subprocess.run for status check and others
|
||||
def mock_run_side_effect(args: list[str], **kwargs: Any) -> MagicMock:
|
||||
@@ -144,17 +148,17 @@ def test_workspace_manager_sanitize_repo_no_changes(
|
||||
@patch("gitea.workspace.subprocess.run")
|
||||
@patch("gitea.workspace.GiteaClient")
|
||||
def test_workspace_manager_sanitize_repo_with_changes(
|
||||
mock_client_class: MagicMock,
|
||||
mock_run: MagicMock
|
||||
mock_client_class: MagicMock, mock_run: MagicMock
|
||||
) -> None:
|
||||
# Setup Gitea client mock
|
||||
mock_client = MagicMock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.repos = MagicMock()
|
||||
mock_user = MagicMock()
|
||||
mock_user.full_name = "Agent Tester"
|
||||
mock_user.login = "agent-test"
|
||||
mock_user.email = "agent-test@example.com"
|
||||
mock_client.get_authenticated_user.return_value = mock_user
|
||||
mock_client.repos.get_authenticated_user.return_value = mock_user
|
||||
|
||||
# Mock subprocess.run to show modified files
|
||||
def mock_run_side_effect(args: list[str], **kwargs: Any) -> MagicMock:
|
||||
@@ -183,20 +187,21 @@ def test_workspace_manager_sanitize_repo_with_changes(
|
||||
@patch("gitea.workspace.subprocess.run")
|
||||
@patch("gitea.workspace.GiteaClient")
|
||||
def test_workspace_manager_sanitize_repo_fails(
|
||||
mock_client_class: MagicMock,
|
||||
mock_run: MagicMock
|
||||
mock_client_class: MagicMock, mock_run: MagicMock
|
||||
) -> None:
|
||||
# Setup Gitea client mock
|
||||
mock_client = MagicMock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.repos = MagicMock()
|
||||
mock_user = MagicMock()
|
||||
mock_user.full_name = "Agent Tester"
|
||||
mock_user.login = "agent-test"
|
||||
mock_user.email = "agent-test@example.com"
|
||||
mock_client.get_authenticated_user.return_value = mock_user
|
||||
mock_client.repos.get_authenticated_user.return_value = mock_user
|
||||
|
||||
# Mock remote set-url to fail
|
||||
import subprocess
|
||||
|
||||
mock_run.side_effect = subprocess.CalledProcessError(1, "git remote set-url")
|
||||
|
||||
workspace = WorkspaceManager()
|
||||
|
||||
Reference in New Issue
Block a user