Skip to content

Commit

Permalink
Merge pull request #9467 from goanpeca/fix/git-not-found
Browse files Browse the repository at this point in the history
PR: Handle git not found error and add regression test
  • Loading branch information
ccordoba12 committed May 30, 2019
2 parents d1c8217 + 2085e35 commit fefac85
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 43 deletions.
13 changes: 13 additions & 0 deletions spyder/utils/tests/test_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# Local imports
from spyder.utils.vcs import (ActionToolNotFound, get_git_refs,
get_git_revision, get_vcs_root, run_vcs_tool)
from spyder.utils import programs


@pytest.mark.skipif(os.environ.get('CI', None) is None,
Expand Down Expand Up @@ -46,6 +47,18 @@ def test_git_revision():
assert all([isinstance(x, str) for x in get_git_revision(root)])


def test_no_git(monkeypatch):

def mockreturn(program_name):
return None

monkeypatch.setattr(programs, 'find_program', mockreturn)
branch_tags, branch, files_modified = get_git_refs(__file__)
assert len(branch_tags) == 0
assert branch == ''
assert len(files_modified) == 0


def test_get_git_refs():
branch_tags, branch, files_modified = get_git_refs(__file__)
assert bool(branch) # This must always return a branch_name
Expand Down
86 changes: 43 additions & 43 deletions spyder/utils/vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,48 +163,48 @@ def get_git_refs(repopath):
if os.path.isfile(repopath):
repopath = os.path.dirname(repopath)

try:

git = programs.find_program('git')

# Files modified
out, err = programs.run_program(
git, ['status', '-s'],
cwd=repopath,
).communicate()

if PY3:
out = out.decode(sys.getdefaultencoding())
files_modifed = [line.strip() for line in out.split('\n') if line]

# Tags
out, err = programs.run_program(
git, ['tag'],
cwd=repopath,
).communicate()

if PY3:
out = out.decode(sys.getdefaultencoding())
tags = [line.strip() for line in out.split('\n') if line]

# Branches
out, err = programs.run_program(
git, ['branch', '-a'],
cwd=repopath,
).communicate()

if PY3:
out = out.decode(sys.getdefaultencoding())

lines = [line.strip() for line in out.split('\n') if line]
for line in lines:
if line.startswith('*'):
line = line.replace('*', '').strip()
branch = line

branches.append(line)

except (subprocess.CalledProcessError, AttributeError, OSError):
pass
git = programs.find_program('git')

if git:
try:
# Files modified
out, err = programs.run_program(
git, ['status', '-s'],
cwd=repopath,
).communicate()

if PY3:
out = out.decode(sys.getdefaultencoding())
files_modifed = [line.strip() for line in out.split('\n') if line]

# Tags
out, err = programs.run_program(
git, ['tag'],
cwd=repopath,
).communicate()

if PY3:
out = out.decode(sys.getdefaultencoding())
tags = [line.strip() for line in out.split('\n') if line]

# Branches
out, err = programs.run_program(
git, ['branch', '-a'],
cwd=repopath,
).communicate()

if PY3:
out = out.decode(sys.getdefaultencoding())

lines = [line.strip() for line in out.split('\n') if line]
for line in lines:
if line.startswith('*'):
line = line.replace('*', '').strip()
branch = line

branches.append(line)

except (subprocess.CalledProcessError, AttributeError, OSError):
pass

return branches + tags, branch, files_modifed

0 comments on commit fefac85

Please sign in to comment.