Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;

import org.gradle.api.file.ProjectLayout;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
Expand All @@ -49,8 +52,15 @@ public class CheckFormat extends FormatterTask {
*/
public static final String DESCRIPTION = "Run Spring Java formatting checks";

private final ProjectLayout projectLayout;

private File reportLocation;

@Inject
public CheckFormat(ProjectLayout projectLayout) {
this.projectLayout = projectLayout;
}

@TaskAction
public void checkFormatting() throws IOException, InterruptedException {
List<File> problems = formatFiles().filter(FileEdit::hasEdits)
Expand All @@ -59,7 +69,9 @@ public void checkFormatting() throws IOException, InterruptedException {
this.reportLocation.getParentFile().mkdirs();
if (!problems.isEmpty()) {
StringBuilder message = new StringBuilder("Formatting violations found in the following files:\n");
problems.stream().forEach((f) -> message.append(" * " + getProject().relativePath(f) + "\n"));
File projectDirectory = this.projectLayout.getProjectDirectory().getAsFile();
problems.stream()
.forEach((f) -> message.append(" * " + projectDirectory.toPath().relativize(f.toPath()) + "\n"));
message.append("\nRun `format` to fix.");
Files.write(this.reportLocation.toPath(), Collections.singletonList(message.toString()),
StandardOpenOption.CREATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.stream.Stream;
Expand Down Expand Up @@ -128,6 +129,26 @@ void whenFirstInvocationFailsThenSecondInvocationFails() throws IOException {
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.FAILED);
}

@Test
void whenUsingConfigurationCacheThenFormattingViolationsAreReported() throws IOException {
GradleBuild gradleBuild = this.gradleBuild.source("src/test/resources/check-bad")
.gradleVersion("9.1.0")
.debug(false);
BuildResult result = gradleBuild.buildAndFail("check", "--configuration-cache");
assertFormattingViolationWithoutConfigurationCacheProblems(result);
result = gradleBuild.buildAndFail("check", "--configuration-cache");
assertFormattingViolationWithoutConfigurationCacheProblems(result);
assertThat(result.getOutput()).contains("Reusing configuration cache.");
}

private void assertFormattingViolationWithoutConfigurationCacheProblems(BuildResult result) {
String sourcePath = Paths.get("src", "main", "java", "simple", "Simple.java").toString();
assertThat(result.getOutput()).contains("Formatting violations found in the following files:")
.contains(" * " + sourcePath)
.doesNotContain("Task.project");
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.FAILED);
}

private void copyNormalizedFolder(Path source, Path target) throws IOException {
try (Stream<Path> stream = Files.walk(source)) {
stream.forEach((child) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class GradleBuild {

private String gradleVersion;

private boolean debug = true;

private GradleVersion expectDeprecationWarnings;

void before() throws IOException {
Expand Down Expand Up @@ -109,7 +111,7 @@ public GradleRunner prepareRunner(String... arguments) throws IOException {
String scriptContent = new String(Files.readAllBytes(buildFile.toPath())).replace("{version}",
getSpringFormatVersion());
Files.write(buildFile.toPath(), scriptContent.getBytes(StandardCharsets.UTF_8));
GradleRunner gradleRunner = GradleRunner.create().withProjectDir(this.projectDir).withDebug(true);
GradleRunner gradleRunner = GradleRunner.create().withProjectDir(this.projectDir).withDebug(this.debug);
if (this.gradleVersion != null) {
gradleRunner.withGradleVersion(this.gradleVersion);
}
Expand Down Expand Up @@ -168,6 +170,11 @@ public String getGradleVersion() {
return this.gradleVersion;
}

public GradleBuild debug(boolean debug) {
this.debug = debug;
return this;
}

private String getSpringFormatVersion() {
return evaluateExpression(
"/*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']" + "/text()");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ buildscript {
apply plugin: 'java'
apply plugin: 'io.spring.javaformat'

sourceCompatibility = 1.8
java {
sourceCompatibility = JavaVersion.VERSION_1_8
}