Skip to content

Commit c63853c

Browse files
committed
Consider indentation style and Java baseline in up-to-date checks
Closes gh-329
1 parent d496c18 commit c63853c

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/SpringJavaFormatPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.gradle.api.tasks.TaskContainer;
2828
import org.gradle.api.tasks.TaskProvider;
2929

30+
import io.spring.javaformat.config.JavaFormatConfig;
3031
import io.spring.javaformat.gradle.tasks.CheckFormat;
3132
import io.spring.javaformat.gradle.tasks.Format;
3233
import io.spring.javaformat.gradle.tasks.FormatterTask;
@@ -78,6 +79,9 @@ private <T extends FormatterTask> TaskProvider<T> addFormatterTask(SourceSet sou
7879
provider.configure((task) -> {
7980
task.setDescription(desc + " for " + sourceSet.getName());
8081
task.setSource(sourceSet.getAllJava());
82+
JavaFormatConfig config = JavaFormatConfig.findFrom(this.project.getProjectDir());
83+
task.getIndentationStyle().convention(config.getIndentationStyle());
84+
task.getJavaBaseline().convention(config.getJavaBaseline());
8185
});
8286
return provider;
8387
}

spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/tasks/FormatterTask.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,10 +19,13 @@
1919
import java.nio.charset.Charset;
2020
import java.util.stream.Stream;
2121

22+
import org.gradle.api.provider.Property;
2223
import org.gradle.api.tasks.Input;
2324
import org.gradle.api.tasks.Optional;
2425
import org.gradle.api.tasks.SourceTask;
2526

27+
import io.spring.javaformat.config.IndentationStyle;
28+
import io.spring.javaformat.config.JavaBaseline;
2629
import io.spring.javaformat.config.JavaFormatConfig;
2730
import io.spring.javaformat.formatter.FileEdit;
2831
import io.spring.javaformat.formatter.FileFormatter;
@@ -36,7 +39,13 @@ public abstract class FormatterTask extends SourceTask {
3639

3740
private String encoding;
3841

42+
private final Property<IndentationStyle> indentationStyle;
43+
44+
private final Property<JavaBaseline> javaBaseline;
45+
3946
FormatterTask() {
47+
this.indentationStyle = getProject().getObjects().property(IndentationStyle.class);
48+
this.javaBaseline = getProject().getObjects().property(JavaBaseline.class);
4049
}
4150

4251
/**
@@ -57,12 +66,30 @@ public void setEncoding(String encoding) {
5766
this.encoding = encoding;
5867
}
5968

69+
/**
70+
* The indentation style used for formatting.
71+
* @return the indentation style
72+
*/
73+
@Input
74+
public Property<IndentationStyle> getIndentationStyle() {
75+
return this.indentationStyle;
76+
}
77+
78+
/**
79+
* The Java baseline used for formatting.
80+
* @return the Java baseline
81+
*/
82+
@Input
83+
public Property<JavaBaseline> getJavaBaseline() {
84+
return this.javaBaseline;
85+
}
86+
6087
/**
6188
* Format the source files and provide a {@link Stream} of {@link FileEdit} instances.
6289
* @return the file edits
6390
*/
6491
protected final Stream<FileEdit> formatFiles() {
65-
JavaFormatConfig javaFormatConfig = JavaFormatConfig.findFrom(getProject().getProjectDir());
92+
JavaFormatConfig javaFormatConfig = JavaFormatConfig.of(this.javaBaseline.get(), this.indentationStyle.get());
6693
FileFormatter formatter = new FileFormatter(javaFormatConfig);
6794
Charset encoding = (getEncoding() != null ? Charset.forName(getEncoding()) : Charset.defaultCharset());
6895
return formatter.formatFiles(getSource().getFiles(), encoding);

spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.file.Path;
2323
import java.nio.file.StandardCopyOption;
2424
import java.nio.file.StandardOpenOption;
25+
import java.util.Arrays;
2526
import java.util.Collections;
2627
import java.util.stream.Stream;
2728

@@ -77,6 +78,41 @@ public void whenFirstInvocationSucceedsAndSourceIsModifiedThenSecondInvocationSu
7778
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
7879
}
7980

81+
@Test
82+
public void whenFirstInvocationSucceedsAndIndentationStyleIsChangedThenSecondInvocationFails() throws IOException {
83+
GradleBuild gradleBuild = this.gradleBuild.source("src/test/resources/check-ok");
84+
BuildResult result = gradleBuild.build("check");
85+
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
86+
Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(),
87+
Arrays.asList("indentation-style=spaces"));
88+
result = gradleBuild.buildAndFail("check");
89+
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.FAILED);
90+
}
91+
92+
@Test
93+
public void whenFirstInvocationFailsAndIndentationStyleIsChangedThenSecondInvocationSucceeds() throws IOException {
94+
GradleBuild gradleBuild = this.gradleBuild.source("src/test/resources/check-spaces");
95+
BuildResult result = gradleBuild.buildAndFail("check");
96+
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.FAILED);
97+
Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(),
98+
Arrays.asList("indentation-style=spaces"));
99+
result = gradleBuild.build("check");
100+
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
101+
}
102+
103+
@Test
104+
public void whenFirstInvocationSucceedsAndJavaBaselineIsChangedThenSecondInvocationSucceedsAndThirdIsUpToDate() throws IOException {
105+
GradleBuild gradleBuild = this.gradleBuild.source("src/test/resources/check-ok");
106+
BuildResult result = gradleBuild.build("check");
107+
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
108+
Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(),
109+
Arrays.asList("java-baseline=8"));
110+
result = gradleBuild.build("check");
111+
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
112+
result = gradleBuild.build("check");
113+
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
114+
}
115+
80116
@Test
81117
public void checkBad() throws IOException {
82118
BuildResult result = this.gradleBuild.source("src/test/resources/check-bad").buildAndFail("check");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
buildscript {
2+
dependencies {
3+
classpath files(pluginClasspath.split(','))
4+
}
5+
}
6+
7+
apply plugin: 'java'
8+
apply plugin: 'io.spring.javaformat'
9+
10+
sourceCompatibility = 1.8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package simple;
2+
3+
/**
4+
* Simple indented with spaces.
5+
*
6+
* @author Andy Wilkinson
7+
* @since 1.0.0
8+
*/
9+
public class Simple {
10+
11+
public static void main(String[] args) throws Exception {
12+
// Main method
13+
}
14+
15+
}

0 commit comments

Comments
 (0)