Skip to content

Commit

Permalink
Suppress checkstyle rules for test sources (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
jskov authored Oct 7, 2024
1 parent b168e50 commit d2a26ea
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 18 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The options are (shows here with their default value).

**Checkstyle**

By default uses [this configuration](./src/main/resources/config/checkstyle/checkstyle-mada.xml)
By default uses [this configuration](./src/main/resources/config/checkstyle/checkstyle-mada.xml) and [these suppressions](./src/main/resources/config/checkstyle/suppressions-mada.xml)

* `dk.mada.style.checkstyle.enabled = true`
Boolean flag allowing the checkstyle checker to be disabled
Expand All @@ -77,7 +77,9 @@ By default uses [this configuration](./src/main/resources/config/checkstyle/chec
* `dk.mada.style.checkstyle.config-path = null`
Optional path to an alternative checkstyle configuration file
This can be a URL; the content will be downloaded and cached (so if you want to update the content, you must change the URL)
Note that the configDir is set to the folder where the mada config and suppressions files are stored.

The suppressions file is hardwired for now, matching files `dk/.*/(accept|fixture|unit)/.*java` and checks `(Javadoc|LineLength|MagicNumber)`.

**ErrorProne**

Expand Down
15 changes: 9 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ dependencies {
processResources {
// Checksum each configuration file so it is easy for the plugin to reason about their state
filesMatching('config/datafile-checksums.properties') {
def eclipseFormatterMadaChecksum = digest(file('src/main/resources/config/spotless/eclipse-formatter-mada.xml'))
def checkstyleMadaChecksum = digest(file('src/main/resources/config/checkstyle/checkstyle-mada.xml'))
def eclipseFormatterMadaChecksum = digestFiles([file('src/main/resources/config/spotless/eclipse-formatter-mada.xml')])
def checkstyleMadaChecksum = digestFiles([file('src/main/resources/config/checkstyle/checkstyle-mada.xml'), file('src/main/resources/config/checkstyle/suppressions-mada.xml')])
filter { l -> l.replace('@ECLIPSE_FORMATTER_MADA@', eclipseFormatterMadaChecksum)
.replace('@CHECKSTYLE_MADA@', checkstyleMadaChecksum) }
}
Expand Down Expand Up @@ -111,8 +111,11 @@ project.afterEvaluate { p ->

tasks.eclipse.doLast { project.mkdir("build/pluginUnderTestMetadata") }

String digest(File f) {
byte[] b = f.readBytes()
byte[] d = java.security.MessageDigest.getInstance("SHA-256").digest(b)
return java.util.HexFormat.of().formatHex(d)
String digestFiles(List<File> files) {
def digester = java.security.MessageDigest.getInstance("MD5")
for (File f : files) {
byte[] b = f.readBytes()
digester.update(b)
}
return java.util.HexFormat.of().formatHex(digester.digest())
}
16 changes: 11 additions & 5 deletions src/main/java/dk/mada/style/config/ConfigFileExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,24 @@ public Path getLocalFileFromConfigPath(String path) {
private Path lazyGetLocalFile(String path, String checksum, Supplier<String> supplier) {
Path madaConfigDir = gradleHomeDir.resolve("mada-data");
try {
String suffix = path.substring(path.lastIndexOf('.'));
String filename = path.replace('/', ':').replace(suffix, "") + "-" + checksum + suffix;
int nameIndex = path.lastIndexOf('/');
String dirname = path.substring(0, nameIndex);
if (dirname.startsWith("/")) {
dirname = dirname.substring(1);
}
String filename = path.substring(nameIndex + 1);

Path configDir = madaConfigDir.resolve(dirname).resolve(checksum);

Path targetFile = madaConfigDir.resolve(filename);
Path markerFile = madaConfigDir.resolve(filename + ".valid");
Path targetFile = configDir.resolve(filename);
Path markerFile = configDir.resolve(filename + ".valid");
if (Files.exists(markerFile)) {
logger.debug("Already have config file {} : {}", path, targetFile);
return targetFile;
}
logger.debug("Missing config file {}, fetching...", path);

Files.createDirectories(madaConfigDir);
Files.createDirectories(configDir);
Files.deleteIfExists(targetFile);

Files.writeString(targetFile, supplier.get());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dk.mada.style.configurators;

import java.nio.file.Path;
import java.util.Objects;

import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.Directory;
import org.gradle.api.logging.Logger;
import org.gradle.api.plugins.quality.Checkstyle;
import org.gradle.api.plugins.quality.CheckstyleExtension;
Expand All @@ -18,13 +20,15 @@
public class CheckstyleConfigurator {
/** The default configuration resource path. */
private static final String CHECKSTYLE_CHECKSTYLE_MADA_XML = "checkstyle/checkstyle-mada.xml";
/** The default suppressions resource path. */
private static final String CHECKSTYLE_SUPPRESSIONS_MADA_XML = "checkstyle/suppressions-mada.xml";
/** The gradle project. */
private final Project project;
/** The gradle logger. */
private final Logger logger;
/** The checkstyle configuration. */
private final CheckstyleConfiguration checkstyleConfig;
/** The default configuration file, shipped with this plugin. */
/** The default configuration file shipped with this plugin. */
private final Path defaultConfigFile;
/** The default file extractor. */
private final ConfigFileExtractor configExtractor;
Expand All @@ -43,6 +47,8 @@ public CheckstyleConfigurator(Project project, CheckstyleConfiguration checkstyl
this.configExtractor = configExtractor;

defaultConfigFile = configExtractor.getLocalConfigFileFromResource(CHECKSTYLE_CHECKSTYLE_MADA_XML);
// The suppressions file is references from the config file
configExtractor.getLocalConfigFileFromResource(CHECKSTYLE_SUPPRESSIONS_MADA_XML);
}

/**
Expand All @@ -51,10 +57,14 @@ public CheckstyleConfigurator(Project project, CheckstyleConfiguration checkstyl
* @param ce the checkstyle extension
*/
public void configure(CheckstyleExtension ce) {
logger.info("Checkstyle config {}", getActiveConfigfile());
Path shippedConfigDir = Objects.requireNonNull(defaultConfigFile.getParent());
ce.getConfigDirectory().convention(toGradleDir(shippedConfigDir));

Path activeConfigfile = getActiveConfigfile();
logger.info("Checkstyle config {}", activeConfigfile);

ce.setIgnoreFailures(checkstyleConfig.ignoreFailures());
ce.setConfigFile(getActiveConfigfile().toFile());
ce.setConfigFile(activeConfigfile.toFile());

String toolVersion = checkstyleConfig.toolVersion();
if (toolVersion != null) {
Expand All @@ -75,6 +85,18 @@ public void configure(CheckstyleExtension ce) {
});
}

/**
* Convert Path (directory) to a Gradle Directory.
*
* Seems the easiest (only?) way.
*
* @param dir the directory
* @return the Gradle Directory
*/
private Directory toGradleDir(Path dir) {
return project.getLayout().getProjectDirectory().dir(dir.toAbsolutePath().toString());
}

private void disableTask(Task t) {
t.setOnlyIf("disabled by mada style", ta -> false);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/config/checkstyle/checkstyle-mada.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
</module>

<!-- https://checkstyle.org/config_filters.html#SuppressionFilter -->
<!-- Note that config_log is set via the plugin's configDirectory -->
<module name="SuppressionFilter">
<property name="file" value="${org.checkstyle.sun.suppressionfilter.config}"
default="checkstyle-suppressions.xml" />
<property name="file" value="${config_loc}/suppressions-mada.xml" />
<property name="optional" value="true"/>
</module>

Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/config/checkstyle/suppressions-mada.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<!-- More leniency for test sources -->
<suppress checks="(Javadoc|LineLength|MagicNumber)" files="dk[\\/].*[\\/](accept|fixture|unit)[\\/].*java" />
</suppressions>
11 changes: 10 additions & 1 deletion src/main/resources/config/datafile-checksums.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# These files from the src/main/resources/config directory get unpacked to
# the Gradle wrapper instance's dir in mada-data/$Path/$Checksum/$Name
# each with a .valid marker.
#
# The checksum should reflect changes to any file to be unpacked in
# the same (Path) directory.
#
# Path/Name : Checksum
checkstyle/checkstyle-mada.xml: @CHECKSTYLE_MADA@
spotless/eclipse-formatter-mada.xml: @ECLIPSE_FORMATTER_MADA@
checkstyle/suppressions-mada.xml: @CHECKSTYLE_MADA@
spotless/eclipse-formatter-mada.xml: @ECLIPSE_FORMATTER_MADA@

0 comments on commit d2a26ea

Please sign in to comment.