Added support for collections as arguments
This commit is contained in:
parent
fe376c4510
commit
7efca76693
4 changed files with 267 additions and 20 deletions
3
examples/.idea/misc.xml
generated
3
examples/.idea/misc.xml
generated
|
@ -1,5 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="PDMPlugin">
|
||||
<option name="skipTestSources" value="false" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build" />
|
||||
</component>
|
||||
|
|
|
@ -79,6 +79,8 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
|
|||
/**
|
||||
* Directory/file to exclude from CheckStyle. The path can be the full, absolute path, or relative to the current
|
||||
* path. Multiple excludes are allowed.
|
||||
*
|
||||
* @see #sourceDir(Collection)
|
||||
*/
|
||||
public CheckstyleOperation exclude(String... path) {
|
||||
for (var p : path) {
|
||||
|
@ -87,6 +89,19 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Directory/file to exclude from CheckStyle. The path can be the full, absolute path, or relative to the current
|
||||
* path. Multiple excludes are allowed.
|
||||
*
|
||||
* @see #exclude(String...)
|
||||
*/
|
||||
public CheckstyleOperation exclude(Collection<String> path) {
|
||||
for (var p : path) {
|
||||
optionsWithArg.put("-e", p);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Directory/file pattern to exclude from CheckStyle. Multiple excludes are allowed.
|
||||
*/
|
||||
|
@ -219,12 +234,24 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
|
|||
|
||||
/**
|
||||
* Specified the file(s) or folder(s) containing the source files to check.
|
||||
*
|
||||
* @see #sourceDir(Collection)
|
||||
*/
|
||||
public CheckstyleOperation sourceDir(String... dir) {
|
||||
sourceDirs.addAll(Arrays.asList(dir));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specified the file(s) or folder(s) containing the source files to check.
|
||||
*
|
||||
* @see #sourceDir(String...)
|
||||
*/
|
||||
public CheckstyleOperation sourceDir(Collection<String> dir) {
|
||||
sourceDirs.addAll(dir);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the length of the tab character. Used only with the {@link #lineColumn(String) lineColum} option.
|
||||
* Default value is {@code 8}.
|
||||
|
|
|
@ -24,13 +24,17 @@ import rife.bld.operations.exceptions.ExitStatusException;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
|
||||
|
||||
class CheckstyleOperationTest {
|
||||
private static final String ADD = "add";
|
||||
private static final String BAR = "bar";
|
||||
private static final String FOO = "foo";
|
||||
private static final String REMOVE = "remove";
|
||||
|
||||
@Test
|
||||
void branchMatchingXpath() {
|
||||
|
@ -47,15 +51,17 @@ class CheckstyleOperationTest {
|
|||
@Test
|
||||
void debug() {
|
||||
var op = new CheckstyleOperation().fromProject(new Project()).debug(true);
|
||||
assertThat(op.options.contains("-d")).as("add").isTrue();
|
||||
assertThat(op.options.contains("-d")).as(ADD).isTrue();
|
||||
op = op.debug(false);
|
||||
assertThat(op.options.contains("-d")).as("remove").isFalse();
|
||||
assertThat(op.options.contains("-d")).as(REMOVE).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void exclude() {
|
||||
var op = new CheckstyleOperation().fromProject(new Project()).exclude(FOO);
|
||||
assertThat(op.optionsWithArg.get("-e")).isEqualTo(FOO);
|
||||
op = new CheckstyleOperation().fromProject(new Project()).exclude(List.of(FOO));
|
||||
assertThat(op.optionsWithArg.get("-e")).as("as list").isEqualTo(FOO);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -66,7 +72,7 @@ class CheckstyleOperationTest {
|
|||
|
||||
@Test
|
||||
void execute() throws IOException, ExitStatusException, InterruptedException {
|
||||
var tmpFile = File.createTempFile("checkstyle", ".txt");
|
||||
var tmpFile = File.createTempFile("checkstyle-google", ".txt");
|
||||
tmpFile.deleteOnExit();
|
||||
var op = new CheckstyleOperation()
|
||||
.fromProject(new WebProject())
|
||||
|
@ -90,19 +96,32 @@ class CheckstyleOperationTest {
|
|||
.startsWith("java -cp ")
|
||||
.endsWith(
|
||||
"com.puppycrawl.tools.checkstyle.Main " +
|
||||
"-d -E " +
|
||||
"-p config/checkstyle.properties " +
|
||||
"-b xpath " +
|
||||
"-c config/checkstyle.xml " +
|
||||
"src/main/java src/test/java");
|
||||
"-d -E " +
|
||||
"-p config/checkstyle.properties " +
|
||||
"-b xpath " +
|
||||
"-c config/checkstyle.xml " +
|
||||
"src/main/java src/test/java");
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeIgnoredModules() {
|
||||
var op = new CheckstyleOperation().fromProject(new Project()).executeIgnoredModules(true);
|
||||
assertThat(op.options.contains("-E")).as("add").isTrue();
|
||||
assertThat(op.options.contains("-E")).as(ADD).isTrue();
|
||||
op = op.executeIgnoredModules(false);
|
||||
assertThat(op.options.contains("-E")).as("remove").isFalse();
|
||||
assertThat(op.options.contains("-E")).as(REMOVE).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeSunChecks() throws IOException {
|
||||
var tmpFile = File.createTempFile("checkstyle-sun", ".txt");
|
||||
tmpFile.deleteOnExit();
|
||||
var op = new CheckstyleOperation()
|
||||
.fromProject(new WebProject())
|
||||
.sourceDir(List.of("src/main/java", "src/test/java"))
|
||||
.configurationFile("src/test/resources/sun_checks.xml")
|
||||
.output(tmpFile.getAbsolutePath());
|
||||
assertThatCode(op::execute).isInstanceOf(ExitStatusException.class);
|
||||
assertThat(tmpFile).exists();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -114,17 +133,17 @@ class CheckstyleOperationTest {
|
|||
@Test
|
||||
void generateXpathSuppression() {
|
||||
var op = new CheckstyleOperation().fromProject(new Project()).generateXpathSuppression(true);
|
||||
assertThat(op.options.contains("-g")).as("add").isTrue();
|
||||
assertThat(op.options.contains("-g")).as(ADD).isTrue();
|
||||
op = op.generateXpathSuppression(false);
|
||||
assertThat(op.options.contains("-g")).as("remove").isFalse();
|
||||
assertThat(op.options.contains("-g")).as(REMOVE).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void javadocTree() {
|
||||
var op = new CheckstyleOperation().fromProject(new Project()).javadocTree(true);
|
||||
assertThat(op.options.contains("-j")).as("add").isTrue();
|
||||
assertThat(op.options.contains("-j")).as(ADD).isTrue();
|
||||
op = op.javadocTree(false);
|
||||
assertThat(op.options.contains("-j")).as("remove").isFalse();
|
||||
assertThat(op.options.contains("-j")).as(REMOVE).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -162,24 +181,24 @@ class CheckstyleOperationTest {
|
|||
@Test
|
||||
void tree() {
|
||||
var op = new CheckstyleOperation().fromProject(new Project()).tree(true);
|
||||
assertThat(op.options.contains("-t")).as("add").isTrue();
|
||||
assertThat(op.options.contains("-t")).as(ADD).isTrue();
|
||||
op = op.tree(false);
|
||||
assertThat(op.options.contains("-t")).as("remove").isFalse();
|
||||
assertThat(op.options.contains("-t")).as(REMOVE).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void treeWithComments() {
|
||||
var op = new CheckstyleOperation().fromProject(new Project()).treeWithComments(true);
|
||||
assertThat(op.options.contains("-T")).as("add").isTrue();
|
||||
assertThat(op.options.contains("-T")).as(ADD).isTrue();
|
||||
op = op.treeWithComments(false);
|
||||
assertThat(op.options.contains("-T")).as("remove").isFalse();
|
||||
assertThat(op.options.contains("-T")).as(REMOVE).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void treeWithJavadoc() {
|
||||
var op = new CheckstyleOperation().fromProject(new Project()).treeWithJavadoc(true);
|
||||
assertThat(op.options.contains("-J")).as("add").isTrue();
|
||||
assertThat(op.options.contains("-J")).as(ADD).isTrue();
|
||||
op = op.treeWithJavadoc(false);
|
||||
assertThat(op.options.contains("-J")).as("remove").isFalse();
|
||||
assertThat(op.options.contains("-J")).as(REMOVE).isFalse();
|
||||
}
|
||||
}
|
198
src/test/resources/sun_checks.xml
Normal file
198
src/test/resources/sun_checks.xml
Normal file
|
@ -0,0 +1,198 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE module PUBLIC
|
||||
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
|
||||
"https://checkstyle.org/dtds/configuration_1_3.dtd">
|
||||
|
||||
<!--
|
||||
|
||||
Checkstyle configuration that checks the sun coding conventions from:
|
||||
|
||||
- the Java Language Specification at
|
||||
https://docs.oracle.com/javase/specs/jls/se11/html/index.html
|
||||
|
||||
- the Sun Code Conventions at https://www.oracle.com/java/technologies/javase/codeconventions-contents.html
|
||||
|
||||
- the Javadoc guidelines at
|
||||
https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html
|
||||
|
||||
- the JDK Api documentation https://docs.oracle.com/en/java/javase/11/
|
||||
|
||||
- some best practices
|
||||
|
||||
Checkstyle is very configurable. Be sure to read the documentation at
|
||||
https://checkstyle.org (or in your downloaded distribution).
|
||||
|
||||
Most Checks are configurable, be sure to consult the documentation.
|
||||
|
||||
To completely disable a check, just comment it out or delete it from the file.
|
||||
To suppress certain violations please review suppression filters.
|
||||
|
||||
Finally, it is worth reading the documentation.
|
||||
|
||||
-->
|
||||
|
||||
<module name="Checker">
|
||||
<!--
|
||||
If you set the basedir property below, then all reported file
|
||||
names will be relative to the specified directory. See
|
||||
https://checkstyle.org/config.html#Checker
|
||||
|
||||
<property name="basedir" value="${basedir}"/>
|
||||
-->
|
||||
<property name="severity" value="error"/>
|
||||
|
||||
<property name="fileExtensions" value="java, properties, xml"/>
|
||||
|
||||
<!-- Excludes all 'module-info.java' files -->
|
||||
<!-- See https://checkstyle.org/filefilters/index.html -->
|
||||
<module name="BeforeExecutionExclusionFileFilter">
|
||||
<property name="fileNamePattern" value="module\-info\.java$"/>
|
||||
</module>
|
||||
|
||||
<!-- https://checkstyle.org/filters/suppressionfilter.html -->
|
||||
<module name="SuppressionFilter">
|
||||
<property name="file" value="${org.checkstyle.sun.suppressionfilter.config}"
|
||||
default="checkstyle-suppressions.xml" />
|
||||
<property name="optional" value="true"/>
|
||||
</module>
|
||||
|
||||
<!-- Checks that a package-info.java file exists for each package. -->
|
||||
<!-- See https://checkstyle.org/checks/javadoc/javadocpackage.html#JavadocPackage -->
|
||||
<module name="JavadocPackage"/>
|
||||
|
||||
<!-- Checks whether files end with a new line. -->
|
||||
<!-- See https://checkstyle.org/checks/misc/newlineatendoffile.html -->
|
||||
<module name="NewlineAtEndOfFile"/>
|
||||
|
||||
<!-- Checks that property files contain the same keys. -->
|
||||
<!-- See https://checkstyle.org/checks/misc/translation.html -->
|
||||
<module name="Translation"/>
|
||||
|
||||
<!-- Checks for Size Violations. -->
|
||||
<!-- See https://checkstyle.org/checks/sizes/index.html -->
|
||||
<module name="FileLength"/>
|
||||
<module name="LineLength">
|
||||
<property name="fileExtensions" value="java"/>
|
||||
</module>
|
||||
|
||||
<!-- Checks for whitespace -->
|
||||
<!-- See https://checkstyle.org/checks/whitespace/index.html -->
|
||||
<module name="FileTabCharacter"/>
|
||||
|
||||
<!-- Miscellaneous other checks. -->
|
||||
<!-- See https://checkstyle.org/checks/misc/index.html -->
|
||||
<module name="RegexpSingleline">
|
||||
<property name="format" value="\s+$"/>
|
||||
<property name="minimum" value="0"/>
|
||||
<property name="maximum" value="0"/>
|
||||
<property name="message" value="Line has trailing spaces."/>
|
||||
</module>
|
||||
|
||||
<!-- Checks for Headers -->
|
||||
<!-- See https://checkstyle.org/checks/header/index.html -->
|
||||
<!-- <module name="Header"> -->
|
||||
<!-- <property name="headerFile" value="${checkstyle.header.file}"/> -->
|
||||
<!-- <property name="fileExtensions" value="java"/> -->
|
||||
<!-- </module> -->
|
||||
|
||||
<module name="TreeWalker">
|
||||
|
||||
<!-- Checks for Javadoc comments. -->
|
||||
<!-- See https://checkstyle.org/checks/javadoc/index.html -->
|
||||
<module name="InvalidJavadocPosition"/>
|
||||
<module name="JavadocMethod"/>
|
||||
<module name="JavadocType"/>
|
||||
<module name="JavadocVariable"/>
|
||||
<module name="JavadocStyle"/>
|
||||
<module name="MissingJavadocMethod"/>
|
||||
|
||||
<!-- Checks for Naming Conventions. -->
|
||||
<!-- See https://checkstyle.org/checks/naming/index.html -->
|
||||
<module name="ConstantName"/>
|
||||
<module name="LocalFinalVariableName"/>
|
||||
<module name="LocalVariableName"/>
|
||||
<module name="MemberName"/>
|
||||
<module name="MethodName"/>
|
||||
<module name="PackageName"/>
|
||||
<module name="ParameterName"/>
|
||||
<module name="StaticVariableName"/>
|
||||
<module name="TypeName"/>
|
||||
|
||||
<!-- Checks for imports -->
|
||||
<!-- See https://checkstyle.org/checks/imports/index.html -->
|
||||
<module name="AvoidStarImport"/>
|
||||
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
|
||||
<module name="RedundantImport"/>
|
||||
<module name="UnusedImports">
|
||||
<property name="processJavadoc" value="false"/>
|
||||
</module>
|
||||
|
||||
<!-- Checks for Size Violations. -->
|
||||
<!-- See https://checkstyle.org/checks/sizes/index.html -->
|
||||
<module name="MethodLength"/>
|
||||
<module name="ParameterNumber"/>
|
||||
|
||||
<!-- Checks for whitespace -->
|
||||
<!-- See https://checkstyle.org/checks/whitespace/index.html -->
|
||||
<module name="EmptyForIteratorPad"/>
|
||||
<module name="GenericWhitespace"/>
|
||||
<module name="MethodParamPad"/>
|
||||
<module name="NoWhitespaceAfter"/>
|
||||
<module name="NoWhitespaceBefore"/>
|
||||
<module name="OperatorWrap"/>
|
||||
<module name="ParenPad"/>
|
||||
<module name="TypecastParenPad"/>
|
||||
<module name="WhitespaceAfter"/>
|
||||
<module name="WhitespaceAround"/>
|
||||
|
||||
<!-- Modifier Checks -->
|
||||
<!-- See https://checkstyle.org/checks/modifier/index.html -->
|
||||
<module name="ModifierOrder"/>
|
||||
<module name="RedundantModifier"/>
|
||||
|
||||
<!-- Checks for blocks. You know, those {}'s -->
|
||||
<!-- See https://checkstyle.org/checks/blocks/index.html -->
|
||||
<module name="AvoidNestedBlocks"/>
|
||||
<module name="EmptyBlock"/>
|
||||
<module name="LeftCurly"/>
|
||||
<module name="NeedBraces"/>
|
||||
<module name="RightCurly"/>
|
||||
|
||||
<!-- Checks for common coding problems -->
|
||||
<!-- See https://checkstyle.org/checks/coding/index.html -->
|
||||
<module name="EmptyStatement"/>
|
||||
<module name="EqualsHashCode"/>
|
||||
<module name="HiddenField"/>
|
||||
<module name="IllegalInstantiation"/>
|
||||
<module name="InnerAssignment"/>
|
||||
<module name="MagicNumber"/>
|
||||
<module name="MissingSwitchDefault"/>
|
||||
<module name="MultipleVariableDeclarations"/>
|
||||
<module name="SimplifyBooleanExpression"/>
|
||||
<module name="SimplifyBooleanReturn"/>
|
||||
|
||||
<!-- Checks for class design -->
|
||||
<!-- See https://checkstyle.org/checks/design/index.html -->
|
||||
<module name="DesignForExtension"/>
|
||||
<module name="FinalClass"/>
|
||||
<module name="HideUtilityClassConstructor"/>
|
||||
<module name="InterfaceIsType"/>
|
||||
<module name="VisibilityModifier"/>
|
||||
|
||||
<!-- Miscellaneous other checks. -->
|
||||
<!-- See https://checkstyle.org/checks/misc/index.html -->
|
||||
<module name="ArrayTypeStyle"/>
|
||||
<module name="FinalParameters"/>
|
||||
<module name="TodoComment"/>
|
||||
<module name="UpperEll"/>
|
||||
|
||||
<!-- https://checkstyle.org/filters/suppressionxpathfilter.html -->
|
||||
<module name="SuppressionXpathFilter">
|
||||
<property name="file" value="${org.checkstyle.sun.suppressionxpathfilter.config}"
|
||||
default="checkstyle-xpath-suppressions.xml" />
|
||||
<property name="optional" value="true"/>
|
||||
</module>
|
||||
|
||||
</module>
|
||||
|
||||
</module>
|
Loading…
Add table
Add a link
Reference in a new issue