From 2085e35d25f9f4765020db3946d2cbf23268715f Mon Sep 17 00:00:00 2001 From: Gonzalo Pena-Castellanos Date: Wed, 29 May 2019 20:50:36 -0500 Subject: [PATCH] Handle git not found error and add regression test --- spyder/utils/tests/test_vcs.py | 13 +++++ spyder/utils/vcs.py | 86 +++++++++++++++++----------------- 2 files changed, 56 insertions(+), 43 deletions(-) diff --git a/spyder/utils/tests/test_vcs.py b/spyder/utils/tests/test_vcs.py index bd3f43f58a3..effdfbdb7df 100644 --- a/spyder/utils/tests/test_vcs.py +++ b/spyder/utils/tests/test_vcs.py @@ -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, @@ -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 diff --git a/spyder/utils/vcs.py b/spyder/utils/vcs.py index c70f9bcb803..d436d5060ab 100644 --- a/spyder/utils/vcs.py +++ b/spyder/utils/vcs.py @@ -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