Skip to content

Commit

Permalink
Issue #7546: adds support to it to load multiple configs at once
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and strkkk committed May 18, 2022
1 parent 1ef8fdd commit 74747a2
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 62 deletions.
2 changes: 1 addition & 1 deletion config/checkstyle_resources_suppressions.xml
Expand Up @@ -909,7 +909,7 @@
<suppress checks="FileLength"
files="[\\/]it[\\/]resources[\\/]com[\\/]google[\\/]checkstyle[\\/]test[\\/]chapter4formatting[\\/]rule42blockindentation[\\/]InputIndentationCorrectFieldAndParameter\.java"/>
<suppress checks="FileLength"
files="[\\/]it[\\/]resources[\\/]com[\\/]google[\\/]checkstyle[\\/]test[\\/]chapter4formatting[\\/]rule412nonemptyblocks[\\/]InputRightCurlyOtherAlone\.java"/>
files="[\\/]it[\\/]resources[\\/]com[\\/]google[\\/]checkstyle[\\/]test[\\/]chapter4formatting[\\/]rule412nonemptyblocks[\\/]InputRightCurlyOther2\.java"/>
<suppress checks="FileLength"
files="[\\/]it[\\/]resources[\\/]com[\\/]google[\\/]checkstyle[\\/]test[\\/]chapter4formatting[\\/]rule412nonemptyblocks[\\/]InputRightCurlyOther\.java"/>
<suppress checks="FileLength"
Expand Down
3 changes: 3 additions & 0 deletions config/sevntu_suppressions.xml
Expand Up @@ -32,6 +32,9 @@
<suppress checks="AvoidModifiersForTypesCheck"
files="MainTest\.java"/>

<!-- Suppressed alternative method of pulling partial configuration from file -->
<suppress checks="CheckstyleTestMakeup" files="[\\/]RightCurlyTest\.java"/>

<!-- No need for constructor with cause -->
<suppress checks="CauseParameterInException" files=".*MetadataGenerationException\.java"/>

Expand Down
Expand Up @@ -20,6 +20,7 @@
package com.google.checkstyle.test.base;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import org.checkstyle.base.AbstractItModuleTestSupport;
Expand Down Expand Up @@ -97,4 +98,16 @@ protected static Configuration getModuleConfig(String moduleName, String moduleI
return getModuleConfig(CONFIGURATION, moduleName, moduleId);
}

/**
* Returns a list of all {@link Configuration} instances for the given module IDs.
*
* @param moduleIds module IDs.
* @return List of {@link Configuration} instances.
* @throws CheckstyleException if there is an error with the config.
*/
protected static List<Configuration> getModuleConfigsByIds(String... moduleIds)
throws CheckstyleException {
return getModuleConfigsByIds(CONFIGURATION, moduleIds);
}

}
Expand Up @@ -32,95 +32,85 @@

public class RightCurlyTest extends AbstractGoogleModuleTestSupport {

private static final String[] MODULES = {
"RightCurlySame", "RightCurlyAlone",
};

@Override
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter4formatting/rule412nonemptyblocks";
}

@Test
public void testRightCurlyAlone() throws Exception {
public void testRightCurly() throws Exception {
final String[] expected = {
"20:17: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 17),
"32:13: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 13),
"79:27: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_BREAK_BEFORE, "}", 27),
"97:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
"108:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
};

final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlySame");
final Configuration checkConfig = createTreeWalkerConfig(getModuleConfigsByIds(MODULES));
final String filePath = getPath("InputRightCurlyOther.java");

final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}

@Test
public void testRightCurlySame() throws Exception {
public void testRightCurly2() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlySame");
final String filePath = getPath("InputRightCurlySame.java");
final Configuration checkConfig = createTreeWalkerConfig(getModuleConfigsByIds(MODULES));
final String filePath = getPath("InputRightCurly2.java");

final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}

@Test
public void testRightCurlySameAndLiteralDoDefault() throws Exception {
public void testRightCurlyLiteralDoDefault() throws Exception {
final String[] expected = {
"62:9: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 9),
"67:13: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 13),
"83:9: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 9),
};

final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlySame");
final Configuration checkConfig = createTreeWalkerConfig(getModuleConfigsByIds(MODULES));
final String filePath = getPath("InputRightCurlyDoWhile.java");

final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}

@Test
public void testRightCurlyAloneOther() throws Exception {
public void testRightCurlyOther() throws Exception {
final String[] expected = {
"72:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
"20:17: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 17),
"32:13: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 13),
"79:27: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_BREAK_BEFORE, "}", 27),
"97:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
"97:6: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 6),
"108:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
"108:6: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 6),
"122:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
"122:6: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 6),
"125:57: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 57),
"148:39: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 39),
"150:61: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 61),
"153:28: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 28),
"163:16: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 16),
"165:30: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 30),
"168:16: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 16),
};

final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlyAlone");
final String filePath = getPath("InputRightCurlyOtherAlone.java");

final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}

@Test
public void testRightCurlyAloneSame() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlyAlone");
final String filePath = getPath("InputRightCurlySame.java");
final Configuration checkConfig = createTreeWalkerConfig(getModuleConfigsByIds(MODULES));
final String filePath = getPath("InputRightCurlyOther2.java");

final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}

