def test_modified_lines_with_commit(self): output = os.linesep.join([ 'baz', '0123456789abcdef31410123456789abcdef3141 2 2 4', 'foo', '0123456789abcdef31410123456789abcdef3141 5 5', 'bar' ]).encode('utf-8') with mock.patch('subprocess.check_output', return_value=output) as check_output: self.assertEqual( [2, 5], list( git.modified_lines( '/home/user/repo/foo/bar.txt', ' M', commit='0123456789abcdef31410123456789abcdef3141'))) self.assertEqual( [2, 5], list( git.modified_lines( '/home/user/repo/foo/bar.txt', 'M ', commit='0123456789abcdef31410123456789abcdef3141'))) expected_calls = [ mock.call([ 'git', 'blame', '--porcelain', '/home/user/repo/foo/bar.txt' ]) ] * 2 self.assertEqual(expected_calls, check_output.call_args_list)
def test_modified_lines_with_commit(self): output = os.linesep.join([ 'baz', '0123456789abcdef31410123456789abcdef3141 2 2 4', 'foo', '0123456789abcdef31410123456789abcdef3141 5 5', 'bar']).encode('utf-8') with mock.patch('subprocess.check_output', return_value=output) as check_output: self.assertEqual( [2, 5], list(git.modified_lines( '/home/user/repo/foo/bar.txt', ' M', commit='0123456789abcdef31410123456789abcdef3141'))) self.assertEqual( [2, 5], list(git.modified_lines( '/home/user/repo/foo/bar.txt', 'M ', commit='0123456789abcdef31410123456789abcdef3141'))) self.assertEqual( [2, 5], list(git.modified_lines( '/home/user/repo/foo/bar.txt', 'MM', commit='0123456789abcdef31410123456789abcdef3141'))) expected_calls = [mock.call( ['git', 'blame', '--porcelain', '/home/user/repo/foo/bar.txt'])] * 3 self.assertEqual(expected_calls, check_output.call_args_list)
def test_modified_lines(self, check_output): check_output.return_value = os.linesep.join([ 'baz', '0000000000000000000000000000000000000000 2 2 4', 'foo', '0000000000000000000000000000000000000000 5 5', 'bar' ]).encode('utf-8') self.assertEqual([2, 5], list( git.modified_lines('/home/user/repo/foo/bar.txt', ' M'))) self.assertEqual([2, 5], list( git.modified_lines('/home/user/repo/foo/bar.txt', 'M '))) self.assertEqual([2, 5], list( git.modified_lines('/home/user/repo/foo/bar.txt', 'MM'))) expected_calls = [ mock.call( ['git', 'blame', '--porcelain', '/home/user/repo/foo/bar.txt']) ] * 3 self.assertEqual(expected_calls, check_output.call_args_list)
def _ParseAndFilterOutput(stdout, sha=None, commit_modified_files=None, tmp_file_map=None): result_errors = [] result_warnings = [] root = xml.dom.minidom.parseString(stdout) for file_element in root.getElementsByTagName('file'): file_name = file_element.attributes['name'].value if tmp_file_map: file_name = tmp_file_map[file_name] modified_lines = None if commit_modified_files: modified_lines = git.modified_lines( file_name, commit_modified_files[file_name], sha) test_class = any(substring in file_name for substring in SUBPATH_FOR_TEST_FILES) test_data_class = any(substring in file_name for substring in SUBPATH_FOR_TEST_DATA_FILES) file_name = os.path.relpath(file_name) errors = file_element.getElementsByTagName('error') for error in errors: line = int(error.attributes['line'].value) rule = error.attributes['source'].value if _ShouldSkip(commit_modified_files, modified_lines, line, rule, test_class, test_data_class): continue column = '' if error.hasAttribute('column'): column = '%s:' % error.attributes['column'].value message = error.attributes['message'].value project = '' if os.environ.get('REPO_PROJECT'): project = '[' + os.environ.get('REPO_PROJECT') + '] ' result = ' %s%s:%s:%s %s' % (project, file_name, line, column, message) severity = error.attributes['severity'].value if severity == 'error': result_errors.append(result) elif severity == 'warning': result_warnings.append(result) return result_errors, result_warnings
def _ParseAndFilterOutput(stdout, sha=None, commit_modified_files=None, tmp_file_map=None): result_errors = [] result_warnings = [] root = xml.dom.minidom.parseString(stdout) for file_element in root.getElementsByTagName('file'): file_name = file_element.attributes['name'].value if tmp_file_map: file_name = tmp_file_map[file_name] modified_lines = None if commit_modified_files: modified_lines = git.modified_lines(file_name, commit_modified_files[file_name], sha) test_class = any(substring in file_name for substring in SUBPATH_FOR_TEST_FILES) test_data_class = any(substring in file_name for substring in SUBPATH_FOR_TEST_DATA_FILES) file_name = os.path.relpath(file_name) errors = file_element.getElementsByTagName('error') for error in errors: line = int(error.attributes['line'].value) rule = error.attributes['source'].value if _ShouldSkip(commit_modified_files, modified_lines, line, rule, test_class, test_data_class): continue column = '' if error.hasAttribute('column'): column = '%s:' % error.attributes['column'].value message = error.attributes['message'].value project = '' if os.environ.get('REPO_PROJECT'): project = '[' + os.environ.get('REPO_PROJECT') + '] ' result = ' %s%s:%s:%s %s' % (project, file_name, line, column, message) severity = error.attributes['severity'].value if severity == 'error': result_errors.append(result) elif severity == 'warning': result_warnings.append(result) return result_errors, result_warnings
def test_modified_lines_no_info(self): self.assertEqual( [], list(git.modified_lines('/home/user/repo/foo/bar.txt', None)))
def test_modified_lines_untracked(self): self.assertEqual( None, git.modified_lines('/home/user/repo/foo/bar.txt', '??'))
def test_modified_lines_new_addition(self): self.assertEqual( None, git.modified_lines('/home/user/repo/foo/bar.txt', 'A '))
def test_modified_lines_no_info(self): self.assertEqual([], list( git.modified_lines('/home/user/repo/foo/bar.txt', None)))