Skip to content

Commit d37ff38

Browse files
add feature to include/exclude testSourceSet to checkstyle configuration
1 parent 3afeccc commit d37ff38

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ exit $status
3131

3232
```gradle
3333
plugins {
34-
id 'io.github.alexeytereshchenko.guardian' version '1.2.2'
34+
id 'io.github.alexeytereshchenko.guardian' version '1.2.5'
3535
}
3636
```
3737

@@ -53,6 +53,7 @@ guardian {
5353
errorThreshold = 0 // Threshold for the number of errors to tolerate before failing the build.
5454
showViolations = true // Whether to display the details of style violations.
5555
version = '10.12.6' // Specifies the version of CheckStyle to use.
56+
includeTest = false // add test sourceSet to checkstyle
5657
}
5758
}
5859

plugin/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = 'io.github.alexeytereshchenko.guardian'
8-
version = '1.2.2'
8+
version = '1.2.5'
99

1010
dependencies {
1111
implementation 'net.ltgt.errorprone:net.ltgt.errorprone.gradle.plugin:3.0.1'

plugin/src/main/java/io/github/alexeytereshchenko/guardian/GuardianPlugin.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import io.github.alexeytereshchenko.guardian.task.DownloadCheckstyleFile;
88
import java.io.File;
99
import java.net.URL;
10-
import java.util.List;
10+
import java.util.ArrayList;
1111
import java.util.Set;
1212
import net.ltgt.gradle.errorprone.CheckSeverity;
1313
import net.ltgt.gradle.errorprone.ErrorProneCompilerArgumentProvider;
@@ -21,6 +21,7 @@
2121
import org.gradle.api.plugins.quality.CheckstyleExtension;
2222
import org.gradle.api.tasks.Copy;
2323
import org.gradle.api.tasks.Delete;
24+
import org.gradle.api.tasks.SourceSet;
2425
import org.gradle.api.tasks.SourceSetContainer;
2526
import org.gradle.api.tasks.compile.JavaCompile;
2627
import org.gradle.language.base.internal.plugins.CleanRule;
@@ -126,29 +127,13 @@ private void configureValidateStyleTask(Project project) {
126127
}
127128

128129
private void configureCheckstyle(Project project, GuardianExtension guardianExtension) {
129-
GuardianCheckStyleExtension checkStyleExtension = guardianExtension.getCheckStyle();
130-
String fileUrl = checkStyleExtension.getFileUrl();
131-
String filePath = fileUrl == null ? getGuardianCheckStyleFilePath(project) : getCustomCheckStyleFilePath(project);
132-
133-
int errorThreshold = checkStyleExtension.getErrorThreshold();
134-
boolean showViolations = checkStyleExtension.isShowViolations();
135-
String version = checkStyleExtension.getVersion();
130+
GuardianCheckStyleExtension guardianCheckStyleExtension = guardianExtension.getCheckStyle();
136131

137132
CheckstyleExtension checkstyleExtension = project.getExtensions()
138133
.findByType(CheckstyleExtension.class);
139134

140135
if (checkstyleExtension != null) {
141-
checkstyleExtension.setToolVersion(version);
142-
checkstyleExtension.setMaxErrors(errorThreshold);
143-
checkstyleExtension.setShowViolations(showViolations);
144-
checkstyleExtension.setMaxWarnings(0);
145-
checkstyleExtension.setIgnoreFailures(false);
146-
checkstyleExtension.setConfigFile(new File(filePath));
147-
148-
SourceSetContainer sourceSets = (SourceSetContainer) project.getProperties()
149-
.get("sourceSets");
150-
checkstyleExtension.setSourceSets(
151-
List.of(sourceSets.getByName("main"), sourceSets.getByName("test")));
136+
configureCheckStyleExtension(project, checkstyleExtension, guardianCheckStyleExtension);
152137
}
153138

154139
// it's bug in a checkstyle plugin https://github.com/gradle/gradle/issues/27035#issuecomment-1814589243
@@ -165,6 +150,36 @@ private void configureCheckstyle(Project project, GuardianExtension guardianExte
165150
});
166151
}
167152

153+
private void configureCheckStyleExtension(
154+
Project project,
155+
CheckstyleExtension checkstyleExtension,
156+
GuardianCheckStyleExtension guardianCheckStyleExtension
157+
) {
158+
String fileUrl = guardianCheckStyleExtension.getFileUrl();
159+
String filePath = fileUrl == null ? getGuardianCheckStyleFilePath(project) : getCustomCheckStyleFilePath(project);
160+
161+
int errorThreshold = guardianCheckStyleExtension.getErrorThreshold();
162+
boolean showViolations = guardianCheckStyleExtension.isShowViolations();
163+
String version = guardianCheckStyleExtension.getVersion();
164+
boolean includeTest = guardianCheckStyleExtension.isIncludeTest();
165+
166+
checkstyleExtension.setToolVersion(version);
167+
checkstyleExtension.setMaxErrors(errorThreshold);
168+
checkstyleExtension.setShowViolations(showViolations);
169+
checkstyleExtension.setMaxWarnings(0);
170+
checkstyleExtension.setIgnoreFailures(false);
171+
checkstyleExtension.setConfigFile(new File(filePath));
172+
173+
SourceSetContainer sourceSetContainer = (SourceSetContainer) project.getProperties().get("sourceSets");
174+
ArrayList<SourceSet> sourceSets = new ArrayList<>();
175+
sourceSets.add(sourceSetContainer.getByName("main"));
176+
if (includeTest) {
177+
sourceSets.add(sourceSetContainer.getByName("test"));
178+
}
179+
180+
checkstyleExtension.setSourceSets(sourceSets);
181+
}
182+
168183
private String getGuardianCheckStyleFilePath(Project project) {
169184
URL url = getClass().getClassLoader().getResource("guardian-checkstyle.xml");
170185
if (url != null) {

plugin/src/main/java/io/github/alexeytereshchenko/guardian/extention/GuardianCheckStyleExtension.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ public class GuardianCheckStyleExtension {
66
private int errorThreshold = 0;
77
private boolean showViolations = true;
88
private String version = "10.12.6";
9+
private boolean includeTest = false;
10+
11+
public boolean isIncludeTest() {
12+
return includeTest;
13+
}
14+
15+
public void setIncludeTest(boolean includeTest) {
16+
this.includeTest = includeTest;
17+
}
918

1019
public String getVersion() {
1120
return version;

0 commit comments

Comments
 (0)