Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions shallow_backup/git_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,14 @@ def git_add_all_commit_push(repo: git.Repo, dry_run: bool = False):
"Pushing to remote:",
f"{repo.remotes.origin.url}[origin/{repo.active_branch.name}]...",
)
repo.git.fetch()
repo.git.push("--set-upstream", "origin", "HEAD")
try:
repo.git.fetch()
repo.git.push("--set-upstream", "origin", "HEAD")
except GitCommandError:
print_red_bold(
"ERROR: Failed to push to remote. Check your internet connection, credentials, and repository permissions."
)
sys.exit(1)
else:
print_yellow_bold("No changes to commit...")

Expand Down
52 changes: 52 additions & 0 deletions tests/test_git_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from unittest.mock import MagicMock

import pytest
from git import GitCommandError

from shallow_backup.git_wrapper import git_add_all_commit_push


def test_git_add_all_commit_push_push_error(mocker):
"""
Test that git_add_all_commit_push handles a GitCommandError on push
correctly.
"""
# Mock the repo object
mock_repo = MagicMock()
mock_repo.is_dirty.return_value = True
mock_repo.active_branch.name = "test_branch"

# Mock the remote object
mock_repo.remotes = MagicMock()
mock_repo.remotes.origin = MagicMock()
mock_repo.remotes.origin.name = "origin"
mock_repo.remotes.origin.url = "test_url"
mock_repo.remotes.__iter__.return_value = [mock_repo.remotes.origin]

# Mock the git.push method to raise a GitCommandError
mocker.patch.object(mock_repo.git, "push",
side_effect=GitCommandError("push", 1, b"error", b"error"))
mocker.patch.object(mock_repo.git, "fetch")

# Mock the other functions that are called
mocker.patch("shallow_backup.git_wrapper.install_trufflehog_git_hook")
mocker.patch("shallow_backup.git_wrapper.git_add_all_and_print_status")
mocker.patch("shallow_backup.git_wrapper.prompt_yes_no",
side_effect=[True, True])
mocker.patch("subprocess.run", return_value=MagicMock(returncode=0))
mock_print_red_bold = mocker.patch(
"shallow_backup.git_wrapper.print_red_bold") # noqa: E501

# Call the function and assert that sys.exit is called with 1
with pytest.raises(SystemExit) as pytest_wrapped_e:
git_add_all_commit_push(mock_repo, dry_run=False)

assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1

# Assert that the error message is printed
mock_print_red_bold.assert_called_once_with(
"ERROR: Failed to push to remote. "
"Check your internet connection, credentials, "
"and repository permissions."
)