Skip to content

Commit

Permalink
Consider indentation style and Java baseline in up-to-date checks
Browse files Browse the repository at this point in the history
Closes gh-329
  • Loading branch information
wilkinsona committed May 24, 2022
1 parent d496c18 commit c63853c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskProvider;

import io.spring.javaformat.config.JavaFormatConfig;
import io.spring.javaformat.gradle.tasks.CheckFormat;
import io.spring.javaformat.gradle.tasks.Format;
import io.spring.javaformat.gradle.tasks.FormatterTask;
Expand Down Expand Up @@ -78,6 +79,9 @@ private <T extends FormatterTask> TaskProvider<T> addFormatterTask(SourceSet sou
provider.configure((task) -> {
task.setDescription(desc + " for " + sourceSet.getName());
task.setSource(sourceSet.getAllJava());
JavaFormatConfig config = JavaFormatConfig.findFrom(this.project.getProjectDir());
task.getIndentationStyle().convention(config.getIndentationStyle());
task.getJavaBaseline().convention(config.getJavaBaseline());
});
return provider;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,10 +19,13 @@
import java.nio.charset.Charset;
import java.util.stream.Stream;

import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.SourceTask;

import io.spring.javaformat.config.IndentationStyle;
import io.spring.javaformat.config.JavaBaseline;
import io.spring.javaformat.config.JavaFormatConfig;
import io.spring.javaformat.formatter.FileEdit;
import io.spring.javaformat.formatter.FileFormatter;
Expand All @@ -36,7 +39,13 @@ public abstract class FormatterTask extends SourceTask {

private String encoding;

private final Property<IndentationStyle> indentationStyle;

private final Property<JavaBaseline> javaBaseline;

FormatterTask() {
this.indentationStyle = getProject().getObjects().property(IndentationStyle.class);
this.javaBaseline = getProject().getObjects().property(JavaBaseline.class);
}

/**
Expand All @@ -57,12 +66,30 @@ public void setEncoding(String encoding) {
this.encoding = encoding;
}

/**
* The indentation style used for formatting.
* @return the indentation style
*/
@Input
public Property<IndentationStyle> getIndentationStyle() {
return this.indentationStyle;
}

/**
* The Java baseline used for formatting.
* @return the Java baseline
*/
@Input
public Property<JavaBaseline> getJavaBaseline() {
return this.javaBaseline;
}

/**
* Format the source files and provide a {@link Stream} of {@link FileEdit} instances.
* @return the file edits
*/
protected final Stream<FileEdit> formatFiles() {
JavaFormatConfig javaFormatConfig = JavaFormatConfig.findFrom(getProject().getProjectDir());
JavaFormatConfig javaFormatConfig = JavaFormatConfig.of(this.javaBaseline.get(), this.indentationStyle.get());
FileFormatter formatter = new FileFormatter(javaFormatConfig);
Charset encoding = (getEncoding() != null ? Charset.forName(getEncoding()) : Charset.defaultCharset());
return formatter.formatFiles(getSource().getFiles(), encoding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Stream;

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

@Test
public void whenFirstInvocationSucceedsAndIndentationStyleIsChangedThenSecondInvocationFails() throws IOException {
GradleBuild gradleBuild = this.gradleBuild.source("src/test/resources/check-ok");
BuildResult result = gradleBuild.build("check");
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(),
Arrays.asList("indentation-style=spaces"));
result = gradleBuild.buildAndFail("check");
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.FAILED);
}

@Test
public void whenFirstInvocationFailsAndIndentationStyleIsChangedThenSecondInvocationSucceeds() throws IOException {
GradleBuild gradleBuild = this.gradleBuild.source("src/test/resources/check-spaces");
BuildResult result = gradleBuild.buildAndFail("check");
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.FAILED);
Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(),
Arrays.asList("indentation-style=spaces"));
result = gradleBuild.build("check");
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

@Test
public void whenFirstInvocationSucceedsAndJavaBaselineIsChangedThenSecondInvocationSucceedsAndThirdIsUpToDate() throws IOException {
GradleBuild gradleBuild = this.gradleBuild.source("src/test/resources/check-ok");
BuildResult result = gradleBuild.build("check");
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(),
Arrays.asList("java-baseline=8"));
result = gradleBuild.build("check");
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
result = gradleBuild.build("check");
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
}

@Test
public void checkBad() throws IOException {
BuildResult result = this.gradleBuild.source("src/test/resources/check-bad").buildAndFail("check");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
buildscript {
dependencies {
classpath files(pluginClasspath.split(','))
}
}

apply plugin: 'java'
apply plugin: 'io.spring.javaformat'

sourceCompatibility = 1.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package simple;

/**
* Simple indented with spaces.
*
* @author Andy Wilkinson
* @since 1.0.0
*/
public class Simple {

public static void main(String[] args) throws Exception {
// Main method
}

}

0 comments on commit c63853c

Please sign in to comment.