def RunCheckstyleOnACommit(commit,
                           classpath=CHECKSTYLE_JAR,
                           config_xml=CHECKSTYLE_STYLE,
                           file_whitelist=None):
    """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.
    classpath: The colon-delimited list of JARs in the classpath.
    config_xml: Path of the checkstyle XML configuration file.
    file_whitelist: A list of whitelisted file paths that should be checked.

  Returns:
    A tuple of errors and warnings.
  """
    if not git.repository_root():
        print 'FAILURE: not inside a git repository'
        sys.exit(1)
    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, explicit_commit)
    commit_modified_files = _FilterFiles(commit_modified_files, file_whitelist)
    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, classpath, config_xml)

    # Remove all the temporary files.
    shutil.rmtree(tmp_dir)

    (errors, warnings) = _ParseAndFilterOutput(stdout, commit,
                                               commit_modified_files,
                                               tmp_file_map)
    _PrintErrorsAndWarnings(errors, warnings)
    return errors, warnings
Beispiel #2
0
def RunCheckstyleOnACommit(commit,
                           classpath=CHECKSTYLE_JAR,
                           config_xml=CHECKSTYLE_STYLE,
                           file_whitelist=None):
  """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.
    classpath: The colon-delimited list of JARs in the classpath.
    config_xml: Path of the checkstyle XML configuration file.
    file_whitelist: A list of whitelisted file paths that should be checked.

  Returns:
    A tuple of errors and warnings.
  """
  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, explicit_commit)
  commit_modified_files = _FilterFiles(commit_modified_files, file_whitelist)
  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, classpath, config_xml)

  # Remove all the temporary files.
  shutil.rmtree(tmp_dir)

  (errors, warnings) = _ParseAndFilterOutput(stdout,
                                             commit,
                                             commit_modified_files,
                                             tmp_file_map)
  _PrintErrorsAndWarnings(errors, warnings)
  return errors, warnings
def RunCheckstyleOnACommit(commit, config_xml=CHECKSTYLE_STYLE):
    """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.
    config_xml: Path of the checkstyle XML configuration file.

  Returns:
    A tuple of errors and warnings.
  """
    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, explicit_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, config_xml)

    # Remove all the temporary files.
    shutil.rmtree(tmp_dir)

    (errors, warnings) = _ParseAndFilterOutput(stdout, commit,
                                               commit_modified_files,
                                               tmp_file_map)
    _PrintErrorsAndWarnings(errors, warnings)
    return errors, warnings
Beispiel #4
0
 def test_last_commit_not_in_repo(self):
     with mock.patch('subprocess.check_output',
                     side_effect=subprocess.CalledProcessError(255, '', '')):
         self.assertEqual(None, git.last_commit())
Beispiel #5
0
 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'])
Beispiel #6
0
 def test_last_commit_not_in_repo(self):
     with mock.patch('subprocess.check_output',
                     side_effect=subprocess.CalledProcessError(255, '',
                                                               '')):
         self.assertEqual(None, git.last_commit())
Beispiel #7
0
 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'])
Beispiel #8
0
 def test_last_commit_not_in_repo(self, check_output):
     check_output.side_effect = subprocess.CalledProcessError(255, '', '')
     self.assertEqual(None, git.last_commit())
Beispiel #9
0
 def test_last_commit(self, check_output):
     self.assertEqual('0a' * 20, git.last_commit())
     check_output.assert_called_once_with(
         ['git', 'rev-parse', 'HEAD'], stderr=subprocess.STDOUT)
Beispiel #10
0
 def test_last_commit_not_in_repo(self, check_output):
     check_output.side_effect = subprocess.CalledProcessError(255, '', '')
     self.assertEqual(None, git.last_commit())
Beispiel #11
0
 def test_last_commit(self, check_output):
     self.assertEqual('0a' * 20, git.last_commit())
     check_output.assert_called_once_with(['git', 'rev-parse', 'HEAD'],
                                          stderr=subprocess.STDOUT)