Skip to content

Commit

Permalink
Set config_loc property for Checkstyle (#235)
Browse files Browse the repository at this point in the history
This is a half-official property, that is not set by Checkstyle itself
by default, but provided by IDE tools for Eclipse and IDEA.

In order to use the same configuration for IDE and Sputnik, it's useful
to have this property set here also

Co-authored-by: Gabor Garancsi <gabor.garancsi@casrd.net>
  • Loading branch information
corebonts and Gabor Garancsi committed Mar 15, 2021
1 parent cb77fc5 commit 3af8af7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
Expand Up @@ -7,6 +7,7 @@
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import pl.touk.sputnik.configuration.Configuration;
Expand All @@ -19,6 +20,8 @@
import pl.touk.sputnik.review.transformer.IOFileTransformer;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;

Expand Down Expand Up @@ -56,10 +59,20 @@ private void innerProcess(@NotNull Review review, @NotNull AuditListener auditLi
@NotNull
private Checker createChecker(@NotNull AuditListener auditListener) {
try {
String configurationFile = getConfigurationFilename();
if (StringUtils.isBlank(configurationFile)) {
throw new ReviewException("Checkstyle configuration file is not specified.");
}
if (!Files.exists(Paths.get(configurationFile))) {
throw new ReviewException("Checkstyle configuration file does not exist.");
}

Checker checker = new Checker();
ClassLoader moduleClassLoader = Checker.class.getClassLoader();
String configurationFile = getConfigurationFilename();
Properties properties = System.getProperties();// loadProperties(new File(System.getProperty(CHECKSTYLE_PROPERTIES_FILE)));

Properties properties = new Properties(System.getProperties());
properties.setProperty("config_loc", new File(configurationFile).getParent());

checker.setModuleClassLoader(moduleClassLoader);
checker.configure(ConfigurationLoader.loadConfiguration(configurationFile, new PropertiesExpander(properties)));
checker.addListener(auditListener);
Expand All @@ -75,7 +88,4 @@ private String getConfigurationFilename() {
log.info("Using Checkstyle configuration file {}", configurationFile);
return configurationFile;
}



}
Expand Up @@ -7,6 +7,7 @@
import com.google.common.collect.ImmutableMap;

import pl.touk.sputnik.TestEnvironment;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.ConfigurationSetup;
import pl.touk.sputnik.configuration.GeneralOption;
import pl.touk.sputnik.review.ReviewResult;
Expand Down Expand Up @@ -38,4 +39,20 @@ void shouldReturnBasicSunViolationsOnSimpleClass() {
"Missing a Javadoc comment."
);
}

@Test
void shouldConsiderSuppressionsWithConfigLocProperty() {
Configuration configWithSuppressions = new ConfigurationSetup().setUp(ImmutableMap.of(
GeneralOption.CHECKSTYLE_CONFIGURATION_FILE.getKey(), "src/test/resources/checkstyle/checkstyle-with-suppressions.xml"));
CheckstyleProcessor fixtureWithSuppressions = new CheckstyleProcessor(configWithSuppressions);

ReviewResult reviewResult = fixtureWithSuppressions.process(review());

assertThat(reviewResult)
.isNotNull()
.extracting(ReviewResult::getViolations).asList()
.hasSize(2)
.extracting("message")
.containsOnly("Missing a Javadoc comment.");
}
}
35 changes: 35 additions & 0 deletions src/test/resources/checkstyle/checkstyle-with-suppressions.xml
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN" "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="SuppressionFilter">
<property name="file" value="${config_loc}/suppressions.xml"/>
</module>
<module name="NewlineAtEndOfFile">
<property name="lineSeparator" value="lf"/>
</module>
<module name="JavadocPackage"/>

<module name="TreeWalker">
<!-- Javadoc Comments -->
<module name="AtclauseOrder"/>
<module name="JavadocMethod">
<property name="allowUndeclaredRTE" value="true"/>
<property name="allowThrowsTagsForSubclasses" value="true"/>
<property name="allowMissingPropertyJavadoc" value="true"/>
</module>
<module name="JavadocParagraph"/>
<module name="JavadocStyle">
<property name="scope" value="public"/>
</module>
<module name="JavadocTagContinuationIndentation"/>
<module name="JavadocType">
<property name="authorFormat" value="\S"/>
<!-- avoid errors on tag '@noinspection' -->
<property name="allowUnknownTags" value="true"/>
</module>
<module name="JavadocVariable"/>
<module name="NonEmptyAtclauseDescription"/>
<module name="SingleLineJavadoc"/>
<module name="SummaryJavadoc"/>
</module>
</module>
6 changes: 6 additions & 0 deletions src/test/resources/checkstyle/suppressions.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN" "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
<suppress files="TestFile.java" checks="JavadocPackage"/>
</suppressions>

0 comments on commit 3af8af7

Please sign in to comment.