1616
1717package io .spring .javaformat .gradle ;
1818
19+ import java .io .File ;
1920import java .io .IOException ;
21+ import java .nio .file .Files ;
22+ import java .nio .file .Path ;
23+ import java .nio .file .StandardCopyOption ;
24+ import java .nio .file .StandardOpenOption ;
25+ import java .util .Collections ;
26+ import java .util .stream .Stream ;
2027
2128import org .gradle .testkit .runner .BuildResult ;
2229import org .gradle .testkit .runner .TaskOutcome ;
2330import org .junit .Rule ;
2431import org .junit .Test ;
32+ import org .junit .rules .TemporaryFolder ;
2533
2634import io .spring .javaformat .gradle .testkit .GradleBuild ;
2735
@@ -37,6 +45,9 @@ public class CheckTaskTests {
3745 @ Rule
3846 public final GradleBuild gradleBuild = new GradleBuild ();
3947
48+ @ Rule
49+ public final TemporaryFolder temp = new TemporaryFolder ();
50+
4051 @ Test
4152 public void checkOk () throws IOException {
4253 BuildResult result = this .gradleBuild .source ("src/test/resources/check-ok" ).build ("check" );
@@ -52,6 +63,18 @@ public void whenFirstInvocationSucceedsThenSecondInvocationIsUpToDate() throws I
5263 assertThat (result .task (":checkFormatMain" ).getOutcome ()).isEqualTo (TaskOutcome .UP_TO_DATE );
5364 }
5465
66+ @ Test
67+ public void whenFirstInvocationSucceedsAndSourceIsModifiedThenSecondInvocationSucceeds () throws IOException {
68+ copyFolder (new File ("src/test/resources/check-ok" ).toPath (), this .temp .getRoot ().toPath ());
69+ GradleBuild gradleBuild = this .gradleBuild .source (this .temp .getRoot ());
70+ BuildResult result = gradleBuild .build ("check" );
71+ assertThat (result .task (":checkFormatMain" ).getOutcome ()).isEqualTo (TaskOutcome .SUCCESS );
72+ Files .write (new File (this .temp .getRoot (), "src/main/java/simple/Simple.java" ).toPath (),
73+ Collections .singletonList ("// A change to the file" ), StandardOpenOption .APPEND );
74+ result = gradleBuild .build ("--debug" , "check" );
75+ assertThat (result .task (":checkFormatMain" ).getOutcome ()).isEqualTo (TaskOutcome .SUCCESS );
76+ }
77+
5578 @ Test
5679 public void checkBad () throws IOException {
5780 BuildResult result = this .gradleBuild .source ("src/test/resources/check-bad" ).buildAndFail ("check" );
@@ -67,4 +90,21 @@ public void whenFirstInvocationFailsThenSecondInvocationFails() throws IOExcepti
6790 assertThat (result .task (":checkFormatMain" ).getOutcome ()).isEqualTo (TaskOutcome .FAILED );
6891 }
6992
93+ private void copyFolder (Path source , Path target ) throws IOException {
94+ try (Stream <Path > stream = Files .walk (source )) {
95+ stream .forEach ((child ) -> {
96+ try {
97+ Path relative = source .relativize (child );
98+ Path destination = target .resolve (relative );
99+ if (!destination .toFile ().isDirectory ()) {
100+ Files .copy (child , destination , StandardCopyOption .REPLACE_EXISTING );
101+ }
102+ }
103+ catch (Exception ex ) {
104+ throw new IllegalStateException (ex );
105+ }
106+ });
107+ }
108+ }
109+
70110}
0 commit comments