@Test
public void testRightCurlyAloneSameAndLiteralDo() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
public void testRightCurlyLiteralDo() throws Exception {
final String[] expected = {
"62:9: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 9),
"67:13: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 13),
"83:9: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 9),
};

final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlyAlone");
final String filePath = getPath("InputRightCurlyDoWhileAlone.java");
final Configuration checkConfig = createTreeWalkerConfig(getModuleConfigsByIds(MODULES));
final String filePath = getPath("InputRightCurlyDoWhile2.java");

final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
Expand Down
95 changes: 94 additions & 1 deletion src/it/java/org/checkstyle/base/AbstractItModuleTestSupport.java
Expand Up @@ -155,6 +155,76 @@ private static boolean isSameModuleId(Configuration conf, String moduleId) {
}
}

/**
* Returns a list of all {@link Configuration} instances for the given module IDs in the
* {@code masterConfig}.
*
* @param masterConfig The master configuration to pull results from.
* @param moduleIds module IDs.
* @return List of {@link Configuration} instances.
* @throws CheckstyleException if there is an error with the config.
*/
protected static List<Configuration> getModuleConfigsByIds(Configuration masterConfig,
String... moduleIds) throws CheckstyleException {
final List<Configuration> result = new ArrayList<>();
for (Configuration currentConfig : masterConfig.getChildren()) {
if ("TreeWalker".equals(currentConfig.getName())) {
for (Configuration moduleConfig : currentConfig.getChildren()) {
final String id = getProperty(moduleConfig, "id");
if (id != null && isIn(id, moduleIds)) {
result.add(moduleConfig);
}
}
}
else {
final String id = getProperty(currentConfig, "id");
if (id != null && isIn(id, moduleIds)) {
result.add(currentConfig);
}
}
}
return result;
}

/**
* Finds the specific property {@code name} in the {@code config}.
*
* @param config The configuration to examine.
* @param name The property name to find.
* @return The property value or {@code null} if not found.
* @throws CheckstyleException if there is an error with the config.
*/
private static String getProperty(Configuration config, String name)
throws CheckstyleException {
String result = null;

if (isIn(name, config.getPropertyNames())) {
result = config.getProperty(name);
}

return result;
}

/**
* Finds the specific ID in a list of IDs.
*
* @param find The ID to find.
* @param list The list of module IDs.
* @return {@code true} if the ID is in the list.
*/
private static boolean isIn(String find, String... list) {
boolean found = false;

for (String item : list) {
if (find.equals(item)) {
found = true;
break;
}
}

return found;
}

/**
* Returns a list of all {@link Configuration} instances for the given
* module name pulled from the {@code masterConfig}.
Expand All @@ -163,7 +233,7 @@ private static boolean isSameModuleId(Configuration conf, String moduleId) {
* @param moduleName module name.
* @return {@link Configuration} instance for the given module name.
*/
protected static List<Configuration> getModuleConfigs(Configuration masterConfig,
private static List<Configuration> getModuleConfigs(Configuration masterConfig,
String moduleName) {
final List<Configuration> result = new ArrayList<>();
for (Configuration currentConfig : masterConfig.getChildren()) {
Expand Down Expand Up @@ -249,6 +319,29 @@ protected static DefaultConfiguration createTreeWalkerConfig(Configuration confi
return rootConfig;
}

/**
* Creates {@link DefaultConfiguration} or the Checker.
* based on the the list of {@link Configuration}.
*
* @param configs list of {@link Configuration} instances.
* @return {@link DefaultConfiguration} for the Checker.
*/
protected static DefaultConfiguration createTreeWalkerConfig(
List<Configuration> configs) {
DefaultConfiguration result = null;

for (Configuration config : configs) {
if (result == null) {
result = (DefaultConfiguration) createTreeWalkerConfig(config).getChildren()[0];
}
else {
result.addChild(config);
}
}

return result;
}

/**
* Creates {@link DefaultConfiguration} for the given {@link Configuration} instance.
*
Expand Down
@@ -1,6 +1,6 @@
package com.google.checkstyle.test.chapter4formatting.rule412nonemptyblocks;

public class InputRightCurlySame {
public class InputRightCurly2 {
public static void main(String[] args) {
boolean after = false;
try {
Expand Down
Expand Up @@ -6,7 +6,7 @@
* Test input for GitHub issue #3090.
* https://github.com/checkstyle/checkstyle/issues/3090
*/
public class InputRightCurlyDoWhileAlone {
public class InputRightCurlyDoWhile2 {

public void foo1() {
do {
Expand Down Expand Up @@ -59,12 +59,12 @@ public void foo4() {

public void foo5() {
do {
} // ok - for alone
} // warn
while (true);
}

public void foo6() {
do {} // ok - for alone
do {} // warn
while (true);
}

Expand All @@ -80,7 +80,7 @@ public void foo8() {

{

} // ok - for alone
} // warn

while

Expand Down
Expand Up @@ -94,7 +94,7 @@ class FooCtor
public FooCtor()
{
i = 1;
}} //ok
}} // warn

/**
* Test input for closing brace if that brace terminates
Expand All @@ -105,7 +105,7 @@ class FooMethod
public void fooMethod()
{
int i = 1;
}} //ok
}} // warn

/**
* Test input for closing brace if that brace terminates
Expand Down

0 comments on commit 74747a2

Please sign in to comment.