Skip to content

Commit

Permalink
fix: gracefully handle when git executable can't be found (#5988)
Browse files Browse the repository at this point in the history
* fix: gracefully handle when git executable can't be found

* update test & format
  • Loading branch information
mndeveci authored Sep 26, 2023
1 parent 576fe6a commit aadeb3a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
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

0 comments on commit aadeb3a

Please sign in to comment.