Skip to content

Commit

Permalink
Do not warn about uncommited files if sha is set explicitly.
Browse files Browse the repository at this point in the history
Bug: 31603810
Test: Added test_GetModifiedFilesUncommittedExplicitCommit, all tests pass
Change-Id: I953f22df5be8030fc8a7145531026220c97a8dd8
  • Loading branch information
liutikas committed Sep 20, 2016
1 parent 0b77686 commit fc518c7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions tools/checkstyle/checkstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ def RunCheckstyleOnACommit(commit, config_xml=CHECKSTYLE_STYLE):
Returns:
A tuple of errors and warnings.
"""
if not commit:
explicit_commit = commit is not None
if not explicit_commit:
_WarnIfUntrackedFiles()
commit = git.last_commit()
print 'Running Checkstyle on %s commit' % commit
commit_modified_files = _GetModifiedFiles(commit)
commit_modified_files = _GetModifiedFiles(commit, explicit_commit)
if not commit_modified_files.keys():
print 'No Java files to check'
return [], []
Expand Down Expand Up @@ -216,10 +217,10 @@ def _ShouldSkip(commit_check, modified_lines, line, rule, test_class=False):
return line not in modified_lines and rule not in FORCED_RULES


def _GetModifiedFiles(commit, out=sys.stdout):
def _GetModifiedFiles(commit, explicit_commit=False, out=sys.stdout):
root = git.repository_root()
pending_files = git.modified_files(root, True)
if pending_files:
if pending_files and not explicit_commit:
out.write(ERROR_UNCOMMITTED)
sys.exit(1)

Expand Down
8 changes: 8 additions & 0 deletions tools/checkstyle/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ def test_GetModifiedFilesUncommitted(self):
checkstyle._GetModifiedFiles(mock_last_commit(), out=out)
self.assertEqual(out.getvalue(), checkstyle.ERROR_UNCOMMITTED)

def test_GetModifiedFilesUncommittedExplicitCommit(self):
checkstyle.git.modified_files = mock_modified_files_uncommitted
out = StringIO()
files = checkstyle._GetModifiedFiles(mock_last_commit(), True, out=out)
output = out.getvalue()
self.assertEqual(output, '')
self.assertEqual(files, {TEST_FILE1: FILE_MODIFIED, TEST_FILE2: FILE_ADDED})

def test_GetModifiedFilesNonJava(self):
checkstyle.git.modified_files = mock_modified_files_non_java
out = StringIO()
Expand Down

0 comments on commit fc518c7

Please sign in to comment.