def _WarnIfUntrackedFiles(out=sys.stdout): """Prints a warning and a list of untracked files if needed.""" root = git.repository_root() untracked_files = git.modified_files(root, False) untracked_files = {f for f in untracked_files if f.endswith('.java')} if untracked_files: out.write(ERROR_UNTRACKED) for untracked_file in untracked_files: out.write(untracked_file + '\n') out.write('\n')
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 _GetModifiedFiles(commit, explicit_commit=False, out=sys.stdout): root = git.repository_root() pending_files = git.modified_files(root, True) if pending_files and not explicit_commit: 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, 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
def test_repository_root_error(self): with mock.patch('subprocess.check_output', side_effect=subprocess.CalledProcessError(1, '', '')): self.assertEqual(None, git.repository_root())
def test_repository_root_ok(self): with mock.patch('subprocess.check_output', return_value=b'/home/user/repo\n') as git_call: self.assertEqual('/home/user/repo', git.repository_root()) git_call.asser_called_once_with( ['git', 'rev-parse', '--show-toplevel'])
def test_repository_root_error(self, check_output): check_output.side_effect = subprocess.CalledProcessError(1, '', '') self.assertEqual(None, git.repository_root())
def test_repository_root_ok(self, check_output): self.assertEqual('/home/user/repo', git.repository_root()) check_output.assert_called_once_with( ['git', 'rev-parse', '--show-toplevel'], stderr=subprocess.STDOUT)
return result MAIN_DIRECTORY = os.path.normpath(os.path.dirname(__file__)) CHECKSTYLE_JAR = os.path.join(MAIN_DIRECTORY, 'checkstyle.jar') CHECKSTYLE_STYLE = os.path.join(MAIN_DIRECTORY, 'android-style.xml') FORCED_RULES = [ 'com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck', 'com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck' ] SKIPPED_RULES_FOR_TEST_FILES = [ 'com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTypeCheck', 'com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck' ] SUBPATH_FOR_TEST_FILES = ['/tests/', '/test/', '/androidTest/'] SUBPATH_FOR_TEST_DATA_FILES = _FindFoldersContaining(git.repository_root(), 'IGNORE_CHECKSTYLE') ERROR_UNCOMMITTED = 'You need to commit all modified files before running Checkstyle\n' ERROR_UNTRACKED = 'You have untracked java files that are not being checked:\n' def RunCheckstyleOnFiles(java_files, classpath=CHECKSTYLE_JAR, config_xml=CHECKSTYLE_STYLE): """Runs Checkstyle checks on a given set of java_files. Args: java_files: A list of files to check. classpath: The colon-delimited list of JARs in the classpath. config_xml: Path of the checkstyle XML configuration file.