Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide user a more informative error when a git clone fails #1210

Merged
merged 6 commits into from
Apr 24, 2023
Merged
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
1 change: 1 addition & 0 deletions changes/1210.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error handling for incomplete or corrupted Github clones of templates has been improved.
4 changes: 4 additions & 0 deletions src/briefcase/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,10 @@ def update_cookiecutter_cache(self, template: str, branch="master"):
# Template cache path exists, but isn't a git repository
# Just use the template directly, rather than attempting an update.
cached_template = template
except ValueError as e:
raise BriefcaseCommandError(
f"Git repository in a weird state, delete {cached_template} and try briefcase create again"
) from e
else:
# If this isn't a repository URL, treat it as a local directory
cached_template = template
Expand Down
32 changes: 31 additions & 1 deletion tests/commands/base/test_update_cookiecutter_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from git import exc as git_exceptions

from briefcase.commands.base import cookiecutter_cache_path
from briefcase.exceptions import TemplateUnsupportedVersion
from briefcase.exceptions import BriefcaseCommandError, TemplateUnsupportedVersion


def test_non_url(base_command, mock_git):
Expand Down Expand Up @@ -189,3 +189,33 @@ def test_cached_missing_branch_template(base_command, mock_git):

# An attempt to access the branch was made
mock_remote.refs.__getitem__.assert_called_once_with("invalid")


def test_value_error(base_command, mock_git):
"""If the git clone fails a ValueError is raised."""
base_command.tools.git = mock_git

mock_repo = mock.MagicMock()
mock_remote = mock.MagicMock()

# Git returns a Repo, that repo can return a remote, and it has
# heads that can be accessed. However, getting the remote will fail if git clone is not complete.
base_command.tools.git.Repo.return_value = mock_repo
mock_repo.remote.side_effect = ValueError("Remote named origin did not exist")

cached_path = cookiecutter_cache_path(
"https://example.com/magic/special-template.git"
)

# Update the cache
with pytest.raises(BriefcaseCommandError, match="Git repository in a weird state"):
base_command.update_cookiecutter_cache(
template="https://example.com/magic/special-template.git", branch="special"
)

# The cookiecutter cache location will be interrogated.
base_command.tools.git.Repo.assert_called_once_with(cached_path)

# The origin of the repo was fetched
mock_repo.remote.assert_called_once_with(name="origin")
mock_remote.fetch.assert_not_called()