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

fix: gracefully handle when git executable can't be found #5988

Merged
merged 3 commits into from
Sep 26, 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
14 changes: 12 additions & 2 deletions samcli/lib/utils/git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from subprocess import check_output
from typing import Optional

from samcli.commands.exceptions import UserException
from samcli.lib.utils import osutils
from samcli.lib.utils.osutils import rmtree_callback

Expand All @@ -36,6 +37,12 @@ class ManifestNotFoundException(Exception):
"""


class GitExecutableNotFoundException(UserException):
"""
Used to thrown when git executable not found in the system
"""


class GitRepo:
"""
Class for managing a Git repo, currently it has a clone functionality only
Expand Down Expand Up @@ -83,9 +90,12 @@ def git_executable() -> str:
# No exception. Let's pick this
return executable
except OSError as ex:
LOG.debug("Unable to find executable %s", executable, exc_info=ex)
LOG.warning("Unable to find executable %s", executable, exc_info=ex)

raise OSError("Cannot find git, was looking at executables: {}".format(executables))
raise GitExecutableNotFoundException(
"This command requires git but we couldn't find the executable, "
f"was looking with following names: {executables}"
)

def clone(self, clone_dir: Path, clone_name: str, replace_existing: bool = False, commit: str = "") -> Path:
"""
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/lib/utils/test_git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
from unittest import TestCase
from unittest.mock import patch, MagicMock, ANY, call
import os
from samcli.lib.utils.git_repo import GitRepo, rmtree_callback, CloneRepoException, CloneRepoUnstableStateException
from samcli.lib.utils.git_repo import (
GitRepo,
rmtree_callback,
CloneRepoException,
CloneRepoUnstableStateException,
GitExecutableNotFoundException,
)

REPO_URL = "REPO URL"
REPO_NAME = "REPO NAME"
Expand Down Expand Up @@ -44,7 +50,7 @@ def test_git_executable_windows(self, mock_platform, mock_popen):
@patch("samcli.lib.utils.git_repo.subprocess.Popen")
def test_git_executable_fails(self, mock_popen):
mock_popen.side_effect = OSError("fail")
with self.assertRaises(OSError):
with self.assertRaises(GitExecutableNotFoundException):
self.repo.git_executable()

@patch("samcli.lib.utils.git_repo.Path.exists")
Expand Down
Loading