From 3af8af799709d06155c0ce3fa5ad53c88c27c3d2 Mon Sep 17 00:00:00 2001 From: corebonts Date: Mon, 15 Mar 2021 17:54:46 +0100 Subject: [PATCH] Set config_loc property for Checkstyle (#235) 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 --- .../checkstyle/CheckstyleProcessor.java | 20 ++++++++--- .../checkstyle/CheckstyleProcessorTest.java | 17 +++++++++ .../checkstyle-with-suppressions.xml | 35 +++++++++++++++++++ .../resources/checkstyle/suppressions.xml | 6 ++++ 4 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 src/test/resources/checkstyle/checkstyle-with-suppressions.xml create mode 100644 src/test/resources/checkstyle/suppressions.xml diff --git a/src/main/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessor.java b/src/main/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessor.java index aa4fbb9a..9e006a31 100644 --- a/src/main/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessor.java +++ b/src/main/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessor.java @@ -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; @@ -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; @@ -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); @@ -75,7 +88,4 @@ private String getConfigurationFilename() { log.info("Using Checkstyle configuration file {}", configurationFile); return configurationFile; } - - - } diff --git a/src/test/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessorTest.java index 281317fb..190559bc 100644 --- a/src/test/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessorTest.java @@ -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; @@ -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."); + } } diff --git a/src/test/resources/checkstyle/checkstyle-with-suppressions.xml b/src/test/resources/checkstyle/checkstyle-with-suppressions.xml new file mode 100644 index 00000000..fd2c8e2f --- /dev/null +++ b/src/test/resources/checkstyle/checkstyle-with-suppressions.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/checkstyle/suppressions.xml b/src/test/resources/checkstyle/suppressions.xml new file mode 100644 index 00000000..3bbd2bc0 --- /dev/null +++ b/src/test/resources/checkstyle/suppressions.xml @@ -0,0 +1,6 @@ + + + + + +