Permalink
Browse files

Issue #3287: added exclude directory option

  • Loading branch information...
rnveach committed Jun 18, 2016
1 parent b02bdda commit cdb02a804751de2080ced0eaab41f3567a5b5d24
View
@@ -149,7 +149,7 @@
<!-- Suppressions from PMD configuration-->
<!-- validateCli is not reasonable to split as encapsulation of logic will be damaged -->
<suppress checks="CyclomaticComplexity" files="Main\.java" lines="212"/>
<suppress checks="CyclomaticComplexity" files="Main\.java" lines="220"/>
<!-- JavadocMethodCheck, JavadocStyleCheck, JavadocUtils.getJavadocTags() - deprecated -->
<suppress checks="CyclomaticComplexity" files="JavadocMethodCheck\.java"/>
<suppress checks="CyclomaticComplexity" files="JavadocStyleCheck\.java"/>
@@ -110,6 +110,12 @@
/** Name for the option '--debug'. */
private static final String OPTION_DEBUG_NAME = "debug";
/** Name for the option 'e'. */
private static final String OPTION_E_NAME = "e";
/** Name for the option '--debug'. */
private static final String OPTION_EXCLUDE_NAME = "exclude";
/** Name for 'xml' format. */
private static final String XML_FORMAT_NAME = "xml";
@@ -145,7 +151,9 @@ public static void main(String... args) throws IOException {
exitStatus = 0;
}
else {
final List<File> filesToProcess = getFilesToProcess(commandLine.getArgs());
final List<File> filesToProcess = getFilesToProcess(
CommonUtils.getListOrNull(commandLine.getOptionValues(OPTION_E_NAME)),
commandLine.getArgs());
// return error if something is wrong in arguments
final List<String> messages = validateCli(commandLine, filesToProcess);
@@ -472,14 +480,16 @@ else if (PLAIN_FORMAT_NAME.equals(format)) {
/**
* Determines the files to process.
* @param dirsToExclude The list of directories to exclude from searching.
* @param filesToProcess
* arguments that were not processed yet but shall be
* @return list of files to process
*/
private static List<File> getFilesToProcess(String... filesToProcess) {
private static List<File> getFilesToProcess(List<String> dirsToExclude,
String... filesToProcess) {
final List<File> files = Lists.newLinkedList();
for (String element : filesToProcess) {
files.addAll(listFiles(new File(element)));
files.addAll(listFiles(new File(element), dirsToExclude));
}
return files;
@@ -490,20 +500,23 @@ else if (PLAIN_FORMAT_NAME.equals(format)) {
* list. Subdirectories are also traversed.
* @param node
* the node to process
* @param dirsToExclude The list of directories to exclude from searching.
* @return found files
*/
private static List<File> listFiles(File node) {
private static List<File> listFiles(File node, List<String> dirsToExclude) {
// could be replaced with org.apache.commons.io.FileUtils.list() method
// if only we add commons-io library
final List<File> result = Lists.newLinkedList();
if (node.canRead()) {
if (node.isDirectory()) {
final File[] files = node.listFiles();
// listFiles() can return null, so we need to check it
if (files != null) {
for (File element : files) {
result.addAll(listFiles(element));
if (dirsToExclude == null || !dirsToExclude.contains(node.getAbsolutePath())) {
final File[] files = node.listFiles();
// listFiles() can return null, so we need to check it
if (files != null) {
for (File element : files) {
result.addAll(listFiles(element, dirsToExclude));
}
}
}
}
@@ -545,6 +558,8 @@ private static Options buildOptions() {
"Print full Abstract Syntax Tree of the file");
options.addOption(OPTION_D_NAME, OPTION_DEBUG_NAME, false,
"Print all debug logging of CheckStyle utility");
options.addOption(OPTION_E_NAME, OPTION_EXCLUDE_NAME, true,
"Directory to exclude from CheckStyle");
return options;
}
@@ -30,6 +30,8 @@
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
@@ -425,4 +427,18 @@ public static String fillTemplateWithStringsByRegexp(
public static boolean isBlank(String str) {
return str == null || CharMatcher.WHITESPACE.matchesAllOf(str);
}
/**
* Converts the {@code array} to a list.
* @param <T> The type of the {@code array}.
* @param array The array to convert.
* @return The list or {@code null}.
*/
public static <T> List<T> getListOrNull(T... array) {
if (array == null) {
return null;
}
return Arrays.asList(array);
}
}
@@ -32,6 +32,7 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
@@ -61,6 +62,7 @@
+ " file...%n"
+ " -c <arg> Sets the check configuration file to use.%n"
+ " -d,--debug Print all debug logging of CheckStyle utility%n"
+ " -e,--exclude <arg> Directory to exclude from CheckStyle%n"
+ " -f <arg> Sets the output format. (plain|xml). Defaults to"
+ " plain%n"
+ " -j,--javadocTree Print Parse tree of the Javadoc comment%n"
@@ -595,30 +597,30 @@ public void checkAssertion() throws IOException {
@Test
@SuppressWarnings("unchecked")
public void testListFilesNotFile() throws Exception {
final Method method = Main.class.getDeclaredMethod("listFiles", File.class);
final Method method = Main.class.getDeclaredMethod("listFiles", File.class, List.class);
method.setAccessible(true);
final File fileMock = mock(File.class);
when(fileMock.canRead()).thenReturn(true);
when(fileMock.isDirectory()).thenReturn(false);
when(fileMock.isFile()).thenReturn(false);
final List<File> result = (List<File>) method.invoke(null, fileMock);
final List<File> result = (List<File>) method.invoke(null, fileMock, null);
assertEquals(0, result.size());
}
@Test
@SuppressWarnings("unchecked")
public void testListFilesDirectoryWithNull() throws Exception {
final Method method = Main.class.getDeclaredMethod("listFiles", File.class);
final Method method = Main.class.getDeclaredMethod("listFiles", File.class, List.class);
method.setAccessible(true);
final File fileMock = mock(File.class);
when(fileMock.canRead()).thenReturn(true);
when(fileMock.isDirectory()).thenReturn(true);
when(fileMock.listFiles()).thenReturn(null);
final List<File> result = (List<File>) method.invoke(null, fileMock);
final List<File> result = (List<File>) method.invoke(null, fileMock, null);
assertEquals(0, result.size());
}
@@ -849,4 +851,29 @@ public void checkAssertion() {
});
Main.main("-c", "/google_checks.xml", getPath("InputMain.java"), "-d");
}
@Test
public void testExcludeOption() throws Exception {
exit.expectSystemExitWithStatus(-1);
exit.checkAssertionAfterwards(new Assertion() {
@Override
public void checkAssertion() {
assertEquals("Files to process must be specified, found 0."
+ System.lineSeparator(), systemOut.getLog());
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "/google_checks.xml", getFilePath(""), "-e", getFilePath(""));
}
@Test
@SuppressWarnings("unchecked")
public void testExcludeDirectoryNotMatch() throws Exception {
final Method method = Main.class.getDeclaredMethod("listFiles", File.class, List.class);
method.setAccessible(true);
final List<File> result = (List<File>) method.invoke(null, new File(getFilePath("")),
Arrays.asList(getFilePath("otherPath")));
assertNotEquals(0, result.size());
}
}
View
@@ -102,6 +102,11 @@ java -D&lt;property&gt;=&lt;value&gt; \
<li>
<code>-d, --debug</code> - Print all debug logging of CheckStyle utility.
</li>
<li>
<code>-e, --exclude excludedDirectory</code> - Directory to exclude from
CheckStyle. The directory <b>must be</b> the full, absolute path. Multiple excludes are
allowed.
</li>
<li>
<code>-v</code> - print product version and exit. Any other option is ignored.
</li>

0 comments on commit cdb02a8

Please sign in to comment.