Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_vcs_git(self):
self.git_repository_root.return_value = self.root
self.assertEquals((gitlint.git, self.root), gitlint.get_vcs_root())
def test_last_commit(self):
with mock.patch('subprocess.check_output',
return_value=b'0a' * 20 + b'\n') as git_call:
self.assertEqual('0a' * 20, git.last_commit())
git_call.asser_called_once_with(
['git', 'rev-parse', 'HEAD'])
def _GetModifiedFiles(commit, out=sys.stdout):
root = git.repository_root()
pending_files = git.modified_files(root, True)
if pending_files:
out.write(ERROR_UNCOMMITTED)
sys.exit(1)
modified_files = git.modified_files(root, True, commit)
modified_files = {f: modified_files[f] for f
in modified_files if f.endswith('.java')}
return modified_files
def RunCheckstyleOnACommit(commit):
"""Runs Checkstyle checks on a given commit.
It will run Checkstyle on the changed Java files in a specified commit SHA-1
and if that is None it will fallback to check the latest commit of the
currently checked out branch.
Args:
commit: A full 40 character SHA-1 of a commit to check.
Returns:
A tuple of errors and warnings.
"""
if not commit:
_WarnIfUntrackedFiles()
commit = git.last_commit()
print 'Running Checkstyle on %s commit' % commit
commit_modified_files = _GetModifiedFiles(commit)
if not commit_modified_files.keys():
print 'No Java files to check'
return [], []
(tmp_dir, tmp_file_map) = _GetTempFilesForCommit(
commit_modified_files.keys(), commit)
java_files = tmp_file_map.keys()
stdout = _ExecuteCheckstyle(java_files)
# Remove all the temporary files.
shutil.rmtree(tmp_dir)
(errors, warnings) = _ParseAndFilterOutput(stdout,
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)
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):
continue
column = ''
if error.hasAttribute('column'):
column = '%s:' % error.attributes['column'].value
def get_vcs_root():
"""Returns the vcs module and the root of the repo.
Returns:
A tuple containing the vcs module to use (git, hg) and the root of the
repository. If no repository exisits then (None, None) is returned.
"""
for vcs in (git, hg):
repo_root = vcs.repository_root()
if repo_root:
return vcs, repo_root
return (None, None)