Skip to content

Commit

Permalink
Support up-to-date checking of Format task
Browse files Browse the repository at this point in the history
See gh-323
  • Loading branch information
larsgrefer authored and wilkinsona committed May 24, 2022
1 parent c63853c commit fde7e85
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
import java.io.IOException;

import org.gradle.api.GradleException;
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.OutputFiles;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;

import io.spring.javaformat.formatter.FileEdit;
Expand Down Expand Up @@ -51,4 +56,15 @@ public void format() throws IOException, InterruptedException {
}
}

@Override
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
public FileTree getSource() {
return super.getSource();
}

@OutputFiles
public FileTree getOutputFiles() {
return super.getSource();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Files;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -50,6 +51,24 @@ public void checkOk() throws IOException {
assertThat(formattedContent).contains("class Simple {").contains(" public static void main");
}

@Test
public void checkUpTpDate() throws IOException {
GradleRunner runner = this.gradleBuild.source("src/test/resources/format").prepareRunner("format");

// 1) Actually format the sources.
BuildResult result1 = runner.build();
assertThat(result1.task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);

// 2) No-op reformat the sources
// Not up-to-date yet because 1) changed the sources.
BuildResult result2 = runner.build();
assertThat(result2.task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);

// 3) Now we should be up-to-date since 2) was effectively a no-op
BuildResult result3 = runner.build();
assertThat(result3.task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
}

@Test
public void checkSpacesOk() throws IOException {
BuildResult result = this.gradleBuild.source("src/test/resources/format-spaces").build("format");
Expand Down

0 comments on commit fde7e85

Please sign in to comment.