diff --git a/.moban.cd/changelog.yml b/.moban.cd/changelog.yml index 24d27822..9c726859 100644 --- a/.moban.cd/changelog.yml +++ b/.moban.cd/changelog.yml @@ -5,6 +5,7 @@ releases: - action: Removed details: - "`#253`: symbolic link in regression pack causes python setup.py to do recursive include" + - "`#209`: Alert moban user when `git` is not available and is used." date: unreleased version: 0.4.3 - changes: diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d458e558..58502302 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,8 @@ Removed #. `#253 `_: symbolic link in regression pack causes python setup.py to do recursive include +#. `#209 `_: Alert moban user + when `git` is not available and is used. 0.4.2 - 08.03.2019 -------------------------------------------------------------------------------- diff --git a/moban/exceptions.py b/moban/exceptions.py index c0ab48b9..f214acba 100644 --- a/moban/exceptions.py +++ b/moban/exceptions.py @@ -24,3 +24,7 @@ class IncorrectDataInput(Exception): class GroupTargetNotFound(Exception): pass + + +class NoGitCommand(Exception): + pass diff --git a/moban/repo.py b/moban/repo.py index 36da0582..615a2a5a 100644 --- a/moban/repo.py +++ b/moban/repo.py @@ -1,12 +1,20 @@ import os +import sys +import subprocess -from moban import reporter, constants +from moban import reporter, constants, exceptions from moban.utils import mkdir_p def git_clone(requires): from git import Repo + if sys.platform != "win32": + # Unfortunately for windows user, the following function + # needs shell=True, which expose security risk. I would + # rather not to trade it with its marginal benefit + make_sure_git_is_available() + moban_home = get_moban_home() mkdir_p(moban_home) @@ -51,3 +59,10 @@ def get_moban_home(): home_dir = user_cache_dir(appname=constants.PROGRAM_NAME) return os.path.join(home_dir, constants.MOBAN_REPOS_DIR_NAME) + + +def make_sure_git_is_available(): + try: + subprocess.check_output(["git", "--help"]) + except Exception: + raise exceptions.NoGitCommand("Please install git command") diff --git a/tests/mobanfile/test_targets.py b/tests/mobanfile/test_targets.py index e239ea83..81dd1139 100644 --- a/tests/mobanfile/test_targets.py +++ b/tests/mobanfile/test_targets.py @@ -37,7 +37,7 @@ def test_handling_group_target(): def test_extract_group_targets(): test_targets = [ {"output": "a.output", "template": "a.template.jj2"}, - {"copy": [{"output": "source"}], "copy1": [{"output1": "source1"}]} + {"copy": [{"output": "source"}], "copy1": [{"output1": "source1"}]}, ] actual = targets.extract_group_targets("copy1", test_targets) expected = [{"copy1": [{"output1": "source1"}]}] diff --git a/tests/test_repo.py b/tests/test_repo.py index c180c18f..4d219d3a 100644 --- a/tests/test_repo.py +++ b/tests/test_repo.py @@ -1,9 +1,15 @@ import os from mock import patch -from nose.tools import eq_ +from nose.tools import eq_, raises -from moban.repo import git_clone, get_repo_name, get_moban_home +from moban.repo import ( + git_clone, + get_repo_name, + get_moban_home, + make_sure_git_is_available, +) +from moban.exceptions import NoGitCommand from moban.definitions import GitRequire @@ -112,3 +118,9 @@ def test_get_repo_name_can_handle_invalid_url(fake_reporter): def test_get_moban_home(_): actual = get_moban_home() eq_(os.path.join("root", "repos"), actual) + + +@raises(NoGitCommand) +@patch("subprocess.check_output", side_effect=Exception) +def test_make_git_is_available(_): + make_sure_git_is_available()