From c103020e9b7f66c002931d152eede043fb88836f Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Thu, 26 Apr 2018 08:31:08 -0700 Subject: [PATCH] improve git project detection test coverage --- tests/test_project.py | 59 +++++++++++++++++++++++++++++++++++++++- wakatime/projects/git.py | 13 ++++----- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/tests/test_project.py b/tests/test_project.py index a006738..8ffcc1b 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -12,7 +12,7 @@ import shutil import tempfile import time from testfixtures import log_capture -from wakatime.compat import u +from wakatime.compat import u, open from wakatime.constants import API_ERROR, SUCCESS from wakatime.exceptions import NotYetImplemented from wakatime.projects.base import BaseProject @@ -467,6 +467,23 @@ class ProjectTestCase(TestCase): entity=entity, ) + def test_git_worktree_not_detected_when_commondir_missing(self): + tempdir = tempfile.mkdtemp() + shutil.copytree('tests/samples/projects/git-worktree', os.path.join(tempdir, 'git-wt')) + shutil.copytree('tests/samples/projects/git', os.path.join(tempdir, 'git')) + shutil.move(os.path.join(tempdir, 'git-wt', 'dot_git'), os.path.join(tempdir, 'git-wt', '.git')) + shutil.move(os.path.join(tempdir, 'git', 'dot_git'), os.path.join(tempdir, 'git', '.git')) + + os.remove(os.path.join(tempdir, 'git', '.git', 'worktrees', 'git-worktree', 'commondir')) + + entity = os.path.join(tempdir, 'git-wt', 'emptyfile.txt') + + self.shared( + expected_project=None, + expected_branch='worktree-detection-branch', + entity=entity, + ) + @log_capture() def test_git_path_from_gitdir_link_file(self, logs): logging.disable(logging.NOTSET) @@ -495,6 +512,28 @@ class ProjectTestCase(TestCase): shutil.move(os.path.join(tempdir, 'git', 'dot_git'), os.path.join(tempdir, 'git', '.git')) shutil.move(os.path.join(tempdir, 'git', 'asubmodule', 'dot_git'), os.path.join(tempdir, 'git', 'asubmodule', '.git')) + self.orig_open = open + self.count = 0 + + with mock.patch('wakatime.projects.git.open') as mock_open: + + def side_effect_function(*args, **kwargs): + self.count += 1 + if self.count <= 1: + raise IOError('') + return self.orig_open(*args, **kwargs) + + mock_open.side_effect = side_effect_function + + git = Git(None) + path = os.path.join(tempdir, 'git', 'asubmodule') + result = git._path_from_gitdir_link_file(path) + + expected = os.path.realpath(os.path.join(tempdir, 'git', '.git', 'modules', 'asubmodule')) + self.assertEquals(expected, result) + self.assertNothingPrinted() + self.assertNothingLogged(logs) + with mock.patch('wakatime.projects.git.open') as mock_open: mock_open.side_effect = UnicodeDecodeError('utf8', ''.encode('utf8'), 0, 0, '') @@ -521,6 +560,24 @@ class ProjectTestCase(TestCase): expected = 'OSError' if self.isPy33OrNewer else 'IOError' self.assertIn(expected, actual) + @log_capture() + def test_git_path_from_gitdir_link_file_handles_invalid_link(self, logs): + logging.disable(logging.NOTSET) + + tempdir = tempfile.mkdtemp() + shutil.copytree('tests/samples/projects/git-with-submodule', os.path.join(tempdir, 'git')) + shutil.move(os.path.join(tempdir, 'git', 'asubmodule', 'dot_git'), os.path.join(tempdir, 'git', 'asubmodule', '.git')) + + path = os.path.join(tempdir, 'git', 'asubmodule') + + git = Git(None) + result = git._path_from_gitdir_link_file(path) + + expected = None + self.assertEquals(expected, result) + self.assertNothingPrinted() + self.assertNothingLogged(logs) + @log_capture() def test_project_map(self, logs): logging.disable(logging.NOTSET) diff --git a/wakatime/projects/git.py b/wakatime/projects/git.py index ea9933d..9acdc78 100644 --- a/wakatime/projects/git.py +++ b/wakatime/projects/git.py @@ -79,8 +79,6 @@ class Git(BaseProject): return True disabled = self._configs.get('submodules_disabled') - if not disabled: - return True if disabled.strip().lower() == 'true': return False @@ -139,12 +137,13 @@ class Git(BaseProject): with open(filepath, 'r', encoding='utf-8') as fh: return fh.readline().strip() except UnicodeDecodeError: - try: - with open(filepath, 'r', encoding=sys.getfilesystemencoding()) as fh: - return fh.readline().strip() - except: - log.traceback(logging.WARNING) + pass except IOError: + pass + try: + with open(filepath, 'r', encoding=sys.getfilesystemencoding()) as fh: + return fh.readline().strip() + except: log.traceback(logging.WARNING) return None